Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update drizzle.mdx #12285

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 76 additions & 72 deletions docs/pages/getting-started/adapters/drizzle.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ To use this adapter, you must have setup Drizzle ORM and Drizzle Kit in your pro
If you want to modify the schema or add additional fields, you can use the following schema as a starting point:

```ts filename="schema.ts"

// Adding the property `casing: 'snake_case'` in your drizzle.config.ts will automatically create snake case field names.

import {
boolean,
timestamp,
Expand All @@ -58,86 +61,87 @@ import { drizzle } from "drizzle-orm/postgres-js"
import type { AdapterAccountType } from "next-auth/adapters"

const connectionString = "postgres://postgres:postgres@localhost:5432/drizzle"
const pool = postgres(connectionString, { max: 1 })

export const db = drizzle(pool)
export const db = drizzle({ connection: connectionString, casing: 'snake_case'});

export const users = pgTable("user", {
id: text("id")
.primaryKey()
.$defaultFn(() => crypto.randomUUID()),
name: text("name"),
email: text("email").unique(),
emailVerified: timestamp("emailVerified", { mode: "date" }),
image: text("image"),
})
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("userId")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
type: text("type").$type<AdapterAccountType>().notNull(),
provider: text("provider").notNull(),
providerAccountId: text("providerAccountId").notNull(),
refresh_token: text("refresh_token"),
access_token: text("access_token"),
expires_at: integer("expires_at"),
token_type: text("token_type"),
scope: text("scope"),
id_token: text("id_token"),
session_state: text("session_state"),
},
(account) => ({
compoundKey: primaryKey({
columns: [account.provider, account.providerAccountId],
}),
})
)

export const sessions = pgTable("session", {
sessionToken: text("sessionToken").primaryKey(),
userId: text("userId")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
expires: timestamp("expires", { mode: "date" }).notNull(),
})
'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("identifier").notNull(),
token: text("token").notNull(),
expires: timestamp("expires", { mode: "date" }).notNull(),
},
(verificationToken) => ({
compositePk: primaryKey({
columns: [verificationToken.identifier, verificationToken.token],
}),
})
)
'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("credentialID").notNull().unique(),
userId: text("userId")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
providerAccountId: text("providerAccountId").notNull(),
credentialPublicKey: text("credentialPublicKey").notNull(),
counter: integer("counter").notNull(),
credentialDeviceType: text("credentialDeviceType").notNull(),
credentialBackedUp: boolean("credentialBackedUp").notNull(),
transports: text("transports"),
},
(authenticator) => ({
compositePK: primaryKey({
columns: [authenticator.userId, authenticator.credentialID],
}),
})
)
'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],
}),
],
);
```

</Accordion>
Expand Down
Loading