@auth/drizzle-adapter 0.2.1 → 0.3.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/index.d.ts +9 -9
- package/index.js +9 -9
- package/package.json +2 -2
- package/src/index.ts +9 -9
- package/src/lib/mysql.ts +4 -4
- package/src/lib/pg.ts +4 -4
- package/src/lib/sqlite.ts +4 -4
- package/lib/mysql.d.ts +0 -226
- package/lib/mysql.d.ts.map +0 -1
- package/lib/mysql.js +0 -192
- package/lib/pg.d.ts +0 -226
- package/lib/pg.d.ts.map +0 -1
- package/lib/pg.js +0 -180
- package/lib/sqlite.d.ts +0 -225
- package/lib/sqlite.d.ts.map +0 -1
- package/lib/sqlite.js +0 -154
- package/lib/utils.d.ts +0 -24
- package/lib/utils.d.ts.map +0 -1
- package/lib/utils.js +0 -12
package/lib/mysql.js
DELETED
|
@@ -1,192 +0,0 @@
|
|
|
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("users", {
|
|
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("accounts", {
|
|
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("sessions", {
|
|
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);
|
|
55
|
-
return {
|
|
56
|
-
async createUser(data) {
|
|
57
|
-
const id = crypto.randomUUID();
|
|
58
|
-
await client.insert(users).values({ ...data, id });
|
|
59
|
-
return await client
|
|
60
|
-
.select()
|
|
61
|
-
.from(users)
|
|
62
|
-
.where(eq(users.id, id))
|
|
63
|
-
.then((res) => res[0]);
|
|
64
|
-
},
|
|
65
|
-
async getUser(data) {
|
|
66
|
-
const thing = (await client
|
|
67
|
-
.select()
|
|
68
|
-
.from(users)
|
|
69
|
-
.where(eq(users.id, data))
|
|
70
|
-
.then((res) => res[0])) ?? null;
|
|
71
|
-
return thing;
|
|
72
|
-
},
|
|
73
|
-
async getUserByEmail(data) {
|
|
74
|
-
const user = (await client
|
|
75
|
-
.select()
|
|
76
|
-
.from(users)
|
|
77
|
-
.where(eq(users.email, data))
|
|
78
|
-
.then((res) => res[0])) ?? null;
|
|
79
|
-
return user;
|
|
80
|
-
},
|
|
81
|
-
async createSession(data) {
|
|
82
|
-
await client.insert(sessions).values(data);
|
|
83
|
-
return await client
|
|
84
|
-
.select()
|
|
85
|
-
.from(sessions)
|
|
86
|
-
.where(eq(sessions.sessionToken, data.sessionToken))
|
|
87
|
-
.then((res) => res[0]);
|
|
88
|
-
},
|
|
89
|
-
async getSessionAndUser(data) {
|
|
90
|
-
const sessionAndUser = (await client
|
|
91
|
-
.select({
|
|
92
|
-
session: sessions,
|
|
93
|
-
user: users,
|
|
94
|
-
})
|
|
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;
|
|
100
|
-
},
|
|
101
|
-
async updateUser(data) {
|
|
102
|
-
if (!data.id) {
|
|
103
|
-
throw new Error("No user id.");
|
|
104
|
-
}
|
|
105
|
-
await client.update(users).set(data).where(eq(users.id, data.id));
|
|
106
|
-
return await client
|
|
107
|
-
.select()
|
|
108
|
-
.from(users)
|
|
109
|
-
.where(eq(users.id, data.id))
|
|
110
|
-
.then((res) => res[0]);
|
|
111
|
-
},
|
|
112
|
-
async updateSession(data) {
|
|
113
|
-
await client
|
|
114
|
-
.update(sessions)
|
|
115
|
-
.set(data)
|
|
116
|
-
.where(eq(sessions.sessionToken, data.sessionToken));
|
|
117
|
-
return await client
|
|
118
|
-
.select()
|
|
119
|
-
.from(sessions)
|
|
120
|
-
.where(eq(sessions.sessionToken, data.sessionToken))
|
|
121
|
-
.then((res) => res[0]);
|
|
122
|
-
},
|
|
123
|
-
async linkAccount(rawAccount) {
|
|
124
|
-
await client
|
|
125
|
-
.insert(accounts)
|
|
126
|
-
.values(rawAccount)
|
|
127
|
-
.then((res) => res[0]);
|
|
128
|
-
},
|
|
129
|
-
async getUserByAccount(account) {
|
|
130
|
-
const dbAccount = (await client
|
|
131
|
-
.select()
|
|
132
|
-
.from(accounts)
|
|
133
|
-
.where(and(eq(accounts.providerAccountId, account.providerAccountId), eq(accounts.provider, account.provider)))
|
|
134
|
-
.leftJoin(users, eq(accounts.userId, users.id))
|
|
135
|
-
.then((res) => res[0])) ?? null;
|
|
136
|
-
if (!dbAccount) {
|
|
137
|
-
return null;
|
|
138
|
-
}
|
|
139
|
-
return dbAccount.users;
|
|
140
|
-
},
|
|
141
|
-
async deleteSession(sessionToken) {
|
|
142
|
-
const session = (await client
|
|
143
|
-
.select()
|
|
144
|
-
.from(sessions)
|
|
145
|
-
.where(eq(sessions.sessionToken, sessionToken))
|
|
146
|
-
.then((res) => res[0])) ?? null;
|
|
147
|
-
await client
|
|
148
|
-
.delete(sessions)
|
|
149
|
-
.where(eq(sessions.sessionToken, sessionToken));
|
|
150
|
-
return session;
|
|
151
|
-
},
|
|
152
|
-
async createVerificationToken(token) {
|
|
153
|
-
await client.insert(verificationTokens).values(token);
|
|
154
|
-
return await client
|
|
155
|
-
.select()
|
|
156
|
-
.from(verificationTokens)
|
|
157
|
-
.where(eq(verificationTokens.identifier, token.identifier))
|
|
158
|
-
.then((res) => res[0]);
|
|
159
|
-
},
|
|
160
|
-
async useVerificationToken(token) {
|
|
161
|
-
try {
|
|
162
|
-
const deletedToken = (await client
|
|
163
|
-
.select()
|
|
164
|
-
.from(verificationTokens)
|
|
165
|
-
.where(and(eq(verificationTokens.identifier, token.identifier), eq(verificationTokens.token, token.token)))
|
|
166
|
-
.then((res) => res[0])) ?? null;
|
|
167
|
-
await client
|
|
168
|
-
.delete(verificationTokens)
|
|
169
|
-
.where(and(eq(verificationTokens.identifier, token.identifier), eq(verificationTokens.token, token.token)));
|
|
170
|
-
return deletedToken;
|
|
171
|
-
}
|
|
172
|
-
catch (err) {
|
|
173
|
-
throw new Error("No verification token found.");
|
|
174
|
-
}
|
|
175
|
-
},
|
|
176
|
-
async deleteUser(id) {
|
|
177
|
-
const user = await client
|
|
178
|
-
.select()
|
|
179
|
-
.from(users)
|
|
180
|
-
.where(eq(users.id, id))
|
|
181
|
-
.then((res) => res[0] ?? null);
|
|
182
|
-
await client.delete(users).where(eq(users.id, id));
|
|
183
|
-
return user;
|
|
184
|
-
},
|
|
185
|
-
async unlinkAccount(account) {
|
|
186
|
-
await client
|
|
187
|
-
.delete(accounts)
|
|
188
|
-
.where(and(eq(accounts.providerAccountId, account.providerAccountId), eq(accounts.provider, account.provider)));
|
|
189
|
-
return undefined;
|
|
190
|
-
},
|
|
191
|
-
};
|
|
192
|
-
}
|
package/lib/pg.d.ts
DELETED
|
@@ -1,226 +0,0 @@
|
|
|
1
|
-
import { PgTableFn } from "drizzle-orm/pg-core";
|
|
2
|
-
import type { PostgresJsDatabase } from "drizzle-orm/postgres-js";
|
|
3
|
-
import type { Adapter } from "@auth/core/adapters";
|
|
4
|
-
export declare function createTables(pgTable: PgTableFn): {
|
|
5
|
-
users: import("drizzle-orm/db.d-b9835153").az<{
|
|
6
|
-
name: "users";
|
|
7
|
-
schema: undefined;
|
|
8
|
-
columns: {
|
|
9
|
-
id: import("drizzle-orm/pg-core").PgText<{
|
|
10
|
-
tableName: "users";
|
|
11
|
-
enumValues: [string, ...string[]];
|
|
12
|
-
name: "id";
|
|
13
|
-
data: string;
|
|
14
|
-
driverParam: string;
|
|
15
|
-
hasDefault: false;
|
|
16
|
-
notNull: true;
|
|
17
|
-
}>;
|
|
18
|
-
name: import("drizzle-orm/pg-core").PgText<{
|
|
19
|
-
tableName: "users";
|
|
20
|
-
name: "name";
|
|
21
|
-
data: string;
|
|
22
|
-
enumValues: [string, ...string[]];
|
|
23
|
-
driverParam: string;
|
|
24
|
-
notNull: false;
|
|
25
|
-
hasDefault: false;
|
|
26
|
-
}>;
|
|
27
|
-
email: import("drizzle-orm/pg-core").PgText<{
|
|
28
|
-
tableName: "users";
|
|
29
|
-
enumValues: [string, ...string[]];
|
|
30
|
-
name: "email";
|
|
31
|
-
data: string;
|
|
32
|
-
driverParam: string;
|
|
33
|
-
hasDefault: false;
|
|
34
|
-
notNull: true;
|
|
35
|
-
}>;
|
|
36
|
-
emailVerified: import("drizzle-orm/pg-core").PgTimestamp<{
|
|
37
|
-
tableName: "users";
|
|
38
|
-
name: "emailVerified";
|
|
39
|
-
data: Date;
|
|
40
|
-
driverParam: string;
|
|
41
|
-
notNull: false;
|
|
42
|
-
hasDefault: false;
|
|
43
|
-
}>;
|
|
44
|
-
image: import("drizzle-orm/pg-core").PgText<{
|
|
45
|
-
tableName: "users";
|
|
46
|
-
name: "image";
|
|
47
|
-
data: string;
|
|
48
|
-
enumValues: [string, ...string[]];
|
|
49
|
-
driverParam: string;
|
|
50
|
-
notNull: false;
|
|
51
|
-
hasDefault: false;
|
|
52
|
-
}>;
|
|
53
|
-
};
|
|
54
|
-
}>;
|
|
55
|
-
accounts: import("drizzle-orm/db.d-b9835153").az<{
|
|
56
|
-
name: "accounts";
|
|
57
|
-
schema: undefined;
|
|
58
|
-
columns: {
|
|
59
|
-
userId: import("drizzle-orm/pg-core").PgText<{
|
|
60
|
-
tableName: "accounts";
|
|
61
|
-
enumValues: [string, ...string[]];
|
|
62
|
-
name: "userId";
|
|
63
|
-
data: string;
|
|
64
|
-
driverParam: string;
|
|
65
|
-
hasDefault: false;
|
|
66
|
-
notNull: true;
|
|
67
|
-
}>;
|
|
68
|
-
type: import("drizzle-orm/pg-core").PgText<{
|
|
69
|
-
tableName: "accounts";
|
|
70
|
-
enumValues: [string, ...string[]];
|
|
71
|
-
name: "type";
|
|
72
|
-
data: "email" | "oidc" | "oauth";
|
|
73
|
-
driverParam: string;
|
|
74
|
-
hasDefault: false;
|
|
75
|
-
notNull: true;
|
|
76
|
-
}>;
|
|
77
|
-
provider: import("drizzle-orm/pg-core").PgText<{
|
|
78
|
-
tableName: "accounts";
|
|
79
|
-
enumValues: [string, ...string[]];
|
|
80
|
-
name: "provider";
|
|
81
|
-
data: string;
|
|
82
|
-
driverParam: string;
|
|
83
|
-
hasDefault: false;
|
|
84
|
-
notNull: true;
|
|
85
|
-
}>;
|
|
86
|
-
providerAccountId: import("drizzle-orm/pg-core").PgText<{
|
|
87
|
-
tableName: "accounts";
|
|
88
|
-
enumValues: [string, ...string[]];
|
|
89
|
-
name: "providerAccountId";
|
|
90
|
-
data: string;
|
|
91
|
-
driverParam: string;
|
|
92
|
-
hasDefault: false;
|
|
93
|
-
notNull: true;
|
|
94
|
-
}>;
|
|
95
|
-
refresh_token: import("drizzle-orm/pg-core").PgText<{
|
|
96
|
-
tableName: "accounts";
|
|
97
|
-
name: "refresh_token";
|
|
98
|
-
data: string;
|
|
99
|
-
enumValues: [string, ...string[]];
|
|
100
|
-
driverParam: string;
|
|
101
|
-
notNull: false;
|
|
102
|
-
hasDefault: false;
|
|
103
|
-
}>;
|
|
104
|
-
access_token: import("drizzle-orm/pg-core").PgText<{
|
|
105
|
-
tableName: "accounts";
|
|
106
|
-
name: "access_token";
|
|
107
|
-
data: string;
|
|
108
|
-
enumValues: [string, ...string[]];
|
|
109
|
-
driverParam: string;
|
|
110
|
-
notNull: false;
|
|
111
|
-
hasDefault: false;
|
|
112
|
-
}>;
|
|
113
|
-
expires_at: import("drizzle-orm/pg-core").PgInteger<{
|
|
114
|
-
tableName: "accounts";
|
|
115
|
-
name: "expires_at";
|
|
116
|
-
data: number;
|
|
117
|
-
driverParam: string | number;
|
|
118
|
-
hasDefault: false;
|
|
119
|
-
notNull: false;
|
|
120
|
-
}>;
|
|
121
|
-
token_type: import("drizzle-orm/pg-core").PgText<{
|
|
122
|
-
tableName: "accounts";
|
|
123
|
-
name: "token_type";
|
|
124
|
-
data: string;
|
|
125
|
-
enumValues: [string, ...string[]];
|
|
126
|
-
driverParam: string;
|
|
127
|
-
notNull: false;
|
|
128
|
-
hasDefault: false;
|
|
129
|
-
}>;
|
|
130
|
-
scope: import("drizzle-orm/pg-core").PgText<{
|
|
131
|
-
tableName: "accounts";
|
|
132
|
-
name: "scope";
|
|
133
|
-
data: string;
|
|
134
|
-
enumValues: [string, ...string[]];
|
|
135
|
-
driverParam: string;
|
|
136
|
-
notNull: false;
|
|
137
|
-
hasDefault: false;
|
|
138
|
-
}>;
|
|
139
|
-
id_token: import("drizzle-orm/pg-core").PgText<{
|
|
140
|
-
tableName: "accounts";
|
|
141
|
-
name: "id_token";
|
|
142
|
-
data: string;
|
|
143
|
-
enumValues: [string, ...string[]];
|
|
144
|
-
driverParam: string;
|
|
145
|
-
notNull: false;
|
|
146
|
-
hasDefault: false;
|
|
147
|
-
}>;
|
|
148
|
-
session_state: import("drizzle-orm/pg-core").PgText<{
|
|
149
|
-
tableName: "accounts";
|
|
150
|
-
name: "session_state";
|
|
151
|
-
data: string;
|
|
152
|
-
enumValues: [string, ...string[]];
|
|
153
|
-
driverParam: string;
|
|
154
|
-
notNull: false;
|
|
155
|
-
hasDefault: false;
|
|
156
|
-
}>;
|
|
157
|
-
};
|
|
158
|
-
}>;
|
|
159
|
-
sessions: import("drizzle-orm/db.d-b9835153").az<{
|
|
160
|
-
name: "sessions";
|
|
161
|
-
schema: undefined;
|
|
162
|
-
columns: {
|
|
163
|
-
sessionToken: import("drizzle-orm/pg-core").PgText<{
|
|
164
|
-
tableName: "sessions";
|
|
165
|
-
enumValues: [string, ...string[]];
|
|
166
|
-
name: "sessionToken";
|
|
167
|
-
data: string;
|
|
168
|
-
driverParam: string;
|
|
169
|
-
hasDefault: false;
|
|
170
|
-
notNull: true;
|
|
171
|
-
}>;
|
|
172
|
-
userId: import("drizzle-orm/pg-core").PgText<{
|
|
173
|
-
tableName: "sessions";
|
|
174
|
-
enumValues: [string, ...string[]];
|
|
175
|
-
name: "userId";
|
|
176
|
-
data: string;
|
|
177
|
-
driverParam: string;
|
|
178
|
-
hasDefault: false;
|
|
179
|
-
notNull: true;
|
|
180
|
-
}>;
|
|
181
|
-
expires: import("drizzle-orm/pg-core").PgTimestamp<{
|
|
182
|
-
tableName: "sessions";
|
|
183
|
-
name: "expires";
|
|
184
|
-
data: Date;
|
|
185
|
-
driverParam: string;
|
|
186
|
-
hasDefault: false;
|
|
187
|
-
notNull: true;
|
|
188
|
-
}>;
|
|
189
|
-
};
|
|
190
|
-
}>;
|
|
191
|
-
verificationTokens: import("drizzle-orm/db.d-b9835153").az<{
|
|
192
|
-
name: "verificationToken";
|
|
193
|
-
schema: undefined;
|
|
194
|
-
columns: {
|
|
195
|
-
identifier: import("drizzle-orm/pg-core").PgText<{
|
|
196
|
-
tableName: "verificationToken";
|
|
197
|
-
enumValues: [string, ...string[]];
|
|
198
|
-
name: "identifier";
|
|
199
|
-
data: string;
|
|
200
|
-
driverParam: string;
|
|
201
|
-
hasDefault: false;
|
|
202
|
-
notNull: true;
|
|
203
|
-
}>;
|
|
204
|
-
token: import("drizzle-orm/pg-core").PgText<{
|
|
205
|
-
tableName: "verificationToken";
|
|
206
|
-
enumValues: [string, ...string[]];
|
|
207
|
-
name: "token";
|
|
208
|
-
data: string;
|
|
209
|
-
driverParam: string;
|
|
210
|
-
hasDefault: false;
|
|
211
|
-
notNull: true;
|
|
212
|
-
}>;
|
|
213
|
-
expires: import("drizzle-orm/pg-core").PgTimestamp<{
|
|
214
|
-
tableName: "verificationToken";
|
|
215
|
-
name: "expires";
|
|
216
|
-
data: Date;
|
|
217
|
-
driverParam: string;
|
|
218
|
-
hasDefault: false;
|
|
219
|
-
notNull: true;
|
|
220
|
-
}>;
|
|
221
|
-
};
|
|
222
|
-
}>;
|
|
223
|
-
};
|
|
224
|
-
export type DefaultSchema = ReturnType<typeof createTables>;
|
|
225
|
-
export declare function pgDrizzleAdapter(client: PostgresJsDatabase<Record<string, never>>, tableFn?: PgTableFn<undefined>): Adapter;
|
|
226
|
-
//# sourceMappingURL=pg.d.ts.map
|
package/lib/pg.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"pg.d.ts","sourceRoot":"","sources":["../src/lib/pg.ts"],"names":[],"mappings":"AACA,OAAO,EAML,SAAS,EACV,MAAM,qBAAqB,CAAA;AAE5B,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAA;AACjE,OAAO,KAAK,EAAE,OAAO,EAAkB,MAAM,qBAAqB,CAAA;AAElE,wBAAgB,YAAY,CAAC,OAAO,EAAE,SAAS;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAoD9C;AAED,MAAM,MAAM,aAAa,GAAG,UAAU,CAAC,OAAO,YAAY,CAAC,CAAA;AAE3D,wBAAgB,gBAAgB,CAC9B,MAAM,EAAE,kBAAkB,CAAC,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC,EACjD,OAAO,uBAAmB,GACzB,OAAO,CAgKT"}
|
package/lib/pg.js
DELETED
|
@@ -1,180 +0,0 @@
|
|
|
1
|
-
import { and, eq } from "drizzle-orm";
|
|
2
|
-
import { timestamp, pgTable as defaultPgTableFn, text, primaryKey, integer, } from "drizzle-orm/pg-core";
|
|
3
|
-
export function createTables(pgTable) {
|
|
4
|
-
const users = pgTable("users", {
|
|
5
|
-
id: text("id").notNull().primaryKey(),
|
|
6
|
-
name: text("name"),
|
|
7
|
-
email: text("email").notNull(),
|
|
8
|
-
emailVerified: timestamp("emailVerified", { mode: "date" }),
|
|
9
|
-
image: text("image"),
|
|
10
|
-
});
|
|
11
|
-
const accounts = pgTable("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 = pgTable("sessions", {
|
|
29
|
-
sessionToken: text("sessionToken").notNull().primaryKey(),
|
|
30
|
-
userId: text("userId")
|
|
31
|
-
.notNull()
|
|
32
|
-
.references(() => users.id, { onDelete: "cascade" }),
|
|
33
|
-
expires: timestamp("expires", { mode: "date" }).notNull(),
|
|
34
|
-
});
|
|
35
|
-
const verificationTokens = pgTable("verificationToken", {
|
|
36
|
-
identifier: text("identifier").notNull(),
|
|
37
|
-
token: text("token").notNull(),
|
|
38
|
-
expires: timestamp("expires", { mode: "date" }).notNull(),
|
|
39
|
-
}, (vt) => ({
|
|
40
|
-
compoundKey: primaryKey(vt.identifier, vt.token),
|
|
41
|
-
}));
|
|
42
|
-
return { users, accounts, sessions, verificationTokens };
|
|
43
|
-
}
|
|
44
|
-
export function pgDrizzleAdapter(client, tableFn = defaultPgTableFn) {
|
|
45
|
-
const { users, accounts, sessions, verificationTokens } = createTables(tableFn);
|
|
46
|
-
return {
|
|
47
|
-
async createUser(data) {
|
|
48
|
-
return await client
|
|
49
|
-
.insert(users)
|
|
50
|
-
.values({ ...data, id: crypto.randomUUID() })
|
|
51
|
-
.returning()
|
|
52
|
-
.then((res) => res[0] ?? null);
|
|
53
|
-
},
|
|
54
|
-
async getUser(data) {
|
|
55
|
-
return await client
|
|
56
|
-
.select()
|
|
57
|
-
.from(users)
|
|
58
|
-
.where(eq(users.id, data))
|
|
59
|
-
.then((res) => res[0] ?? null);
|
|
60
|
-
},
|
|
61
|
-
async getUserByEmail(data) {
|
|
62
|
-
return await client
|
|
63
|
-
.select()
|
|
64
|
-
.from(users)
|
|
65
|
-
.where(eq(users.email, data))
|
|
66
|
-
.then((res) => res[0] ?? null);
|
|
67
|
-
},
|
|
68
|
-
async createSession(data) {
|
|
69
|
-
return await client
|
|
70
|
-
.insert(sessions)
|
|
71
|
-
.values(data)
|
|
72
|
-
.returning()
|
|
73
|
-
.then((res) => res[0]);
|
|
74
|
-
},
|
|
75
|
-
async getSessionAndUser(data) {
|
|
76
|
-
return await client
|
|
77
|
-
.select({
|
|
78
|
-
session: sessions,
|
|
79
|
-
user: users,
|
|
80
|
-
})
|
|
81
|
-
.from(sessions)
|
|
82
|
-
.where(eq(sessions.sessionToken, data))
|
|
83
|
-
.innerJoin(users, eq(users.id, sessions.userId))
|
|
84
|
-
.then((res) => res[0] ?? null);
|
|
85
|
-
},
|
|
86
|
-
async updateUser(data) {
|
|
87
|
-
if (!data.id) {
|
|
88
|
-
throw new Error("No user id.");
|
|
89
|
-
}
|
|
90
|
-
return await client
|
|
91
|
-
.update(users)
|
|
92
|
-
.set(data)
|
|
93
|
-
.where(eq(users.id, data.id))
|
|
94
|
-
.returning()
|
|
95
|
-
.then((res) => res[0]);
|
|
96
|
-
},
|
|
97
|
-
async updateSession(data) {
|
|
98
|
-
return await client
|
|
99
|
-
.update(sessions)
|
|
100
|
-
.set(data)
|
|
101
|
-
.where(eq(sessions.sessionToken, data.sessionToken))
|
|
102
|
-
.returning()
|
|
103
|
-
.then((res) => res[0]);
|
|
104
|
-
},
|
|
105
|
-
async linkAccount(rawAccount) {
|
|
106
|
-
const updatedAccount = await client
|
|
107
|
-
.insert(accounts)
|
|
108
|
-
.values(rawAccount)
|
|
109
|
-
.returning()
|
|
110
|
-
.then((res) => res[0]);
|
|
111
|
-
// Drizzle will return `null` for fields that are not defined.
|
|
112
|
-
// However, the return type is expecting `undefined`.
|
|
113
|
-
const account = {
|
|
114
|
-
...updatedAccount,
|
|
115
|
-
access_token: updatedAccount.access_token ?? undefined,
|
|
116
|
-
token_type: updatedAccount.token_type ?? undefined,
|
|
117
|
-
id_token: updatedAccount.id_token ?? undefined,
|
|
118
|
-
refresh_token: updatedAccount.refresh_token ?? undefined,
|
|
119
|
-
scope: updatedAccount.scope ?? undefined,
|
|
120
|
-
expires_at: updatedAccount.expires_at ?? undefined,
|
|
121
|
-
session_state: updatedAccount.session_state ?? undefined,
|
|
122
|
-
};
|
|
123
|
-
return account;
|
|
124
|
-
},
|
|
125
|
-
async getUserByAccount(account) {
|
|
126
|
-
const dbAccount = (await client
|
|
127
|
-
.select()
|
|
128
|
-
.from(accounts)
|
|
129
|
-
.where(and(eq(accounts.providerAccountId, account.providerAccountId), eq(accounts.provider, account.provider)))
|
|
130
|
-
.leftJoin(users, eq(accounts.userId, users.id))
|
|
131
|
-
.then((res) => res[0])) ?? null;
|
|
132
|
-
if (!dbAccount) {
|
|
133
|
-
return null;
|
|
134
|
-
}
|
|
135
|
-
return dbAccount.users;
|
|
136
|
-
},
|
|
137
|
-
async deleteSession(sessionToken) {
|
|
138
|
-
const session = await client
|
|
139
|
-
.delete(sessions)
|
|
140
|
-
.where(eq(sessions.sessionToken, sessionToken))
|
|
141
|
-
.returning()
|
|
142
|
-
.then((res) => res[0] ?? null);
|
|
143
|
-
return session;
|
|
144
|
-
},
|
|
145
|
-
async createVerificationToken(token) {
|
|
146
|
-
return await client
|
|
147
|
-
.insert(verificationTokens)
|
|
148
|
-
.values(token)
|
|
149
|
-
.returning()
|
|
150
|
-
.then((res) => res[0]);
|
|
151
|
-
},
|
|
152
|
-
async useVerificationToken(token) {
|
|
153
|
-
try {
|
|
154
|
-
return await client
|
|
155
|
-
.delete(verificationTokens)
|
|
156
|
-
.where(and(eq(verificationTokens.identifier, token.identifier), eq(verificationTokens.token, token.token)))
|
|
157
|
-
.returning()
|
|
158
|
-
.then((res) => res[0] ?? null);
|
|
159
|
-
}
|
|
160
|
-
catch (err) {
|
|
161
|
-
throw new Error("No verification token found.");
|
|
162
|
-
}
|
|
163
|
-
},
|
|
164
|
-
async deleteUser(id) {
|
|
165
|
-
await client
|
|
166
|
-
.delete(users)
|
|
167
|
-
.where(eq(users.id, id))
|
|
168
|
-
.returning()
|
|
169
|
-
.then((res) => res[0] ?? null);
|
|
170
|
-
},
|
|
171
|
-
async unlinkAccount(account) {
|
|
172
|
-
const { type, provider, providerAccountId, userId } = await client
|
|
173
|
-
.delete(accounts)
|
|
174
|
-
.where(and(eq(accounts.providerAccountId, account.providerAccountId), eq(accounts.provider, account.provider)))
|
|
175
|
-
.returning()
|
|
176
|
-
.then((res) => res[0] ?? null);
|
|
177
|
-
return { provider, type, providerAccountId, userId };
|
|
178
|
-
},
|
|
179
|
-
};
|
|
180
|
-
}
|