@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/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
<img height="64px" src="https://authjs.dev/img/logo-sm.png" />
|
|
5
5
|
</a>
|
|
6
6
|
<a href="https://github.com/drizzle-team/drizzle-orm" target="_blank">
|
|
7
|
-
<img height="64px" src="https://authjs.dev/img/adapters/drizzle
|
|
7
|
+
<img height="64px" src="https://authjs.dev/img/adapters/drizzle.svg"/>
|
|
8
8
|
</a>
|
|
9
9
|
<h3 align="center"><b>Drizzle ORM Adapter</b> - NextAuth.js / Auth.js</a></h3>
|
|
10
10
|
<p align="center" style="align: center;">
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
<img src="https://img.shields.io/npm/dm/@auth/drizzle-adapter?label=%20downloads&style=flat-square" alt="Downloads" />
|
|
19
19
|
</a>
|
|
20
20
|
<a href="https://github.com/nextauthjs/next-auth/stargazers">
|
|
21
|
-
<img src="https://img.shields.io/github/stars/nextauthjs/next-auth?style=flat-square" alt="
|
|
21
|
+
<img src="https://img.shields.io/github/stars/nextauthjs/next-auth?style=flat-square" alt="GitHub Stars" />
|
|
22
22
|
</a>
|
|
23
23
|
</p>
|
|
24
24
|
</p>
|
package/index.d.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>
|
|
@@ -17,250 +17,5 @@
|
|
|
17
17
|
*/
|
|
18
18
|
import { DefaultSchema, SqlFlavorOptions } from "./lib/utils.js";
|
|
19
19
|
import type { Adapter } from "@auth/core/adapters";
|
|
20
|
-
/**
|
|
21
|
-
* Create db instance and pass it to adapter. Add this adapter to your `auth.ts` Auth.js configuration object:
|
|
22
|
-
*
|
|
23
|
-
* ```ts title="auth.ts"
|
|
24
|
-
* import NextAuth from "next-auth"
|
|
25
|
-
* import Google from "next-auth/providers/google"
|
|
26
|
-
* import { DrizzleAdapter } from "@auth/drizzle-adapter"
|
|
27
|
-
* import { db } from "./db.ts"
|
|
28
|
-
*
|
|
29
|
-
* export const { handlers, auth } = NextAuth({
|
|
30
|
-
* adapter: DrizzleAdapter(db),
|
|
31
|
-
* providers: [
|
|
32
|
-
* Google,
|
|
33
|
-
* ],
|
|
34
|
-
* })
|
|
35
|
-
* ```
|
|
36
|
-
*
|
|
37
|
-
* 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).
|
|
38
|
-
*
|
|
39
|
-
* :::info
|
|
40
|
-
* 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.
|
|
41
|
-
* :::
|
|
42
|
-
*
|
|
43
|
-
* ```ts title="auth.ts"
|
|
44
|
-
* import NextAuth from "next-auth"
|
|
45
|
-
* import Google from "next-auth/providers/google"
|
|
46
|
-
* import { DrizzleAdapter } from "@auth/drizzle-adapter"
|
|
47
|
-
* import { accounts, sessions, users, verificationTokens } from "./schema"
|
|
48
|
-
* import { db } from "./db.ts"
|
|
49
|
-
*
|
|
50
|
-
* export const { handlers, auth } = NextAuth({
|
|
51
|
-
* adapter: DrizzleAdapter(db, { usersTable: users, accountsTable: accounts, sessionsTable: sessions, verificationTokensTable: verificationTokens }),
|
|
52
|
-
* providers: [
|
|
53
|
-
* Google,
|
|
54
|
-
* ],
|
|
55
|
-
* })
|
|
56
|
-
* ```
|
|
57
|
-
*
|
|
58
|
-
* ## Setup
|
|
59
|
-
*
|
|
60
|
-
* 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.
|
|
61
|
-
* Additionally, you may extend the schema from the minimum requirements to suit your needs.
|
|
62
|
-
*
|
|
63
|
-
* - [Postgres](#postgres)
|
|
64
|
-
* - [MySQL](#mysql)
|
|
65
|
-
* - [SQLite](#sqlite)
|
|
66
|
-
*
|
|
67
|
-
* ### Postgres
|
|
68
|
-
|
|
69
|
-
* ```ts title="schema.ts"
|
|
70
|
-
* import {
|
|
71
|
-
* timestamp,
|
|
72
|
-
* pgTable,
|
|
73
|
-
* text,
|
|
74
|
-
* primaryKey,
|
|
75
|
-
* integer
|
|
76
|
-
* } from "drizzle-orm/pg-core"
|
|
77
|
-
* import type { AdapterAccount } from '@auth/core/adapters'
|
|
78
|
-
* import { randomUUID } from "crypto"
|
|
79
|
-
*
|
|
80
|
-
* export const users = pgTable("user", {
|
|
81
|
-
* id: text("id").primaryKey().$defaultFn(() => randomUUID()),
|
|
82
|
-
* name: text("name"),
|
|
83
|
-
* email: text("email").notNull(),
|
|
84
|
-
* emailVerified: timestamp("emailVerified", { mode: "date" }),
|
|
85
|
-
* image: text("image"),
|
|
86
|
-
* })
|
|
87
|
-
*
|
|
88
|
-
* export const accounts = pgTable(
|
|
89
|
-
* "account",
|
|
90
|
-
* {
|
|
91
|
-
* userId: text("userId")
|
|
92
|
-
* .notNull()
|
|
93
|
-
* .references(() => users.id, { onDelete: "cascade" }),
|
|
94
|
-
* type: text("type").notNull(),
|
|
95
|
-
* provider: text("provider").notNull(),
|
|
96
|
-
* providerAccountId: text("providerAccountId").notNull(),
|
|
97
|
-
* refresh_token: text("refresh_token"),
|
|
98
|
-
* access_token: text("access_token"),
|
|
99
|
-
* expires_at: integer("expires_at"),
|
|
100
|
-
* token_type: text("token_type"),
|
|
101
|
-
* scope: text("scope"),
|
|
102
|
-
* id_token: text("id_token"),
|
|
103
|
-
* session_state: text("session_state"),
|
|
104
|
-
* },
|
|
105
|
-
* (account) => ({
|
|
106
|
-
* compoundKey: primaryKey({ columns: [account.provider, account.providerAccountId] }),
|
|
107
|
-
* })
|
|
108
|
-
* )
|
|
109
|
-
*
|
|
110
|
-
* export const sessions = pgTable("session", {
|
|
111
|
-
* sessionToken: text("sessionToken").primaryKey(),
|
|
112
|
-
* userId: text("userId")
|
|
113
|
-
* .notNull()
|
|
114
|
-
* .references(() => users.id, { onDelete: "cascade" }),
|
|
115
|
-
* expires: timestamp("expires", { mode: "date" }).notNull(),
|
|
116
|
-
* })
|
|
117
|
-
*
|
|
118
|
-
* export const verificationTokens = pgTable(
|
|
119
|
-
* "verificationToken",
|
|
120
|
-
* {
|
|
121
|
-
* identifier: text("identifier").notNull(),
|
|
122
|
-
* token: text("token").notNull(),
|
|
123
|
-
* expires: timestamp("expires", { mode: "date" }).notNull(),
|
|
124
|
-
* },
|
|
125
|
-
* (vt) => ({
|
|
126
|
-
* compoundKey: primaryKey({ columns: [vt.identifier, vt.token] }),
|
|
127
|
-
* })
|
|
128
|
-
* )
|
|
129
|
-
* ```
|
|
130
|
-
*
|
|
131
|
-
* ### MySQL
|
|
132
|
-
*
|
|
133
|
-
* 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.
|
|
134
|
-
*
|
|
135
|
-
* ```ts title="schema.ts"
|
|
136
|
-
* import {
|
|
137
|
-
* int,
|
|
138
|
-
* timestamp,
|
|
139
|
-
* mysqlTable,
|
|
140
|
-
* primaryKey,
|
|
141
|
-
* varchar,
|
|
142
|
-
* } from "drizzle-orm/mysql-core"
|
|
143
|
-
* import type { AdapterAccount } from "@auth/core/adapters"
|
|
144
|
-
*
|
|
145
|
-
* export const users = mysqlTable("user", {
|
|
146
|
-
* id: varchar("id", { length: 255 }).primaryKey().$defaultFn(() => randomUUID()),
|
|
147
|
-
* name: varchar("name", { length: 255 }),
|
|
148
|
-
* email: varchar("email", { length: 255 }).notNull(),
|
|
149
|
-
* emailVerified: timestamp("emailVerified", { mode: "date", fsp: 3 }),
|
|
150
|
-
* image: varchar("image", { length: 255 }),
|
|
151
|
-
* })
|
|
152
|
-
*
|
|
153
|
-
* export const accounts = mysqlTable(
|
|
154
|
-
* "account",
|
|
155
|
-
* {
|
|
156
|
-
* userId: varchar("userId", { length: 255 })
|
|
157
|
-
* .notNull()
|
|
158
|
-
* .references(() => users.id, { onDelete: "cascade" }),
|
|
159
|
-
* type: varchar("type", { length: 255 }).notNull(),
|
|
160
|
-
* provider: varchar("provider", { length: 255 }).notNull(),
|
|
161
|
-
* providerAccountId: varchar("providerAccountId", { length: 255 }).notNull(),
|
|
162
|
-
* refresh_token: varchar("refresh_token", { length: 255 }),
|
|
163
|
-
* access_token: varchar("access_token", { length: 255 }),
|
|
164
|
-
* expires_at: int("expires_at"),
|
|
165
|
-
* token_type: varchar("token_type", { length: 255 }),
|
|
166
|
-
* scope: varchar("scope", { length: 255 }),
|
|
167
|
-
* id_token: varchar("id_token", { length: 2048 }),
|
|
168
|
-
* session_state: varchar("session_state", { length: 255 }),
|
|
169
|
-
* },
|
|
170
|
-
* (account) => ({
|
|
171
|
-
* compoundKey: primaryKey({
|
|
172
|
-
columns: [account.provider, account.providerAccountId],
|
|
173
|
-
})
|
|
174
|
-
* })
|
|
175
|
-
* )
|
|
176
|
-
*
|
|
177
|
-
* export const sessions = mysqlTable("session", {
|
|
178
|
-
* sessionToken: varchar("sessionToken", { length: 255 }).primaryKey(),
|
|
179
|
-
* userId: varchar("userId", { length: 255 })
|
|
180
|
-
* .notNull()
|
|
181
|
-
* .references(() => users.id, { onDelete: "cascade" }),
|
|
182
|
-
* expires: timestamp("expires", { mode: "date" }).notNull(),
|
|
183
|
-
* })
|
|
184
|
-
*
|
|
185
|
-
* export const verificationTokens = mysqlTable(
|
|
186
|
-
* "verificationToken",
|
|
187
|
-
* {
|
|
188
|
-
* identifier: varchar("identifier", { length: 255 }).notNull(),
|
|
189
|
-
* token: varchar("token", { length: 255 }).notNull(),
|
|
190
|
-
* expires: timestamp("expires", { mode: "date" }).notNull(),
|
|
191
|
-
* },
|
|
192
|
-
* (vt) => ({
|
|
193
|
-
* compoundKey: primaryKey({ columns: [vt.identifier, vt.token] }),
|
|
194
|
-
* })
|
|
195
|
-
* )
|
|
196
|
-
* ```
|
|
197
|
-
*
|
|
198
|
-
* ### SQLite
|
|
199
|
-
*
|
|
200
|
-
* ```ts title="schema.ts"
|
|
201
|
-
* import { integer, sqliteTable, text, primaryKey } from "drizzle-orm/sqlite-core"
|
|
202
|
-
* import type { AdapterAccount } from "@auth/core/adapters"
|
|
203
|
-
*
|
|
204
|
-
* export const users = sqliteTable("user", {
|
|
205
|
-
* id: text("id").primaryKey().$defaultFn(() => randomUUID()),
|
|
206
|
-
* name: text("name"),
|
|
207
|
-
* email: text("email").notNull(),
|
|
208
|
-
* emailVerified: integer("emailVerified", { mode: "timestamp_ms" }),
|
|
209
|
-
* image: text("image"),
|
|
210
|
-
* })
|
|
211
|
-
*
|
|
212
|
-
* export const accounts = sqliteTable(
|
|
213
|
-
* "account",
|
|
214
|
-
* {
|
|
215
|
-
* userId: text("userId")
|
|
216
|
-
* .notNull()
|
|
217
|
-
* .references(() => users.id, { onDelete: "cascade" }),
|
|
218
|
-
* type: text("type").notNull(),
|
|
219
|
-
* provider: text("provider").notNull(),
|
|
220
|
-
* providerAccountId: text("providerAccountId").notNull(),
|
|
221
|
-
* refresh_token: text("refresh_token"),
|
|
222
|
-
* access_token: text("access_token"),
|
|
223
|
-
* expires_at: integer("expires_at"),
|
|
224
|
-
* token_type: text("token_type"),
|
|
225
|
-
* scope: text("scope"),
|
|
226
|
-
* id_token: text("id_token"),
|
|
227
|
-
* session_state: text("session_state"),
|
|
228
|
-
* },
|
|
229
|
-
* (account) => ({
|
|
230
|
-
* compoundKey: primaryKey({
|
|
231
|
-
columns: [account.provider, account.providerAccountId],
|
|
232
|
-
})
|
|
233
|
-
* })
|
|
234
|
-
* )
|
|
235
|
-
*
|
|
236
|
-
* export const sessions = sqliteTable("session", {
|
|
237
|
-
* sessionToken: text("sessionToken").primaryKey(),
|
|
238
|
-
* userId: text("userId")
|
|
239
|
-
* .notNull()
|
|
240
|
-
* .references(() => users.id, { onDelete: "cascade" }),
|
|
241
|
-
* expires: integer("expires", { mode: "timestamp_ms" }).notNull(),
|
|
242
|
-
* })
|
|
243
|
-
*
|
|
244
|
-
* export const verificationTokens = sqliteTable(
|
|
245
|
-
* "verificationToken",
|
|
246
|
-
* {
|
|
247
|
-
* identifier: text("identifier").notNull(),
|
|
248
|
-
* token: text("token").notNull(),
|
|
249
|
-
* expires: integer("expires", { mode: "timestamp_ms" }).notNull(),
|
|
250
|
-
* },
|
|
251
|
-
* (vt) => ({
|
|
252
|
-
* compoundKey: primaryKey({ columns: [vt.identifier, vt.token] }),
|
|
253
|
-
* })
|
|
254
|
-
* )
|
|
255
|
-
* ```
|
|
256
|
-
*
|
|
257
|
-
* ## Migrating your database
|
|
258
|
-
* With your schema now described in your code, you'll need to migrate your database to your schema.
|
|
259
|
-
*
|
|
260
|
-
* 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).
|
|
261
|
-
*
|
|
262
|
-
* ---
|
|
263
|
-
*
|
|
264
|
-
**/
|
|
265
20
|
export declare function DrizzleAdapter<SqlFlavor extends SqlFlavorOptions>(db: SqlFlavor, schema?: DefaultSchema<SqlFlavor>): Adapter;
|
|
266
21
|
//# sourceMappingURL=index.d.ts.map
|
package/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AASH,OAAO,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAA;AAEhE,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AASH,OAAO,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAA;AAEhE,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAElD,wBAAgB,cAAc,CAAC,SAAS,SAAS,gBAAgB,EAC/D,EAAE,EAAE,SAAS,EACb,MAAM,CAAC,EAAE,aAAa,CAAC,SAAS,CAAC,GAChC,OAAO,CAYT"}
|
package/index.js
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>
|
|
@@ -22,251 +22,6 @@ import { BaseSQLiteDatabase } from "drizzle-orm/sqlite-core";
|
|
|
22
22
|
import { MySqlDrizzleAdapter } from "./lib/mysql.js";
|
|
23
23
|
import { PostgresDrizzleAdapter } from "./lib/pg.js";
|
|
24
24
|
import { SQLiteDrizzleAdapter } from "./lib/sqlite.js";
|
|
25
|
-
/**
|
|
26
|
-
* Create db instance and pass it to adapter. Add this adapter to your `auth.ts` Auth.js configuration object:
|
|
27
|
-
*
|
|
28
|
-
* ```ts title="auth.ts"
|
|
29
|
-
* import NextAuth from "next-auth"
|
|
30
|
-
* import Google from "next-auth/providers/google"
|
|
31
|
-
* import { DrizzleAdapter } from "@auth/drizzle-adapter"
|
|
32
|
-
* import { db } from "./db.ts"
|
|
33
|
-
*
|
|
34
|
-
* export const { handlers, auth } = NextAuth({
|
|
35
|
-
* adapter: DrizzleAdapter(db),
|
|
36
|
-
* providers: [
|
|
37
|
-
* Google,
|
|
38
|
-
* ],
|
|
39
|
-
* })
|
|
40
|
-
* ```
|
|
41
|
-
*
|
|
42
|
-
* 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).
|
|
43
|
-
*
|
|
44
|
-
* :::info
|
|
45
|
-
* 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.
|
|
46
|
-
* :::
|
|
47
|
-
*
|
|
48
|
-
* ```ts title="auth.ts"
|
|
49
|
-
* import NextAuth from "next-auth"
|
|
50
|
-
* import Google from "next-auth/providers/google"
|
|
51
|
-
* import { DrizzleAdapter } from "@auth/drizzle-adapter"
|
|
52
|
-
* import { accounts, sessions, users, verificationTokens } from "./schema"
|
|
53
|
-
* import { db } from "./db.ts"
|
|
54
|
-
*
|
|
55
|
-
* export const { handlers, auth } = NextAuth({
|
|
56
|
-
* adapter: DrizzleAdapter(db, { usersTable: users, accountsTable: accounts, sessionsTable: sessions, verificationTokensTable: verificationTokens }),
|
|
57
|
-
* providers: [
|
|
58
|
-
* Google,
|
|
59
|
-
* ],
|
|
60
|
-
* })
|
|
61
|
-
* ```
|
|
62
|
-
*
|
|
63
|
-
* ## Setup
|
|
64
|
-
*
|
|
65
|
-
* 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.
|
|
66
|
-
* Additionally, you may extend the schema from the minimum requirements to suit your needs.
|
|
67
|
-
*
|
|
68
|
-
* - [Postgres](#postgres)
|
|
69
|
-
* - [MySQL](#mysql)
|
|
70
|
-
* - [SQLite](#sqlite)
|
|
71
|
-
*
|
|
72
|
-
* ### Postgres
|
|
73
|
-
|
|
74
|
-
* ```ts title="schema.ts"
|
|
75
|
-
* import {
|
|
76
|
-
* timestamp,
|
|
77
|
-
* pgTable,
|
|
78
|
-
* text,
|
|
79
|
-
* primaryKey,
|
|
80
|
-
* integer
|
|
81
|
-
* } from "drizzle-orm/pg-core"
|
|
82
|
-
* import type { AdapterAccount } from '@auth/core/adapters'
|
|
83
|
-
* import { randomUUID } from "crypto"
|
|
84
|
-
*
|
|
85
|
-
* export const users = pgTable("user", {
|
|
86
|
-
* id: text("id").primaryKey().$defaultFn(() => randomUUID()),
|
|
87
|
-
* name: text("name"),
|
|
88
|
-
* email: text("email").notNull(),
|
|
89
|
-
* emailVerified: timestamp("emailVerified", { mode: "date" }),
|
|
90
|
-
* image: text("image"),
|
|
91
|
-
* })
|
|
92
|
-
*
|
|
93
|
-
* export const accounts = pgTable(
|
|
94
|
-
* "account",
|
|
95
|
-
* {
|
|
96
|
-
* userId: text("userId")
|
|
97
|
-
* .notNull()
|
|
98
|
-
* .references(() => users.id, { onDelete: "cascade" }),
|
|
99
|
-
* type: text("type").notNull(),
|
|
100
|
-
* provider: text("provider").notNull(),
|
|
101
|
-
* providerAccountId: text("providerAccountId").notNull(),
|
|
102
|
-
* refresh_token: text("refresh_token"),
|
|
103
|
-
* access_token: text("access_token"),
|
|
104
|
-
* expires_at: integer("expires_at"),
|
|
105
|
-
* token_type: text("token_type"),
|
|
106
|
-
* scope: text("scope"),
|
|
107
|
-
* id_token: text("id_token"),
|
|
108
|
-
* session_state: text("session_state"),
|
|
109
|
-
* },
|
|
110
|
-
* (account) => ({
|
|
111
|
-
* compoundKey: primaryKey({ columns: [account.provider, account.providerAccountId] }),
|
|
112
|
-
* })
|
|
113
|
-
* )
|
|
114
|
-
*
|
|
115
|
-
* export const sessions = pgTable("session", {
|
|
116
|
-
* sessionToken: text("sessionToken").primaryKey(),
|
|
117
|
-
* userId: text("userId")
|
|
118
|
-
* .notNull()
|
|
119
|
-
* .references(() => users.id, { onDelete: "cascade" }),
|
|
120
|
-
* expires: timestamp("expires", { mode: "date" }).notNull(),
|
|
121
|
-
* })
|
|
122
|
-
*
|
|
123
|
-
* export const verificationTokens = pgTable(
|
|
124
|
-
* "verificationToken",
|
|
125
|
-
* {
|
|
126
|
-
* identifier: text("identifier").notNull(),
|
|
127
|
-
* token: text("token").notNull(),
|
|
128
|
-
* expires: timestamp("expires", { mode: "date" }).notNull(),
|
|
129
|
-
* },
|
|
130
|
-
* (vt) => ({
|
|
131
|
-
* compoundKey: primaryKey({ columns: [vt.identifier, vt.token] }),
|
|
132
|
-
* })
|
|
133
|
-
* )
|
|
134
|
-
* ```
|
|
135
|
-
*
|
|
136
|
-
* ### MySQL
|
|
137
|
-
*
|
|
138
|
-
* 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.
|
|
139
|
-
*
|
|
140
|
-
* ```ts title="schema.ts"
|
|
141
|
-
* import {
|
|
142
|
-
* int,
|
|
143
|
-
* timestamp,
|
|
144
|
-
* mysqlTable,
|
|
145
|
-
* primaryKey,
|
|
146
|
-
* varchar,
|
|
147
|
-
* } from "drizzle-orm/mysql-core"
|
|
148
|
-
* import type { AdapterAccount } from "@auth/core/adapters"
|
|
149
|
-
*
|
|
150
|
-
* export const users = mysqlTable("user", {
|
|
151
|
-
* id: varchar("id", { length: 255 }).primaryKey().$defaultFn(() => randomUUID()),
|
|
152
|
-
* name: varchar("name", { length: 255 }),
|
|
153
|
-
* email: varchar("email", { length: 255 }).notNull(),
|
|
154
|
-
* emailVerified: timestamp("emailVerified", { mode: "date", fsp: 3 }),
|
|
155
|
-
* image: varchar("image", { length: 255 }),
|
|
156
|
-
* })
|
|
157
|
-
*
|
|
158
|
-
* export const accounts = mysqlTable(
|
|
159
|
-
* "account",
|
|
160
|
-
* {
|
|
161
|
-
* userId: varchar("userId", { length: 255 })
|
|
162
|
-
* .notNull()
|
|
163
|
-
* .references(() => users.id, { onDelete: "cascade" }),
|
|
164
|
-
* type: varchar("type", { length: 255 }).notNull(),
|
|
165
|
-
* provider: varchar("provider", { length: 255 }).notNull(),
|
|
166
|
-
* providerAccountId: varchar("providerAccountId", { length: 255 }).notNull(),
|
|
167
|
-
* refresh_token: varchar("refresh_token", { length: 255 }),
|
|
168
|
-
* access_token: varchar("access_token", { length: 255 }),
|
|
169
|
-
* expires_at: int("expires_at"),
|
|
170
|
-
* token_type: varchar("token_type", { length: 255 }),
|
|
171
|
-
* scope: varchar("scope", { length: 255 }),
|
|
172
|
-
* id_token: varchar("id_token", { length: 2048 }),
|
|
173
|
-
* session_state: varchar("session_state", { length: 255 }),
|
|
174
|
-
* },
|
|
175
|
-
* (account) => ({
|
|
176
|
-
* compoundKey: primaryKey({
|
|
177
|
-
columns: [account.provider, account.providerAccountId],
|
|
178
|
-
})
|
|
179
|
-
* })
|
|
180
|
-
* )
|
|
181
|
-
*
|
|
182
|
-
* export const sessions = mysqlTable("session", {
|
|
183
|
-
* sessionToken: varchar("sessionToken", { length: 255 }).primaryKey(),
|
|
184
|
-
* userId: varchar("userId", { length: 255 })
|
|
185
|
-
* .notNull()
|
|
186
|
-
* .references(() => users.id, { onDelete: "cascade" }),
|
|
187
|
-
* expires: timestamp("expires", { mode: "date" }).notNull(),
|
|
188
|
-
* })
|
|
189
|
-
*
|
|
190
|
-
* export const verificationTokens = mysqlTable(
|
|
191
|
-
* "verificationToken",
|
|
192
|
-
* {
|
|
193
|
-
* identifier: varchar("identifier", { length: 255 }).notNull(),
|
|
194
|
-
* token: varchar("token", { length: 255 }).notNull(),
|
|
195
|
-
* expires: timestamp("expires", { mode: "date" }).notNull(),
|
|
196
|
-
* },
|
|
197
|
-
* (vt) => ({
|
|
198
|
-
* compoundKey: primaryKey({ columns: [vt.identifier, vt.token] }),
|
|
199
|
-
* })
|
|
200
|
-
* )
|
|
201
|
-
* ```
|
|
202
|
-
*
|
|
203
|
-
* ### SQLite
|
|
204
|
-
*
|
|
205
|
-
* ```ts title="schema.ts"
|
|
206
|
-
* import { integer, sqliteTable, text, primaryKey } from "drizzle-orm/sqlite-core"
|
|
207
|
-
* import type { AdapterAccount } from "@auth/core/adapters"
|
|
208
|
-
*
|
|
209
|
-
* export const users = sqliteTable("user", {
|
|
210
|
-
* id: text("id").primaryKey().$defaultFn(() => randomUUID()),
|
|
211
|
-
* name: text("name"),
|
|
212
|
-
* email: text("email").notNull(),
|
|
213
|
-
* emailVerified: integer("emailVerified", { mode: "timestamp_ms" }),
|
|
214
|
-
* image: text("image"),
|
|
215
|
-
* })
|
|
216
|
-
*
|
|
217
|
-
* export const accounts = sqliteTable(
|
|
218
|
-
* "account",
|
|
219
|
-
* {
|
|
220
|
-
* userId: text("userId")
|
|
221
|
-
* .notNull()
|
|
222
|
-
* .references(() => users.id, { onDelete: "cascade" }),
|
|
223
|
-
* type: text("type").notNull(),
|
|
224
|
-
* provider: text("provider").notNull(),
|
|
225
|
-
* providerAccountId: text("providerAccountId").notNull(),
|
|
226
|
-
* refresh_token: text("refresh_token"),
|
|
227
|
-
* access_token: text("access_token"),
|
|
228
|
-
* expires_at: integer("expires_at"),
|
|
229
|
-
* token_type: text("token_type"),
|
|
230
|
-
* scope: text("scope"),
|
|
231
|
-
* id_token: text("id_token"),
|
|
232
|
-
* session_state: text("session_state"),
|
|
233
|
-
* },
|
|
234
|
-
* (account) => ({
|
|
235
|
-
* compoundKey: primaryKey({
|
|
236
|
-
columns: [account.provider, account.providerAccountId],
|
|
237
|
-
})
|
|
238
|
-
* })
|
|
239
|
-
* )
|
|
240
|
-
*
|
|
241
|
-
* export const sessions = sqliteTable("session", {
|
|
242
|
-
* sessionToken: text("sessionToken").primaryKey(),
|
|
243
|
-
* userId: text("userId")
|
|
244
|
-
* .notNull()
|
|
245
|
-
* .references(() => users.id, { onDelete: "cascade" }),
|
|
246
|
-
* expires: integer("expires", { mode: "timestamp_ms" }).notNull(),
|
|
247
|
-
* })
|
|
248
|
-
*
|
|
249
|
-
* export const verificationTokens = sqliteTable(
|
|
250
|
-
* "verificationToken",
|
|
251
|
-
* {
|
|
252
|
-
* identifier: text("identifier").notNull(),
|
|
253
|
-
* token: text("token").notNull(),
|
|
254
|
-
* expires: integer("expires", { mode: "timestamp_ms" }).notNull(),
|
|
255
|
-
* },
|
|
256
|
-
* (vt) => ({
|
|
257
|
-
* compoundKey: primaryKey({ columns: [vt.identifier, vt.token] }),
|
|
258
|
-
* })
|
|
259
|
-
* )
|
|
260
|
-
* ```
|
|
261
|
-
*
|
|
262
|
-
* ## Migrating your database
|
|
263
|
-
* With your schema now described in your code, you'll need to migrate your database to your schema.
|
|
264
|
-
*
|
|
265
|
-
* 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).
|
|
266
|
-
*
|
|
267
|
-
* ---
|
|
268
|
-
*
|
|
269
|
-
**/
|
|
270
25
|
export function DrizzleAdapter(db, schema) {
|
|
271
26
|
if (is(db, MySqlDatabase)) {
|
|
272
27
|
return MySqlDrizzleAdapter(db, schema);
|