Drizzle ORM Adapter
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. Read more here: Drizzle ORM.
Example Usage
Make sure you have Drizzle installed and configured. Then, you can use the Drizzle adapter to connect to your database.
import { faireAuth } from "faire-auth";
import { drizzleAdapter } from "faire-auth/adapters/drizzle";
import { db } from "./database.ts";
export const auth = faireAuth({
database: drizzleAdapter(db, {
provider: "sqlite", // or "pg" or "mysql"
}),
//... the rest of your config
});Schema generation & migration
The Faire Auth CLI allows you to generate or migrate your database schema based on your Faire Auth configuration and plugins.
To generate the schema required by Faire Auth, run the following command:
npx @faire-auth/cli@latest generateTo generate and apply the migration, run the following commands:
npx drizzle-kit generate # generate the migration file
npx drizzle-kit migrate # apply the migrationModifying 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 { faireAuth } from "faire-auth";
import { db } from "./drizzle";
import { drizzleAdapter } from "faire-auth/adapters/drizzle";
import { schema } from "./schema";
export const auth = faireAuth({
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:
export const auth = faireAuth({
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 & faire-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:
export const auth = faireAuth({
database: drizzleAdapter(db, {
provider: "sqlite", // or "pg" or "mysql"
schema,
}),
user: {
fields: {
email: "email_address",
}
}
});Using Plural Table Names
If all your tables are using plural form, you can just pass the usePlural option:
export const auth = faireAuth({
database: drizzleAdapter(db, {
...
usePlural: true,
}),
});Performance Tips
If you're looking for performance improvements or tips, take a look at our guide to performance optimizations.