@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/README.md +1 -1
- package/index.d.ts +1 -246
- package/index.d.ts.map +1 -1
- package/index.js +1 -246
- package/lib/mysql.d.ts +3 -294
- package/lib/mysql.d.ts.map +1 -1
- package/lib/mysql.js +62 -52
- package/lib/pg.d.ts +3 -294
- package/lib/pg.d.ts.map +1 -1
- package/lib/pg.js +60 -52
- package/lib/sqlite.d.ts +3 -294
- package/lib/sqlite.d.ts.map +1 -1
- package/lib/sqlite.js +58 -50
- package/package.json +5 -6
- package/src/index.ts +1 -245
- package/src/lib/mysql.ts +80 -62
- package/src/lib/pg.ts +79 -63
- package/src/lib/sqlite.ts +76 -60
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
|
@@ -15,86 +15,104 @@ import {
|
|
|
15
15
|
import type {
|
|
16
16
|
Adapter,
|
|
17
17
|
AdapterAccount,
|
|
18
|
+
AdapterAccountType,
|
|
18
19
|
AdapterSession,
|
|
19
20
|
AdapterUser,
|
|
20
21
|
VerificationToken,
|
|
21
22
|
} from "@auth/core/adapters"
|
|
22
23
|
|
|
23
|
-
export
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
24
|
+
export function defineTables(
|
|
25
|
+
schema: Partial<DefaultMySqlSchema> = {}
|
|
26
|
+
): Required<DefaultMySqlSchema> {
|
|
27
|
+
const usersTable =
|
|
28
|
+
schema.usersTable ??
|
|
29
|
+
(mysqlTable("user", {
|
|
30
|
+
id: varchar("id", { length: 255 })
|
|
31
|
+
.primaryKey()
|
|
32
|
+
.$defaultFn(() => crypto.randomUUID()),
|
|
33
|
+
name: varchar("name", { length: 255 }),
|
|
34
|
+
email: varchar("email", { length: 255 }).notNull(),
|
|
35
|
+
emailVerified: timestamp("emailVerified", { mode: "date", fsp: 3 }),
|
|
36
|
+
image: varchar("image", { length: 255 }),
|
|
37
|
+
}) satisfies DefaultMySqlUsersTable)
|
|
32
38
|
|
|
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
|
-
|
|
39
|
+
const accountsTable =
|
|
40
|
+
schema.accountsTable ??
|
|
41
|
+
(mysqlTable(
|
|
42
|
+
"account",
|
|
43
|
+
{
|
|
44
|
+
userId: varchar("userId", { length: 255 })
|
|
45
|
+
.notNull()
|
|
46
|
+
.references(() => usersTable.id, { onDelete: "cascade" }),
|
|
47
|
+
type: varchar("type", { length: 255 })
|
|
48
|
+
.$type<AdapterAccountType>()
|
|
49
|
+
.notNull(),
|
|
50
|
+
provider: varchar("provider", { length: 255 }).notNull(),
|
|
51
|
+
providerAccountId: varchar("providerAccountId", {
|
|
52
|
+
length: 255,
|
|
53
|
+
}).notNull(),
|
|
54
|
+
refresh_token: varchar("refresh_token", { length: 255 }),
|
|
55
|
+
access_token: varchar("access_token", { length: 255 }),
|
|
56
|
+
expires_at: int("expires_at"),
|
|
57
|
+
token_type: varchar("token_type", { length: 255 }),
|
|
58
|
+
scope: varchar("scope", { length: 255 }),
|
|
59
|
+
id_token: varchar("id_token", { length: 2048 }),
|
|
60
|
+
session_state: varchar("session_state", { length: 255 }),
|
|
61
|
+
},
|
|
62
|
+
(account) => ({
|
|
63
|
+
compositePk: primaryKey({
|
|
64
|
+
columns: [account.provider, account.providerAccountId],
|
|
65
|
+
}),
|
|
66
|
+
})
|
|
67
|
+
) satisfies DefaultMySqlAccountsTable)
|
|
58
68
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
69
|
+
const sessionsTable =
|
|
70
|
+
schema.sessionsTable ??
|
|
71
|
+
(mysqlTable("session", {
|
|
72
|
+
sessionToken: varchar("sessionToken", { length: 255 }).primaryKey(),
|
|
73
|
+
userId: varchar("userId", { length: 255 })
|
|
74
|
+
.notNull()
|
|
75
|
+
.references(() => usersTable.id, { onDelete: "cascade" }),
|
|
76
|
+
expires: timestamp("expires", { mode: "date" }).notNull(),
|
|
77
|
+
}) satisfies DefaultMySqlSessionsTable)
|
|
66
78
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
79
|
+
const verificationTokensTable =
|
|
80
|
+
schema.verificationTokensTable ??
|
|
81
|
+
(mysqlTable(
|
|
82
|
+
"verificationToken",
|
|
83
|
+
{
|
|
84
|
+
identifier: varchar("identifier", { length: 255 }).notNull(),
|
|
85
|
+
token: varchar("token", { length: 255 }).notNull(),
|
|
86
|
+
expires: timestamp("expires", { mode: "date" }).notNull(),
|
|
87
|
+
},
|
|
88
|
+
(vt) => ({
|
|
89
|
+
compositePk: primaryKey({ columns: [vt.identifier, vt.token] }),
|
|
90
|
+
})
|
|
91
|
+
) satisfies DefaultMySqlVerificationTokenTable)
|
|
92
|
+
|
|
93
|
+
return {
|
|
94
|
+
usersTable,
|
|
95
|
+
accountsTable,
|
|
96
|
+
sessionsTable,
|
|
97
|
+
verificationTokensTable,
|
|
98
|
+
}
|
|
99
|
+
}
|
|
78
100
|
|
|
79
101
|
export function MySqlDrizzleAdapter(
|
|
80
102
|
client: MySqlDatabase<QueryResultHKT, PreparedQueryHKTBase, any>,
|
|
81
|
-
schema
|
|
82
|
-
usersTable: mysqlUsersTable,
|
|
83
|
-
accountsTable: mysqlAccountsTable,
|
|
84
|
-
sessionsTable: mysqlSessionsTable,
|
|
85
|
-
verificationTokensTable: mysqlVerificationTokensTable,
|
|
86
|
-
}
|
|
103
|
+
schema?: DefaultMySqlSchema
|
|
87
104
|
): Adapter {
|
|
88
105
|
const { usersTable, accountsTable, sessionsTable, verificationTokensTable } =
|
|
89
|
-
schema
|
|
106
|
+
defineTables(schema)
|
|
90
107
|
|
|
91
108
|
return {
|
|
92
109
|
async createUser(data: AdapterUser) {
|
|
110
|
+
const { id, ...insertData } = data
|
|
93
111
|
const hasDefaultId = getTableColumns(usersTable)["id"]["hasDefault"]
|
|
94
112
|
|
|
95
113
|
await client
|
|
96
114
|
.insert(usersTable)
|
|
97
|
-
.values(hasDefaultId ?
|
|
115
|
+
.values(hasDefaultId ? insertData : { ...insertData, id })
|
|
98
116
|
|
|
99
117
|
return client
|
|
100
118
|
.select()
|
|
@@ -442,6 +460,6 @@ export type DefaultMySqlVerificationTokenTable = MySqlTableWithColumns<{
|
|
|
442
460
|
export type DefaultMySqlSchema = {
|
|
443
461
|
usersTable: DefaultMySqlUsersTable
|
|
444
462
|
accountsTable: DefaultMySqlAccountsTable
|
|
445
|
-
sessionsTable
|
|
446
|
-
verificationTokensTable
|
|
463
|
+
sessionsTable?: DefaultMySqlSessionsTable
|
|
464
|
+
verificationTokensTable?: DefaultMySqlVerificationTokenTable
|
|
447
465
|
}
|
package/src/lib/pg.ts
CHANGED
|
@@ -14,88 +14,104 @@ import {
|
|
|
14
14
|
import type {
|
|
15
15
|
Adapter,
|
|
16
16
|
AdapterAccount,
|
|
17
|
+
AdapterAccountType,
|
|
17
18
|
AdapterSession,
|
|
18
19
|
AdapterUser,
|
|
19
20
|
VerificationToken,
|
|
20
21
|
} from "@auth/core/adapters"
|
|
21
22
|
|
|
22
|
-
export
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
23
|
+
export function defineTables(
|
|
24
|
+
schema: Partial<DefaultPostgresSchema> = {}
|
|
25
|
+
): Required<DefaultPostgresSchema> {
|
|
26
|
+
const usersTable =
|
|
27
|
+
schema.usersTable ??
|
|
28
|
+
(pgTable("user", {
|
|
29
|
+
id: text("id")
|
|
30
|
+
.primaryKey()
|
|
31
|
+
.$defaultFn(() => crypto.randomUUID()),
|
|
32
|
+
name: text("name"),
|
|
33
|
+
email: text("email").notNull(),
|
|
34
|
+
emailVerified: timestamp("emailVerified", { mode: "date" }),
|
|
35
|
+
image: text("image"),
|
|
36
|
+
}) satisfies DefaultPostgresUsersTable)
|
|
31
37
|
|
|
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
|
-
|
|
38
|
+
const accountsTable =
|
|
39
|
+
schema.accountsTable ??
|
|
40
|
+
(pgTable(
|
|
41
|
+
"account",
|
|
42
|
+
{
|
|
43
|
+
userId: text("userId")
|
|
44
|
+
.notNull()
|
|
45
|
+
.references(() => usersTable.id, { onDelete: "cascade" }),
|
|
46
|
+
type: text("type").$type<AdapterAccountType>().notNull(),
|
|
47
|
+
provider: text("provider").notNull(),
|
|
48
|
+
providerAccountId: text("providerAccountId").notNull(),
|
|
49
|
+
refresh_token: text("refresh_token"),
|
|
50
|
+
access_token: text("access_token"),
|
|
51
|
+
expires_at: integer("expires_at"),
|
|
52
|
+
token_type: text("token_type"),
|
|
53
|
+
scope: text("scope"),
|
|
54
|
+
id_token: text("id_token"),
|
|
55
|
+
session_state: text("session_state"),
|
|
56
|
+
},
|
|
57
|
+
(table) => {
|
|
58
|
+
return {
|
|
59
|
+
compositePk: primaryKey({
|
|
60
|
+
columns: [table.provider, table.providerAccountId],
|
|
61
|
+
}),
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
) satisfies DefaultPostgresAccountsTable)
|
|
57
65
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
66
|
+
const sessionsTable =
|
|
67
|
+
schema.sessionsTable ??
|
|
68
|
+
(pgTable("session", {
|
|
69
|
+
sessionToken: text("sessionToken").primaryKey(),
|
|
70
|
+
userId: text("userId")
|
|
71
|
+
.notNull()
|
|
72
|
+
.references(() => usersTable.id, { onDelete: "cascade" }),
|
|
73
|
+
expires: timestamp("expires", { mode: "date" }).notNull(),
|
|
74
|
+
}) satisfies DefaultPostgresSessionsTable)
|
|
65
75
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
76
|
+
const verificationTokensTable =
|
|
77
|
+
schema.verificationTokensTable ??
|
|
78
|
+
(pgTable(
|
|
79
|
+
"verificationToken",
|
|
80
|
+
{
|
|
81
|
+
identifier: text("identifier").notNull(),
|
|
82
|
+
token: text("token").notNull(),
|
|
83
|
+
expires: timestamp("expires", { mode: "date" }).notNull(),
|
|
84
|
+
},
|
|
85
|
+
(table) => {
|
|
86
|
+
return {
|
|
87
|
+
compositePk: primaryKey({ columns: [table.identifier, table.token] }),
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
) satisfies DefaultPostgresVerificationTokenTable)
|
|
91
|
+
|
|
92
|
+
return {
|
|
93
|
+
usersTable,
|
|
94
|
+
accountsTable,
|
|
95
|
+
sessionsTable,
|
|
96
|
+
verificationTokensTable,
|
|
77
97
|
}
|
|
78
|
-
|
|
98
|
+
}
|
|
79
99
|
|
|
80
100
|
export function PostgresDrizzleAdapter(
|
|
81
101
|
client: PgDatabase<QueryResultHKT, any>,
|
|
82
|
-
schema
|
|
83
|
-
usersTable: postgresUsersTable,
|
|
84
|
-
accountsTable: postgresAccountsTable,
|
|
85
|
-
sessionsTable: postgresSessionsTable,
|
|
86
|
-
verificationTokensTable: postgresVerificationTokensTable,
|
|
87
|
-
}
|
|
102
|
+
schema?: DefaultPostgresSchema
|
|
88
103
|
): Adapter {
|
|
89
104
|
const { usersTable, accountsTable, sessionsTable, verificationTokensTable } =
|
|
90
|
-
schema
|
|
105
|
+
defineTables(schema)
|
|
91
106
|
|
|
92
107
|
return {
|
|
93
108
|
async createUser(data: AdapterUser) {
|
|
109
|
+
const { id, ...insertData } = data
|
|
94
110
|
const hasDefaultId = getTableColumns(usersTable)["id"]["hasDefault"]
|
|
95
111
|
|
|
96
112
|
return client
|
|
97
113
|
.insert(usersTable)
|
|
98
|
-
.values(hasDefaultId ?
|
|
114
|
+
.values(hasDefaultId ? insertData : { ...insertData, id })
|
|
99
115
|
.returning()
|
|
100
116
|
.then((res) => res[0])
|
|
101
117
|
},
|
|
@@ -413,6 +429,6 @@ export type DefaultPostgresVerificationTokenTable = PgTableWithColumns<{
|
|
|
413
429
|
export type DefaultPostgresSchema = {
|
|
414
430
|
usersTable: DefaultPostgresUsersTable
|
|
415
431
|
accountsTable: DefaultPostgresAccountsTable
|
|
416
|
-
sessionsTable
|
|
417
|
-
verificationTokensTable
|
|
432
|
+
sessionsTable?: DefaultPostgresSessionsTable
|
|
433
|
+
verificationTokensTable?: DefaultPostgresVerificationTokenTable
|
|
418
434
|
}
|