@auth/drizzle-adapter 1.0.0 → 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/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 style={{fontWeight: "normal"}}>Official <a href="https://orm.drizzle.team">Drizzle ORM</a> adapter for Auth.js / NextAuth.js.</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>
@@ -27,276 +27,6 @@ import { DefaultSchema, SqlFlavorOptions } from "./lib/utils.js"
27
27
 
28
28
  import type { Adapter } from "@auth/core/adapters"
29
29
 
30
- export {
31
- postgresUsersTable,
32
- postgresAccountsTable,
33
- postgresSessionsTable,
34
- postgresVerificationTokensTable,
35
- } from "./lib/pg.js"
36
- export {
37
- sqliteUsersTable,
38
- sqliteAccountsTable,
39
- sqliteSessionsTable,
40
- sqliteVerificationTokensTable,
41
- } from "./lib/sqlite.js"
42
- export {
43
- mysqlUsersTable,
44
- mysqlAccountsTable,
45
- mysqlSessionsTable,
46
- mysqlVerificationTokensTable,
47
- } from "./lib/mysql.js"
48
- /**
49
- * Add this adapter to your `auth.ts` Auth.js configuration object:
50
- *
51
- * ```ts title="auth.ts"
52
- * import NextAuth from "next-auth"
53
- * import Google from "next-auth/providers/google"
54
- * import { DrizzleAdapter } from "@auth/drizzle-adapter"
55
- * import { db } from "./schema"
56
- *
57
- * export const { handlers, auth } = NextAuth({
58
- * adapter: DrizzleAdapter(db),
59
- * providers: [
60
- * Google,
61
- * ],
62
- * })
63
- * ```
64
- *
65
- * :::info
66
- * If you want to use your own tables, you can pass them as a second argument
67
- * :::
68
- *
69
- * ```ts title="auth.ts"
70
- * import NextAuth from "next-auth"
71
- * import Google from "next-auth/providers/google"
72
- * import { DrizzleAdapter } from "@auth/drizzle-adapter"
73
- * import { db, accounts, sessions, users, verificationTokens } from "./schema"
74
- *
75
- * export const { handlers, auth } = NextAuth({
76
- * adapter: DrizzleAdapter(db, { usersTable: users, accountsTable: accounts, sessionsTable: sessions, verificationTokensTable: verificationTokens }),
77
- * providers: [
78
- * Google,
79
- * ],
80
- * })
81
- * ```
82
- *
83
- * ## Setup
84
- *
85
- * 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.
86
- * Additionally, you may extend the schema from the minimum requirements to suit your needs.
87
- *
88
- * - [Postgres](#postgres)
89
- * - [MySQL](#mysql)
90
- * - [SQLite](#sqlite)
91
- *
92
- * ### Postgres
93
-
94
- * ```ts title="schema.ts"
95
- * import {
96
- * timestamp,
97
- * pgTable,
98
- * text,
99
- * primaryKey,
100
- * integer
101
- * } from "drizzle-orm/pg-core"
102
- * import type { AdapterAccount } from '@auth/core/adapters'
103
- * import { randomUUID } from "crypto"
104
- *
105
- * export const users = pgTable("user", {
106
- * id: text("id").primaryKey().$defaultFn(() => randomUUID()),
107
- * name: text("name"),
108
- * email: text("email").notNull().unique(),
109
- * emailVerified: timestamp("emailVerified", { mode: "date" }),
110
- * image: text("image"),
111
- * })
112
- *
113
- * export const accounts = pgTable(
114
- * "account",
115
- * {
116
- * userId: text("userId")
117
- * .notNull()
118
- * .references(() => users.id, { onDelete: "cascade" }),
119
- * type: text("type").notNull(),
120
- * provider: text("provider").notNull(),
121
- * providerAccountId: text("providerAccountId").notNull(),
122
- * refresh_token: text("refresh_token"),
123
- * access_token: text("access_token"),
124
- * expires_at: integer("expires_at"),
125
- * token_type: text("token_type"),
126
- * scope: text("scope"),
127
- * id_token: text("id_token"),
128
- * session_state: text("session_state"),
129
- * },
130
- * (account) => ({
131
- * userIdIdx: index().on(account.userId),
132
- * compoundKey: primaryKey({ columns: [account.provider, account.providerAccountId] }),
133
- * })
134
- * )
135
- *
136
- * export const sessions = pgTable("session", {
137
- * id: text("id").primaryKey().$defaultFn(() => randomUUID()),
138
- * sessionToken: text("sessionToken").notNull().unique(),
139
- * userId: text("userId")
140
- * .notNull()
141
- * .references(() => users.id, { onDelete: "cascade" }),
142
- * expires: timestamp("expires", { mode: "date" }).notNull(),
143
- * }, (session) => ({
144
- * userIdIdx: index().on(session.userId)
145
- * }))
146
- *
147
- * export const verificationTokens = pgTable(
148
- * "verificationToken",
149
- * {
150
- * identifier: text("identifier").notNull(),
151
- * token: text("token").notNull().unique(),
152
- * expires: timestamp("expires", { mode: "date" }).notNull(),
153
- * },
154
- * (vt) => ({
155
- * compoundKey: primaryKey({ columns: [vt.identifier, vt.token] }),
156
- * })
157
- * )
158
- * ```
159
- *
160
- * ### MySQL
161
- *
162
- * ```ts title="schema.ts"
163
- * import {
164
- * int,
165
- * timestamp,
166
- * mysqlTable,
167
- * primaryKey,
168
- * varchar,
169
- * } from "drizzle-orm/mysql-core"
170
- * import type { AdapterAccount } from "@auth/core/adapters"
171
- *
172
- * export const users = mysqlTable("user", {
173
- * id: varchar("id", { length: 255 }).primaryKey().$defaultFn(() => randomUUID()),
174
- * name: varchar("name", { length: 255 }),
175
- * email: varchar("email", { length: 255 }).notNull().unique(),
176
- * emailVerified: timestamp("emailVerified", { mode: "date", fsp: 3 }),
177
- * image: varchar("image", { length: 255 }),
178
- * })
179
- *
180
- * export const accounts = mysqlTable(
181
- * "account",
182
- * {
183
- * userId: varchar("userId", { length: 255 })
184
- * .notNull()
185
- * .references(() => users.id, { onDelete: "cascade" }),
186
- * type: varchar("type", { length: 255 }).notNull(),
187
- * provider: varchar("provider", { length: 255 }).notNull(),
188
- * providerAccountId: varchar("providerAccountId", { length: 255 }).notNull(),
189
- * refresh_token: varchar("refresh_token", { length: 255 }),
190
- * access_token: varchar("access_token", { length: 255 }),
191
- * expires_at: int("expires_at"),
192
- * token_type: varchar("token_type", { length: 255 }),
193
- * scope: varchar("scope", { length: 255 }),
194
- * id_token: varchar("id_token", { length: 2048 }),
195
- * session_state: varchar("session_state", { length: 255 }),
196
- * },
197
- * (account) => ({
198
- * compoundKey: primaryKey({
199
- columns: [account.provider, account.providerAccountId],
200
- }),
201
- userIdIdx: index('Account_userId_index').on(account.userId)
202
- * })
203
- * )
204
- *
205
- * export const sessions = mysqlTable("session", {
206
- * id: varchar("id", { length: 255 }).primaryKey().$defaultFn(() => randomUUID()),
207
- * sessionToken: varchar("sessionToken", { length: 255 }).notNull().unique(),
208
- * userId: varchar("userId", { length: 255 })
209
- * .notNull()
210
- * .references(() => users.id, { onDelete: "cascade" }),
211
- * expires: timestamp("expires", { mode: "date" }).notNull(),
212
- * }, (session) => ({
213
- * userIdIdx: index('Session_userId_index').on(session.userId)
214
- * }))
215
- *
216
- * export const verificationTokens = mysqlTable(
217
- * "verificationToken",
218
- * {
219
- * identifier: varchar("identifier", { length: 255 }).notNull(),
220
- * token: varchar("token", { length: 255 }).notNull().unique(),
221
- * expires: timestamp("expires", { mode: "date" }).notNull(),
222
- * },
223
- * (vt) => ({
224
- * compoundKey: primaryKey({ columns: [vt.identifier, vt.token] }),
225
- * })
226
- * )
227
- * ```
228
- *
229
- * ### SQLite
230
- *
231
- * ```ts title="schema.ts"
232
- * import { integer, sqliteTable, text, primaryKey } from "drizzle-orm/sqlite-core"
233
- * import type { AdapterAccount } from "@auth/core/adapters"
234
- *
235
- * export const users = sqliteTable("user", {
236
- * id: text("id").primaryKey().$defaultFn(() => randomUUID()),
237
- * name: text("name"),
238
- * email: text("email").notNull().unique(),
239
- * emailVerified: integer("emailVerified", { mode: "timestamp_ms" }),
240
- * image: text("image"),
241
- * })
242
- *
243
- * export const accounts = sqliteTable(
244
- * "account",
245
- * {
246
- * userId: text("userId")
247
- * .notNull()
248
- * .references(() => users.id, { onDelete: "cascade" }),
249
- * type: text("type").notNull(),
250
- * provider: text("provider").notNull(),
251
- * providerAccountId: text("providerAccountId").notNull(),
252
- * refresh_token: text("refresh_token"),
253
- * access_token: text("access_token"),
254
- * expires_at: integer("expires_at"),
255
- * token_type: text("token_type"),
256
- * scope: text("scope"),
257
- * id_token: text("id_token"),
258
- * session_state: text("session_state"),
259
- * },
260
- * (account) => ({
261
- * compoundKey: primaryKey({
262
- columns: [account.provider, account.providerAccountId],
263
- }),
264
- userIdIdx: index('Account_userId_index').on(account.userId)
265
- * })
266
- * )
267
- *
268
- * export const sessions = sqliteTable("session", {
269
- * id: text("id").primaryKey().$defaultFn(() => randomUUID())
270
- * sessionToken: text("sessionToken").notNull().unique(),
271
- * userId: text("userId")
272
- * .notNull()
273
- * .references(() => users.id, { onDelete: "cascade" }),
274
- * expires: integer("expires", { mode: "timestamp_ms" }).notNull(),
275
- * }, (table) => ({
276
- * userIdIdx: index('Session_userId_index').on(table.userId)
277
- * }))
278
- *
279
- * export const verificationTokens = sqliteTable(
280
- * "verificationToken",
281
- * {
282
- * identifier: text("identifier").notNull(),
283
- * token: text("token").notNull().unique(),
284
- * expires: integer("expires", { mode: "timestamp_ms" }).notNull(),
285
- * },
286
- * (vt) => ({
287
- * compoundKey: primaryKey({ columns: [vt.identifier, vt.token] }),
288
- * })
289
- * )
290
- * ```
291
- *
292
- * ## Migrating your database
293
- * With your schema now described in your code, you'll need to migrate your database to your schema.
294
- *
295
- * For full documentation on how to run migrations with Drizzle, [visit the Drizzle documentation](https://orm.drizzle.team/kit-docs/overview#running-migrations).
296
- *
297
- * ---
298
- *
299
- **/
300
30
  export function DrizzleAdapter<SqlFlavor extends SqlFlavorOptions>(
301
31
  db: SqlFlavor,
302
32
  schema?: DefaultSchema<SqlFlavor>