Skip to content

Commit

Permalink
Allow custom next-auth table names, column names for drizzle
Browse files Browse the repository at this point in the history
  • Loading branch information
venables committed Sep 17, 2023
1 parent 84351f1 commit 4cdd24c
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 60 deletions.
15 changes: 13 additions & 2 deletions auth.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import GitHub from "next-auth/providers/github"
import Google from "next-auth/providers/google"

import { HttpEmailProvider } from "@/lib/auth/http-email-provider"
import { db, pgTable } from "@/lib/db"
import {
accountsTable,
db,
sessionsTable,
usersTable,
verificationTokensTable
} from "@/lib/db"

/**
* https://auth-docs-git-feat-nextjs-auth-authjs.vercel.app/guides/upgrade-to-v5#edge-compatibility
Expand All @@ -13,7 +19,12 @@ export default {
/**
* https://authjs.dev/reference/adapter/drizzle
*/
adapter: DrizzleAdapter(db, pgTable),
adapter: DrizzleAdapter(db, undefined, {
users: usersTable,
accounts: accountsTable,
sessions: sessionsTable,
verificationTokens: verificationTokensTable
}),

/**
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
CREATE EXTENSION IF NOT EXISTS "citext";

CREATE TABLE IF NOT EXISTS "accounts" (
"userId" text NOT NULL,
"user_id" text NOT NULL,
"type" text NOT NULL,
"provider" text NOT NULL,
"providerAccountId" text NOT NULL,
"provider_account_id" text NOT NULL,
"refresh_token" text,
"access_token" text,
"expires_at" integer,
"token_type" text,
"scope" text,
"id_token" text,
"session_state" text,
CONSTRAINT accounts_provider_providerAccountId PRIMARY KEY("provider","providerAccountId")
CONSTRAINT accounts_provider_provider_account_id PRIMARY KEY("provider","provider_account_id")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "sessions" (
"sessionToken" text PRIMARY KEY NOT NULL,
"userId" text NOT NULL,
"session_token" text PRIMARY KEY NOT NULL,
"user_id" text NOT NULL,
"expires" timestamp NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "users" (
"id" text PRIMARY KEY NOT NULL,
"name" text,
"email" "citext" NOT NULL,
"emailVerified" timestamp,
"email_verified" timestamp,
"image" text,
CONSTRAINT "users_email_unique" UNIQUE("email")
);
Expand All @@ -38,13 +38,13 @@ CREATE TABLE IF NOT EXISTS "verification_tokens" (
);
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "accounts" ADD CONSTRAINT "accounts_userId_users_id_fk" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE cascade ON UPDATE no action;
ALTER TABLE "accounts" ADD CONSTRAINT "accounts_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "sessions" ADD CONSTRAINT "sessions_userId_users_id_fk" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE cascade ON UPDATE no action;
ALTER TABLE "sessions" ADD CONSTRAINT "sessions_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
40 changes: 20 additions & 20 deletions drizzle/meta/0000_snapshot.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"version": "5",
"dialect": "pg",
"id": "57d3b764-7ffe-4d06-b34d-9ea9e5605840",
"id": "0c494241-df41-44f7-a705-29ce1c53abe3",
"prevId": "00000000-0000-0000-0000-000000000000",
"tables": {
"accounts": {
"name": "accounts",
"schema": "",
"columns": {
"userId": {
"name": "userId",
"user_id": {
"name": "user_id",
"type": "text",
"primaryKey": false,
"notNull": true
Expand All @@ -26,8 +26,8 @@
"primaryKey": false,
"notNull": true
},
"providerAccountId": {
"name": "providerAccountId",
"provider_account_id": {
"name": "provider_account_id",
"type": "text",
"primaryKey": false,
"notNull": true
Expand Down Expand Up @@ -77,20 +77,20 @@
},
"indexes": {},
"foreignKeys": {
"accounts_userId_users_id_fk": {
"name": "accounts_userId_users_id_fk",
"accounts_user_id_users_id_fk": {
"name": "accounts_user_id_users_id_fk",
"tableFrom": "accounts",
"tableTo": "users",
"columnsFrom": ["userId"],
"columnsFrom": ["user_id"],
"columnsTo": ["id"],
"onDelete": "cascade",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {
"accounts_provider_providerAccountId": {
"name": "accounts_provider_providerAccountId",
"columns": ["provider", "providerAccountId"]
"accounts_provider_provider_account_id": {
"name": "accounts_provider_provider_account_id",
"columns": ["provider", "provider_account_id"]
}
},
"uniqueConstraints": {}
Expand All @@ -99,14 +99,14 @@
"name": "sessions",
"schema": "",
"columns": {
"sessionToken": {
"name": "sessionToken",
"session_token": {
"name": "session_token",
"type": "text",
"primaryKey": true,
"notNull": true
},
"userId": {
"name": "userId",
"user_id": {
"name": "user_id",
"type": "text",
"primaryKey": false,
"notNull": true
Expand All @@ -120,11 +120,11 @@
},
"indexes": {},
"foreignKeys": {
"sessions_userId_users_id_fk": {
"name": "sessions_userId_users_id_fk",
"sessions_user_id_users_id_fk": {
"name": "sessions_user_id_users_id_fk",
"tableFrom": "sessions",
"tableTo": "users",
"columnsFrom": ["userId"],
"columnsFrom": ["user_id"],
"columnsTo": ["id"],
"onDelete": "cascade",
"onUpdate": "no action"
Expand Down Expand Up @@ -155,8 +155,8 @@
"primaryKey": false,
"notNull": true
},
"emailVerified": {
"name": "emailVerified",
"email_verified": {
"name": "email_verified",
"type": "timestamp",
"primaryKey": false,
"notNull": false
Expand Down
4 changes: 2 additions & 2 deletions drizzle/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
{
"idx": 0,
"version": "5",
"when": 1692498355601,
"tag": "0000_wild_dark_beast",
"when": 1694913558730,
"tag": "0000_flashy_mantis",
"breakpoints": true
}
]
Expand Down
38 changes: 10 additions & 28 deletions lib/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,34 @@ import { type AdapterAccount } from "@auth/core/adapters"
import { createId } from "@paralleldrive/cuid2"
import {
integer,
pgTableCreator,
pgTable,
primaryKey,
text,
timestamp
} from "drizzle-orm/pg-core"

import { citext } from "./custom-types/citext"

/**
* Modify the default Auth.js tables to be snake case and plural.
*/
export const pgTable = pgTableCreator((name) => {
switch (name) {
case "user":
return "users"
case "account":
return "accounts"
case "session":
return "sessions"
case "verificationToken":
return "verification_tokens"
default:
return name
}
})

