@auth/drizzle-adapter 0.1.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/lib/mysql.ts CHANGED
@@ -2,75 +2,87 @@ import { and, eq } from "drizzle-orm"
2
2
  import {
3
3
  int,
4
4
  timestamp,
5
- mysqlTable,
5
+ mysqlTable as defaultMySqlTableFn,
6
6
  primaryKey,
7
7
  varchar,
8
+ MySqlTableFn,
8
9
  } from "drizzle-orm/mysql-core"
9
10
 
10
11
  import type { Adapter, AdapterAccount } from "@auth/core/adapters"
11
12
  import type { MySql2Database } from "drizzle-orm/mysql2"
12
13
 
13
- export const users = mysqlTable("users", {
14
- id: varchar("id", { length: 255 }).notNull().primaryKey(),
15
- name: varchar("name", { length: 255 }),
16
- email: varchar("email", { length: 255 }).notNull(),
17
- emailVerified: timestamp("emailVerified", {
18
- mode: "date",
19
- fsp: 3,
20
- }).defaultNow(),
21
- image: varchar("image", { length: 255 }),
22
- })
23
-
24
- export const accounts = mysqlTable(
25
- "accounts",
26
- {
14
+ export function createTables(mySqlTable: MySqlTableFn) {
15
+ const users = mySqlTable("users", {
16
+ id: varchar("id", { length: 255 }).notNull().primaryKey(),
17
+ name: varchar("name", { length: 255 }),
18
+ email: varchar("email", { length: 255 }).notNull(),
19
+ emailVerified: timestamp("emailVerified", {
20
+ mode: "date",
21
+ fsp: 3,
22
+ }).defaultNow(),
23
+ image: varchar("image", { length: 255 }),
24
+ })
25
+
26
+ const accounts = mySqlTable(
27
+ "accounts",
28
+ {
29
+ userId: varchar("userId", { length: 255 })
30
+ .notNull()
31
+ .references(() => users.id, { onDelete: "cascade" }),
32
+ type: varchar("type", { length: 255 })
33
+ .$type<AdapterAccount["type"]>()
34
+ .notNull(),
35
+ provider: varchar("provider", { length: 255 }).notNull(),
36
+ providerAccountId: varchar("providerAccountId", {
37
+ length: 255,
38
+ }).notNull(),
39
+ refresh_token: varchar("refresh_token", { length: 255 }),
40
+ access_token: varchar("access_token", { length: 255 }),
41
+ expires_at: int("expires_at"),
42
+ token_type: varchar("token_type", { length: 255 }),
43
+ scope: varchar("scope", { length: 255 }),
44
+ id_token: varchar("id_token", { length: 255 }),
45
+ session_state: varchar("session_state", { length: 255 }),
46
+ },
47
+ (account) => ({
48
+ compoundKey: primaryKey(account.provider, account.providerAccountId),
49
+ })
50
+ )
51
+
52
+ const sessions = mySqlTable("sessions", {
53
+ sessionToken: varchar("sessionToken", { length: 255 })
54
+ .notNull()
55
+ .primaryKey(),
27
56
  userId: varchar("userId", { length: 255 })
28
57
  .notNull()
29
58
  .references(() => users.id, { onDelete: "cascade" }),
30
- type: varchar("type", { length: 255 })
31
- .$type<AdapterAccount["type"]>()
32
- .notNull(),
33
- provider: varchar("provider", { length: 255 }).notNull(),
34
- providerAccountId: varchar("providerAccountId", { length: 255 }).notNull(),
35
- refresh_token: varchar("refresh_token", { length: 255 }),
36
- access_token: varchar("access_token", { length: 255 }),
37
- expires_at: int("expires_at"),
38
- token_type: varchar("token_type", { length: 255 }),
39
- scope: varchar("scope", { length: 255 }),
40
- id_token: varchar("id_token", { length: 255 }),
41
- session_state: varchar("session_state", { length: 255 }),
42
- },
43
- (account) => ({
44
- compoundKey: primaryKey(account.provider, account.providerAccountId),
45
- })
46
- )
47
-
48
- export const sessions = mysqlTable("sessions", {
49
- sessionToken: varchar("sessionToken", { length: 255 }).notNull().primaryKey(),
50
- userId: varchar("userId", { length: 255 })
51
- .notNull()
52
- .references(() => users.id, { onDelete: "cascade" }),
53
- expires: timestamp("expires", { mode: "date" }).notNull(),
54
- })
55
-
56
- export const verificationTokens = mysqlTable(
57
- "verificationToken",
58
- {
59
- identifier: varchar("identifier", { length: 255 }).notNull(),
60
- token: varchar("token", { length: 255 }).notNull(),
61
59
  expires: timestamp("expires", { mode: "date" }).notNull(),
62
- },
63
- (vt) => ({
64
- compoundKey: primaryKey(vt.identifier, vt.token),
65
60
  })
66
- )
67
61
 
68
- export const schema = { users, accounts, sessions, verificationTokens }
69
- export type DefaultSchema = typeof schema
62
+ const verificationTokens = mySqlTable(
63
+ "verificationToken",
64
+ {
65
+ identifier: varchar("identifier", { length: 255 }).notNull(),
66
+ token: varchar("token", { length: 255 }).notNull(),
67
+ expires: timestamp("expires", { mode: "date" }).notNull(),
68
+ },
69
+ (vt) => ({
70
+ compoundKey: primaryKey(vt.identifier, vt.token),
71
+ })
72
+ )
73
+
74
+ return { users, accounts, sessions, verificationTokens }
75
+ }
76
+
77
+ export type DefaultSchema = ReturnType<typeof createTables>
70
78
 
71
79
  export function mySqlDrizzleAdapter(
72
- client: MySql2Database<Record<string, never>>
80
+ client: MySql2Database<Record<string, never>>,
81
+ tableFn = defaultMySqlTableFn
73
82
  ): Adapter {
83
+ const { users, accounts, sessions, verificationTokens } =
84
+ createTables(tableFn)
85
+
74
86
  return {
75
87
  async createUser(data) {
76
88
  const id = crypto.randomUUID()
package/src/lib/pg.ts CHANGED
@@ -1,71 +1,79 @@
1
1
  import { and, eq } from "drizzle-orm"
2
2
  import {
3
3
  timestamp,
4
- pgTable,
4
+ pgTable as defaultPgTableFn,
5
5
  text,
6
6
  primaryKey,
7
7
  integer,
8
+ PgTableFn,
8
9
  } from "drizzle-orm/pg-core"
9
10
 
10
11
  import type { PostgresJsDatabase } from "drizzle-orm/postgres-js"
11
12
  import type { Adapter, AdapterAccount } from "@auth/core/adapters"
12
13
 
13
- export const users = pgTable("users", {
14
- id: text("id").notNull().primaryKey(),
15
- name: text("name"),
16
- email: text("email").notNull(),
17
- emailVerified: timestamp("emailVerified", { mode: "date" }),
18
- image: text("image"),
19
- })
20
-
21
- export const accounts = pgTable(
22
- "accounts",
23
- {
14
+ export function createTables(pgTable: PgTableFn) {
15
+ const users = pgTable("users", {
16
+ id: text("id").notNull().primaryKey(),
17
+ name: text("name"),
18
+ email: text("email").notNull(),
19
+ emailVerified: timestamp("emailVerified", { mode: "date" }),
20
+ image: text("image"),
21
+ })
22
+
23
+ const accounts = pgTable(
24
+ "accounts",
25
+ {
26
+ userId: text("userId")
27
+ .notNull()
28
+ .references(() => users.id, { onDelete: "cascade" }),
29
+ type: text("type").$type<AdapterAccount["type"]>().notNull(),
30
+ provider: text("provider").notNull(),
31
+ providerAccountId: text("providerAccountId").notNull(),
32
+ refresh_token: text("refresh_token"),
33
+ access_token: text("access_token"),
34
+ expires_at: integer("expires_at"),
35
+ token_type: text("token_type"),
36
+ scope: text("scope"),
37
+ id_token: text("id_token"),
38
+ session_state: text("session_state"),
39
+ },
40
+ (account) => ({
41
+ compoundKey: primaryKey(account.provider, account.providerAccountId),
42
+ })
43
+ )
44
+
45
+ const sessions = pgTable("sessions", {
46
+ sessionToken: text("sessionToken").notNull().primaryKey(),
24
47
  userId: text("userId")
25
48
  .notNull()
26
49
  .references(() => users.id, { onDelete: "cascade" }),
27
- type: text("type").$type<AdapterAccount["type"]>().notNull(),
28
- provider: text("provider").notNull(),
29
- providerAccountId: text("providerAccountId").notNull(),
30
- refresh_token: text("refresh_token"),
31
- access_token: text("access_token"),
32
- expires_at: integer("expires_at"),
33
- token_type: text("token_type"),
34
- scope: text("scope"),
35
- id_token: text("id_token"),
36
- session_state: text("session_state"),
37
- },
38
- (account) => ({
39
- compoundKey: primaryKey(account.provider, account.providerAccountId),
40
- })
41
- )
42
-
43
- export const sessions = pgTable("sessions", {
44
- sessionToken: text("sessionToken").notNull().primaryKey(),
45
- userId: text("userId")
46
- .notNull()
47
- .references(() => users.id, { onDelete: "cascade" }),
48
- expires: timestamp("expires", { mode: "date" }).notNull(),
49
- })
50
-
51
- export const verificationTokens = pgTable(
52
- "verificationToken",
53
- {
54
- identifier: text("identifier").notNull(),
55
- token: text("token").notNull(),
56
50
  expires: timestamp("expires", { mode: "date" }).notNull(),
57
- },
58
- (vt) => ({
59
- compoundKey: primaryKey(vt.identifier, vt.token),
60
51
  })
61
- )
62
52
 
63
- export const schema = { users, accounts, sessions, verificationTokens }
64
- export type DefaultSchema = typeof schema
53
+ const verificationTokens = pgTable(
54
+ "verificationToken",
55
+ {
56
+ identifier: text("identifier").notNull(),
57
+ token: text("token").notNull(),
58
+ expires: timestamp("expires", { mode: "date" }).notNull(),
59
+ },
60
+ (vt) => ({
61
+ compoundKey: primaryKey(vt.identifier, vt.token),
62
+ })
63
+ )
64
+
65
+ return { users, accounts, sessions, verificationTokens }
66
+ }
67
+
68
+ export type DefaultSchema = ReturnType<typeof createTables>
65
69
 
66
70
  export function pgDrizzleAdapter(
67
- client: PostgresJsDatabase<Record<string, never>>
71
+ client: PostgresJsDatabase<Record<string, never>>,
72
+ tableFn = defaultPgTableFn
68
73
  ): Adapter {
74
+ const { users, accounts, sessions, verificationTokens } =
75
+ createTables(tableFn)
76
+
69
77
  return {
70
78
  async createUser(data) {
71
79
  return await client
package/src/lib/sqlite.ts CHANGED
@@ -1,70 +1,78 @@
1
1
  import { eq, and } from "drizzle-orm"
2
2
  import {
3
3
  integer,
4
- sqliteTable,
4
+ sqliteTable as defaultSqliteTableFn,
5
5
  text,
6
6
  primaryKey,
7
7
  BaseSQLiteDatabase,
8
+ SQLiteTableFn,
8
9
  } from "drizzle-orm/sqlite-core"
9
10
 
10
11
  import type { Adapter, AdapterAccount } from "@auth/core/adapters"
11
12
 
12
- export const users = sqliteTable("users", {
13
- id: text("id").notNull().primaryKey(),
14
- name: text("name"),
15
- email: text("email").notNull(),
16
- emailVerified: integer("emailVerified", { mode: "timestamp_ms" }),
17
- image: text("image"),
18
- })
13
+ export function createTables(sqliteTable: SQLiteTableFn) {
14
+ const users = sqliteTable("users", {
15
+ id: text("id").notNull().primaryKey(),
16
+ name: text("name"),
17
+ email: text("email").notNull(),
18
+ emailVerified: integer("emailVerified", { mode: "timestamp_ms" }),
19
+ image: text("image"),
20
+ })
21
+
22
+ const accounts = sqliteTable(
23
+ "accounts",
24
+ {
25
+ userId: text("userId")
26
+ .notNull()
27
+ .references(() => users.id, { onDelete: "cascade" }),
28
+ type: text("type").$type<AdapterAccount["type"]>().notNull(),
29
+ provider: text("provider").notNull(),
30
+ providerAccountId: text("providerAccountId").notNull(),
31
+ refresh_token: text("refresh_token"),
32
+ access_token: text("access_token"),
33
+ expires_at: integer("expires_at"),
34
+ token_type: text("token_type"),
35
+ scope: text("scope"),
36
+ id_token: text("id_token"),
37
+ session_state: text("session_state"),
38
+ },
39
+ (account) => ({
40
+ compoundKey: primaryKey(account.provider, account.providerAccountId),
41
+ })
42
+ )
19
43
 
20
- export const accounts = sqliteTable(
21
- "accounts",
22
- {
44
+ const sessions = sqliteTable("sessions", {
45
+ sessionToken: text("sessionToken").notNull().primaryKey(),
23
46
  userId: text("userId")
24
47
  .notNull()
25
48
  .references(() => users.id, { onDelete: "cascade" }),
26
- type: text("type").$type<AdapterAccount["type"]>().notNull(),
27
- provider: text("provider").notNull(),
28
- providerAccountId: text("providerAccountId").notNull(),
29
- refresh_token: text("refresh_token"),
30
- access_token: text("access_token"),
31
- expires_at: integer("expires_at"),
32
- token_type: text("token_type"),
33
- scope: text("scope"),
34
- id_token: text("id_token"),
35
- session_state: text("session_state"),
36
- },
37
- (account) => ({
38
- compoundKey: primaryKey(account.provider, account.providerAccountId),
49
+ expires: integer("expires", { mode: "timestamp_ms" }).notNull(),
39
50
  })
40
- )
41
51
 
42
- export const sessions = sqliteTable("sessions", {
43
- sessionToken: text("sessionToken").notNull().primaryKey(),
44
- userId: text("userId")
45
- .notNull()
46
- .references(() => users.id, { onDelete: "cascade" }),
47
- expires: integer("expires", { mode: "timestamp_ms" }).notNull(),
48
- })
52
+ const verificationTokens = sqliteTable(
53
+ "verificationToken",
54
+ {
55
+ identifier: text("identifier").notNull(),
56
+ token: text("token").notNull(),
57
+ expires: integer("expires", { mode: "timestamp_ms" }).notNull(),
58
+ },
59
+ (vt) => ({
60
+ compoundKey: primaryKey(vt.identifier, vt.token),
61
+ })
62
+ )
49
63
 
50
- export const verificationTokens = sqliteTable(
51
- "verificationToken",
52
- {
53
- identifier: text("identifier").notNull(),
54
- token: text("token").notNull(),
55
- expires: integer("expires", { mode: "timestamp_ms" }).notNull(),
56
- },
57
- (vt) => ({
58
- compoundKey: primaryKey(vt.identifier, vt.token),
59
- })
60
- )
64
+ return { users, accounts, sessions, verificationTokens }
65
+ }
61
66
 
62
- export const schema = { users, accounts, sessions, verificationTokens }
63
- export type DefaultSchema = typeof schema
67
+ export type DefaultSchema = ReturnType<typeof createTables>
64
68
 
65
69
  export function SQLiteDrizzleAdapter(
66
- client: BaseSQLiteDatabase<any, any>
70
+ client: BaseSQLiteDatabase<any, any>,
71
+ tableFn = defaultSqliteTableFn
67
72
  ): Adapter {
73
+ const { users, accounts, sessions, verificationTokens } =
74
+ createTables(tableFn)
75
+
68
76
  return {
69
77
  createUser(data) {
70
78
  return client
package/src/lib/utils.ts CHANGED
@@ -2,9 +2,9 @@ import { MySqlDatabase } from "drizzle-orm/mysql-core"
2
2
  import { PgDatabase } from "drizzle-orm/pg-core"
3
3
  import { BaseSQLiteDatabase } from "drizzle-orm/sqlite-core"
4
4
 
5
- import type { AnyMySqlTable } from "drizzle-orm/mysql-core"
6
- import type { AnyPgTable } from "drizzle-orm/pg-core"
7
- import type { AnySQLiteTable } from "drizzle-orm/sqlite-core"
5
+ import type { AnyMySqlTable, MySqlTableFn } from "drizzle-orm/mysql-core"
6
+ import type { AnyPgTable, PgTableFn } from "drizzle-orm/pg-core"
7
+ import type { AnySQLiteTable, SQLiteTableFn } from "drizzle-orm/sqlite-core"
8
8
  import type { DefaultSchema as PgSchema } from "./pg.js"
9
9
  import type { DefaultSchema as MySqlSchema } from "./mysql.js"
10
10
  import type { DefaultSchema as SQLiteSchema } from "./sqlite.js"
@@ -32,6 +32,14 @@ export type ClientFlavors<Flavor> = Flavor extends AnyMySqlDatabase
32
32
  ? MinimumSchema["sqlite"]
33
33
  : never
34
34
 
35
+ export type TableFn<Flavor> = Flavor extends AnyMySqlDatabase
36
+ ? MySqlTableFn
37
+ : Flavor extends AnyPgDatabase
38
+ ? PgTableFn
39
+ : Flavor extends AnySQLiteDatabase
40
+ ? SQLiteTableFn
41
+ : AnySQLiteTable
42
+
35
43
  export function isMySqlDatabase(
36
44
  db: any
37
45
  ): db is MySqlDatabase<any, any, any, any> {