@auth/drizzle-adapter 0.8.2 → 1.0.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/mysql.js CHANGED
@@ -1,189 +1,181 @@
1
1
  import { and, eq } from "drizzle-orm";
2
- import { int, timestamp, mysqlTable as defaultMySqlTableFn, primaryKey, varchar, } from "drizzle-orm/mysql-core";
3
- export function createTables(mySqlTable) {
4
- const users = mySqlTable("user", {
5
- id: varchar("id", { length: 255 }).notNull().primaryKey(),
6
- name: varchar("name", { length: 255 }),
7
- email: varchar("email", { length: 255 }).notNull(),
8
- emailVerified: timestamp("emailVerified", {
9
- mode: "date",
10
- fsp: 3,
11
- }).defaultNow(),
12
- image: varchar("image", { length: 255 }),
13
- });
14
- const accounts = mySqlTable("account", {
15
- userId: varchar("userId", { length: 255 })
16
- .notNull()
17
- .references(() => users.id, { onDelete: "cascade" }),
18
- type: varchar("type", { length: 255 })
19
- .$type()
20
- .notNull(),
21
- provider: varchar("provider", { length: 255 }).notNull(),
22
- providerAccountId: varchar("providerAccountId", {
23
- length: 255,
24
- }).notNull(),
25
- refresh_token: varchar("refresh_token", { length: 255 }),
26
- access_token: varchar("access_token", { length: 255 }),
27
- expires_at: int("expires_at"),
28
- token_type: varchar("token_type", { length: 255 }),
29
- scope: varchar("scope", { length: 255 }),
30
- id_token: varchar("id_token", { length: 255 }),
31
- session_state: varchar("session_state", { length: 255 }),
32
- }, (account) => ({
33
- compoundKey: primaryKey(account.provider, account.providerAccountId),
34
- }));
35
- const sessions = mySqlTable("session", {
36
- sessionToken: varchar("sessionToken", { length: 255 })
37
- .notNull()
38
- .primaryKey(),
39
- userId: varchar("userId", { length: 255 })
40
- .notNull()
41
- .references(() => users.id, { onDelete: "cascade" }),
42
- expires: timestamp("expires", { mode: "date" }).notNull(),
43
- });
44
- const verificationTokens = mySqlTable("verificationToken", {
45
- identifier: varchar("identifier", { length: 255 }).notNull(),
46
- token: varchar("token", { length: 255 }).notNull(),
47
- expires: timestamp("expires", { mode: "date" }).notNull(),
48
- }, (vt) => ({
49
- compoundKey: primaryKey(vt.identifier, vt.token),
50
- }));
51
- return { users, accounts, sessions, verificationTokens };
52
- }
53
- export function mySqlDrizzleAdapter(client, tableFn = defaultMySqlTableFn) {
54
- const { users, accounts, sessions, verificationTokens } = createTables(tableFn);
2
+ import { int, timestamp, primaryKey, varchar, mysqlTable, index, } from "drizzle-orm/mysql-core";
3
+ import { randomUUID } from "crypto";
4
+ export const mysqlUsersTable = mysqlTable("user", {
5
+ id: varchar("id", { length: 255 })
6
+ .primaryKey()
7
+ .$defaultFn(() => randomUUID()),
8
+ name: varchar("name", { length: 255 }),
9
+ email: varchar("email", { length: 255 }).notNull().unique(),
10
+ emailVerified: timestamp("emailVerified", { mode: "date", fsp: 3 }),
11
+ image: varchar("image", { length: 255 }),
12
+ });
13
+ export const mysqlAccountsTable = mysqlTable("account", {
14
+ userId: varchar("userId", { length: 255 })
15
+ .notNull()
16
+ .references(() => mysqlUsersTable.id, { onDelete: "cascade" }),
17
+ type: varchar("type", { length: 255 }).notNull(),
18
+ provider: varchar("provider", { length: 255 }).notNull(),
19
+ providerAccountId: varchar("providerAccountId", { length: 255 }).notNull(),
20
+ refresh_token: varchar("refresh_token", { length: 255 }),
21
+ access_token: varchar("access_token", { length: 255 }),
22
+ expires_at: int("expires_at"),
23
+ token_type: varchar("token_type", { length: 255 }),
24
+ scope: varchar("scope", { length: 255 }),
25
+ id_token: varchar("id_token", { length: 2048 }),
26
+ session_state: varchar("session_state", { length: 255 }),
27
+ }, (account) => ({
28
+ compositePk: primaryKey({
29
+ columns: [account.provider, account.providerAccountId],
30
+ }),
31
+ userIdIdx: index("Account_userId_index").on(account.userId),
32
+ }));
33
+ export const mysqlSessionsTable = mysqlTable("session", {
34
+ id: varchar("id", { length: 255 })
35
+ .primaryKey()
36
+ .$defaultFn(() => randomUUID()),
37
+ sessionToken: varchar("sessionToken", { length: 255 }).notNull().unique(),
38
+ userId: varchar("userId", { length: 255 })
39
+ .notNull()
40
+ .references(() => mysqlUsersTable.id, { onDelete: "cascade" }),
41
+ expires: timestamp("expires", { mode: "date" }).notNull(),
42
+ }, (session) => ({
43
+ userIdIdx: index("Session_userId_index").on(session.userId),
44
+ }));
45
+ export const mysqlVerificationTokensTable = mysqlTable("verificationToken", {
46
+ identifier: varchar("identifier", { length: 255 }).notNull(),
47
+ token: varchar("token", { length: 255 }).notNull().unique(),
48
+ expires: timestamp("expires", { mode: "date" }).notNull(),
49
+ }, (vt) => ({
50
+ compositePk: primaryKey({ columns: [vt.identifier, vt.token] }),
51
+ }));
52
+ export function MySqlDrizzleAdapter(client, schema = {
53
+ usersTable: mysqlUsersTable,
54
+ accountsTable: mysqlAccountsTable,
55
+ sessionsTable: mysqlSessionsTable,
56
+ verificationTokensTable: mysqlVerificationTokensTable,
57
+ }) {
58
+ const { usersTable, accountsTable, sessionsTable, verificationTokensTable } = schema;
55
59
  return {
56
60
  async createUser(data) {
57
- const id = crypto.randomUUID();
58
- await client.insert(users).values({ ...data, id });
59
- return await client
61
+ const id = randomUUID();
62
+ await client.insert(usersTable).values({ ...data, id });
63
+ return client
60
64
  .select()
61
- .from(users)
62
- .where(eq(users.id, id))
65
+ .from(usersTable)
66
+ .where(eq(usersTable.id, id))
63
67
  .then((res) => res[0]);
64
68
  },
65
- async getUser(data) {
66
- const thing = (await client
69
+ async getUser(userId) {
70
+ return client
67
71
  .select()
68
- .from(users)
69
- .where(eq(users.id, data))
70
- .then((res) => res[0])) ?? null;
71
- return thing;
72
+ .from(usersTable)
73
+ .where(eq(usersTable.id, userId))
74
+ .then((res) => (res.length > 0 ? res[0] : null));
72
75
  },
73
- async getUserByEmail(data) {
74
- const user = (await client
76
+ async getUserByEmail(email) {
77
+ return client
75
78
  .select()
76
- .from(users)
77
- .where(eq(users.email, data))
78
- .then((res) => res[0])) ?? null;
79
- return user;
79
+ .from(usersTable)
80
+ .where(eq(usersTable.email, email))
81
+ .then((res) => (res.length > 0 ? res[0] : null));
80
82
  },
81
83
  async createSession(data) {
82
- await client.insert(sessions).values(data);
83
- return await client
84
+ const id = randomUUID();
85
+ await client.insert(sessionsTable).values({ ...data, id });
86
+ return client
84
87
  .select()
85
- .from(sessions)
86
- .where(eq(sessions.sessionToken, data.sessionToken))
88
+ .from(sessionsTable)
89
+ .where(eq(sessionsTable.id, id))
87
90
  .then((res) => res[0]);
88
91
  },
89
- async getSessionAndUser(data) {
90
- const sessionAndUser = (await client
92
+ async getSessionAndUser(sessionToken) {
93
+ return client
91
94
  .select({
92
- session: sessions,
93
- user: users,
95
+ session: sessionsTable,
96
+ user: usersTable,
94
97
  })
95
- .from(sessions)
96
- .where(eq(sessions.sessionToken, data))
97
- .innerJoin(users, eq(users.id, sessions.userId))
98
- .then((res) => res[0])) ?? null;
99
- return sessionAndUser;
98
+ .from(sessionsTable)
99
+ .where(eq(sessionsTable.sessionToken, sessionToken))
100
+ .innerJoin(usersTable, eq(usersTable.id, sessionsTable.userId))
101
+ .then((res) => (res.length > 0 ? res[0] : null));
100
102
  },
101
103
  async updateUser(data) {
102
104
  if (!data.id) {
103
105
  throw new Error("No user id.");
104
106
  }
105
- await client.update(users).set(data).where(eq(users.id, data.id));
106
- return await client
107
+ await client
108
+ .update(usersTable)
109
+ .set(data)
110
+ .where(eq(usersTable.id, data.id));
111
+ const [result] = await client
107
112
  .select()
108
- .from(users)
109
- .where(eq(users.id, data.id))
110
- .then((res) => res[0]);
113
+ .from(usersTable)
114
+ .where(eq(usersTable.id, data.id));
115
+ if (!result) {
116
+ throw new Error("No user found.");
117
+ }
118
+ return result;
111
119
  },
112
120
  async updateSession(data) {
113
121
  await client
114
- .update(sessions)
122
+ .update(sessionsTable)
115
123
  .set(data)
116
- .where(eq(sessions.sessionToken, data.sessionToken));
117
- return await client
124
+ .where(eq(sessionsTable.sessionToken, data.sessionToken));
125
+ return client
118
126
  .select()
119
- .from(sessions)
120
- .where(eq(sessions.sessionToken, data.sessionToken))
127
+ .from(sessionsTable)
128
+ .where(eq(sessionsTable.sessionToken, data.sessionToken))
121
129
  .then((res) => res[0]);
122
130
  },
123
- async linkAccount(rawAccount) {
124
- await client.insert(accounts).values(rawAccount);
131
+ async linkAccount(data) {
132
+ await client.insert(accountsTable).values(data);
125
133
  },
126
134
  async getUserByAccount(account) {
127
- const dbAccount = (await client
128
- .select()
129
- .from(accounts)
130
- .where(and(eq(accounts.providerAccountId, account.providerAccountId), eq(accounts.provider, account.provider)))
131
- .leftJoin(users, eq(accounts.userId, users.id))
132
- .then((res) => res[0])) ?? null;
133
- if (!dbAccount) {
134
- return null;
135
- }
136
- return dbAccount.user;
135
+ const result = await client
136
+ .select({
137
+ account: accountsTable,
138
+ user: usersTable,
139
+ })
140
+ .from(accountsTable)
141
+ .innerJoin(usersTable, eq(accountsTable.userId, usersTable.id))
142
+ .where(and(eq(accountsTable.provider, account.provider), eq(accountsTable.providerAccountId, account.providerAccountId)))
143
+ .then((res) => res[0]);
144
+ return result?.user ?? null;
137
145
  },
138
146
  async deleteSession(sessionToken) {
139
- const session = (await client
140
- .select()
141
- .from(sessions)
142
- .where(eq(sessions.sessionToken, sessionToken))
143
- .then((res) => res[0])) ?? null;
144
147
  await client
145
- .delete(sessions)
146
- .where(eq(sessions.sessionToken, sessionToken));
147
- return session;
148
+ .delete(sessionsTable)
149
+ .where(eq(sessionsTable.sessionToken, sessionToken));
148
150
  },
149
- async createVerificationToken(token) {
150
- await client.insert(verificationTokens).values(token);
151
- return await client
151
+ async createVerificationToken(data) {
152
+ await client.insert(verificationTokensTable).values(data);
153
+ return client
152
154
  .select()
153
- .from(verificationTokens)
154
- .where(eq(verificationTokens.identifier, token.identifier))
155
+ .from(verificationTokensTable)
156
+ .where(eq(verificationTokensTable.identifier, data.identifier))
155
157
  .then((res) => res[0]);
156
158
  },
157
- async useVerificationToken(token) {
158
- try {
159
- const deletedToken = (await client
160
- .select()
161
- .from(verificationTokens)
162
- .where(and(eq(verificationTokens.identifier, token.identifier), eq(verificationTokens.token, token.token)))
163
- .then((res) => res[0])) ?? null;
159
+ async useVerificationToken(params) {
160
+ const deletedToken = await client
161
+ .select()
162
+ .from(verificationTokensTable)
163
+ .where(and(eq(verificationTokensTable.identifier, params.identifier), eq(verificationTokensTable.token, params.token)))
164
+ .then((res) => (res.length > 0 ? res[0] : null));
165
+ if (deletedToken) {
164
166
  await client
165
- .delete(verificationTokens)
166
- .where(and(eq(verificationTokens.identifier, token.identifier), eq(verificationTokens.token, token.token)));
167
- return deletedToken;
168
- }
169
- catch (err) {
170
- throw new Error("No verification token found.");
167
+ .delete(verificationTokensTable)
168
+ .where(and(eq(verificationTokensTable.identifier, params.identifier), eq(verificationTokensTable.token, params.token)));
171
169
  }
170
+ return deletedToken;
172
171
  },
173
172
  async deleteUser(id) {
174
- const user = await client
175
- .select()
176
- .from(users)
177
- .where(eq(users.id, id))
178
- .then((res) => res[0] ?? null);
179
- await client.delete(users).where(eq(users.id, id));
180
- return user;
173
+ await client.delete(usersTable).where(eq(usersTable.id, id));
181
174
  },
182
- async unlinkAccount(account) {
175
+ async unlinkAccount(params) {
183
176
  await client
184
- .delete(accounts)
185
- .where(and(eq(accounts.providerAccountId, account.providerAccountId), eq(accounts.provider, account.provider)));
186
- return undefined;
177
+ .delete(accountsTable)
178
+ .where(and(eq(accountsTable.provider, params.provider), eq(accountsTable.providerAccountId, params.providerAccountId)));
187
179
  },
188
180
  };
189
181
  }