@auth/drizzle-adapter 0.0.0-manual.ffc00566 → 0.2.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/pg.ts CHANGED
@@ -1,106 +1,109 @@
1
+ import { and, eq } from "drizzle-orm"
1
2
  import {
2
3
  timestamp,
3
- pgTable,
4
+ pgTable as defaultPgTableFn,
4
5
  text,
5
6
  primaryKey,
6
7
  integer,
8
+ PgTableFn,
7
9
  } from "drizzle-orm/pg-core"
8
- import { PostgresJsDatabase } from "drizzle-orm/postgres-js"
9
- import { Adapter, AdapterAccount } from "@auth/core/adapters"
10
- import { and, eq } from "drizzle-orm"
11
10
 
12
- /** @internal */
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
- /** @internal */
22
- export const accounts = pgTable(
23
- "accounts",
24
- {
11
+ import type { PostgresJsDatabase } from "drizzle-orm/postgres-js"
12
+ import type { Adapter, AdapterAccount } from "@auth/core/adapters"
13
+
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(),
25
47
  userId: text("userId")
26
48
  .notNull()
27
49
  .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
- )
43
-
44
- /** @internal */
45
- export const sessions = pgTable("sessions", {
46
- sessionToken: text("sessionToken").notNull().primaryKey(),
47
- userId: text("userId")
48
- .notNull()
49
- .references(() => users.id, { onDelete: "cascade" }),
50
- expires: timestamp("expires", { mode: "date" }).notNull(),
51
- })
52
-
53
- /** @internal */
54
- export const verificationTokens = pgTable(
55
- "verificationToken",
56
- {
57
- identifier: text("identifier").notNull(),
58
- token: text("token").notNull(),
59
50
  expires: timestamp("expires", { mode: "date" }).notNull(),
60
- },
61
- (vt) => ({
62
- compoundKey: primaryKey(vt.identifier, vt.token),
63
51
  })
64
- )
65
52
 
66
- /** @internal */
67
- export const schema = { users, accounts, sessions, verificationTokens }
68
- 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>
69
69
 
70
- /** @internal */
71
70
  export function pgDrizzleAdapter(
72
- client: PostgresJsDatabase<Record<string, never>>
71
+ client: PostgresJsDatabase<Record<string, never>>,
72
+ tableFn = defaultPgTableFn
73
73
  ): Adapter {
74
+ const { users, accounts, sessions, verificationTokens } =
75
+ createTables(tableFn)
76
+
74
77
  return {
75
- createUser: async (data) => {
78
+ async createUser(data) {
76
79
  return await client
77
80
  .insert(users)
78
81
  .values({ ...data, id: crypto.randomUUID() })
79
82
  .returning()
80
83
  .then((res) => res[0] ?? null)
81
84
  },
82
- getUser: async (data) => {
85
+ async getUser(data) {
83
86
  return await client
84
87
  .select()
85
88
  .from(users)
86
89
  .where(eq(users.id, data))
87
90
  .then((res) => res[0] ?? null)
88
91
  },
89
- getUserByEmail: async (data) => {
92
+ async getUserByEmail(data) {
90
93
  return await client
91
94
  .select()
92
95
  .from(users)
93
96
  .where(eq(users.email, data))
94
97
  .then((res) => res[0] ?? null)
95
98
  },
96
- createSession: async (data) => {
99
+ async createSession(data) {
97
100
  return await client
98
101
  .insert(sessions)
99
102
  .values(data)
100
103
  .returning()
101
104
  .then((res) => res[0])
102
105
  },
103
- getSessionAndUser: async (data) => {
106
+ async getSessionAndUser(data) {
104
107
  return await client
105
108
  .select({
106
109
  session: sessions,
@@ -111,7 +114,7 @@ export function pgDrizzleAdapter(
111
114
  .innerJoin(users, eq(users.id, sessions.userId))
112
115
  .then((res) => res[0] ?? null)
113
116
  },
114
- updateUser: async (data) => {
117
+ async updateUser(data) {
115
118
  if (!data.id) {
116
119
  throw new Error("No user id.")
117
120
  }
@@ -123,7 +126,7 @@ export function pgDrizzleAdapter(
123
126
  .returning()
124
127
  .then((res) => res[0])
125
128
  },
126
- updateSession: async (data) => {
129
+ async updateSession(data) {
127
130
  return await client
128
131
  .update(sessions)
129
132
  .set(data)
@@ -131,7 +134,7 @@ export function pgDrizzleAdapter(
131
134
  .returning()
132
135
  .then((res) => res[0])
133
136
  },
134
- linkAccount: async (rawAccount) => {
137
+ async linkAccount(rawAccount) {
135
138
  const updatedAccount = await client
136
139
  .insert(accounts)
137
140
  .values(rawAccount)
@@ -153,7 +156,7 @@ export function pgDrizzleAdapter(
153
156
 
154
157
  return account
155
158
  },
156
- getUserByAccount: async (account) => {
159
+ async getUserByAccount(account) {
157
160
  const dbAccount =
158
161
  (await client
159
162
  .select()
@@ -173,7 +176,7 @@ export function pgDrizzleAdapter(
173
176
 
174
177
  return dbAccount.users
175
178
  },
176
- deleteSession: async (sessionToken) => {
179
+ async deleteSession(sessionToken) {
177
180
  const session = await client
178
181
  .delete(sessions)
179
182
  .where(eq(sessions.sessionToken, sessionToken))
@@ -182,14 +185,14 @@ export function pgDrizzleAdapter(
182
185
 
183
186
  return session
184
187
  },
185
- createVerificationToken: async (token) => {
188
+ async createVerificationToken(token) {
186
189
  return await client
187
190
  .insert(verificationTokens)
188
191
  .values(token)
189
192
  .returning()
190
193
  .then((res) => res[0])
191
194
  },
192
- useVerificationToken: async (token) => {
195
+ async useVerificationToken(token) {
193
196
  try {
194
197
  return await client
195
198
  .delete(verificationTokens)
@@ -205,14 +208,14 @@ export function pgDrizzleAdapter(
205
208
  throw new Error("No verification token found.")
206
209
  }
207
210
  },
208
- deleteUser: async (id) => {
211
+ async deleteUser(id) {
209
212
  await client
210
213
  .delete(users)
211
214
  .where(eq(users.id, id))
212
215
  .returning()
213
216
  .then((res) => res[0] ?? null)
214
217
  },
215
- unlinkAccount: async (account) => {
218
+ async unlinkAccount(account) {
216
219
  const { type, provider, providerAccountId, userId } = await client
217
220
  .delete(accounts)
218
221
  .where(
package/src/lib/sqlite.ts CHANGED
@@ -1,95 +1,98 @@
1
+ import { eq, and } from "drizzle-orm"
1
2
  import {
2
3
  integer,
3
- sqliteTable,
4
+ sqliteTable as defaultSqliteTableFn,
4
5
  text,
5
6
  primaryKey,
6
7
  BaseSQLiteDatabase,
8
+ SQLiteTableFn,
7
9
  } from "drizzle-orm/sqlite-core"
8
- import { Adapter, AdapterAccount } from "@auth/core/adapters"
9
- import { eq, and } from "drizzle-orm"
10
10
 
11
- /** @internal */
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
- })
11
+ import type { Adapter, AdapterAccount } from "@auth/core/adapters"
12
+
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
- /** @internal */
21
- export const accounts = sqliteTable(
22
- "accounts",
23
- {
44
+ const sessions = sqliteTable("sessions", {
45
+ sessionToken: text("sessionToken").notNull().primaryKey(),
24
46
  userId: text("userId")
25
47
  .notNull()
26
48
  .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),
49
+ expires: integer("expires", { mode: "timestamp_ms" }).notNull(),
40
50
  })
41
- )
42
51
 
43
- /** @internal */
44
- export const sessions = sqliteTable("sessions", {
45
- sessionToken: text("sessionToken").notNull().primaryKey(),
46
- userId: text("userId")
47
- .notNull()
48
- .references(() => users.id, { onDelete: "cascade" }),
49
- expires: integer("expires", { mode: "timestamp_ms" }).notNull(),
50
- })
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
+ )
51
63
 
52
- /** @internal */
53
- export const verificationTokens = sqliteTable(
54
- "verificationToken",
55
- {
56
- identifier: text("identifier").notNull(),
57
- token: text("token").notNull(),
58
- expires: integer("expires", { mode: "timestamp_ms" }).notNull(),
59
- },
60
- (vt) => ({
61
- compoundKey: primaryKey(vt.identifier, vt.token),
62
- })
63
- )
64
+ return { users, accounts, sessions, verificationTokens }
65
+ }
64
66
 
65
- /** @internal */
66
- export const schema = { users, accounts, sessions, verificationTokens }
67
- export type DefaultSchema = typeof schema
67
+ export type DefaultSchema = ReturnType<typeof createTables>
68
68
 
69
- /** @internal */
70
69
  export function SQLiteDrizzleAdapter(
71
- client: BaseSQLiteDatabase<any, any>
70
+ client: BaseSQLiteDatabase<any, any>,
71
+ tableFn = defaultSqliteTableFn
72
72
  ): Adapter {
73
+ const { users, accounts, sessions, verificationTokens } =
74
+ createTables(tableFn)
75
+
73
76
  return {
74
- createUser: (data) => {
77
+ createUser(data) {
75
78
  return client
76
79
  .insert(users)
77
80
  .values({ ...data, id: crypto.randomUUID() })
78
81
  .returning()
79
82
  .get()
80
83
  },
81
- getUser: (data) => {
84
+ getUser(data) {
82
85
  return client.select().from(users).where(eq(users.id, data)).get() ?? null
83
86
  },
84
- getUserByEmail: (data) => {
87
+ getUserByEmail(data) {
85
88
  return (
86
89
  client.select().from(users).where(eq(users.email, data)).get() ?? null
87
90
  )
88
91
  },
89
- createSession: (data) => {
92
+ createSession(data) {
90
93
  return client.insert(sessions).values(data).returning().get()
91
94
  },
92
- getSessionAndUser: (data) => {
95
+ getSessionAndUser(data) {
93
96
  return (
94
97
  client
95
98
  .select({
@@ -102,7 +105,7 @@ export function SQLiteDrizzleAdapter(
102
105
  .get() ?? null
103
106
  )
104
107
  },
105
- updateUser: (data) => {
108
+ updateUser(data) {
106
109
  if (!data.id) {
107
110
  throw new Error("No user id.")
108
111
  }
@@ -114,7 +117,7 @@ export function SQLiteDrizzleAdapter(
114
117
  .returning()
115
118
  .get()
116
119
  },
117
- updateSession: (data) => {
120
+ updateSession(data) {
118
121
  return client
119
122
  .update(sessions)
120
123
  .set(data)
@@ -122,7 +125,7 @@ export function SQLiteDrizzleAdapter(
122
125
  .returning()
123
126
  .get()
124
127
  },
125
- linkAccount: (rawAccount) => {
128
+ linkAccount(rawAccount) {
126
129
  const updatedAccount = client
127
130
  .insert(accounts)
128
131
  .values(rawAccount)
@@ -143,7 +146,7 @@ export function SQLiteDrizzleAdapter(
143
146
 
144
147
  return account
145
148
  },
146
- getUserByAccount: (account) => {
149
+ getUserByAccount(account) {
147
150
  const results = client
148
151
  .select()
149
152
  .from(accounts)
@@ -158,7 +161,7 @@ export function SQLiteDrizzleAdapter(
158
161
 
159
162
  return results?.users ?? null
160
163
  },
161
- deleteSession: (sessionToken) => {
164
+ deleteSession(sessionToken) {
162
165
  return (
163
166
  client
164
167
  .delete(sessions)
@@ -167,10 +170,10 @@ export function SQLiteDrizzleAdapter(
167
170
  .get() ?? null
168
171
  )
169
172
  },
170
- createVerificationToken: (token) => {
173
+ createVerificationToken(token) {
171
174
  return client.insert(verificationTokens).values(token).returning().get()
172
175
  },
173
- useVerificationToken: (token) => {
176
+ useVerificationToken(token) {
174
177
  try {
175
178
  return (
176
179
  client
@@ -188,10 +191,10 @@ export function SQLiteDrizzleAdapter(
188
191
  throw new Error("No verification token found.")
189
192
  }
190
193
  },
191
- deleteUser: (id) => {
194
+ deleteUser(id) {
192
195
  return client.delete(users).where(eq(users.id, id)).returning().get()
193
196
  },
194
- unlinkAccount: (account) => {
197
+ unlinkAccount(account) {
195
198
  client
196
199
  .delete(accounts)
197
200
  .where(
package/src/lib/utils.ts CHANGED
@@ -1,9 +1,13 @@
1
- import { AnyMySqlTable, MySqlDatabase } from "drizzle-orm/mysql-core"
2
- import { AnyPgTable, PgDatabase } from "drizzle-orm/pg-core"
3
- import { AnySQLiteTable, BaseSQLiteDatabase } from "drizzle-orm/sqlite-core"
4
- import { DefaultSchema as PgSchema } from "./pg"
5
- import { DefaultSchema as MySqlSchema } from "./mysql"
6
- import { DefaultSchema as SQLiteSchema } from "./sqlite"
1
+ import { MySqlDatabase } from "drizzle-orm/mysql-core"
2
+ import { PgDatabase } from "drizzle-orm/pg-core"
3
+ import { BaseSQLiteDatabase } from "drizzle-orm/sqlite-core"
4
+
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
+ import type { DefaultSchema as PgSchema } from "./pg.js"
9
+ import type { DefaultSchema as MySqlSchema } from "./mysql.js"
10
+ import type { DefaultSchema as SQLiteSchema } from "./sqlite.js"
7
11
 
8
12
  export type AnyMySqlDatabase = MySqlDatabase<any, any>
9
13
  export type AnyPgDatabase = PgDatabase<any, any, any>
@@ -28,6 +32,14 @@ export type ClientFlavors<Flavor> = Flavor extends AnyMySqlDatabase
28
32
  ? MinimumSchema["sqlite"]
29
33
  : never
30
34
 
35
+ export type TableFn<Flavor> = Flavor extends AnyMySqlDatabase
36
+ ? MySqlTableFn
37
+ : Flavor extends AnyPgDatabase
38
+ ? PgTableFn
39
+ : Flavor extends AnySQLiteDatabase
40
+ ? AnySQLiteTable
41
+ : SQLiteTableFn
42
+
31
43
  export function isMySqlDatabase(
32
44
  db: any
33
45
  ): db is MySqlDatabase<any, any, any, any> {