@auth/drizzle-adapter 0.0.0-manual.2501f898 → 0.0.0-manual.4331e59d

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/index.d.ts ADDED
@@ -0,0 +1,241 @@
1
+ /**
2
+ * <div style={{display: "flex", justifyContent: "space-between", alignItems: "center", padding: 16}}>
3
+ * <p style={{fontWeight: "normal"}}>Official <a href="https://orm.drizzle.team">Drizzle ORM</a> adapter for Auth.js / NextAuth.js.</p>
4
+ * <a href="https://orm.drizzle.team">
5
+ * <img style={{display: "block"}} src="/img/adapters/drizzle-orm.png" width="38" />
6
+ * </a>
7
+ * </div>
8
+ *
9
+ * ## Installation
10
+ *
11
+ * ```bash npm2yarn2pnpm
12
+ * npm install drizzle-orm @auth/drizzle-adapter
13
+ * npm install drizzle-kit --save-dev
14
+ * ```
15
+ *
16
+ * @module @auth/drizzle-adapter
17
+ */
18
+ import { SqlFlavorOptions } from "./lib/utils";
19
+ import type { Adapter } from "@auth/core/adapters";
20
+ /**
21
+ * Add the adapter to your `app/api/[...nextauth]/route.js` next-auth configuration object.
22
+ *
23
+ * ```ts title="pages/api/auth/[...nextauth].ts"
24
+ * import NextAuth from "next-auth"
25
+ * import GoogleProvider from "next-auth/providers/google"
26
+ * import { DrizzleAdapter } from "@auth/drizzle-adapter"
27
+ * import { db } from "./schema"
28
+ *
29
+ * export default NextAuth({
30
+ * adapter: DrizzleAdapter(db),
31
+ * providers: [
32
+ * GoogleProvider({
33
+ * clientId: process.env.GOOGLE_CLIENT_ID,
34
+ * clientSecret: process.env.GOOGLE_CLIENT_SECRET,
35
+ * }),
36
+ * ],
37
+ * })
38
+ * ```
39
+ *
40
+ * ## Setup
41
+ *
42
+ * First, create a schema that includes [the minimum requirements for a `next-auth` adapter](/reference/adapters#models). You can select your favorite SQL flavor below and copy it.
43
+ * Additionally, you may extend the schema from the minimum requirements to suit your needs.
44
+ *
45
+ * - [Postgres](#postgres)
46
+ * - [MySQL](#mysql)
47
+ * - [SQLite](#sqlite)
48
+ *
49
+ * ### Postgres
50
+
51
+ * ```ts title="schema.ts"
52
+ * import {
53
+ * timestamp,
54
+ * pgTable,
55
+ * text,
56
+ * primaryKey,
57
+ * integer
58
+ * } from "drizzle-orm/pg-core"
59
+ * import type { AdapterAccount } from '@auth/core/adapters'
60
+ *
61
+ * export const users = pgTable("users", {
62
+ * id: text("id").notNull().primaryKey(),
63
+ * name: text("name"),
64
+ * email: text("email").notNull(),
65
+ * emailVerified: timestamp("emailVerified", { mode: "date" }),
66
+ * image: text("image"),
67
+ * })
68
+ *
69
+ * export const accounts = pgTable(
70
+ * "accounts",
71
+ * {
72
+ * userId: text("userId")
73
+ * .notNull()
74
+ * .references(() => users.id, { onDelete: "cascade" }),
75
+ * type: text("type").$type<AdapterAccount["type"]>().notNull(),
76
+ * provider: text("provider").notNull(),
77
+ * providerAccountId: text("providerAccountId").notNull(),
78
+ * refresh_token: text("refresh_token"),
79
+ * access_token: text("access_token"),
80
+ * expires_at: integer("expires_at"),
81
+ * token_type: text("token_type"),
82
+ * scope: text("scope"),
83
+ * id_token: text("id_token"),
84
+ * session_state: text("session_state"),
85
+ * },
86
+ * (account) => ({
87
+ * compoundKey: primaryKey(account.provider, account.providerAccountId),
88
+ * })
89
+ * )
90
+ *
91
+ * export const sessions = pgTable("sessions", {
92
+ * sessionToken: text("sessionToken").notNull().primaryKey(),
93
+ * userId: text("userId")
94
+ * .notNull()
95
+ * .references(() => users.id, { onDelete: "cascade" }),
96
+ * expires: timestamp("expires", { mode: "date" }).notNull(),
97
+ * })
98
+ *
99
+ * export const verificationTokens = pgTable(
100
+ * "verificationToken",
101
+ * {
102
+ * identifier: text("identifier").notNull(),
103
+ * token: text("token").notNull(),
104
+ * expires: timestamp("expires", { mode: "date" }).notNull(),
105
+ * },
106
+ * (vt) => ({
107
+ * compoundKey: primaryKey(vt.identifier, vt.token),
108
+ * })
109
+ * )
110
+ * ```
111
+ *
112
+ * ### MySQL
113
+ *
114
+ * ```ts title="schema.ts"
115
+ * import {
116
+ * int,
117
+ * timestamp,
118
+ * mysqlTable,
119
+ * primaryKey,
120
+ * varchar,
121
+ * } from "drizzle-orm/mysql-core"
122
+ * import type { AdapterAccount } from "@auth/core/adapters"
123
+ *
124
+ * export const users = mysqlTable("users", {
125
+ * id: varchar("id", { length: 255 }).notNull().primaryKey(),
126
+ * name: varchar("name", { length: 255 }),
127
+ * email: varchar("email", { length: 255 }).notNull(),
128
+ * emailVerified: timestamp("emailVerified", { mode: "date", fsp: 3 }).defaultNow(),
129
+ * image: varchar("image", { length: 255 }),
130
+ * })
131
+ *
132
+ * export const accounts = mysqlTable(
133
+ * "accounts",
134
+ * {
135
+ * userId: varchar("userId", { length: 255 })
136
+ * .notNull()
137
+ * .references(() => users.id, { onDelete: "cascade" }),
138
+ * type: varchar("type", { length: 255 }).$type<AdapterAccount["type"]>().notNull(),
139
+ * provider: varchar("provider", { length: 255 }).notNull(),
140
+ * providerAccountId: varchar("providerAccountId", { length: 255 }).notNull(),
141
+ * refresh_token: varchar("refresh_token", { length: 255 }),
142
+ * access_token: varchar("access_token", { length: 255 }),
143
+ * expires_at: int("expires_at"),
144
+ * token_type: varchar("token_type", { length: 255 }),
145
+ * scope: varchar("scope", { length: 255 }),
146
+ * id_token: varchar("id_token", { length: 255 }),
147
+ * session_state: varchar("session_state", { length: 255 }),
148
+ * },
149
+ * (account) => ({
150
+ * compoundKey: primaryKey(account.provider, account.providerAccountId),
151
+ * })
152
+ * )
153
+ *
154
+ * export const sessions = mysqlTable("sessions", {
155
+ * sessionToken: varchar("sessionToken", { length: 255 }).notNull().primaryKey(),
156
+ * userId: varchar("userId", { length: 255 })
157
+ * .notNull()
158
+ * .references(() => users.id, { onDelete: "cascade" }),
159
+ * expires: timestamp("expires", { mode: "date" }).notNull(),
160
+ * })
161
+ *
162
+ * export const verificationTokens = mysqlTable(
163
+ * "verificationToken",
164
+ * {
165
+ * identifier: varchar("identifier", { length: 255 }).notNull(),
166
+ * token: varchar("token", { length: 255 }).notNull(),
167
+ * expires: timestamp("expires", { mode: "date" }).notNull(),
168
+ * },
169
+ * (vt) => ({
170
+ * compoundKey: primaryKey(vt.identifier, vt.token),
171
+ * })
172
+ * )
173
+ * ```
174
+ *
175
+ * ### SQLite
176
+ *
177
+ * ```ts title="schema.ts"
178
+ * import { integer, sqliteTable, text, primaryKey } from "drizzle-orm/sqlite-core"
179
+ * import type { AdapterAccount } from "@auth/core/adapters"
180
+ *
181
+ * export const users = sqliteTable("users", {
182
+ * id: text("id").notNull().primaryKey(),
183
+ * name: text("name"),
184
+ * email: text("email").notNull(),
185
+ * emailVerified: integer("emailVerified", { mode: "timestamp_ms" }),
186
+ * image: text("image"),
187
+ * })
188
+ *
189
+ * export const accounts = sqliteTable(
190
+ * "accounts",
191
+ * {
192
+ * userId: text("userId")
193
+ * .notNull()
194
+ * .references(() => users.id, { onDelete: "cascade" }),
195
+ * type: text("type").$type<AdapterAccount["type"]>().notNull(),
196
+ * provider: text("provider").notNull(),
197
+ * providerAccountId: text("providerAccountId").notNull(),
198
+ * refresh_token: text("refresh_token"),
199
+ * access_token: text("access_token"),
200
+ * expires_at: integer("expires_at"),
201
+ * token_type: text("token_type"),
202
+ * scope: text("scope"),
203
+ * id_token: text("id_token"),
204
+ * session_state: text("session_state"),
205
+ * },
206
+ * (account) => ({
207
+ * compoundKey: primaryKey(account.provider, account.providerAccountId),
208
+ * })
209
+ * )
210
+ *
211
+ * export const sessions = sqliteTable("sessions", {
212
+ * sessionToken: text("sessionToken").notNull().primaryKey(),
213
+ * userId: text("userId")
214
+ * .notNull()
215
+ * .references(() => users.id, { onDelete: "cascade" }),
216
+ * expires: integer("expires", { mode: "timestamp_ms" }).notNull(),
217
+ * })
218
+ *
219
+ * export const verificationTokens = sqliteTable(
220
+ * "verificationToken",
221
+ * {
222
+ * identifier: text("identifier").notNull(),
223
+ * token: text("token").notNull(),
224
+ * expires: integer("expires", { mode: "timestamp_ms" }).notNull(),
225
+ * },
226
+ * (vt) => ({
227
+ * compoundKey: primaryKey(vt.identifier, vt.token),
228
+ * })
229
+ * )
230
+ * ```
231
+ *
232
+ * ## Migrating your database
233
+ * With your schema now described in your code, you'll need to migrate your database to your schema.
234
+ *
235
+ * For full documentation on how to run migrations with Drizzle, [visit the Drizzle documentation](https://orm.drizzle.team/kit-docs/overview#running-migrations).
236
+ *
237
+ * ---
238
+ *
239
+ **/
240
+ export declare function DrizzleAdapter<SqlFlavor extends SqlFlavorOptions>(db: SqlFlavor): Adapter;
241
+ //# sourceMappingURL=index.d.ts.map
package/index.d.ts.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAKH,OAAO,EAIL,gBAAgB,EACjB,MAAM,aAAa,CAAA;AAEpB,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAElD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IA2NI;AACJ,wBAAgB,cAAc,CAAC,SAAS,SAAS,gBAAgB,EAC/D,EAAE,EAAE,SAAS,GACZ,OAAO,CAeT"}
package/index.js ADDED
@@ -0,0 +1,254 @@
1
+ /**
2
+ * <div style={{display: "flex", justifyContent: "space-between", alignItems: "center", padding: 16}}>
3
+ * <p style={{fontWeight: "normal"}}>Official <a href="https://orm.drizzle.team">Drizzle ORM</a> adapter for Auth.js / NextAuth.js.</p>
4
+ * <a href="https://orm.drizzle.team">
5
+ * <img style={{display: "block"}} src="/img/adapters/drizzle-orm.png" width="38" />
6
+ * </a>
7
+ * </div>
8
+ *
9
+ * ## Installation
10
+ *
11
+ * ```bash npm2yarn2pnpm
12
+ * npm install drizzle-orm @auth/drizzle-adapter
13
+ * npm install drizzle-kit --save-dev
14
+ * ```
15
+ *
16
+ * @module @auth/drizzle-adapter
17
+ */
18
+ import { mySqlDrizzleAdapter } from "./lib/mysql";
19
+ import { pgDrizzleAdapter } from "./lib/pg";
20
+ import { SQLiteDrizzleAdapter } from "./lib/sqlite";
21
+ import { isMySqlDatabase, isPgDatabase, isSQLiteDatabase, } from "./lib/utils";
22
+ /**
23
+ * Add the adapter to your `app/api/[...nextauth]/route.js` next-auth configuration object.
24
+ *
25
+ * ```ts title="pages/api/auth/[...nextauth].ts"
26
+ * import NextAuth from "next-auth"
27
+ * import GoogleProvider from "next-auth/providers/google"
28
+ * import { DrizzleAdapter } from "@auth/drizzle-adapter"
29
+ * import { db } from "./schema"
30
+ *
31
+ * export default NextAuth({
32
+ * adapter: DrizzleAdapter(db),
33
+ * providers: [
34
+ * GoogleProvider({
35
+ * clientId: process.env.GOOGLE_CLIENT_ID,
36
+ * clientSecret: process.env.GOOGLE_CLIENT_SECRET,
37
+ * }),
38
+ * ],
39
+ * })
40
+ * ```
41
+ *
42
+ * ## Setup
43
+ *
44
+ * First, create a schema that includes [the minimum requirements for a `next-auth` adapter](/reference/adapters#models). You can select your favorite SQL flavor below and copy it.
45
+ * Additionally, you may extend the schema from the minimum requirements to suit your needs.
46
+ *
47
+ * - [Postgres](#postgres)
48
+ * - [MySQL](#mysql)
49
+ * - [SQLite](#sqlite)
50
+ *
51
+ * ### Postgres
52
+
53
+ * ```ts title="schema.ts"
54
+ * import {
55
+ * timestamp,
56
+ * pgTable,
57
+ * text,
58
+ * primaryKey,
59
+ * integer
60
+ * } from "drizzle-orm/pg-core"
61
+ * import type { AdapterAccount } from '@auth/core/adapters'
62
+ *
63
+ * export const users = pgTable("users", {
64
+ * id: text("id").notNull().primaryKey(),
65
+ * name: text("name"),
66
+ * email: text("email").notNull(),
67
+ * emailVerified: timestamp("emailVerified", { mode: "date" }),
68
+ * image: text("image"),
69
+ * })
70
+ *
71
+ * export const accounts = pgTable(
72
+ * "accounts",
73
+ * {
74
+ * userId: text("userId")
75
+ * .notNull()
76
+ * .references(() => users.id, { onDelete: "cascade" }),
77
+ * type: text("type").$type<AdapterAccount["type"]>().notNull(),
78
+ * provider: text("provider").notNull(),
79
+ * providerAccountId: text("providerAccountId").notNull(),
80
+ * refresh_token: text("refresh_token"),
81
+ * access_token: text("access_token"),
82
+ * expires_at: integer("expires_at"),
83
+ * token_type: text("token_type"),
84
+ * scope: text("scope"),
85
+ * id_token: text("id_token"),
86
+ * session_state: text("session_state"),
87
+ * },
88
+ * (account) => ({
89
+ * compoundKey: primaryKey(account.provider, account.providerAccountId),
90
+ * })
91
+ * )
92
+ *
93
+ * export const sessions = pgTable("sessions", {
94
+ * sessionToken: text("sessionToken").notNull().primaryKey(),
95
+ * userId: text("userId")
96
+ * .notNull()
97
+ * .references(() => users.id, { onDelete: "cascade" }),
98
+ * expires: timestamp("expires", { mode: "date" }).notNull(),
99
+ * })
100
+ *
101
+ * export const verificationTokens = pgTable(
102
+ * "verificationToken",
103
+ * {
104
+ * identifier: text("identifier").notNull(),
105
+ * token: text("token").notNull(),
106
+ * expires: timestamp("expires", { mode: "date" }).notNull(),
107
+ * },
108
+ * (vt) => ({
109
+ * compoundKey: primaryKey(vt.identifier, vt.token),
110
+ * })
111
+ * )
112
+ * ```
113
+ *
114
+ * ### MySQL
115
+ *
116
+ * ```ts title="schema.ts"
117
+ * import {
118
+ * int,
119
+ * timestamp,
120
+ * mysqlTable,
121
+ * primaryKey,
122
+ * varchar,
123
+ * } from "drizzle-orm/mysql-core"
124
+ * import type { AdapterAccount } from "@auth/core/adapters"
125
+ *
126
+ * export const users = mysqlTable("users", {
127
+ * id: varchar("id", { length: 255 }).notNull().primaryKey(),
128
+ * name: varchar("name", { length: 255 }),
129
+ * email: varchar("email", { length: 255 }).notNull(),
130
+ * emailVerified: timestamp("emailVerified", { mode: "date", fsp: 3 }).defaultNow(),
131
+ * image: varchar("image", { length: 255 }),
132
+ * })
133
+ *
134
+ * export const accounts = mysqlTable(
135
+ * "accounts",
136
+ * {
137
+ * userId: varchar("userId", { length: 255 })
138
+ * .notNull()
139
+ * .references(() => users.id, { onDelete: "cascade" }),
140
+ * type: varchar("type", { length: 255 }).$type<AdapterAccount["type"]>().notNull(),
141
+ * provider: varchar("provider", { length: 255 }).notNull(),
142
+ * providerAccountId: varchar("providerAccountId", { length: 255 }).notNull(),
143
+ * refresh_token: varchar("refresh_token", { length: 255 }),
144
+ * access_token: varchar("access_token", { length: 255 }),
145
+ * expires_at: int("expires_at"),
146
+ * token_type: varchar("token_type", { length: 255 }),
147
+ * scope: varchar("scope", { length: 255 }),
148
+ * id_token: varchar("id_token", { length: 255 }),
149
+ * session_state: varchar("session_state", { length: 255 }),
150
+ * },
151
+ * (account) => ({
152
+ * compoundKey: primaryKey(account.provider, account.providerAccountId),
153
+ * })
154
+ * )
155
+ *
156
+ * export const sessions = mysqlTable("sessions", {
157
+ * sessionToken: varchar("sessionToken", { length: 255 }).notNull().primaryKey(),
158
+ * userId: varchar("userId", { length: 255 })
159
+ * .notNull()
160
+ * .references(() => users.id, { onDelete: "cascade" }),
161
+ * expires: timestamp("expires", { mode: "date" }).notNull(),
162
+ * })
163
+ *
164
+ * export const verificationTokens = mysqlTable(
165
+ * "verificationToken",
166
+ * {
167
+ * identifier: varchar("identifier", { length: 255 }).notNull(),
168
+ * token: varchar("token", { length: 255 }).notNull(),
169
+ * expires: timestamp("expires", { mode: "date" }).notNull(),
170
+ * },
171
+ * (vt) => ({
172
+ * compoundKey: primaryKey(vt.identifier, vt.token),
173
+ * })
174
+ * )
175
+ * ```
176
+ *
177
+ * ### SQLite
178
+ *
179
+ * ```ts title="schema.ts"
180
+ * import { integer, sqliteTable, text, primaryKey } from "drizzle-orm/sqlite-core"
181
+ * import type { AdapterAccount } from "@auth/core/adapters"
182
+ *
183
+ * export const users = sqliteTable("users", {
184
+ * id: text("id").notNull().primaryKey(),
185
+ * name: text("name"),
186
+ * email: text("email").notNull(),
187
+ * emailVerified: integer("emailVerified", { mode: "timestamp_ms" }),
188
+ * image: text("image"),
189
+ * })
190
+ *
191
+ * export const accounts = sqliteTable(
192
+ * "accounts",
193
+ * {
194
+ * userId: text("userId")
195
+ * .notNull()
196
+ * .references(() => users.id, { onDelete: "cascade" }),
197
+ * type: text("type").$type<AdapterAccount["type"]>().notNull(),
198
+ * provider: text("provider").notNull(),
199
+ * providerAccountId: text("providerAccountId").notNull(),
200
+ * refresh_token: text("refresh_token"),
201
+ * access_token: text("access_token"),
202
+ * expires_at: integer("expires_at"),
203
+ * token_type: text("token_type"),
204
+ * scope: text("scope"),
205
+ * id_token: text("id_token"),
206
+ * session_state: text("session_state"),
207
+ * },
208
+ * (account) => ({
209
+ * compoundKey: primaryKey(account.provider, account.providerAccountId),
210
+ * })
211
+ * )
212
+ *
213
+ * export const sessions = sqliteTable("sessions", {
214
+ * sessionToken: text("sessionToken").notNull().primaryKey(),
215
+ * userId: text("userId")
216
+ * .notNull()
217
+ * .references(() => users.id, { onDelete: "cascade" }),
218
+ * expires: integer("expires", { mode: "timestamp_ms" }).notNull(),
219
+ * })
220
+ *
221
+ * export const verificationTokens = sqliteTable(
222
+ * "verificationToken",
223
+ * {
224
+ * identifier: text("identifier").notNull(),
225
+ * token: text("token").notNull(),
226
+ * expires: integer("expires", { mode: "timestamp_ms" }).notNull(),
227
+ * },
228
+ * (vt) => ({
229
+ * compoundKey: primaryKey(vt.identifier, vt.token),
230
+ * })
231
+ * )
232
+ * ```
233
+ *
234
+ * ## Migrating your database
235
+ * With your schema now described in your code, you'll need to migrate your database to your schema.
236
+ *
237
+ * For full documentation on how to run migrations with Drizzle, [visit the Drizzle documentation](https://orm.drizzle.team/kit-docs/overview#running-migrations).
238
+ *
239
+ * ---
240
+ *
241
+ **/
242
+ export function DrizzleAdapter(db) {
243
+ if (isMySqlDatabase(db)) {
244
+ // We need to cast to unknown since the type overlaps (PScale is MySQL based)
245
+ return mySqlDrizzleAdapter(db);
246
+ }
247
+ if (isPgDatabase(db)) {
248
+ return pgDrizzleAdapter(db);
249
+ }
250
+ if (isSQLiteDatabase(db)) {
251
+ return SQLiteDrizzleAdapter(db);
252
+ }
253
+ throw new Error("Unsupported database type in Auth.js Drizzle adapter.");
254
+ }