@auth/drizzle-adapter 0.9.0 → 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/README.md +1 -1
- package/index.d.ts +59 -32
- package/index.d.ts.map +1 -1
- package/index.js +64 -37
- package/lib/mysql.d.ts +317 -296
- package/lib/mysql.d.ts.map +1 -1
- package/lib/mysql.js +134 -142
- package/lib/pg.d.ts +317 -296
- package/lib/pg.d.ts.map +1 -1
- package/lib/pg.js +128 -123
- package/lib/sqlite.d.ts +317 -296
- package/lib/sqlite.d.ts.map +1 -1
- package/lib/sqlite.js +109 -108
- package/lib/utils.d.ts +10 -21
- package/lib/utils.d.ts.map +1 -1
- package/lib/utils.js +1 -7
- package/package.json +7 -7
- package/src/index.ts +83 -41
- package/src/lib/mysql.ts +209 -190
- package/src/lib/pg.ts +194 -155
- package/src/lib/sqlite.ts +175 -133
- package/src/lib/utils.ts +22 -41
package/lib/pg.js
CHANGED
|
@@ -1,165 +1,170 @@
|
|
|
1
1
|
import { and, eq } from "drizzle-orm";
|
|
2
|
-
import { timestamp,
|
|
3
|
-
import {
|
|
4
|
-
export
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
|
|
2
|
+
import { timestamp, text, primaryKey, integer, pgTable, index, } from "drizzle-orm/pg-core";
|
|
3
|
+
import { randomUUID } from "crypto";
|
|
4
|
+
export const postgresUsersTable = pgTable("user", {
|
|
5
|
+
id: text("id")
|
|
6
|
+
.primaryKey()
|
|
7
|
+
.$defaultFn(() => randomUUID()),
|
|
8
|
+
name: text("name"),
|
|
9
|
+
email: text("email").notNull().unique(),
|
|
10
|
+
emailVerified: timestamp("emailVerified", { mode: "date" }),
|
|
11
|
+
image: text("image"),
|
|
12
|
+
});
|
|
13
|
+
export const postgresAccountsTable = pgTable("account", {
|
|
14
|
+
userId: text("userId")
|
|
15
|
+
.notNull()
|
|
16
|
+
.references(() => postgresUsersTable.id, { onDelete: "cascade" }),
|
|
17
|
+
type: text("type").notNull(),
|
|
18
|
+
provider: text("provider").notNull(),
|
|
19
|
+
providerAccountId: text("providerAccountId").notNull(),
|
|
20
|
+
refresh_token: text("refresh_token"),
|
|
21
|
+
access_token: text("access_token"),
|
|
22
|
+
expires_at: integer("expires_at"),
|
|
23
|
+
token_type: text("token_type"),
|
|
24
|
+
scope: text("scope"),
|
|
25
|
+
id_token: text("id_token"),
|
|
26
|
+
session_state: text("session_state"),
|
|
27
|
+
}, (table) => {
|
|
28
|
+
return {
|
|
29
|
+
userIdIdx: index().on(table.userId),
|
|
30
|
+
compositePk: primaryKey({
|
|
31
|
+
columns: [table.provider, table.providerAccountId],
|
|
32
|
+
}),
|
|
33
|
+
};
|
|
34
|
+
});
|
|
35
|
+
export const postgresSessionsTable = pgTable("session", {
|
|
36
|
+
id: text("id")
|
|
37
|
+
.primaryKey()
|
|
38
|
+
.$defaultFn(() => randomUUID()),
|
|
39
|
+
sessionToken: text("sessionToken").notNull().unique(),
|
|
40
|
+
userId: text("userId")
|
|
41
|
+
.notNull()
|
|
42
|
+
.references(() => postgresUsersTable.id, { onDelete: "cascade" }),
|
|
43
|
+
expires: timestamp("expires", { mode: "date" }).notNull(),
|
|
44
|
+
}, (table) => {
|
|
45
|
+
return {
|
|
46
|
+
userIdIdx: index().on(table.userId),
|
|
47
|
+
};
|
|
48
|
+
});
|
|
49
|
+
export const postgresVerificationTokensTable = pgTable("verificationToken", {
|
|
50
|
+
identifier: text("identifier").notNull(),
|
|
51
|
+
token: text("token").notNull().unique(),
|
|
52
|
+
expires: timestamp("expires", { mode: "date" }).notNull(),
|
|
53
|
+
}, (table) => {
|
|
54
|
+
return {
|
|
55
|
+
compositePk: primaryKey({ columns: [table.identifier, table.token] }),
|
|
56
|
+
};
|
|
57
|
+
});
|
|
58
|
+
export function PostgresDrizzleAdapter(client, schema = {
|
|
59
|
+
usersTable: postgresUsersTable,
|
|
60
|
+
accountsTable: postgresAccountsTable,
|
|
61
|
+
sessionsTable: postgresSessionsTable,
|
|
62
|
+
verificationTokensTable: postgresVerificationTokensTable,
|
|
63
|
+
}) {
|
|
64
|
+
const { usersTable, accountsTable, sessionsTable, verificationTokensTable } = schema;
|
|
47
65
|
return {
|
|
48
66
|
async createUser(data) {
|
|
49
|
-
return
|
|
50
|
-
.insert(
|
|
51
|
-
.values(
|
|
67
|
+
return client
|
|
68
|
+
.insert(usersTable)
|
|
69
|
+
.values(data)
|
|
52
70
|
.returning()
|
|
53
|
-
.then((res) => res[0]
|
|
71
|
+
.then((res) => res[0]);
|
|
54
72
|
},
|
|
55
|
-
async getUser(
|
|
56
|
-
return
|
|
73
|
+
async getUser(userId) {
|
|
74
|
+
return client
|
|
57
75
|
.select()
|
|
58
|
-
.from(
|
|
59
|
-
.where(eq(
|
|
60
|
-
.then((res) => res[0]
|
|
76
|
+
.from(usersTable)
|
|
77
|
+
.where(eq(usersTable.id, userId))
|
|
78
|
+
.then((res) => (res.length > 0 ? res[0] : null));
|
|
61
79
|
},
|
|
62
|
-
async getUserByEmail(
|
|
63
|
-
return
|
|
80
|
+
async getUserByEmail(email) {
|
|
81
|
+
return client
|
|
64
82
|
.select()
|
|
65
|
-
.from(
|
|
66
|
-
.where(eq(
|
|
67
|
-
.then((res) => res[0]
|
|
83
|
+
.from(usersTable)
|
|
84
|
+
.where(eq(usersTable.email, email))
|
|
85
|
+
.then((res) => (res.length > 0 ? res[0] : null));
|
|
68
86
|
},
|
|
69
87
|
async createSession(data) {
|
|
70
|
-
return
|
|
71
|
-
.insert(
|
|
88
|
+
return client
|
|
89
|
+
.insert(sessionsTable)
|
|
72
90
|
.values(data)
|
|
73
91
|
.returning()
|
|
74
92
|
.then((res) => res[0]);
|
|
75
93
|
},
|
|
76
|
-
async getSessionAndUser(
|
|
77
|
-
return
|
|
94
|
+
async getSessionAndUser(sessionToken) {
|
|
95
|
+
return client
|
|
78
96
|
.select({
|
|
79
|
-
session:
|
|
80
|
-
user:
|
|
97
|
+
session: sessionsTable,
|
|
98
|
+
user: usersTable,
|
|
81
99
|
})
|
|
82
|
-
.from(
|
|
83
|
-
.where(eq(
|
|
84
|
-
.innerJoin(
|
|
85
|
-
.then((res) => res[0]
|
|
100
|
+
.from(sessionsTable)
|
|
101
|
+
.where(eq(sessionsTable.sessionToken, sessionToken))
|
|
102
|
+
.innerJoin(usersTable, eq(usersTable.id, sessionsTable.userId))
|
|
103
|
+
.then((res) => (res.length > 0 ? res[0] : null));
|
|
86
104
|
},
|
|
87
105
|
async updateUser(data) {
|
|
88
106
|
if (!data.id) {
|
|
89
107
|
throw new Error("No user id.");
|
|
90
108
|
}
|
|
91
|
-
|
|
92
|
-
.update(
|
|
109
|
+
const [result] = await client
|
|
110
|
+
.update(usersTable)
|
|
93
111
|
.set(data)
|
|
94
|
-
.where(eq(
|
|
95
|
-
.returning()
|
|
96
|
-
|
|
112
|
+
.where(eq(usersTable.id, data.id))
|
|
113
|
+
.returning();
|
|
114
|
+
if (!result) {
|
|
115
|
+
throw new Error("No user found.");
|
|
116
|
+
}
|
|
117
|
+
return result;
|
|
97
118
|
},
|
|
98
119
|
async updateSession(data) {
|
|
99
|
-
return
|
|
100
|
-
.update(
|
|
120
|
+
return client
|
|
121
|
+
.update(sessionsTable)
|
|
101
122
|
.set(data)
|
|
102
|
-
.where(eq(
|
|
123
|
+
.where(eq(sessionsTable.sessionToken, data.sessionToken))
|
|
103
124
|
.returning()
|
|
104
125
|
.then((res) => res[0]);
|
|
105
126
|
},
|
|
106
|
-
async linkAccount(
|
|
107
|
-
|
|
108
|
-
.insert(accounts)
|
|
109
|
-
.values(rawAccount)
|
|
110
|
-
.returning()
|
|
111
|
-
.then((res) => res[0]));
|
|
127
|
+
async linkAccount(data) {
|
|
128
|
+
await client.insert(accountsTable).values(data);
|
|
112
129
|
},
|
|
113
130
|
async getUserByAccount(account) {
|
|
114
|
-
const
|
|
115
|
-
.select(
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
.
|
|
120
|
-
|
|
131
|
+
const result = await client
|
|
132
|
+
.select({
|
|
133
|
+
account: accountsTable,
|
|
134
|
+
user: usersTable,
|
|
135
|
+
})
|
|
136
|
+
.from(accountsTable)
|
|
137
|
+
.innerJoin(usersTable, eq(accountsTable.userId, usersTable.id))
|
|
138
|
+
.where(and(eq(accountsTable.provider, account.provider), eq(accountsTable.providerAccountId, account.providerAccountId)))
|
|
139
|
+
.then((res) => res[0]);
|
|
140
|
+
return result?.user ?? null;
|
|
121
141
|
},
|
|
122
142
|
async deleteSession(sessionToken) {
|
|
123
|
-
|
|
124
|
-
.delete(
|
|
125
|
-
.where(eq(
|
|
126
|
-
.returning()
|
|
127
|
-
.then((res) => res[0] ?? null);
|
|
128
|
-
return session;
|
|
143
|
+
await client
|
|
144
|
+
.delete(sessionsTable)
|
|
145
|
+
.where(eq(sessionsTable.sessionToken, sessionToken));
|
|
129
146
|
},
|
|
130
|
-
async createVerificationToken(
|
|
131
|
-
return
|
|
132
|
-
.insert(
|
|
133
|
-
.values(
|
|
147
|
+
async createVerificationToken(data) {
|
|
148
|
+
return client
|
|
149
|
+
.insert(verificationTokensTable)
|
|
150
|
+
.values(data)
|
|
134
151
|
.returning()
|
|
135
152
|
.then((res) => res[0]);
|
|
136
153
|
},
|
|
137
|
-
async useVerificationToken(
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
.then((res) => res[0] ?? null);
|
|
144
|
-
}
|
|
145
|
-
catch (err) {
|
|
146
|
-
throw new Error("No verification token found.");
|
|
147
|
-
}
|
|
154
|
+
async useVerificationToken(params) {
|
|
155
|
+
return client
|
|
156
|
+
.delete(verificationTokensTable)
|
|
157
|
+
.where(and(eq(verificationTokensTable.identifier, params.identifier), eq(verificationTokensTable.token, params.token)))
|
|
158
|
+
.returning()
|
|
159
|
+
.then((res) => (res.length > 0 ? res[0] : null));
|
|
148
160
|
},
|
|
149
161
|
async deleteUser(id) {
|
|
150
|
-
await client
|
|
151
|
-
.delete(users)
|
|
152
|
-
.where(eq(users.id, id))
|
|
153
|
-
.returning()
|
|
154
|
-
.then((res) => res[0] ?? null);
|
|
162
|
+
await client.delete(usersTable).where(eq(usersTable.id, id));
|
|
155
163
|
},
|
|
156
|
-
async unlinkAccount(
|
|
157
|
-
|
|
158
|
-
.delete(
|
|
159
|
-
.where(and(eq(
|
|
160
|
-
.returning()
|
|
161
|
-
.then((res) => res[0] ?? null);
|
|
162
|
-
return { provider, type, providerAccountId, userId };
|
|
164
|
+
async unlinkAccount(params) {
|
|
165
|
+
await client
|
|
166
|
+
.delete(accountsTable)
|
|
167
|
+
.where(and(eq(accountsTable.provider, params.provider), eq(accountsTable.providerAccountId, params.providerAccountId)));
|
|
163
168
|
},
|
|
164
169
|
};
|
|
165
170
|
}
|