@auth/drizzle-adapter 1.0.1 → 1.1.0

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/sqlite.ts CHANGED
@@ -12,84 +12,100 @@ import {
12
12
  import type {
13
13
  Adapter,
14
14
  AdapterAccount,
15
+ AdapterAccountType,
15
16
  AdapterSession,
16
17
  AdapterUser,
17
18
  VerificationToken,
18
19
  } from "@auth/core/adapters"
19
20
 
20
- export const sqliteUsersTable = sqliteTable("user", {
21
- id: text("id")
22
- .primaryKey()
23
- .$defaultFn(() => crypto.randomUUID()),
24
- name: text("name"),
25
- email: text("email").notNull(),
26
- emailVerified: integer("emailVerified", { mode: "timestamp_ms" }),
27
- image: text("image"),
28
- }) satisfies DefaultSQLiteUsersTable
21
+ export function defineTables(
22
+ schema: Partial<DefaultSQLiteSchema> = {}
23
+ ): Required<DefaultSQLiteSchema> {
24
+ const usersTable =
25
+ schema.usersTable ??
26
+ (sqliteTable("user", {
27
+ id: text("id")
28
+ .primaryKey()
29
+ .$defaultFn(() => crypto.randomUUID()),
30
+ name: text("name"),
31
+ email: text("email").notNull(),
32
+ emailVerified: integer("emailVerified", { mode: "timestamp_ms" }),
33
+ image: text("image"),
34
+ }) satisfies DefaultSQLiteUsersTable)
29
35
 
30
- export const sqliteAccountsTable = sqliteTable(
31
- "account",
32
- {
33
- userId: text("userId")
34
- .notNull()
35
- .references(() => sqliteUsersTable.id, { onDelete: "cascade" }),
36
- type: text("type").$type<AdapterAccount["type"]>().notNull(),
37
- provider: text("provider").notNull(),
38
- providerAccountId: text("providerAccountId").notNull(),
39
- refresh_token: text("refresh_token"),
40
- access_token: text("access_token"),
41
- expires_at: integer("expires_at"),
42
- token_type: text("token_type"),
43
- scope: text("scope"),
44
- id_token: text("id_token"),
45
- session_state: text("session_state"),
46
- },
47
- (account) => ({
48
- compositePk: primaryKey({
49
- columns: [account.provider, account.providerAccountId],
50
- }),
51
- })
52
- ) satisfies DefaultSQLiteAccountsTable
36
+ const accountsTable =
37
+ schema.accountsTable ??
38
+ (sqliteTable(
39
+ "account",
40
+ {
41
+ userId: text("userId")
42
+ .notNull()
43
+ .references(() => usersTable.id, { onDelete: "cascade" }),
44
+ type: text("type").$type<AdapterAccountType>().notNull(),
45
+ provider: text("provider").notNull(),
46
+ providerAccountId: text("providerAccountId").notNull(),
47
+ refresh_token: text("refresh_token"),
48
+ access_token: text("access_token"),
49
+ expires_at: integer("expires_at"),
50
+ token_type: text("token_type"),
51
+ scope: text("scope"),
52
+ id_token: text("id_token"),
53
+ session_state: text("session_state"),
54
+ },
55
+ (account) => ({
56
+ compositePk: primaryKey({
57
+ columns: [account.provider, account.providerAccountId],
58
+ }),
59
+ })
60
+ ) satisfies DefaultSQLiteAccountsTable)
53
61
 
54
- export const sqliteSessionsTable = sqliteTable("session", {
55
- sessionToken: text("sessionToken").primaryKey(),
56
- userId: text("userId")
57
- .notNull()
58
- .references(() => sqliteUsersTable.id, { onDelete: "cascade" }),
59
- expires: integer("expires", { mode: "timestamp_ms" }).notNull(),
60
- }) satisfies DefaultSQLiteSessionsTable
62
+ const sessionsTable =
63
+ schema.sessionsTable ??
64
+ (sqliteTable("session", {
65
+ sessionToken: text("sessionToken").primaryKey(),
66
+ userId: text("userId")
67
+ .notNull()
68
+ .references(() => usersTable.id, { onDelete: "cascade" }),
69
+ expires: integer("expires", { mode: "timestamp_ms" }).notNull(),
70
+ }) satisfies DefaultSQLiteSessionsTable)
61
71
 
62
- export const sqliteVerificationTokensTable = sqliteTable(
63
- "verificationToken",
64
- {
65
- identifier: text("identifier").notNull(),
66
- token: text("token").notNull(),
67
- expires: integer("expires", { mode: "timestamp_ms" }).notNull(),
68
- },
69
- (vt) => ({
70
- compositePk: primaryKey({ columns: [vt.identifier, vt.token] }),
71
- })
72
- ) satisfies DefaultSQLiteVerificationTokenTable
72
+ const verificationTokensTable =
73
+ schema.verificationTokensTable ??
74
+ (sqliteTable(
75
+ "verificationToken",
76
+ {
77
+ identifier: text("identifier").notNull(),
78
+ token: text("token").notNull(),
79
+ expires: integer("expires", { mode: "timestamp_ms" }).notNull(),
80
+ },
81
+ (vt) => ({
82
+ compositePk: primaryKey({ columns: [vt.identifier, vt.token] }),
83
+ })
84
+ ) satisfies DefaultSQLiteVerificationTokenTable)
85
+
86
+ return {
87
+ usersTable,
88
+ accountsTable,
89
+ sessionsTable,
90
+ verificationTokensTable,
91
+ }
92
+ }
73
93
 
74
94
  export function SQLiteDrizzleAdapter(
75
95
  client: BaseSQLiteDatabase<"sync" | "async", any, any>,
76
- schema: DefaultSQLiteSchema = {
77
- usersTable: sqliteUsersTable,
78
- accountsTable: sqliteAccountsTable,
79
- sessionsTable: sqliteSessionsTable,
80
- verificationTokensTable: sqliteVerificationTokensTable,
81
- }
96
+ schema?: DefaultSQLiteSchema
82
97
  ): Adapter {
83
98
  const { usersTable, accountsTable, sessionsTable, verificationTokensTable } =
84
- schema
99
+ defineTables(schema)
85
100
 
86
101
  return {
87
102
  async createUser(data: AdapterUser) {
103
+ const { id, ...insertData } = data
88
104
  const hasDefaultId = getTableColumns(usersTable)["id"]["hasDefault"]
89
105
 
90
106
  return client
91
107
  .insert(usersTable)
92
- .values(hasDefaultId ? data : { ...data, id: crypto.randomUUID() })
108
+ .values(hasDefaultId ? insertData : { ...insertData, id })
93
109
  .returning()
94
110
  .get()
95
111
  },
@@ -416,6 +432,6 @@ export type DefaultSQLiteVerificationTokenTable = SQLiteTableWithColumns<{
416
432
  export type DefaultSQLiteSchema = {
417
433
  usersTable: DefaultSQLiteUsersTable
418
434
  accountsTable: DefaultSQLiteAccountsTable
419
- sessionsTable: DefaultSQLiteSessionsTable
420
- verificationTokensTable: DefaultSQLiteVerificationTokenTable
435
+ sessionsTable?: DefaultSQLiteSessionsTable
436
+ verificationTokensTable?: DefaultSQLiteVerificationTokenTable
421
437
  }