@auth/drizzle-adapter 1.0.1 → 1.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/README.md +2 -2
- package/index.d.ts +1 -246
- package/index.d.ts.map +1 -1
- package/index.js +1 -246
- package/lib/mysql.d.ts +64 -299
- package/lib/mysql.d.ts.map +1 -1
- package/lib/mysql.js +134 -53
- package/lib/pg.d.ts +63 -298
- package/lib/pg.d.ts.map +1 -1
- package/lib/pg.js +116 -53
- package/lib/sqlite.d.ts +63 -298
- package/lib/sqlite.d.ts.map +1 -1
- package/lib/sqlite.js +121 -52
- package/package.json +5 -6
- package/src/index.ts +1 -245
- package/src/lib/mysql.ts +245 -73
- package/src/lib/pg.ts +222 -69
- package/src/lib/sqlite.ts +223 -67
package/src/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* <div style={{display: "flex", justifyContent: "space-between", alignItems: "center", padding: 16}}>
|
|
3
|
-
* <p
|
|
3
|
+
* <p>Official <a href="https://orm.drizzle.team">Drizzle ORM</a> adapter for Auth.js / NextAuth.js.</p>
|
|
4
4
|
* <a href="https://orm.drizzle.team">
|
|
5
5
|
* <img style={{display: "block"}} src="/img/adapters/drizzle.svg" width="38" />
|
|
6
6
|
* </a>
|
|
@@ -26,251 +26,7 @@ import { DefaultSQLiteSchema, SQLiteDrizzleAdapter } from "./lib/sqlite.js"
|
|
|
26
26
|
import { DefaultSchema, SqlFlavorOptions } from "./lib/utils.js"
|
|
27
27
|
|
|
28
28
|
import type { Adapter } from "@auth/core/adapters"
|
|
29
|
-
/**
|
|
30
|
-
* Create db instance and pass it to adapter. Add this adapter to your `auth.ts` Auth.js configuration object:
|
|
31
|
-
*
|
|
32
|
-
* ```ts title="auth.ts"
|
|
33
|
-
* import NextAuth from "next-auth"
|
|
34
|
-
* import Google from "next-auth/providers/google"
|
|
35
|
-
* import { DrizzleAdapter } from "@auth/drizzle-adapter"
|
|
36
|
-
* import { db } from "./db.ts"
|
|
37
|
-
*
|
|
38
|
-
* export const { handlers, auth } = NextAuth({
|
|
39
|
-
* adapter: DrizzleAdapter(db),
|
|
40
|
-
* providers: [
|
|
41
|
-
* Google,
|
|
42
|
-
* ],
|
|
43
|
-
* })
|
|
44
|
-
* ```
|
|
45
|
-
*
|
|
46
|
-
* Follow the Drizzle documentation for [PostgreSQL setup](https://orm.drizzle.team/docs/get-started-postgresql), [MySQL setup](https://orm.drizzle.team/docs/get-started-mysql) and [SQLite setup](https://orm.drizzle.team/docs/get-started-sqlite).
|
|
47
|
-
*
|
|
48
|
-
* :::info
|
|
49
|
-
* If you want to use your own tables, you can pass them as a second argument. If you add non-nullable columns, make sure to provide a default value or rewrite functions to handle the missing values.
|
|
50
|
-
* :::
|
|
51
|
-
*
|
|
52
|
-
* ```ts title="auth.ts"
|
|
53
|
-
* import NextAuth from "next-auth"
|
|
54
|
-
* import Google from "next-auth/providers/google"
|
|
55
|
-
* import { DrizzleAdapter } from "@auth/drizzle-adapter"
|
|
56
|
-
* import { accounts, sessions, users, verificationTokens } from "./schema"
|
|
57
|
-
* import { db } from "./db.ts"
|
|
58
|
-
*
|
|
59
|
-
* export const { handlers, auth } = NextAuth({
|
|
60
|
-
* adapter: DrizzleAdapter(db, { usersTable: users, accountsTable: accounts, sessionsTable: sessions, verificationTokensTable: verificationTokens }),
|
|
61
|
-
* providers: [
|
|
62
|
-
* Google,
|
|
63
|
-
* ],
|
|
64
|
-
* })
|
|
65
|
-
* ```
|
|
66
|
-
*
|
|
67
|
-
* ## Setup
|
|
68
|
-
*
|
|
69
|
-
* First, create a schema that includes [the minimum requirements for a `next-auth` adapter](/reference/core/adapters#models). You can select your favorite SQL flavor below and copy it.
|
|
70
|
-
* Additionally, you may extend the schema from the minimum requirements to suit your needs.
|
|
71
|
-
*
|
|
72
|
-
* - [Postgres](#postgres)
|
|
73
|
-
* - [MySQL](#mysql)
|
|
74
|
-
* - [SQLite](#sqlite)
|
|
75
|
-
*
|
|
76
|
-
* ### Postgres
|
|
77
29
|
|
|
78
|
-
* ```ts title="schema.ts"
|
|
79
|
-
* import {
|
|
80
|
-
* timestamp,
|
|
81
|
-
* pgTable,
|
|
82
|
-
* text,
|
|
83
|
-
* primaryKey,
|
|
84
|
-
* integer
|
|
85
|
-
* } from "drizzle-orm/pg-core"
|
|
86
|
-
* import type { AdapterAccount } from '@auth/core/adapters'
|
|
87
|
-
* import { randomUUID } from "crypto"
|
|
88
|
-
*
|
|
89
|
-
* export const users = pgTable("user", {
|
|
90
|
-
* id: text("id").primaryKey().$defaultFn(() => randomUUID()),
|
|
91
|
-
* name: text("name"),
|
|
92
|
-
* email: text("email").notNull(),
|
|
93
|
-
* emailVerified: timestamp("emailVerified", { mode: "date" }),
|
|
94
|
-
* image: text("image"),
|
|
95
|
-
* })
|
|
96
|
-
*
|
|
97
|
-
* export const accounts = pgTable(
|
|
98
|
-
* "account",
|
|
99
|
-
* {
|
|
100
|
-
* userId: text("userId")
|
|
101
|
-
* .notNull()
|
|
102
|
-
* .references(() => users.id, { onDelete: "cascade" }),
|
|
103
|
-
* type: text("type").notNull(),
|
|
104
|
-
* provider: text("provider").notNull(),
|
|
105
|
-
* providerAccountId: text("providerAccountId").notNull(),
|
|
106
|
-
* refresh_token: text("refresh_token"),
|
|
107
|
-
* access_token: text("access_token"),
|
|
108
|
-
* expires_at: integer("expires_at"),
|
|
109
|
-
* token_type: text("token_type"),
|
|
110
|
-
* scope: text("scope"),
|
|
111
|
-
* id_token: text("id_token"),
|
|
112
|
-
* session_state: text("session_state"),
|
|
113
|
-
* },
|
|
114
|
-
* (account) => ({
|
|
115
|
-
* compoundKey: primaryKey({ columns: [account.provider, account.providerAccountId] }),
|
|
116
|
-
* })
|
|
117
|
-
* )
|
|
118
|
-
*
|
|
119
|
-
* export const sessions = pgTable("session", {
|
|
120
|
-
* sessionToken: text("sessionToken").primaryKey(),
|
|
121
|
-
* userId: text("userId")
|
|
122
|
-
* .notNull()
|
|
123
|
-
* .references(() => users.id, { onDelete: "cascade" }),
|
|
124
|
-
* expires: timestamp("expires", { mode: "date" }).notNull(),
|
|
125
|
-
* })
|
|
126
|
-
*
|
|
127
|
-
* export const verificationTokens = pgTable(
|
|
128
|
-
* "verificationToken",
|
|
129
|
-
* {
|
|
130
|
-
* identifier: text("identifier").notNull(),
|
|
131
|
-
* token: text("token").notNull(),
|
|
132
|
-
* expires: timestamp("expires", { mode: "date" }).notNull(),
|
|
133
|
-
* },
|
|
134
|
-
* (vt) => ({
|
|
135
|
-
* compoundKey: primaryKey({ columns: [vt.identifier, vt.token] }),
|
|
136
|
-
* })
|
|
137
|
-
* )
|
|
138
|
-
* ```
|
|
139
|
-
*
|
|
140
|
-
* ### MySQL
|
|
141
|
-
*
|
|
142
|
-
* In MySQL, there's no `returning` clause, so in the `createUser` function, we first insert a new user and then search by `email` to get the user's data. To make the search faster, we suggest adding an index to the `email` column.
|
|
143
|
-
*
|
|
144
|
-
* ```ts title="schema.ts"
|
|
145
|
-
* import {
|
|
146
|
-
* int,
|
|
147
|
-
* timestamp,
|
|
148
|
-
* mysqlTable,
|
|
149
|
-
* primaryKey,
|
|
150
|
-
* varchar,
|
|
151
|
-
* } from "drizzle-orm/mysql-core"
|
|
152
|
-
* import type { AdapterAccount } from "@auth/core/adapters"
|
|
153
|
-
*
|
|
154
|
-
* export const users = mysqlTable("user", {
|
|
155
|
-
* id: varchar("id", { length: 255 }).primaryKey().$defaultFn(() => randomUUID()),
|
|
156
|
-
* name: varchar("name", { length: 255 }),
|
|
157
|
-
* email: varchar("email", { length: 255 }).notNull(),
|
|
158
|
-
* emailVerified: timestamp("emailVerified", { mode: "date", fsp: 3 }),
|
|
159
|
-
* image: varchar("image", { length: 255 }),
|
|
160
|
-
* })
|
|
161
|
-
*
|
|
162
|
-
* export const accounts = mysqlTable(
|
|
163
|
-
* "account",
|
|
164
|
-
* {
|
|
165
|
-
* userId: varchar("userId", { length: 255 })
|
|
166
|
-
* .notNull()
|
|
167
|
-
* .references(() => users.id, { onDelete: "cascade" }),
|
|
168
|
-
* type: varchar("type", { length: 255 }).notNull(),
|
|
169
|
-
* provider: varchar("provider", { length: 255 }).notNull(),
|
|
170
|
-
* providerAccountId: varchar("providerAccountId", { length: 255 }).notNull(),
|
|
171
|
-
* refresh_token: varchar("refresh_token", { length: 255 }),
|
|
172
|
-
* access_token: varchar("access_token", { length: 255 }),
|
|
173
|
-
* expires_at: int("expires_at"),
|
|
174
|
-
* token_type: varchar("token_type", { length: 255 }),
|
|
175
|
-
* scope: varchar("scope", { length: 255 }),
|
|
176
|
-
* id_token: varchar("id_token", { length: 2048 }),
|
|
177
|
-
* session_state: varchar("session_state", { length: 255 }),
|
|
178
|
-
* },
|
|
179
|
-
* (account) => ({
|
|
180
|
-
* compoundKey: primaryKey({
|
|
181
|
-
columns: [account.provider, account.providerAccountId],
|
|
182
|
-
})
|
|
183
|
-
* })
|
|
184
|
-
* )
|
|
185
|
-
*
|
|
186
|
-
* export const sessions = mysqlTable("session", {
|
|
187
|
-
* sessionToken: varchar("sessionToken", { length: 255 }).primaryKey(),
|
|
188
|
-
* userId: varchar("userId", { length: 255 })
|
|
189
|
-
* .notNull()
|
|
190
|
-
* .references(() => users.id, { onDelete: "cascade" }),
|
|
191
|
-
* expires: timestamp("expires", { mode: "date" }).notNull(),
|
|
192
|
-
* })
|
|
193
|
-
*
|
|
194
|
-
* export const verificationTokens = mysqlTable(
|
|
195
|
-
* "verificationToken",
|
|
196
|
-
* {
|
|
197
|
-
* identifier: varchar("identifier", { length: 255 }).notNull(),
|
|
198
|
-
* token: varchar("token", { length: 255 }).notNull(),
|
|
199
|
-
* expires: timestamp("expires", { mode: "date" }).notNull(),
|
|
200
|
-
* },
|
|
201
|
-
* (vt) => ({
|
|
202
|
-
* compoundKey: primaryKey({ columns: [vt.identifier, vt.token] }),
|
|
203
|
-
* })
|
|
204
|
-
* )
|
|
205
|
-
* ```
|
|
206
|
-
*
|
|
207
|
-
* ### SQLite
|
|
208
|
-
*
|
|
209
|
-
* ```ts title="schema.ts"
|
|
210
|
-
* import { integer, sqliteTable, text, primaryKey } from "drizzle-orm/sqlite-core"
|
|
211
|
-
* import type { AdapterAccount } from "@auth/core/adapters"
|
|
212
|
-
*
|
|
213
|
-
* export const users = sqliteTable("user", {
|
|
214
|
-
* id: text("id").primaryKey().$defaultFn(() => randomUUID()),
|
|
215
|
-
* name: text("name"),
|
|
216
|
-
* email: text("email").notNull(),
|
|
217
|
-
* emailVerified: integer("emailVerified", { mode: "timestamp_ms" }),
|
|
218
|
-
* image: text("image"),
|
|
219
|
-
* })
|
|
220
|
-
*
|
|
221
|
-
* export const accounts = sqliteTable(
|
|
222
|
-
* "account",
|
|
223
|
-
* {
|
|
224
|
-
* userId: text("userId")
|
|
225
|
-
* .notNull()
|
|
226
|
-
* .references(() => users.id, { onDelete: "cascade" }),
|
|
227
|
-
* type: text("type").notNull(),
|
|
228
|
-
* provider: text("provider").notNull(),
|
|
229
|
-
* providerAccountId: text("providerAccountId").notNull(),
|
|
230
|
-
* refresh_token: text("refresh_token"),
|
|
231
|
-
* access_token: text("access_token"),
|
|
232
|
-
* expires_at: integer("expires_at"),
|
|
233
|
-
* token_type: text("token_type"),
|
|
234
|
-
* scope: text("scope"),
|
|
235
|
-
* id_token: text("id_token"),
|
|
236
|
-
* session_state: text("session_state"),
|
|
237
|
-
* },
|
|
238
|
-
* (account) => ({
|
|
239
|
-
* compoundKey: primaryKey({
|
|
240
|
-
columns: [account.provider, account.providerAccountId],
|
|
241
|
-
})
|
|
242
|
-
* })
|
|
243
|
-
* )
|
|
244
|
-
*
|
|
245
|
-
* export const sessions = sqliteTable("session", {
|
|
246
|
-
* sessionToken: text("sessionToken").primaryKey(),
|
|
247
|
-
* userId: text("userId")
|
|
248
|
-
* .notNull()
|
|
249
|
-
* .references(() => users.id, { onDelete: "cascade" }),
|
|
250
|
-
* expires: integer("expires", { mode: "timestamp_ms" }).notNull(),
|
|
251
|
-
* })
|
|
252
|
-
*
|
|
253
|
-
* export const verificationTokens = sqliteTable(
|
|
254
|
-
* "verificationToken",
|
|
255
|
-
* {
|
|
256
|
-
* identifier: text("identifier").notNull(),
|
|
257
|
-
* token: text("token").notNull(),
|
|
258
|
-
* expires: integer("expires", { mode: "timestamp_ms" }).notNull(),
|
|
259
|
-
* },
|
|
260
|
-
* (vt) => ({
|
|
261
|
-
* compoundKey: primaryKey({ columns: [vt.identifier, vt.token] }),
|
|
262
|
-
* })
|
|
263
|
-
* )
|
|
264
|
-
* ```
|
|
265
|
-
*
|
|
266
|
-
* ## Migrating your database
|
|
267
|
-
* With your schema now described in your code, you'll need to migrate your database to your schema.
|
|
268
|
-
*
|
|
269
|
-
* For full documentation on how to run migrations with Drizzle, [visit the Drizzle documentation](https://orm.drizzle.team/kit-docs/overview#running-migrations) or check how to apply changes directly to the database with [push command](https://orm.drizzle.team/kit-docs/overview#prototyping-with-db-push).
|
|
270
|
-
*
|
|
271
|
-
* ---
|
|
272
|
-
*
|
|
273
|
-
**/
|
|
274
30
|
export function DrizzleAdapter<SqlFlavor extends SqlFlavorOptions>(
|
|
275
31
|
db: SqlFlavor,
|
|
276
32
|
schema?: DefaultSchema<SqlFlavor>
|
package/src/lib/mysql.ts
CHANGED
|
@@ -2,99 +2,158 @@ import { and, eq, getTableColumns } from "drizzle-orm"
|
|
|
2
2
|
import {
|
|
3
3
|
MySqlColumn,
|
|
4
4
|
MySqlDatabase,
|
|
5
|
-
|
|
6
|
-
PreparedQueryHKTBase,
|
|
7
|
-
QueryResultHKT,
|
|
5
|
+
boolean,
|
|
8
6
|
int,
|
|
9
7
|
mysqlTable,
|
|
10
8
|
primaryKey,
|
|
11
9
|
timestamp,
|
|
12
10
|
varchar,
|
|
11
|
+
QueryResultHKT,
|
|
12
|
+
PreparedQueryHKTBase,
|
|
13
|
+
MySqlTableWithColumns,
|
|
13
14
|
} from "drizzle-orm/mysql-core"
|
|
14
15
|
|
|
15
16
|
import type {
|
|
16
17
|
Adapter,
|
|
17
18
|
AdapterAccount,
|
|
19
|
+
AdapterAccountType,
|
|
18
20
|
AdapterSession,
|
|
19
21
|
AdapterUser,
|
|
20
22
|
VerificationToken,
|
|
23
|
+
AdapterAuthenticator,
|
|
21
24
|
} from "@auth/core/adapters"
|
|
22
25
|
|
|
23
|
-
export
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
})
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
26
|
+
export function defineTables(
|
|
27
|
+
schema: Partial<DefaultMySqlSchema> = {}
|
|
28
|
+
): Required<DefaultMySqlSchema> {
|
|
29
|
+
const usersTable =
|
|
30
|
+
schema.usersTable ??
|
|
31
|
+
(mysqlTable("user", {
|
|
32
|
+
id: varchar("id", { length: 255 })
|
|
33
|
+
.primaryKey()
|
|
34
|
+
.$defaultFn(() => crypto.randomUUID()),
|
|
35
|
+
name: varchar("name", { length: 255 }),
|
|
36
|
+
email: varchar("email", { length: 255 }).notNull(),
|
|
37
|
+
emailVerified: timestamp("emailVerified", { mode: "date", fsp: 3 }),
|
|
38
|
+
image: varchar("image", { length: 255 }),
|
|
39
|
+
}) satisfies DefaultMySqlUsersTable)
|
|
40
|
+
|
|
41
|
+
const accountsTable =
|
|
42
|
+
schema.accountsTable ??
|
|
43
|
+
(mysqlTable(
|
|
44
|
+
"account",
|
|
45
|
+
{
|
|
46
|
+
userId: varchar("userId", { length: 255 })
|
|
47
|
+
.notNull()
|
|
48
|
+
.references(() => usersTable.id, { onDelete: "cascade" }),
|
|
49
|
+
type: varchar("type", { length: 255 })
|
|
50
|
+
.$type<AdapterAccountType>()
|
|
51
|
+
.notNull(),
|
|
52
|
+
provider: varchar("provider", { length: 255 }).notNull(),
|
|
53
|
+
providerAccountId: varchar("providerAccountId", {
|
|
54
|
+
length: 255,
|
|
55
|
+
}).notNull(),
|
|
56
|
+
refresh_token: varchar("refresh_token", { length: 255 }),
|
|
57
|
+
access_token: varchar("access_token", { length: 255 }),
|
|
58
|
+
expires_at: int("expires_at"),
|
|
59
|
+
token_type: varchar("token_type", { length: 255 }),
|
|
60
|
+
scope: varchar("scope", { length: 255 }),
|
|
61
|
+
id_token: varchar("id_token", { length: 2048 }),
|
|
62
|
+
session_state: varchar("session_state", { length: 255 }),
|
|
63
|
+
},
|
|
64
|
+
(account) => ({
|
|
65
|
+
compositePk: primaryKey({
|
|
66
|
+
columns: [account.provider, account.providerAccountId],
|
|
67
|
+
}),
|
|
68
|
+
})
|
|
69
|
+
) satisfies DefaultMySqlAccountsTable)
|
|
70
|
+
|
|
71
|
+
const sessionsTable =
|
|
72
|
+
schema.sessionsTable ??
|
|
73
|
+
(mysqlTable("session", {
|
|
74
|
+
sessionToken: varchar("sessionToken", { length: 255 }).primaryKey(),
|
|
75
|
+
userId: varchar("userId", { length: 255 })
|
|
76
|
+
.notNull()
|
|
77
|
+
.references(() => usersTable.id, { onDelete: "cascade" }),
|
|
78
|
+
expires: timestamp("expires", { mode: "date" }).notNull(),
|
|
79
|
+
}) satisfies DefaultMySqlSessionsTable)
|
|
80
|
+
|
|
81
|
+
const verificationTokensTable =
|
|
82
|
+
schema.verificationTokensTable ??
|
|
83
|
+
(mysqlTable(
|
|
84
|
+
"verificationToken",
|
|
85
|
+
{
|
|
86
|
+
identifier: varchar("identifier", { length: 255 }).notNull(),
|
|
87
|
+
token: varchar("token", { length: 255 }).notNull(),
|
|
88
|
+
expires: timestamp("expires", { mode: "date" }).notNull(),
|
|
89
|
+
},
|
|
90
|
+
(verficationToken) => ({
|
|
91
|
+
compositePk: primaryKey({
|
|
92
|
+
columns: [verficationToken.identifier, verficationToken.token],
|
|
93
|
+
}),
|
|
94
|
+
})
|
|
95
|
+
) satisfies DefaultMySqlVerificationTokenTable)
|
|
96
|
+
|
|
97
|
+
const authenticatorsTable =
|
|
98
|
+
schema.authenticatorsTable ??
|
|
99
|
+
(mysqlTable(
|
|
100
|
+
"authenticator",
|
|
101
|
+
{
|
|
102
|
+
credentialID: varchar("credentialID", { length: 255 })
|
|
103
|
+
.notNull()
|
|
104
|
+
.unique(),
|
|
105
|
+
userId: varchar("userId", { length: 255 })
|
|
106
|
+
.notNull()
|
|
107
|
+
.references(() => usersTable.id, { onDelete: "cascade" }),
|
|
108
|
+
providerAccountId: varchar("providerAccountId", {
|
|
109
|
+
length: 255,
|
|
110
|
+
}).notNull(),
|
|
111
|
+
credentialPublicKey: varchar("credentialPublicKey", {
|
|
112
|
+
length: 255,
|
|
113
|
+
}).notNull(),
|
|
114
|
+
counter: int("counter").notNull(),
|
|
115
|
+
credentialDeviceType: varchar("credentialDeviceType", {
|
|
116
|
+
length: 255,
|
|
117
|
+
}).notNull(),
|
|
118
|
+
credentialBackedUp: boolean("credentialBackedUp").notNull(),
|
|
119
|
+
transports: varchar("transports", { length: 255 }),
|
|
120
|
+
},
|
|
121
|
+
(authenticator) => ({
|
|
122
|
+
compositePk: primaryKey({
|
|
123
|
+
columns: [authenticator.userId, authenticator.credentialID],
|
|
124
|
+
}),
|
|
125
|
+
})
|
|
126
|
+
) satisfies DefaultMySqlAuthenticatorTable)
|
|
127
|
+
|
|
128
|
+
return {
|
|
129
|
+
usersTable,
|
|
130
|
+
accountsTable,
|
|
131
|
+
sessionsTable,
|
|
132
|
+
verificationTokensTable,
|
|
133
|
+
authenticatorsTable,
|
|
134
|
+
}
|
|
135
|
+
}
|
|
78
136
|
|
|
79
137
|
export function MySqlDrizzleAdapter(
|
|
80
138
|
client: MySqlDatabase<QueryResultHKT, PreparedQueryHKTBase, any>,
|
|
81
|
-
schema
|
|
82
|
-
usersTable: mysqlUsersTable,
|
|
83
|
-
accountsTable: mysqlAccountsTable,
|
|
84
|
-
sessionsTable: mysqlSessionsTable,
|
|
85
|
-
verificationTokensTable: mysqlVerificationTokensTable,
|
|
86
|
-
}
|
|
139
|
+
schema?: DefaultMySqlSchema
|
|
87
140
|
): Adapter {
|
|
88
|
-
const {
|
|
89
|
-
|
|
141
|
+
const {
|
|
142
|
+
usersTable,
|
|
143
|
+
accountsTable,
|
|
144
|
+
sessionsTable,
|
|
145
|
+
verificationTokensTable,
|
|
146
|
+
authenticatorsTable,
|
|
147
|
+
} = defineTables(schema)
|
|
90
148
|
|
|
91
149
|
return {
|
|
92
150
|
async createUser(data: AdapterUser) {
|
|
151
|
+
const { id, ...insertData } = data
|
|
93
152
|
const hasDefaultId = getTableColumns(usersTable)["id"]["hasDefault"]
|
|
94
153
|
|
|
95
154
|
await client
|
|
96
155
|
.insert(usersTable)
|
|
97
|
-
.values(hasDefaultId ?
|
|
156
|
+
.values(hasDefaultId ? insertData : { ...insertData, id })
|
|
98
157
|
|
|
99
158
|
return client
|
|
100
159
|
.select()
|
|
@@ -252,21 +311,77 @@ export function MySqlDrizzleAdapter(
|
|
|
252
311
|
)
|
|
253
312
|
)
|
|
254
313
|
},
|
|
314
|
+
async getAccount(providerAccountId: string, provider: string) {
|
|
315
|
+
return client
|
|
316
|
+
.select()
|
|
317
|
+
.from(accountsTable)
|
|
318
|
+
.where(
|
|
319
|
+
and(
|
|
320
|
+
eq(accountsTable.provider, provider),
|
|
321
|
+
eq(accountsTable.providerAccountId, providerAccountId)
|
|
322
|
+
)
|
|
323
|
+
)
|
|
324
|
+
.then((res) => res[0] ?? null) as Promise<AdapterAccount | null>
|
|
325
|
+
},
|
|
326
|
+
async createAuthenticator(data: AdapterAuthenticator) {
|
|
327
|
+
await client.insert(authenticatorsTable).values(data)
|
|
328
|
+
|
|
329
|
+
return await client
|
|
330
|
+
.select()
|
|
331
|
+
.from(authenticatorsTable)
|
|
332
|
+
.where(eq(authenticatorsTable.credentialID, data.credentialID))
|
|
333
|
+
.then((res) => res[0] ?? null)
|
|
334
|
+
},
|
|
335
|
+
async getAuthenticator(credentialID: string) {
|
|
336
|
+
return await client
|
|
337
|
+
.select()
|
|
338
|
+
.from(authenticatorsTable)
|
|
339
|
+
.where(eq(authenticatorsTable.credentialID, credentialID))
|
|
340
|
+
.then((res) => res[0] ?? null)
|
|
341
|
+
},
|
|
342
|
+
async listAuthenticatorsByUserId(userId: string) {
|
|
343
|
+
return await client
|
|
344
|
+
.select()
|
|
345
|
+
.from(authenticatorsTable)
|
|
346
|
+
.where(eq(authenticatorsTable.userId, userId))
|
|
347
|
+
.then((res) => res)
|
|
348
|
+
},
|
|
349
|
+
async updateAuthenticatorCounter(credentialID: string, newCounter: number) {
|
|
350
|
+
await client
|
|
351
|
+
.update(authenticatorsTable)
|
|
352
|
+
.set({ counter: newCounter })
|
|
353
|
+
.where(eq(authenticatorsTable.credentialID, credentialID))
|
|
354
|
+
|
|
355
|
+
const authenticator = await client
|
|
356
|
+
.select()
|
|
357
|
+
.from(authenticatorsTable)
|
|
358
|
+
.where(eq(authenticatorsTable.credentialID, credentialID))
|
|
359
|
+
.then((res) => res[0])
|
|
360
|
+
|
|
361
|
+
if (!authenticator) throw new Error("Authenticator not found.")
|
|
362
|
+
|
|
363
|
+
return authenticator
|
|
364
|
+
},
|
|
255
365
|
}
|
|
256
366
|
}
|
|
257
367
|
|
|
258
368
|
type DefaultMyqlColumn<
|
|
259
369
|
T extends {
|
|
260
|
-
data: string | number | Date
|
|
261
|
-
dataType: "string" | "number" | "date"
|
|
370
|
+
data: string | number | boolean | Date
|
|
371
|
+
dataType: "string" | "number" | "boolean" | "date"
|
|
262
372
|
notNull: boolean
|
|
263
|
-
columnType:
|
|
373
|
+
columnType:
|
|
374
|
+
| "MySqlVarChar"
|
|
375
|
+
| "MySqlText"
|
|
376
|
+
| "MySqlBoolean"
|
|
377
|
+
| "MySqlTimestamp"
|
|
378
|
+
| "MySqlInt"
|
|
264
379
|
},
|
|
265
380
|
> = MySqlColumn<{
|
|
266
381
|
name: string
|
|
267
382
|
columnType: T["columnType"]
|
|
268
383
|
data: T["data"]
|
|
269
|
-
driverParam: string | number
|
|
384
|
+
driverParam: string | number | boolean
|
|
270
385
|
notNull: T["notNull"]
|
|
271
386
|
hasDefault: boolean
|
|
272
387
|
enumValues: any
|
|
@@ -439,9 +554,66 @@ export type DefaultMySqlVerificationTokenTable = MySqlTableWithColumns<{
|
|
|
439
554
|
schema: string | undefined
|
|
440
555
|
}>
|
|
441
556
|
|
|
557
|
+
export type DefaultMySqlAuthenticatorTable = MySqlTableWithColumns<{
|
|
558
|
+
name: string
|
|
559
|
+
columns: {
|
|
560
|
+
credentialID: DefaultMyqlColumn<{
|
|
561
|
+
columnType: "MySqlVarChar" | "MySqlText"
|
|
562
|
+
data: string
|
|
563
|
+
notNull: true
|
|
564
|
+
dataType: "string"
|
|
565
|
+
}>
|
|
566
|
+
userId: DefaultMyqlColumn<{
|
|
567
|
+
columnType: "MySqlVarChar" | "MySqlText"
|
|
568
|
+
data: string
|
|
569
|
+
notNull: true
|
|
570
|
+
dataType: "string"
|
|
571
|
+
}>
|
|
572
|
+
providerAccountId: DefaultMyqlColumn<{
|
|
573
|
+
columnType: "MySqlVarChar" | "MySqlText"
|
|
574
|
+
data: string
|
|
575
|
+
notNull: true
|
|
576
|
+
dataType: "string"
|
|
577
|
+
}>
|
|
578
|
+
credentialPublicKey: DefaultMyqlColumn<{
|
|
579
|
+
columnType: "MySqlVarChar" | "MySqlText"
|
|
580
|
+
data: string
|
|
581
|
+
notNull: true
|
|
582
|
+
dataType: "string"
|
|
583
|
+
}>
|
|
584
|
+
counter: DefaultMyqlColumn<{
|
|
585
|
+
columnType: "MySqlInt"
|
|
586
|
+
data: number
|
|
587
|
+
notNull: true
|
|
588
|
+
dataType: "number"
|
|
589
|
+
}>
|
|
590
|
+
credentialDeviceType: DefaultMyqlColumn<{
|
|
591
|
+
columnType: "MySqlVarChar" | "MySqlText"
|
|
592
|
+
data: string
|
|
593
|
+
notNull: true
|
|
594
|
+
dataType: "string"
|
|
595
|
+
}>
|
|
596
|
+
credentialBackedUp: DefaultMyqlColumn<{
|
|
597
|
+
columnType: "MySqlBoolean"
|
|
598
|
+
data: boolean
|
|
599
|
+
notNull: true
|
|
600
|
+
dataType: "boolean"
|
|
601
|
+
}>
|
|
602
|
+
transports: DefaultMyqlColumn<{
|
|
603
|
+
columnType: "MySqlVarChar" | "MySqlText"
|
|
604
|
+
data: string
|
|
605
|
+
notNull: false
|
|
606
|
+
dataType: "string"
|
|
607
|
+
}>
|
|
608
|
+
}
|
|
609
|
+
dialect: "mysql"
|
|
610
|
+
schema: string | undefined
|
|
611
|
+
}>
|
|
612
|
+
|
|
442
613
|
export type DefaultMySqlSchema = {
|
|
443
614
|
usersTable: DefaultMySqlUsersTable
|
|
444
615
|
accountsTable: DefaultMySqlAccountsTable
|
|
445
|
-
sessionsTable
|
|
446
|
-
verificationTokensTable
|
|
616
|
+
sessionsTable?: DefaultMySqlSessionsTable
|
|
617
|
+
verificationTokensTable?: DefaultMySqlVerificationTokenTable
|
|
618
|
+
authenticatorsTable?: DefaultMySqlAuthenticatorTable
|
|
447
619
|
}
|