I have just submitted a PR to update the Auth JS docs for this.
Here's my schema, there are a few edits:
1) in your index.ts i changed it to set the casing to snake case
2) in your drizzle.config.ts i did the same
3) authjs schema:
You no longer need to pass in names for the fields:
'drizzle-orm';
import {
type AnyPgColumn,
boolean,
integer,
pgTable,
primaryKey,
text,
timestamp,
} from 'drizzle-orm/pg-core';
import type { AdapterAccountType } from 'next-auth/adapters';
export const users = pgTable('user', {
id: text()
.primaryKey()
.$defaultFn(() => crypto.randomUUID()),
name: text(),
email: text().unique(),
emailVerified: timestamp({ mode: 'date' }),
password: text(),
image: text(),
});
export const accounts = pgTable(
'account',
{
userId: text()
.notNull()
.references(() => users.id, { onDelete: 'cascade' }),
type: text().$type<AdapterAccountType>().notNull(),
provider: text().notNull(),
providerAccountId: text().notNull(),
refresh_token: text(),
access_token: text(),
expires_at: integer(),
token_type: text(),
scope: text(),
id_token: text(),
session_state: text(),
},
(account) => [
primaryKey({
columns: [account.provider, account.providerAccountId], // Specify columns as an array.
name: 'provider_providerAccountId_pk', // Optional: name for the primary key.
}),
],
);
export const sessions = pgTable('session', {
sessionToken: text().primaryKey(),
userId: text()
.notNull()
.references(() => users.id, { onDelete: 'cascade' }),
expires: timestamp({ mode: 'date' }).notNull(),
});
export const verificationTokens = pgTable(
'verificationToken',
{
identifier: text().notNull(),
token: text().notNull(),
expires: timestamp({ mode: 'date' }).notNull(),
},
(verificationToken) => [
primaryKey({
columns: [verificationToken.identifier, verificationToken.token],
}),
],
);
export const authenticators = pgTable(
'authenticator',
{
credentialID: text().notNull().unique(),
userId: text()
.notNull()
.references(() => users.id, { onDelete: 'cascade' }),
providerAccountId: text().notNull(),
credentialPublicKey: text().notNull(),
counter: integer().notNull(),
credentialDeviceType: text().notNull(),
credentialBackedUp: boolean().notNull(),
transports: text(),
},
(authenticator) => [
primaryKey({
columns: [authenticator.userId, authenticator.credentialID],
}),
],
);
'drizzle-orm';
import {
type AnyPgColumn,
boolean,
integer,
pgTable,
primaryKey,
text,
timestamp,
} from 'drizzle-orm/pg-core';
import type { AdapterAccountType } from 'next-auth/adapters';
export const users = pgTable('user', {
id: text()
.primaryKey()
.$defaultFn(() => crypto.randomUUID()),
name: text(),
email: text().unique(),
emailVerified: timestamp({ mode: 'date' }),
password: text(),
image: text(),
});
export const accounts = pgTable(
'account',
{
userId: text()
.notNull()
.references(() => users.id, { onDelete: 'cascade' }),
type: text().$type<AdapterAccountType>().notNull(),
provider: text().notNull(),
providerAccountId: text().notNull(),
refresh_token: text(),
access_token: text(),
expires_at: integer(),
token_type: text(),
scope: text(),
id_token: text(),
session_state: text(),
},
(account) => [
primaryKey({
columns: [account.provider, account.providerAccountId], // Specify columns as an array.
name: 'provider_providerAccountId_pk', // Optional: name for the primary key.
}),
],
);
export const sessions = pgTable('session', {
sessionToken: text().primaryKey(),
userId: text()
.notNull()
.references(() => users.id, { onDelete: 'cascade' }),
expires: timestamp({ mode: 'date' }).notNull(),
});
export const verificationTokens = pgTable(
'verificationToken',
{
identifier: text().notNull(),
token: text().notNull(),
expires: timestamp({ mode: 'date' }).notNull(),
},
(verificationToken) => [
primaryKey({
columns: [verificationToken.identifier, verificationToken.token],
}),
],
);
export const authenticators = pgTable(
'authenticator',
{
credentialID: text().notNull().unique(),
userId: text()
.notNull()
.references(() => users.id, { onDelete: 'cascade' }),
providerAccountId: text().notNull(),
credentialPublicKey: text().notNull(),
counter: integer().notNull(),
credentialDeviceType: text().notNull(),
credentialBackedUp: boolean().notNull(),
transports: text(),
},
(authenticator) => [
primaryKey({
columns: [authenticator.userId, authenticator.credentialID],
}),
],
);