export const usersTable = pgTable("user", {
export const usersTable = pgTable("users", {
id: text("id")
.notNull()
.primaryKey()
.$defaultFn(() => createId()),
name: text("name"),
email: citext("email").notNull().unique(),
emailVerified: timestamp("emailVerified", { mode: "date" }),
emailVerified: timestamp("email_verified", { mode: "date" }),
image: text("image")
})

export const accountsTable = pgTable(
"account",
"accounts",
{
userId: text("userId")
userId: text("user_id")
.notNull()
.references(() => usersTable.id, { onDelete: "cascade" }),
type: text("type").$type<AdapterAccount["type"]>().notNull(),
provider: text("provider").notNull(),
providerAccountId: text("providerAccountId").notNull(),
providerAccountId: text("provider_account_id").notNull(),
refresh_token: text("refresh_token"),
access_token: text("access_token"),
expires_at: integer("expires_at"),
Expand All @@ -61,16 +43,16 @@ export const accountsTable = pgTable(
})
)

export const sessionsTable = pgTable("session", {
sessionToken: text("sessionToken").notNull().primaryKey(),
userId: text("userId")
export const sessionsTable = pgTable("sessions", {
sessionToken: text("session_token").notNull().primaryKey(),
userId: text("user_id")
.notNull()
.references(() => usersTable.id, { onDelete: "cascade" }),
expires: timestamp("expires", { mode: "date" }).notNull()
})

export const verificationTokensTable = pgTable(
"verificationToken",
"verification_tokens",
{
identifier: text("identifier").notNull(),
token: text("token").notNull(),
Expand Down
71 changes: 71 additions & 0 deletions patches/@auth+drizzle-adapter+0.3.2.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
diff --git a/node_modules/@auth/drizzle-adapter/index.d.ts b/node_modules/@auth/drizzle-adapter/index.d.ts
index 00166e5..8a3f0cc 100644
--- a/node_modules/@auth/drizzle-adapter/index.d.ts
+++ b/node_modules/@auth/drizzle-adapter/index.d.ts
@@ -15,7 +15,7 @@
*
* @module @auth/drizzle-adapter
*/
-import { SqlFlavorOptions, TableFn } from "./lib/utils.js";
+import { SqlFlavorOptions, TableFn, MinimumSchema } from "./lib/utils.js";
import type { Adapter } from "@auth/core/adapters";
/**
* Add the adapter to your `pages/api/[...nextauth].ts` next-auth configuration object.
@@ -241,5 +241,5 @@ import type { Adapter } from "@auth/core/adapters";
* ---
*
**/
-export declare function DrizzleAdapter<SqlFlavor extends SqlFlavorOptions>(db: SqlFlavor, table?: TableFn<SqlFlavor>): Adapter;
+export declare function DrizzleAdapter<SqlFlavor extends SqlFlavorOptions>(db: SqlFlavor, table?: TableFn<SqlFlavor>, tables?: MinimumSchema["pg"]): Adapter;
//# sourceMappingURL=index.d.ts.map
diff --git a/node_modules/@auth/drizzle-adapter/index.js b/node_modules/@auth/drizzle-adapter/index.js
index ebe29a9..f4e7dea 100644
--- a/node_modules/@auth/drizzle-adapter/index.js
+++ b/node_modules/@auth/drizzle-adapter/index.js
@@ -246,12 +246,12 @@ import { is } from "drizzle-orm";
* ---
*
**/
-export function DrizzleAdapter(db, table) {
+export function DrizzleAdapter(db, table, tables) {
if (is(db, MySqlDatabase)) {
return mySqlDrizzleAdapter(db, table);
}
else if (is(db, PgDatabase)) {
- return pgDrizzleAdapter(db, table);
+ return pgDrizzleAdapter(db, table, tables);
}
else if (is(db, BaseSQLiteDatabase)) {
return SQLiteDrizzleAdapter(db, table);
diff --git a/node_modules/@auth/drizzle-adapter/lib/pg.d.ts b/node_modules/@auth/drizzle-adapter/lib/pg.d.ts
index 1d710fa..4ef8957 100644
--- a/node_modules/@auth/drizzle-adapter/lib/pg.d.ts
+++ b/node_modules/@auth/drizzle-adapter/lib/pg.d.ts
@@ -221,5 +221,5 @@ export declare function createTables(pgTable: PgTableFn): {
}>;
};
export type DefaultSchema = ReturnType<typeof createTables>;
-export declare function pgDrizzleAdapter(client: InstanceType<typeof PgDatabase>, tableFn?: PgTableFn<undefined>): Adapter;
+export declare function pgDrizzleAdapter(client: InstanceType<typeof PgDatabase>, tableFn?: PgTableFn<undefined>, tables?: DefaultSchema): Adapter;
//# sourceMappingURL=pg.d.ts.map
diff --git a/node_modules/@auth/drizzle-adapter/lib/pg.js b/node_modules/@auth/drizzle-adapter/lib/pg.js
index f3d333f..647fd60 100644
--- a/node_modules/@auth/drizzle-adapter/lib/pg.js
+++ b/node_modules/@auth/drizzle-adapter/lib/pg.js
@@ -41,13 +41,13 @@ export function createTables(pgTable) {
}));
return { users, accounts, sessions, verificationTokens };
}
-export function pgDrizzleAdapter(client, tableFn = defaultPgTableFn) {
- const { users, accounts, sessions, verificationTokens } = createTables(tableFn);
+export function pgDrizzleAdapter(client, tableFn = defaultPgTableFn, tables = undefined) {
+ const { users, accounts, sessions, verificationTokens } = tables || createTables(tableFn);
return {
async createUser(data) {
return await client
.insert(users)
- .values({ ...data, id: crypto.randomUUID() })
+ .values(data)
.returning()
.then((res) => res[0] ?? null);
},

1 comment on commit 4cdd24c

@vercel
Copy link

@vercel vercel bot commented on 4cdd24c Sep 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.