@coursebuilder/adapter-drizzle 0.0.1
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/LICENSE +21 -0
- package/index.d.ts +6 -0
- package/index.d.ts.map +1 -0
- package/index.js +20 -0
- package/lib/mysql.d.ts +505 -0
- package/lib/mysql.d.ts.map +1 -0
- package/lib/mysql.js +263 -0
- package/lib/pg.d.ts +299 -0
- package/lib/pg.d.ts.map +1 -0
- package/lib/pg.js +164 -0
- package/lib/sqlite.d.ts +299 -0
- package/lib/sqlite.d.ts.map +1 -0
- package/lib/sqlite.js +138 -0
- package/lib/utils.d.ts +26 -0
- package/lib/utils.d.ts.map +1 -0
- package/lib/utils.js +7 -0
- package/package.json +53 -0
- package/schema.d.ts +1023 -0
- package/schema.d.ts.map +1 -0
- package/schema.js +318 -0
- package/src/index.ts +28 -0
- package/src/lib/mysql.ts +344 -0
- package/src/lib/pg.ts +198 -0
- package/src/lib/sqlite.ts +168 -0
- package/src/lib/utils.ts +48 -0
package/lib/pg.js
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { and, eq } from 'drizzle-orm';
|
|
2
|
+
import { pgTable as defaultPgTableFn, integer, primaryKey, text, timestamp, } from 'drizzle-orm/pg-core';
|
|
3
|
+
export function createTables(pgTable) {
|
|
4
|
+
const users = pgTable('user', {
|
|
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('account', {
|
|
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('session', {
|
|
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
|
+
await client
|
|
107
|
+
.insert(accounts)
|
|
108
|
+
.values(rawAccount)
|
|
109
|
+
.returning()
|
|
110
|
+
.then((res) => res[0]);
|
|
111
|
+
},
|
|
112
|
+
async getUserByAccount(account) {
|
|
113
|
+
const dbAccount = (await client
|
|
114
|
+
.select()
|
|
115
|
+
.from(accounts)
|
|
116
|
+
.where(and(eq(accounts.providerAccountId, account.providerAccountId), eq(accounts.provider, account.provider)))
|
|
117
|
+
.leftJoin(users, eq(accounts.userId, users.id))
|
|
118
|
+
.then((res) => res[0])) ?? null;
|
|
119
|
+
return dbAccount?.user ?? null;
|
|
120
|
+
},
|
|
121
|
+
async deleteSession(sessionToken) {
|
|
122
|
+
const session = await client
|
|
123
|
+
.delete(sessions)
|
|
124
|
+
.where(eq(sessions.sessionToken, sessionToken))
|
|
125
|
+
.returning()
|
|
126
|
+
.then((res) => res[0] ?? null);
|
|
127
|
+
return session;
|
|
128
|
+
},
|
|
129
|
+
async createVerificationToken(token) {
|
|
130
|
+
return await client
|
|
131
|
+
.insert(verificationTokens)
|
|
132
|
+
.values(token)
|
|
133
|
+
.returning()
|
|
134
|
+
.then((res) => res[0]);
|
|
135
|
+
},
|
|
136
|
+
async useVerificationToken(token) {
|
|
137
|
+
try {
|
|
138
|
+
return await client
|
|
139
|
+
.delete(verificationTokens)
|
|
140
|
+
.where(and(eq(verificationTokens.identifier, token.identifier), eq(verificationTokens.token, token.token)))
|
|
141
|
+
.returning()
|
|
142
|
+
.then((res) => res[0] ?? null);
|
|
143
|
+
}
|
|
144
|
+
catch (err) {
|
|
145
|
+
throw new Error('No verification token found.');
|
|
146
|
+
}
|
|
147
|
+
},
|
|
148
|
+
async deleteUser(id) {
|
|
149
|
+
await client
|
|
150
|
+
.delete(users)
|
|
151
|
+
.where(eq(users.id, id))
|
|
152
|
+
.returning()
|
|
153
|
+
.then((res) => res[0] ?? null);
|
|
154
|
+
},
|
|
155
|
+
async unlinkAccount(account) {
|
|
156
|
+
const { type, provider, providerAccountId, userId } = await client
|
|
157
|
+
.delete(accounts)
|
|
158
|
+
.where(and(eq(accounts.providerAccountId, account.providerAccountId), eq(accounts.provider, account.provider)))
|
|
159
|
+
.returning()
|
|
160
|
+
.then((res) => res[0] ?? null);
|
|
161
|
+
return { provider, type, providerAccountId, userId };
|
|
162
|
+
},
|
|
163
|
+
};
|
|
164
|
+
}
|
package/lib/sqlite.d.ts
ADDED
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
import type { Adapter } from '@auth/core/adapters';
|
|
2
|
+
import { BaseSQLiteDatabase, SQLiteTableFn } from 'drizzle-orm/sqlite-core';
|
|
3
|
+
export declare function createTables(sqliteTable: SQLiteTableFn): {
|
|
4
|
+
users: import("drizzle-orm/sqlite-core/table.js").SQLiteTableWithColumns<{
|
|
5
|
+
name: "user";
|
|
6
|
+
schema: undefined;
|
|
7
|
+
columns: {
|
|
8
|
+
id: import("drizzle-orm/sqlite-core/index.js").SQLiteColumn<{
|
|
9
|
+
name: "id";
|
|
10
|
+
tableName: "user";
|
|
11
|
+
dataType: "string";
|
|
12
|
+
columnType: "SQLiteText";
|
|
13
|
+
data: string;
|
|
14
|
+
driverParam: string;
|
|
15
|
+
notNull: true;
|
|
16
|
+
hasDefault: false;
|
|
17
|
+
enumValues: [string, ...string[]];
|
|
18
|
+
baseColumn: never;
|
|
19
|
+
}, object>;
|
|
20
|
+
name: import("drizzle-orm/sqlite-core/index.js").SQLiteColumn<{
|
|
21
|
+
name: "name";
|
|
22
|
+
tableName: "user";
|
|
23
|
+
dataType: "string";
|
|
24
|
+
columnType: "SQLiteText";
|
|
25
|
+
data: string;
|
|
26
|
+
driverParam: string;
|
|
27
|
+
notNull: false;
|
|
28
|
+
hasDefault: false;
|
|
29
|
+
enumValues: [string, ...string[]];
|
|
30
|
+
baseColumn: never;
|
|
31
|
+
}, object>;
|
|
32
|
+
email: import("drizzle-orm/sqlite-core/index.js").SQLiteColumn<{
|
|
33
|
+
name: "email";
|
|
34
|
+
tableName: "user";
|
|
35
|
+
dataType: "string";
|
|
36
|
+
columnType: "SQLiteText";
|
|
37
|
+
data: string;
|
|
38
|
+
driverParam: string;
|
|
39
|
+
notNull: true;
|
|
40
|
+
hasDefault: false;
|
|
41
|
+
enumValues: [string, ...string[]];
|
|
42
|
+
baseColumn: never;
|
|
43
|
+
}, object>;
|
|
44
|
+
emailVerified: import("drizzle-orm/sqlite-core/index.js").SQLiteColumn<{
|
|
45
|
+
name: "emailVerified";
|
|
46
|
+
tableName: "user";
|
|
47
|
+
dataType: "date";
|
|
48
|
+
columnType: "SQLiteTimestamp";
|
|
49
|
+
data: Date;
|
|
50
|
+
driverParam: number;
|
|
51
|
+
notNull: false;
|
|
52
|
+
hasDefault: false;
|
|
53
|
+
enumValues: undefined;
|
|
54
|
+
baseColumn: never;
|
|
55
|
+
}, object>;
|
|
56
|
+
image: import("drizzle-orm/sqlite-core/index.js").SQLiteColumn<{
|
|
57
|
+
name: "image";
|
|
58
|
+
tableName: "user";
|
|
59
|
+
dataType: "string";
|
|
60
|
+
columnType: "SQLiteText";
|
|
61
|
+
data: string;
|
|
62
|
+
driverParam: string;
|
|
63
|
+
notNull: false;
|
|
64
|
+
hasDefault: false;
|
|
65
|
+
enumValues: [string, ...string[]];
|
|
66
|
+
baseColumn: never;
|
|
67
|
+
}, object>;
|
|
68
|
+
};
|
|
69
|
+
dialect: "sqlite";
|
|
70
|
+
}>;
|
|
71
|
+
accounts: import("drizzle-orm/sqlite-core/table.js").SQLiteTableWithColumns<{
|
|
72
|
+
name: "account";
|
|
73
|
+
schema: undefined;
|
|
74
|
+
columns: {
|
|
75
|
+
userId: import("drizzle-orm/sqlite-core/index.js").SQLiteColumn<{
|
|
76
|
+
name: "userId";
|
|
77
|
+
tableName: "account";
|
|
78
|
+
dataType: "string";
|
|
79
|
+
columnType: "SQLiteText";
|
|
80
|
+
data: string;
|
|
81
|
+
driverParam: string;
|
|
82
|
+
notNull: true;
|
|
83
|
+
hasDefault: false;
|
|
84
|
+
enumValues: [string, ...string[]];
|
|
85
|
+
baseColumn: never;
|
|
86
|
+
}, object>;
|
|
87
|
+
type: import("drizzle-orm/sqlite-core/index.js").SQLiteColumn<{
|
|
88
|
+
name: "type";
|
|
89
|
+
tableName: "account";
|
|
90
|
+
dataType: "string";
|
|
91
|
+
columnType: "SQLiteText";
|
|
92
|
+
data: "email" | "oidc" | "oauth" | "webauthn";
|
|
93
|
+
driverParam: string;
|
|
94
|
+
notNull: true;
|
|
95
|
+
hasDefault: false;
|
|
96
|
+
enumValues: [string, ...string[]];
|
|
97
|
+
baseColumn: never;
|
|
98
|
+
}, object>;
|
|
99
|
+
provider: import("drizzle-orm/sqlite-core/index.js").SQLiteColumn<{
|
|
100
|
+
name: "provider";
|
|
101
|
+
tableName: "account";
|
|
102
|
+
dataType: "string";
|
|
103
|
+
columnType: "SQLiteText";
|
|
104
|
+
data: string;
|
|
105
|
+
driverParam: string;
|
|
106
|
+
notNull: true;
|
|
107
|
+
hasDefault: false;
|
|
108
|
+
enumValues: [string, ...string[]];
|
|
109
|
+
baseColumn: never;
|
|
110
|
+
}, object>;
|
|
111
|
+
providerAccountId: import("drizzle-orm/sqlite-core/index.js").SQLiteColumn<{
|
|
112
|
+
name: "providerAccountId";
|
|
113
|
+
tableName: "account";
|
|
114
|
+
dataType: "string";
|
|
115
|
+
columnType: "SQLiteText";
|
|
116
|
+
data: string;
|
|
117
|
+
driverParam: string;
|
|
118
|
+
notNull: true;
|
|
119
|
+
hasDefault: false;
|
|
120
|
+
enumValues: [string, ...string[]];
|
|
121
|
+
baseColumn: never;
|
|
122
|
+
}, object>;
|
|
123
|
+
refresh_token: import("drizzle-orm/sqlite-core/index.js").SQLiteColumn<{
|
|
124
|
+
name: "refresh_token";
|
|
125
|
+
tableName: "account";
|
|
126
|
+
dataType: "string";
|
|
127
|
+
columnType: "SQLiteText";
|
|
128
|
+
data: string;
|
|
129
|
+
driverParam: string;
|
|
130
|
+
notNull: false;
|
|
131
|
+
hasDefault: false;
|
|
132
|
+
enumValues: [string, ...string[]];
|
|
133
|
+
baseColumn: never;
|
|
134
|
+
}, object>;
|
|
135
|
+
access_token: import("drizzle-orm/sqlite-core/index.js").SQLiteColumn<{
|
|
136
|
+
name: "access_token";
|
|
137
|
+
tableName: "account";
|
|
138
|
+
dataType: "string";
|
|
139
|
+
columnType: "SQLiteText";
|
|
140
|
+
data: string;
|
|
141
|
+
driverParam: string;
|
|
142
|
+
notNull: false;
|
|
143
|
+
hasDefault: false;
|
|
144
|
+
enumValues: [string, ...string[]];
|
|
145
|
+
baseColumn: never;
|
|
146
|
+
}, object>;
|
|
147
|
+
expires_at: import("drizzle-orm/sqlite-core/index.js").SQLiteColumn<{
|
|
148
|
+
name: "expires_at";
|
|
149
|
+
tableName: "account";
|
|
150
|
+
dataType: "number";
|
|
151
|
+
columnType: "SQLiteInteger";
|
|
152
|
+
data: number;
|
|
153
|
+
driverParam: number;
|
|
154
|
+
notNull: false;
|
|
155
|
+
hasDefault: false;
|
|
156
|
+
enumValues: undefined;
|
|
157
|
+
baseColumn: never;
|
|
158
|
+
}, object>;
|
|
159
|
+
token_type: import("drizzle-orm/sqlite-core/index.js").SQLiteColumn<{
|
|
160
|
+
name: "token_type";
|
|
161
|
+
tableName: "account";
|
|
162
|
+
dataType: "string";
|
|
163
|
+
columnType: "SQLiteText";
|
|
164
|
+
data: string;
|
|
165
|
+
driverParam: string;
|
|
166
|
+
notNull: false;
|
|
167
|
+
hasDefault: false;
|
|
168
|
+
enumValues: [string, ...string[]];
|
|
169
|
+
baseColumn: never;
|
|
170
|
+
}, object>;
|
|
171
|
+
scope: import("drizzle-orm/sqlite-core/index.js").SQLiteColumn<{
|
|
172
|
+
name: "scope";
|
|
173
|
+
tableName: "account";
|
|
174
|
+
dataType: "string";
|
|
175
|
+
columnType: "SQLiteText";
|
|
176
|
+
data: string;
|
|
177
|
+
driverParam: string;
|
|
178
|
+
notNull: false;
|
|
179
|
+
hasDefault: false;
|
|
180
|
+
enumValues: [string, ...string[]];
|
|
181
|
+
baseColumn: never;
|
|
182
|
+
}, object>;
|
|
183
|
+
id_token: import("drizzle-orm/sqlite-core/index.js").SQLiteColumn<{
|
|
184
|
+
name: "id_token";
|
|
185
|
+
tableName: "account";
|
|
186
|
+
dataType: "string";
|
|
187
|
+
columnType: "SQLiteText";
|
|
188
|
+
data: string;
|
|
189
|
+
driverParam: string;
|
|
190
|
+
notNull: false;
|
|
191
|
+
hasDefault: false;
|
|
192
|
+
enumValues: [string, ...string[]];
|
|
193
|
+
baseColumn: never;
|
|
194
|
+
}, object>;
|
|
195
|
+
session_state: import("drizzle-orm/sqlite-core/index.js").SQLiteColumn<{
|
|
196
|
+
name: "session_state";
|
|
197
|
+
tableName: "account";
|
|
198
|
+
dataType: "string";
|
|
199
|
+
columnType: "SQLiteText";
|
|
200
|
+
data: string;
|
|
201
|
+
driverParam: string;
|
|
202
|
+
notNull: false;
|
|
203
|
+
hasDefault: false;
|
|
204
|
+
enumValues: [string, ...string[]];
|
|
205
|
+
baseColumn: never;
|
|
206
|
+
}, object>;
|
|
207
|
+
};
|
|
208
|
+
dialect: "sqlite";
|
|
209
|
+
}>;
|
|
210
|
+
sessions: import("drizzle-orm/sqlite-core/table.js").SQLiteTableWithColumns<{
|
|
211
|
+
name: "session";
|
|
212
|
+
schema: undefined;
|
|
213
|
+
columns: {
|
|
214
|
+
sessionToken: import("drizzle-orm/sqlite-core/index.js").SQLiteColumn<{
|
|
215
|
+
name: "sessionToken";
|
|
216
|
+
tableName: "session";
|
|
217
|
+
dataType: "string";
|
|
218
|
+
columnType: "SQLiteText";
|
|
219
|
+
data: string;
|
|
220
|
+
driverParam: string;
|
|
221
|
+
notNull: true;
|
|
222
|
+
hasDefault: false;
|
|
223
|
+
enumValues: [string, ...string[]];
|
|
224
|
+
baseColumn: never;
|
|
225
|
+
}, object>;
|
|
226
|
+
userId: import("drizzle-orm/sqlite-core/index.js").SQLiteColumn<{
|
|
227
|
+
name: "userId";
|
|
228
|
+
tableName: "session";
|
|
229
|
+
dataType: "string";
|
|
230
|
+
columnType: "SQLiteText";
|
|
231
|
+
data: string;
|
|
232
|
+
driverParam: string;
|
|
233
|
+
notNull: true;
|
|
234
|
+
hasDefault: false;
|
|
235
|
+
enumValues: [string, ...string[]];
|
|
236
|
+
baseColumn: never;
|
|
237
|
+
}, object>;
|
|
238
|
+
expires: import("drizzle-orm/sqlite-core/index.js").SQLiteColumn<{
|
|
239
|
+
name: "expires";
|
|
240
|
+
tableName: "session";
|
|
241
|
+
dataType: "date";
|
|
242
|
+
columnType: "SQLiteTimestamp";
|
|
243
|
+
data: Date;
|
|
244
|
+
driverParam: number;
|
|
245
|
+
notNull: true;
|
|
246
|
+
hasDefault: false;
|
|
247
|
+
enumValues: undefined;
|
|
248
|
+
baseColumn: never;
|
|
249
|
+
}, object>;
|
|
250
|
+
};
|
|
251
|
+
dialect: "sqlite";
|
|
252
|
+
}>;
|
|
253
|
+
verificationTokens: import("drizzle-orm/sqlite-core/table.js").SQLiteTableWithColumns<{
|
|
254
|
+
name: "verificationToken";
|
|
255
|
+
schema: undefined;
|
|
256
|
+
columns: {
|
|
257
|
+
identifier: import("drizzle-orm/sqlite-core/index.js").SQLiteColumn<{
|
|
258
|
+
name: "identifier";
|
|
259
|
+
tableName: "verificationToken";
|
|
260
|
+
dataType: "string";
|
|
261
|
+
columnType: "SQLiteText";
|
|
262
|
+
data: string;
|
|
263
|
+
driverParam: string;
|
|
264
|
+
notNull: true;
|
|
265
|
+
hasDefault: false;
|
|
266
|
+
enumValues: [string, ...string[]];
|
|
267
|
+
baseColumn: never;
|
|
268
|
+
}, object>;
|
|
269
|
+
token: import("drizzle-orm/sqlite-core/index.js").SQLiteColumn<{
|
|
270
|
+
name: "token";
|
|
271
|
+
tableName: "verificationToken";
|
|
272
|
+
dataType: "string";
|
|
273
|
+
columnType: "SQLiteText";
|
|
274
|
+
data: string;
|
|
275
|
+
driverParam: string;
|
|
276
|
+
notNull: true;
|
|
277
|
+
hasDefault: false;
|
|
278
|
+
enumValues: [string, ...string[]];
|
|
279
|
+
baseColumn: never;
|
|
280
|
+
}, object>;
|
|
281
|
+
expires: import("drizzle-orm/sqlite-core/index.js").SQLiteColumn<{
|
|
282
|
+
name: "expires";
|
|
283
|
+
tableName: "verificationToken";
|
|
284
|
+
dataType: "date";
|
|
285
|
+
columnType: "SQLiteTimestamp";
|
|
286
|
+
data: Date;
|
|
287
|
+
driverParam: number;
|
|
288
|
+
notNull: true;
|
|
289
|
+
hasDefault: false;
|
|
290
|
+
enumValues: undefined;
|
|
291
|
+
baseColumn: never;
|
|
292
|
+
}, object>;
|
|
293
|
+
};
|
|
294
|
+
dialect: "sqlite";
|
|
295
|
+
}>;
|
|
296
|
+
};
|
|
297
|
+
export type DefaultSchema = ReturnType<typeof createTables>;
|
|
298
|
+
export declare function SQLiteDrizzleAdapter(client: InstanceType<typeof BaseSQLiteDatabase>, tableFn?: SQLiteTableFn<undefined>): Adapter;
|
|
299
|
+
//# sourceMappingURL=sqlite.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sqlite.d.ts","sourceRoot":"","sources":["../src/lib/sqlite.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAkB,MAAM,qBAAqB,CAAA;AAElE,OAAO,EACL,kBAAkB,EAIlB,aAAa,EAEd,MAAM,yBAAyB,CAAA;AAIhC,wBAAgB,YAAY,CAAC,WAAW,EAAE,aAAa;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAoDtD;AAED,MAAM,MAAM,aAAa,GAAG,UAAU,CAAC,OAAO,YAAY,CAAC,CAAA;AAE3D,wBAAgB,oBAAoB,CAClC,MAAM,EAAE,YAAY,CAAC,OAAO,kBAAkB,CAAC,EAC/C,OAAO,2BAAuB,GAC7B,OAAO,CA+FT"}
|
package/lib/sqlite.js
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { and, eq } from 'drizzle-orm';
|
|
2
|
+
import { sqliteTable as defaultSqliteTableFn, integer, primaryKey, text, } from 'drizzle-orm/sqlite-core';
|
|
3
|
+
import { stripUndefined } from './utils.js';
|
|
4
|
+
export function createTables(sqliteTable) {
|
|
5
|
+
const users = sqliteTable('user', {
|
|
6
|
+
id: text('id').notNull().primaryKey(),
|
|
7
|
+
name: text('name'),
|
|
8
|
+
email: text('email').notNull(),
|
|
9
|
+
emailVerified: integer('emailVerified', { mode: 'timestamp_ms' }),
|
|
10
|
+
image: text('image'),
|
|
11
|
+
});
|
|
12
|
+
const accounts = sqliteTable('account', {
|
|
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
|
+
const sessions = sqliteTable('session', {
|
|
30
|
+
sessionToken: text('sessionToken').notNull().primaryKey(),
|
|
31
|
+
userId: text('userId')
|
|
32
|
+
.notNull()
|
|
33
|
+
.references(() => users.id, { onDelete: 'cascade' }),
|
|
34
|
+
expires: integer('expires', { mode: 'timestamp_ms' }).notNull(),
|
|
35
|
+
});
|
|
36
|
+
const verificationTokens = sqliteTable('verificationToken', {
|
|
37
|
+
identifier: text('identifier').notNull(),
|
|
38
|
+
token: text('token').notNull(),
|
|
39
|
+
expires: integer('expires', { mode: 'timestamp_ms' }).notNull(),
|
|
40
|
+
}, (vt) => ({
|
|
41
|
+
compoundKey: primaryKey(vt.identifier, vt.token),
|
|
42
|
+
}));
|
|
43
|
+
return { users, accounts, sessions, verificationTokens };
|
|
44
|
+
}
|
|
45
|
+
export function SQLiteDrizzleAdapter(client, tableFn = defaultSqliteTableFn) {
|
|
46
|
+
const { users, accounts, sessions, verificationTokens } = createTables(tableFn);
|
|
47
|
+
return {
|
|
48
|
+
async createUser(data) {
|
|
49
|
+
return client
|
|
50
|
+
.insert(users)
|
|
51
|
+
.values({ ...data, id: crypto.randomUUID() })
|
|
52
|
+
.returning()
|
|
53
|
+
.get();
|
|
54
|
+
},
|
|
55
|
+
async getUser(data) {
|
|
56
|
+
const result = await client.select().from(users).where(eq(users.id, data)).get();
|
|
57
|
+
return result ?? null;
|
|
58
|
+
},
|
|
59
|
+
async getUserByEmail(data) {
|
|
60
|
+
const result = await client.select().from(users).where(eq(users.email, data)).get();
|
|
61
|
+
return result ?? null;
|
|
62
|
+
},
|
|
63
|
+
createSession(data) {
|
|
64
|
+
return client.insert(sessions).values(data).returning().get();
|
|
65
|
+
},
|
|
66
|
+
async getSessionAndUser(data) {
|
|
67
|
+
const result = await client
|
|
68
|
+
.select({ session: sessions, user: users })
|
|
69
|
+
.from(sessions)
|
|
70
|
+
.where(eq(sessions.sessionToken, data))
|
|
71
|
+
.innerJoin(users, eq(users.id, sessions.userId))
|
|
72
|
+
.get();
|
|
73
|
+
return result ?? null;
|
|
74
|
+
},
|
|
75
|
+
async updateUser(data) {
|
|
76
|
+
if (!data.id) {
|
|
77
|
+
throw new Error('No user id.');
|
|
78
|
+
}
|
|
79
|
+
const result = await client.update(users).set(data).where(eq(users.id, data.id)).returning().get();
|
|
80
|
+
return result ?? null;
|
|
81
|
+
},
|
|
82
|
+
async updateSession(data) {
|
|
83
|
+
const result = await client
|
|
84
|
+
.update(sessions)
|
|
85
|
+
.set(data)
|
|
86
|
+
.where(eq(sessions.sessionToken, data.sessionToken))
|
|
87
|
+
.returning()
|
|
88
|
+
.get();
|
|
89
|
+
return result ?? null;
|
|
90
|
+
},
|
|
91
|
+
async linkAccount(rawAccount) {
|
|
92
|
+
return stripUndefined(await client.insert(accounts).values(rawAccount).returning().get());
|
|
93
|
+
},
|
|
94
|
+
async getUserByAccount(account) {
|
|
95
|
+
const results = await client
|
|
96
|
+
.select()
|
|
97
|
+
.from(accounts)
|
|
98
|
+
.leftJoin(users, eq(users.id, accounts.userId))
|
|
99
|
+
.where(and(eq(accounts.provider, account.provider), eq(accounts.providerAccountId, account.providerAccountId)))
|
|
100
|
+
.get();
|
|
101
|
+
if (!results) {
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
return Promise.resolve(results).then((results) => results.user);
|
|
105
|
+
},
|
|
106
|
+
async deleteSession(sessionToken) {
|
|
107
|
+
const result = await client.delete(sessions).where(eq(sessions.sessionToken, sessionToken)).returning().get();
|
|
108
|
+
return result ?? null;
|
|
109
|
+
},
|
|
110
|
+
async createVerificationToken(token) {
|
|
111
|
+
const result = await client.insert(verificationTokens).values(token).returning().get();
|
|
112
|
+
return result ?? null;
|
|
113
|
+
},
|
|
114
|
+
async useVerificationToken(token) {
|
|
115
|
+
try {
|
|
116
|
+
const result = await client
|
|
117
|
+
.delete(verificationTokens)
|
|
118
|
+
.where(and(eq(verificationTokens.identifier, token.identifier), eq(verificationTokens.token, token.token)))
|
|
119
|
+
.returning()
|
|
120
|
+
.get();
|
|
121
|
+
return result ?? null;
|
|
122
|
+
}
|
|
123
|
+
catch (err) {
|
|
124
|
+
throw new Error('No verification token found.');
|
|
125
|
+
}
|
|
126
|
+
},
|
|
127
|
+
async deleteUser(id) {
|
|
128
|
+
const result = await client.delete(users).where(eq(users.id, id)).returning().get();
|
|
129
|
+
return result ?? null;
|
|
130
|
+
},
|
|
131
|
+
async unlinkAccount(account) {
|
|
132
|
+
await client
|
|
133
|
+
.delete(accounts)
|
|
134
|
+
.where(and(eq(accounts.providerAccountId, account.providerAccountId), eq(accounts.provider, account.provider)))
|
|
135
|
+
.run();
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
}
|
package/lib/utils.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { MySqlDatabase } from 'drizzle-orm/mysql-core';
|
|
2
|
+
import type { AnyMySqlTable, MySqlTableFn } from 'drizzle-orm/mysql-core';
|
|
3
|
+
import { PgDatabase } from 'drizzle-orm/pg-core';
|
|
4
|
+
import type { AnyPgTable, PgTableFn } from 'drizzle-orm/pg-core';
|
|
5
|
+
import { BaseSQLiteDatabase } from 'drizzle-orm/sqlite-core';
|
|
6
|
+
import type { AnySQLiteTable, SQLiteTableFn } from 'drizzle-orm/sqlite-core';
|
|
7
|
+
import type { DefaultSchema as MySqlSchema } from './mysql.js';
|
|
8
|
+
import type { DefaultSchema as PgSchema } from './pg.js';
|
|
9
|
+
import type { DefaultSchema as SQLiteSchema } from './sqlite.js';
|
|
10
|
+
export type AnyMySqlDatabase = MySqlDatabase<any, any>;
|
|
11
|
+
export type AnyPgDatabase = PgDatabase<any, any, any>;
|
|
12
|
+
export type AnySQLiteDatabase = BaseSQLiteDatabase<any, any, any, any>;
|
|
13
|
+
export interface MinimumSchema {
|
|
14
|
+
mysql: MySqlSchema & Record<string, AnyMySqlTable>;
|
|
15
|
+
pg: PgSchema & Record<string, AnyPgTable>;
|
|
16
|
+
sqlite: SQLiteSchema & Record<string, AnySQLiteTable>;
|
|
17
|
+
}
|
|
18
|
+
export type SqlFlavorOptions = AnyMySqlDatabase | AnyPgDatabase | AnySQLiteDatabase;
|
|
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 ? SQLiteTableFn : AnySQLiteTable;
|
|
21
|
+
type NonNullableProps<T> = {
|
|
22
|
+
[P in keyof T]: null extends T[P] ? never : P;
|
|
23
|
+
}[keyof T];
|
|
24
|
+
export declare function stripUndefined<T>(obj: T): Pick<T, NonNullableProps<T>>;
|
|
25
|
+
export {};
|
|
26
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/lib/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAA;AACtD,OAAO,KAAK,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAA;AACzE,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAA;AAChD,OAAO,KAAK,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAA;AAChE,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAA;AAC5D,OAAO,KAAK,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAA;AAE5E,OAAO,KAAK,EAAE,aAAa,IAAI,WAAW,EAAE,MAAM,YAAY,CAAA;AAC9D,OAAO,KAAK,EAAE,aAAa,IAAI,QAAQ,EAAE,MAAM,SAAS,CAAA;AACxD,OAAO,KAAK,EAAE,aAAa,IAAI,YAAY,EAAE,MAAM,aAAa,CAAA;AAEhE,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,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,CAAA;IAClD,EAAE,EAAE,QAAQ,GAAG,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAA;IACzC,MAAM,EAAE,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE,cAAc,CAAC,CAAA;CACtD;AAED,MAAM,MAAM,gBAAgB,GAAG,gBAAgB,GAAG,aAAa,GAAG,iBAAiB,CAAA;AAEnF,MAAM,MAAM,aAAa,CAAC,MAAM,IAAI,MAAM,SAAS,gBAAgB,GAC/D,aAAa,CAAC,OAAO,CAAC,GACtB,MAAM,SAAS,aAAa,GAC1B,aAAa,CAAC,IAAI,CAAC,GACnB,MAAM,SAAS,iBAAiB,GAC9B,aAAa,CAAC,QAAQ,CAAC,GACvB,KAAK,CAAA;AAEb,MAAM,MAAM,OAAO,CAAC,MAAM,IAAI,MAAM,SAAS,gBAAgB,GACzD,YAAY,GACZ,MAAM,SAAS,aAAa,GAC1B,SAAS,GACT,MAAM,SAAS,iBAAiB,GAC9B,aAAa,GACb,cAAc,CAAA;AAEtB,KAAK,gBAAgB,CAAC,CAAC,IAAI;KACxB,CAAC,IAAI,MAAM,CAAC,GAAG,IAAI,SAAS,CAAC,CAAC,CAAC,CAAC,GAAG,KAAK,GAAG,CAAC;CAC9C,CAAC,MAAM,CAAC,CAAC,CAAA;AAEV,wBAAgB,cAAc,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,IAAI,CAAC,CAAC,EAAE,gBAAgB,CAAC,CAAC,CAAC,CAAC,CAItE"}
|