Databases
Drizzle ORM Adapter
Integrate Entro Auth with Drizzle ORM.
Drizzle ORM is a powerful and flexible ORM for Node.js and TypeScript. It provides a simple and intuitive API for working with databases, and supports a wide range of databases including MySQL, PostgreSQL, SQLite, and more.
Before getting started, make sure you have Drizzle installed and configured. For more information, see Drizzle Documentation
The adapter requires Drizzle ORM v1 (drizzle-orm@1.0.0-rc.6 or later) and
its Relations v2 API. Drizzle 0.x is not supported.
Installation
To use the Drizzle adapter, you need to install the @entro314labs/entro-auth-drizzle-adapter package:
npm install @entro314labs/entro-auth-drizzle-adapterExample Usage
You can use the Drizzle adapter to connect to your database as follows.
import { entroAuth } from "@entro314labs/entro-auth";
import { drizzleAdapter } from "@entro314labs/entro-auth-drizzle-adapter";
import { db } from "./database.ts";
import * as schema from "./auth-schema.ts";
export const auth = entroAuth({
database: drizzleAdapter(db, {
provider: "sqlite", // or "pg" or "mysql"
schema,
}),
//... the rest of your config
});The generated schema exports authRelations, a
relations part.
Pass it to Drizzle, after your app's own relations if you have any:
import { authRelations } from "./auth-schema.ts";
import { relations } from "./app-schema.ts";
export const db = drizzle({
client,
relations: { ...relations, ...authRelations },
});Schema generation & migration
The Entro Auth CLI allows you to generate or migrate your database schema based on your Entro Auth configuration and plugins.
To generate the schema required by Entro Auth, run the following command:
npx @entro314labs/entro-auth-cli@latest generateTo generate and apply the migration, run the following commands:
npx drizzle-kit generate # generate the migration fileJoins
Database joins are useful when Entro Auth needs to fetch related data from multiple tables in a single query.
Endpoints like /get-session, /get-full-organization and many others benefit greatly from this feature,
seeing upwards of 2x to 3x performance improvements depending on database latency.
The Drizzle adapter supports joins out of the box since version 1.4.0.
To enable this feature, set advanced.database.joins to true in your auth configuration.
import { entroAuth } from "@entro314labs/entro-auth";
export const auth = entroAuth({
advanced: {
database: {
joins: true,
},
},
});Joins read the relations Drizzle was created with. Make sure the generated
authRelations is passed to drizzle() as shown above. If your schema file
has no authRelations export, run npx @entro314labs/entro-auth-cli@latest generate to
regenerate it.
When a table has several foreign keys to the same table, the CLI gives each relation its own key, named after the foreign key:
export const authRelations = defineRelationsPart({ users, tests }, (r) => ({
users: {
testsByUserId: r.many.tests({ from: r.users.id, to: r.tests.userId }),
testsByManagerId: r.many.tests({ from: r.users.id, to: r.tests.managerId }),
},
tests: {
user: r.one.users({ from: r.tests.userId, to: r.users.id }),
ManagerIdUser: r.one.users({ from: r.tests.managerId, to: r.users.id }),
},
}));Do not keep both singular and plural aliases for the same foreign key (for
example, both user and users). Drizzle treats those as separate relations
and cannot infer which reverse relation a join should use.
Modifying Table Names
The Drizzle adapter expects the schema you define to match the table names. For example, if your Drizzle schema maps the user table to users, you need to manually pass the schema and map it to the user table.
import { entroAuth } from "@entro314labs/entro-auth";
import { db } from "./drizzle";
import { drizzleAdapter } from "@entro314labs/entro-auth-drizzle-adapter";
import { schema } from "./schema";
export const auth = entroAuth({
database: drizzleAdapter(db, {
provider: "sqlite", // or "pg" or "mysql"
schema: {
...schema,
user: schema.users,
},
}),
});You can either modify the provided schema values like the example above,
or you can mutate the auth config's modelName property directly.
For example:
import { entroAuth } from "@entro314labs/entro-auth";
export const auth = entroAuth({
database: drizzleAdapter(db, {
provider: "sqlite", // or "pg" or "mysql"
schema,
}),
user: {
modelName: "users",
}
});Modifying Field Names
We map field names based on property you passed to your Drizzle schema.
For example, if you want to modify the email field to email_address,
you simply need to change the Drizzle schema to:
export const user = mysqlTable("user", {
// Changed field name without changing the schema property name
// This allows drizzle & entro-auth to still use the original field name,
// while your DB uses the modified field name
email: varchar("email_address", { length: 255 }).notNull().unique(),
// ... others
});You can either modify the Drizzle schema like the example above,
or you can mutate the auth config's fields property directly.
For example:
import { entroAuth } from "@entro314labs/entro-auth";
export const auth = entroAuth({
database: drizzleAdapter(db, {
provider: "sqlite", // or "pg" or "mysql"
schema,
}),
user: {
fields: {
email: "email_address",
}
}
});Date Columns
The schema the CLI generates stores dates with Drizzle's date mode, such as timestamp() on PostgreSQL and MySQL, and integer({ mode: "timestamp_ms" }) on SQLite. Date columns that hold strings also work: timestamp({ mode: "string" }), MySQL datetime({ mode: "string" }), and SQLite text().
For a string column, Entro Auth writes the date in UTC: ISO 8601 (2026-01-16T16:50:54.888Z), or 2026-01-16 16:50:54.888 on MySQL. It reads a value without a time zone as UTC. On PostgreSQL, timestamp({ mode: "string", withTimezone: true }) stores an absolute instant, and the values it returns carry an offset, so reading them does not depend on that rule.
Using Plural Table Names
If all your tables are using plural form, you can just pass the usePlural option:
import { entroAuth } from "@entro314labs/entro-auth";
export const auth = entroAuth({
database: drizzleAdapter(db, {
...
usePlural: true,
}),
});Custom Schema namespace
If you're using PostgreSQL and you want to generate the schema with a custom schema namespace,
you can pass the schemaName option to the Drizzle adapter.
export const auth = entroAuth({
database: drizzleAdapter(db, {
provider: "pg",
schemaName: "auth",
}),
});Then when using the Entro Auth CLI, it will generate the schema that looks something like this:
npx @entro314labs/entro-auth-cli@latest generateexport const authSchema = pgSchema("auth");
export const user = authSchema.table("user", {...});
export const session = authSchema.table("session", {...});Additional Information
- If you're looking for performance improvements or tips, take a look at our guide to performance optimizations.