From b3599a8c3c69dfa56f9205d60d7c0afcf7df888a Mon Sep 17 00:00:00 2001 From: Leon Lonsdale Date: Sat, 23 Nov 2024 16:07:12 +0000 Subject: [PATCH] Update drizzle.mdx the drizzle api has deprecated the return of objects in the extra config of table setup, and replaced with array type returns. Additionally, snake case table field names is now automatic if the property "casing: 'snake_case' " is passed into both the db connection setup and the drizzle config. --- .../getting-started/adapters/drizzle.mdx | 148 +++++++++--------- 1 file changed, 76 insertions(+), 72 deletions(-) diff --git a/docs/pages/getting-started/adapters/drizzle.mdx b/docs/pages/getting-started/adapters/drizzle.mdx index 09c9be0428..1938d9617a 100644 --- a/docs/pages/getting-started/adapters/drizzle.mdx +++ b/docs/pages/getting-started/adapters/drizzle.mdx @@ -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, @@ -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().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().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], + }), + ], +); ```