DT
Drizzle Teamโ€ข5w ago
Raheel

0.36.0 - The third parameter in Drizzle ORM becomes an array

Hello guys, I'm trying to setup Auth.js with Neon Postgres + Drizzle Adapter. The example given at think link: https://github.com/drizzle-team/drizzle-orm/releases/tag/0.36.0 for third param becomes an array is too basic for me to understand how do I change Auth.js schemas for accounts and sessions table to work with new version of Drizzle. I'd very much appreciate if someone can change the following schema from object to array for third param i.e. setting up a compound primary key. Thank you. 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], }), }) )
GitHub
Release 0.36.0 ยท drizzle-team/drizzle-orm
This version of drizzle-orm requires [email protected] to enable all new features New Features The third parameter in Drizzle ORM becomes an array The object API is still available but deprecate...
5 Replies
ionztorm
ionztormโ€ข5w ago
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
export const db = drizzle({ connection: process.env.DATABASE_URL, casing: 'snake_case' });
export const db = drizzle({ connection: process.env.DATABASE_URL, casing: 'snake_case' });
2) in your drizzle.config.ts i did the same
export default defineConfig({
schema: './src/db/schema',
out: './src/db/migrations',
dialect: 'postgresql',
dbCredentials: {
url: env.DATABASE_URL,
},
casing: 'snake_case',
});
export default defineConfig({
schema: './src/db/schema',
out: './src/db/migrations',
dialect: 'postgresql',
dbCredentials: {
url: env.DATABASE_URL,
},
casing: 'snake_case',
});
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],
}),
],
);
you can find the PR here in the meantime: https://github.com/nextauthjs/next-auth/pull/12285
Raheel
RaheelOPโ€ข5w ago
Thank you so much @ionztorm for the quick help, really appreciate it.
ionztorm
ionztormโ€ข5w ago
np I had the same problem about an hour ago ๐Ÿ‘
Raheel
RaheelOPโ€ข5w ago
Coming from the php background and learning React/Next, i find it so frustrating about the issues and setbacks in JS world, with laravel, Symfony I never found errors or missing docs in stable releases. Anyways not giving up ๐Ÿ˜†
ionztorm
ionztormโ€ข5w ago
nope never give up! I think in this case we are using 2 packages that are both in beta, so the docs will alwayas be a little behind until full release
Want results from more Discord servers?
Add your server