@auth/drizzle-adapter 0.0.0-manual.ffc00566 → 0.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/lib/sqlite.js CHANGED
@@ -1,69 +1,66 @@
1
- import { integer, sqliteTable, text, primaryKey, } from "drizzle-orm/sqlite-core";
2
1
  import { eq, and } from "drizzle-orm";
3
- /** @internal */
4
- export const users = sqliteTable("users", {
5
- id: text("id").notNull().primaryKey(),
6
- name: text("name"),
7
- email: text("email").notNull(),
8
- emailVerified: integer("emailVerified", { mode: "timestamp_ms" }),
9
- image: text("image"),
10
- });
11
- /** @internal */
12
- export const accounts = sqliteTable("accounts", {
13
- userId: text("userId")
14
- .notNull()
15
- .references(() => users.id, { onDelete: "cascade" }),
16
- type: text("type").$type().notNull(),
17
- provider: text("provider").notNull(),
18
- providerAccountId: text("providerAccountId").notNull(),
19
- refresh_token: text("refresh_token"),
20
- access_token: text("access_token"),
21
- expires_at: integer("expires_at"),
22
- token_type: text("token_type"),
23
- scope: text("scope"),
24
- id_token: text("id_token"),
25
- session_state: text("session_state"),
26
- }, (account) => ({
27
- compoundKey: primaryKey(account.provider, account.providerAccountId),
28
- }));
29
- /** @internal */
30
- export const sessions = sqliteTable("sessions", {
31
- sessionToken: text("sessionToken").notNull().primaryKey(),
32
- userId: text("userId")
33
- .notNull()
34
- .references(() => users.id, { onDelete: "cascade" }),
35
- expires: integer("expires", { mode: "timestamp_ms" }).notNull(),
36
- });
37
- /** @internal */
38
- export const verificationTokens = sqliteTable("verificationToken", {
39
- identifier: text("identifier").notNull(),
40
- token: text("token").notNull(),
41
- expires: integer("expires", { mode: "timestamp_ms" }).notNull(),
42
- }, (vt) => ({
43
- compoundKey: primaryKey(vt.identifier, vt.token),
44
- }));
45
- /** @internal */
46
- export const schema = { users, accounts, sessions, verificationTokens };
47
- /** @internal */
48
- export function SQLiteDrizzleAdapter(client) {
2
+ import { integer, sqliteTable as defaultSqliteTableFn, text, primaryKey, } from "drizzle-orm/sqlite-core";
3
+ export function createTables(sqliteTable) {
4
+ const users = sqliteTable("users", {
5
+ id: text("id").notNull().primaryKey(),
6
+ name: text("name"),
7
+ email: text("email").notNull(),
8
+ emailVerified: integer("emailVerified", { mode: "timestamp_ms" }),
9
+ image: text("image"),
10
+ });
11
+ const accounts = sqliteTable("accounts", {
12
+ userId: text("userId")
13
+ .notNull()
14
+ .references(() => users.id, { onDelete: "cascade" }),
15
+ type: text("type").$type().notNull(),
16
+ provider: text("provider").notNull(),
17
+ providerAccountId: text("providerAccountId").notNull(),
18
+ refresh_token: text("refresh_token"),
19
+ access_token: text("access_token"),
20
+ expires_at: integer("expires_at"),
21
+ token_type: text("token_type"),
22
+ scope: text("scope"),
23
+ id_token: text("id_token"),
24
+ session_state: text("session_state"),
25
+ }, (account) => ({
26
+ compoundKey: primaryKey(account.provider, account.providerAccountId),
27
+ }));
28
+ const sessions = sqliteTable("sessions", {
29
+ sessionToken: text("sessionToken").notNull().primaryKey(),
30
+ userId: text("userId")
31
+ .notNull()
32
+ .references(() => users.id, { onDelete: "cascade" }),
33
+ expires: integer("expires", { mode: "timestamp_ms" }).notNull(),
34
+ });
35
+ const verificationTokens = sqliteTable("verificationToken", {
36
+ identifier: text("identifier").notNull(),
37
+ token: text("token").notNull(),
38
+ expires: integer("expires", { mode: "timestamp_ms" }).notNull(),
39
+ }, (vt) => ({
40
+ compoundKey: primaryKey(vt.identifier, vt.token),
41
+ }));
42
+ return { users, accounts, sessions, verificationTokens };
43
+ }
44
+ export function SQLiteDrizzleAdapter(client, tableFn = defaultSqliteTableFn) {
45
+ const { users, accounts, sessions, verificationTokens } = createTables(tableFn);
49
46
  return {
50
- createUser: (data) => {
47
+ createUser(data) {
51
48
  return client
52
49
  .insert(users)
53
50
  .values({ ...data, id: crypto.randomUUID() })
54
51
  .returning()
55
52
  .get();
56
53
  },
57
- getUser: (data) => {
54
+ getUser(data) {
58
55
  return client.select().from(users).where(eq(users.id, data)).get() ?? null;
59
56
  },
60
- getUserByEmail: (data) => {
57
+ getUserByEmail(data) {
61
58
  return (client.select().from(users).where(eq(users.email, data)).get() ?? null);
62
59
  },
63
- createSession: (data) => {
60
+ createSession(data) {
64
61
  return client.insert(sessions).values(data).returning().get();
65
62
  },
66
- getSessionAndUser: (data) => {
63
+ getSessionAndUser(data) {
67
64
  return (client
68
65
  .select({
69
66
  session: sessions,
@@ -74,7 +71,7 @@ export function SQLiteDrizzleAdapter(client) {
74
71
  .innerJoin(users, eq(users.id, sessions.userId))
75
72
  .get() ?? null);
76
73
  },
77
- updateUser: (data) => {
74
+ updateUser(data) {
78
75
  if (!data.id) {
79
76
  throw new Error("No user id.");
80
77
  }
@@ -85,7 +82,7 @@ export function SQLiteDrizzleAdapter(client) {
85
82
  .returning()
86
83
  .get();
87
84
  },
88
- updateSession: (data) => {
85
+ updateSession(data) {
89
86
  return client
90
87
  .update(sessions)
91
88
  .set(data)
@@ -93,7 +90,7 @@ export function SQLiteDrizzleAdapter(client) {
93
90
  .returning()
94
91
  .get();
95
92
  },
96
- linkAccount: (rawAccount) => {
93
+ linkAccount(rawAccount) {
97
94
  const updatedAccount = client
98
95
  .insert(accounts)
99
96
  .values(rawAccount)
@@ -112,7 +109,7 @@ export function SQLiteDrizzleAdapter(client) {
112
109
  };
113
110
  return account;
114
111
  },
115
- getUserByAccount: (account) => {
112
+ getUserByAccount(account) {
116
113
  const results = client
117
114
  .select()
118
115
  .from(accounts)
@@ -121,17 +118,17 @@ export function SQLiteDrizzleAdapter(client) {
121
118
  .get();
122
119
  return results?.users ?? null;
123
120
  },
124
- deleteSession: (sessionToken) => {
121
+ deleteSession(sessionToken) {
125
122
  return (client
126
123
  .delete(sessions)
127
124
  .where(eq(sessions.sessionToken, sessionToken))
128
125
  .returning()
129
126
  .get() ?? null);
130
127
  },
131
- createVerificationToken: (token) => {
128
+ createVerificationToken(token) {
132
129
  return client.insert(verificationTokens).values(token).returning().get();
133
130
  },
134
- useVerificationToken: (token) => {
131
+ useVerificationToken(token) {
135
132
  try {
136
133
  return (client
137
134
  .delete(verificationTokens)
@@ -143,10 +140,10 @@ export function SQLiteDrizzleAdapter(client) {
143
140
  throw new Error("No verification token found.");
144
141
  }
145
142
  },
146
- deleteUser: (id) => {
143
+ deleteUser(id) {
147
144
  return client.delete(users).where(eq(users.id, id)).returning().get();
148
145
  },
149
- unlinkAccount: (account) => {
146
+ unlinkAccount(account) {
150
147
  client
151
148
  .delete(accounts)
152
149
  .where(and(eq(accounts.providerAccountId, account.providerAccountId), eq(accounts.provider, account.provider)))
package/lib/utils.d.ts CHANGED
@@ -1,9 +1,12 @@
1
- import { AnyMySqlTable, MySqlDatabase } from "drizzle-orm/mysql-core";
2
- import { AnyPgTable, PgDatabase } from "drizzle-orm/pg-core";
3
- import { AnySQLiteTable, BaseSQLiteDatabase } from "drizzle-orm/sqlite-core";
4
- import { DefaultSchema as PgSchema } from "./pg";
5
- import { DefaultSchema as MySqlSchema } from "./mysql";
6
- import { DefaultSchema as SQLiteSchema } from "./sqlite";
1
+ import { MySqlDatabase } from "drizzle-orm/mysql-core";
2
+ import { PgDatabase } from "drizzle-orm/pg-core";
3
+ import { BaseSQLiteDatabase } from "drizzle-orm/sqlite-core";
4
+ import type { AnyMySqlTable, MySqlTableFn } from "drizzle-orm/mysql-core";
5
+ import type { AnyPgTable, PgTableFn } from "drizzle-orm/pg-core";
6
+ import type { AnySQLiteTable, SQLiteTableFn } from "drizzle-orm/sqlite-core";
7
+ import type { DefaultSchema as PgSchema } from "./pg.js";
8
+ import type { DefaultSchema as MySqlSchema } from "./mysql.js";
9
+ import type { DefaultSchema as SQLiteSchema } from "./sqlite.js";
7
10
  export type AnyMySqlDatabase = MySqlDatabase<any, any>;
8
11
  export type AnyPgDatabase = PgDatabase<any, any, any>;
9
12
  export type AnySQLiteDatabase = BaseSQLiteDatabase<any, any, any, any>;
@@ -14,6 +17,7 @@ export interface MinimumSchema {
14
17
  }
15
18
  export type SqlFlavorOptions = AnyMySqlDatabase | AnyPgDatabase | AnySQLiteDatabase;
16
19
  export type ClientFlavors<Flavor> = Flavor extends AnyMySqlDatabase ? MinimumSchema["mysql"] : Flavor extends AnyPgDatabase ? MinimumSchema["pg"] : Flavor extends AnySQLiteDatabase ? MinimumSchema["sqlite"] : never;
20
+ export type TableFn<Flavor> = Flavor extends AnyMySqlDatabase ? MySqlTableFn : Flavor extends AnyPgDatabase ? PgTableFn : Flavor extends AnySQLiteDatabase ? AnySQLiteTable : SQLiteTableFn;
17
21
  export declare function isMySqlDatabase(db: any): db is MySqlDatabase<any, any, any, any>;
18
22
  export declare function isPgDatabase(db: any): db is PgDatabase<any, any, any>;
19
23
  export declare function isSQLiteDatabase(db: any): db is AnySQLiteDatabase;
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/lib/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAA;AACrE,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAA;AAC5D,OAAO,EAAE,cAAc,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAA;AAC5E,OAAO,EAAE,aAAa,IAAI,QAAQ,EAAE,MAAM,MAAM,CAAA;AAChD,OAAO,EAAE,aAAa,IAAI,WAAW,EAAE,MAAM,SAAS,CAAA;AACtD,OAAO,EAAE,aAAa,IAAI,YAAY,EAAE,MAAM,UAAU,CAAA;AAExD,MAAM,MAAM,gBAAgB,GAAG,aAAa,CAAC,GAAG,EAAE,GAAG,CAAC,CAAA;AACtD,MAAM,MAAM,aAAa,GAAG,UAAU,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CAAA;AACrD,MAAM,MAAM,iBAAiB,GAAG,kBAAkB,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CAAA;AAEtE,MAAM,WAAW,aAAa;IAC5B,KAAK,EAAE,WAAW,GAAG,MA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G,GACN,EAAE,IAAI,aAAa,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CAEzC;AAED,wBAAgB,YAAY,CAAC,EAAE,EAAE,GAAG,GAAG,EAAE,IAAI,UAAU,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CAErE;AAED,wBAAgB,gBAAgB,CAAC,EAAE,EAAE,GAAG,GAAG,EAAE,IAAI,iBAAiB,CAEjE"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@auth/drizzle-adapter",
3
- "version": "0.0.0-manual.ffc00566",
3
+ "version": "0.2.0",
4
4
  "description": "Drizzle adapter for Auth.js.",
5
5
  "homepage": "https://authjs.dev",
6
6
  "repository": "https://github.com/nextauthjs/next-auth",
@@ -36,7 +36,7 @@
36
36
  "access": "public"
37
37
  },
38
38
  "dependencies": {
39
- "@auth/core": "0.10.0"
39
+ "@auth/core": "0.10.1"
40
40
  },
41
41
  "devDependencies": {
42
42
  "@types/better-sqlite3": "^7.6.4",
@@ -47,8 +47,8 @@
47
47
  "jest": "^27.4.3",
48
48
  "mysql2": "^3.2.0",
49
49
  "postgres": "^3.3.4",
50
- "@next-auth/tsconfig": "0.0.0",
51
- "@next-auth/adapter-test": "0.0.0"
50
+ "@next-auth/adapter-test": "0.0.0",
51
+ "@next-auth/tsconfig": "0.0.0"
52
52
  },
53
53
  "jest": {
54
54
  "preset": "@next-auth/adapter-test/jest"
package/src/index.ts CHANGED
@@ -16,20 +16,24 @@
16
16
  * @module @auth/drizzle-adapter
17
17
  */
18
18
 
19
- import { mySqlDrizzleAdapter } from "./lib/mysql"
20
- import { pgDrizzleAdapter } from "./lib/pg"
21
- import { SQLiteDrizzleAdapter } from "./lib/sqlite"
19
+ import { MySqlTableFn } from "drizzle-orm/mysql-core/index.js"
20
+ import { PgTableFn } from "drizzle-orm/pg-core/index.js"
21
+ import { SQLiteTableFn } from "drizzle-orm/sqlite-core/index.js"
22
+ import { mySqlDrizzleAdapter } from "./lib/mysql.js"
23
+ import { pgDrizzleAdapter } from "./lib/pg.js"
24
+ import { SQLiteDrizzleAdapter } from "./lib/sqlite.js"
22
25
  import {
23
26
  isMySqlDatabase,
24
27
  isPgDatabase,
25
28
  isSQLiteDatabase,
26
29
  SqlFlavorOptions,
27
- } from "./lib/utils"
30
+ TableFn,
31
+ } from "./lib/utils.js"
28
32
 
29
33
  import type { Adapter } from "@auth/core/adapters"
30
34
 
31
35
  /**
32
- * Add the adapter to your `app/api/[...nextauth]/route.js` next-auth configuration object.
36
+ * Add the adapter to your `pages/api/[...nextauth].ts` next-auth configuration object.
33
37
  *
34
38
  * ```ts title="pages/api/auth/[...nextauth].ts"
35
39
  * import NextAuth from "next-auth"
@@ -47,6 +51,10 @@ import type { Adapter } from "@auth/core/adapters"
47
51
  * ],
48
52
  * })
49
53
  * ```
54
+ *
55
+ * :::info
56
+ * If you're using multi-project schemas, you can pass your table function as a second argument
57
+ * :::
50
58
  *
51
59
  * ## Setup
52
60
  *
@@ -249,19 +257,20 @@ import type { Adapter } from "@auth/core/adapters"
249
257
  *
250
258
  **/
251
259
  export function DrizzleAdapter<SqlFlavor extends SqlFlavorOptions>(
252
- db: SqlFlavor
260
+ db: SqlFlavor,
261
+ table?: TableFn<SqlFlavor>
253
262
  ): Adapter {
254
263
  if (isMySqlDatabase(db)) {
255
264
  // We need to cast to unknown since the type overlaps (PScale is MySQL based)
256
- return mySqlDrizzleAdapter(db)
265
+ return mySqlDrizzleAdapter(db, table as MySqlTableFn)
257
266
  }
258
267
 
259
268
  if (isPgDatabase(db)) {
260
- return pgDrizzleAdapter(db)
269
+ return pgDrizzleAdapter(db, table as PgTableFn)
261
270
  }
262
271
 
263
272
  if (isSQLiteDatabase(db)) {
264
- return SQLiteDrizzleAdapter(db)
273
+ return SQLiteDrizzleAdapter(db, table as SQLiteTableFn)
265
274
  }
266
275
 
267
276
  throw new Error("Unsupported database type in Auth.js Drizzle adapter.")
package/src/lib/mysql.ts CHANGED
@@ -2,82 +2,89 @@ import { and, eq } from "drizzle-orm"
2
2
  import {
3
3
  int,
4
4
  timestamp,
5
- mysqlTable,
5
+ mysqlTable as defaultMySqlTableFn,
6
6
  primaryKey,
7
7
  varchar,
8
+ MySqlTableFn,
8
9
  } from "drizzle-orm/mysql-core"
10
+
9
11
  import type { Adapter, AdapterAccount } from "@auth/core/adapters"
10
- import { MySql2Database } from "drizzle-orm/mysql2"
12
+ import type { MySql2Database } from "drizzle-orm/mysql2"
13
+
14
+ export function createTables(mySqlTable: MySqlTableFn) {
15
+ const users = mySqlTable("users", {
16
+ id: varchar("id", { length: 255 }).notNull().primaryKey(),
17
+ name: varchar("name", { length: 255 }),
18
+ email: varchar("email", { length: 255 }).notNull(),
19
+ emailVerified: timestamp("emailVerified", {
20
+ mode: "date",
21
+ fsp: 3,
22
+ }).defaultNow(),
23
+ image: varchar("image", { length: 255 }),
24
+ })
11
25
 
12
- /** @internal */
13
- export const users = mysqlTable("users", {
14
- id: varchar("id", { length: 255 }).notNull().primaryKey(),
15
- name: varchar("name", { length: 255 }),
16
- email: varchar("email", { length: 255 }).notNull(),
17
- emailVerified: timestamp("emailVerified", {
18
- mode: "date",
19
- fsp: 3,
20
- }).defaultNow(),
21
- image: varchar("image", { length: 255 }),
22
- })
26
+ const accounts = mySqlTable(
27
+ "accounts",
28
+ {
29
+ userId: varchar("userId", { length: 255 })
30
+ .notNull()
31
+ .references(() => users.id, { onDelete: "cascade" }),
32
+ type: varchar("type", { length: 255 })
33
+ .$type<AdapterAccount["type"]>()
34
+ .notNull(),
35
+ provider: varchar("provider", { length: 255 }).notNull(),
36
+ providerAccountId: varchar("providerAccountId", {
37
+ length: 255,
38
+ }).notNull(),
39
+ refresh_token: varchar("refresh_token", { length: 255 }),
40
+ access_token: varchar("access_token", { length: 255 }),
41
+ expires_at: int("expires_at"),
42
+ token_type: varchar("token_type", { length: 255 }),
43
+ scope: varchar("scope", { length: 255 }),
44
+ id_token: varchar("id_token", { length: 255 }),
45
+ session_state: varchar("session_state", { length: 255 }),
46
+ },
47
+ (account) => ({
48
+ compoundKey: primaryKey(account.provider, account.providerAccountId),
49
+ })
50
+ )
23
51
 
24
- /** @internal */
25
- export const accounts = mysqlTable(
26
- "accounts",
27
- {
52
+ const sessions = mySqlTable("sessions", {
53
+ sessionToken: varchar("sessionToken", { length: 255 })
54
+ .notNull()
55
+ .primaryKey(),
28
56
  userId: varchar("userId", { length: 255 })
29
57
  .notNull()
30
58
  .references(() => users.id, { onDelete: "cascade" }),
31
- type: varchar("type", { length: 255 })
32
- .$type<AdapterAccount["type"]>()
33
- .notNull(),
34
- provider: varchar("provider", { length: 255 }).notNull(),
35
- providerAccountId: varchar("providerAccountId", { length: 255 }).notNull(),
36
- refresh_token: varchar("refresh_token", { length: 255 }),
37
- access_token: varchar("access_token", { length: 255 }),
38
- expires_at: int("expires_at"),
39
- token_type: varchar("token_type", { length: 255 }),
40
- scope: varchar("scope", { length: 255 }),
41
- id_token: varchar("id_token", { length: 255 }),
42
- session_state: varchar("session_state", { length: 255 }),
43
- },
44
- (account) => ({
45
- compoundKey: primaryKey(account.provider, account.providerAccountId),
59
+ expires: timestamp("expires", { mode: "date" }).notNull(),
46
60
  })
47
- )
48
61
 
49
- /** @internal */
50
- export const sessions = mysqlTable("sessions", {
51
- sessionToken: varchar("sessionToken", { length: 255 }).notNull().primaryKey(),
52
- userId: varchar("userId", { length: 255 })
53
- .notNull()
54
- .references(() => users.id, { onDelete: "cascade" }),
55
- expires: timestamp("expires", { mode: "date" }).notNull(),
56
- })
62
+ const verificationTokens = mySqlTable(
63
+ "verificationToken",
64
+ {
65
+ identifier: varchar("identifier", { length: 255 }).notNull(),
66
+ token: varchar("token", { length: 255 }).notNull(),
67
+ expires: timestamp("expires", { mode: "date" }).notNull(),
68
+ },
69
+ (vt) => ({
70
+ compoundKey: primaryKey(vt.identifier, vt.token),
71
+ })
72
+ )
57
73
 
58
- /** @internal */
59
- export const verificationTokens = mysqlTable(
60
- "verificationToken",
61
- {
62
- identifier: varchar("identifier", { length: 255 }).notNull(),
63
- token: varchar("token", { length: 255 }).notNull(),
64
- expires: timestamp("expires", { mode: "date" }).notNull(),
65
- },
66
- (vt) => ({
67
- compoundKey: primaryKey(vt.identifier, vt.token),
68
- })
69
- )
74
+ return { users, accounts, sessions, verificationTokens }
75
+ }
70
76
 
71
- /** @internal */
72
- export const schema = { users, accounts, sessions, verificationTokens }
73
- export type DefaultSchema = typeof schema
77
+ export type DefaultSchema = ReturnType<typeof createTables>
74
78
 
75
- /** @internal */
76
79
  export function mySqlDrizzleAdapter(
77
- client: MySql2Database<Record<string, never>>
80
+ client: MySql2Database<Record<string, never>>,
81
+ tableFn = defaultMySqlTableFn
78
82
  ): Adapter {
83
+ const { users, accounts, sessions, verificationTokens } =
84
+ createTables(tableFn)
85
+
79
86
  return {
80
- createUser: async (data) => {
87
+ async createUser(data) {
81
88
  const id = crypto.randomUUID()
82
89
 
83
90
  await client.insert(users).values({ ...data, id })
@@ -88,7 +95,7 @@ export function mySqlDrizzleAdapter(
88
95
  .where(eq(users.id, id))
89
96
  .then((res) => res[0])
90
97
  },
91
- getUser: async (data) => {
98
+ async getUser(data) {
92
99
  const thing =
93
100
  (await client
94
101
  .select()
@@ -98,7 +105,7 @@ export function mySqlDrizzleAdapter(
98
105
 
99
106
  return thing
100
107
  },
101
- getUserByEmail: async (data) => {
108
+ async getUserByEmail(data) {
102
109
  const user =
103
110
  (await client
104
111
  .select()
@@ -108,7 +115,7 @@ export function mySqlDrizzleAdapter(
108
115
 
109
116
  return user
110
117
  },
111
- createSession: async (data) => {
118
+ async createSession(data) {
112
119
  await client.insert(sessions).values(data)
113
120
 
114
121
  return await client
@@ -117,7 +124,7 @@ export function mySqlDrizzleAdapter(
117
124
  .where(eq(sessions.sessionToken, data.sessionToken))
118
125
  .then((res) => res[0])
119
126
  },
120
- getSessionAndUser: async (data) => {
127
+ async getSessionAndUser(data) {
121
128
  const sessionAndUser =
122
129
  (await client
123
130
  .select({
@@ -131,7 +138,7 @@ export function mySqlDrizzleAdapter(
131
138
 
132
139
  return sessionAndUser
133
140
  },
134
- updateUser: async (data) => {
141
+ async updateUser(data) {
135
142
  if (!data.id) {
136
143
  throw new Error("No user id.")
137
144
  }
@@ -144,7 +151,7 @@ export function mySqlDrizzleAdapter(
144
151
  .where(eq(users.id, data.id))
145
152
  .then((res) => res[0])
146
153
  },
147
- updateSession: async (data) => {
154
+ async updateSession(data) {
148
155
  await client
149
156
  .update(sessions)
150
157
  .set(data)
@@ -156,13 +163,13 @@ export function mySqlDrizzleAdapter(
156
163
  .where(eq(sessions.sessionToken, data.sessionToken))
157
164
  .then((res) => res[0])
158
165
  },
159
- linkAccount: async (rawAccount) => {
166
+ async linkAccount(rawAccount) {
160
167
  await client
161
168
  .insert(accounts)
162
169
  .values(rawAccount)
163
170
  .then((res) => res[0])
164
171
  },
165
- getUserByAccount: async (account) => {
172
+ async getUserByAccount(account) {
166
173
  const dbAccount =
167
174
  (await client
168
175
  .select()
@@ -182,7 +189,7 @@ export function mySqlDrizzleAdapter(
182
189
 
183
190
  return dbAccount.users
184
191
  },
185
- deleteSession: async (sessionToken) => {
192
+ async deleteSession(sessionToken) {
186
193
  const session =
187
194
  (await client
188
195
  .select()
@@ -196,7 +203,7 @@ export function mySqlDrizzleAdapter(
196
203
 
197
204
  return session
198
205
  },
199
- createVerificationToken: async (token) => {
206
+ async createVerificationToken(token) {
200
207
  await client.insert(verificationTokens).values(token)
201
208
 
202
209
  return await client
@@ -205,7 +212,7 @@ export function mySqlDrizzleAdapter(
205
212
  .where(eq(verificationTokens.identifier, token.identifier))
206
213
  .then((res) => res[0])
207
214
  },
208
- useVerificationToken: async (token) => {
215
+ async useVerificationToken(token) {
209
216
  try {
210
217
  const deletedToken =
211
218
  (await client
@@ -233,7 +240,7 @@ export function mySqlDrizzleAdapter(
233
240
  throw new Error("No verification token found.")
234
241
  }
235
242
  },
236
- deleteUser: async (id) => {
243
+ async deleteUser(id) {
237
244
  const user = await client
238
245
  .select()
239
246
  .from(users)
@@ -244,7 +251,7 @@ export function mySqlDrizzleAdapter(
244
251
 
245
252
  return user
246
253
  },
247
- unlinkAccount: async (account) => {
254
+ async unlinkAccount(account) {
248
255
  await client
249
256
  .delete(accounts)
250
257
  .where(