forked from JitPackJoyride/lucia-adapter-edgedb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedgedb.ts
330 lines (293 loc) · 9.73 KB
/
edgedb.ts
1
2
3
4
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import type { Client } from "edgedb";
import type {
Adapter,
GlobalDatabaseSessionAttributes,
GlobalDatabaseUserAttributes,
InitializeAdapter,
} from "lucia";
import { uuidValidate } from "./utils";
export const edgedbAdapter = (
client: Client,
e: any,
modelNames: {
user: string;
session: string | null;
key: string;
}
): InitializeAdapter<Adapter> => {
const getModels = () => {
if (!modelNames) {
return {
User: e["User"],
Session: e["UserSession"],
Key: e["UserKey"],
};
}
return {
User: e[modelNames.user],
Session: modelNames.session ? e[modelNames.session] : null,
Key: e[modelNames.key],
};
};
const { User, Session, Key } = getModels();
return (LuciaError) => {
return {
getUser: async (userId) => {
const query = e.select(
User,
(userObj: GlobalDatabaseUserAttributes) => ({
...User["*"],
filter_single: e.op(userObj.id, "=", e.cast(e.uuid, userId)),
})
);
return await query.run(client);
},
setUser: async (user, key) => {
if (uuidValidate(user.id) === false) {
// In EdgeDB, the id should always be a UUID
// If it's not, we need to delete it, so that EdgeDB can generate a new one
// This does degrade developer experience as the id won't necessarily be available in the response
delete user.id;
}
if (!key) {
const query = e.insert(User, user);
await query.run(client);
return;
}
try {
await client.transaction(async (tx) => {
const userInsertQuery = e.insert(User, user);
const result = await userInsertQuery.run(tx);
// Removing user_id from key object and changing id to key_id
const { id: key_id, user_id, ...keyWithoutUserId } = key;
const newKey = {
key_id,
...keyWithoutUserId,
};
const keyInsertQuery = e.insert(Key, {
...newKey,
user: e.select(User, (userObj: GlobalDatabaseUserAttributes) => ({
filter_single: e.op(userObj.id, "=", e.cast(e.uuid, result.id)),
})),
});
await keyInsertQuery.run(tx);
});
} catch (error) {
// Catch duplicate key errors
type ErrorCasting = { message?: string };
if (
(error as ErrorCasting).message &&
(error as ErrorCasting).message?.includes(
`${modelNames.key}: key_id`
)
) {
throw new LuciaError("AUTH_DUPLICATE_KEY_ID");
}
throw error;
}
},
deleteUser: async (userId) => {
const query = e.delete(
User,
(userObj: GlobalDatabaseUserAttributes) => ({
filter_single: e.op(userObj.id, "=", e.cast(e.uuid, userId)),
})
);
await query.run(client);
},
updateUser: async (userId, partialUser) => {
const query = e.update(
User,
(userObj: GlobalDatabaseUserAttributes) => ({
filter_single: e.op(userObj.id, "=", e.cast(e.uuid, userId)),
set: partialUser,
})
);
await query.run(client);
},
getSession: async (sessionId) => {
if (Session === null) {
throw new Error("Session table not defined");
}
const query = e.select(
Session,
(sessionObj: GlobalDatabaseSessionAttributes) => ({
...Session["*"],
filter_single: e.op(sessionObj.id, "=", e.cast(e.uuid, sessionId)),
})
);
return await query.run(client);
},
getSessionsByUserId: async (userId) => {
if (Session === null) {
throw new Error("Session table not defined");
}
const query = e.select(
Session,
(sessionObj: GlobalDatabaseSessionAttributes) => ({
...Session["*"],
filter: e.op(sessionObj.user.id, "=", e.cast(e.uuid, userId)),
})
);
return await query.run(client);
},
setSession: async (session) => {
if (Session === null) {
throw new Error("Session table not defined");
}
if (uuidValidate(session.id) === false) {
// In EdgeDB, the id should always be a UUID
// If it's not, we need to delete it, so that EdgeDB can generate a new one
// This does degrade developer experience as the id won't be available in the response
delete session.id;
}
try {
const { user_id, ...sessionWithoutUserId } = session;
const query = e.insert(Session, {
...sessionWithoutUserId,
user: e.select(User, (userObj: GlobalDatabaseUserAttributes) => ({
filter_single: e.op(userObj.id, "=", e.cast(e.uuid, user_id)),
})),
});
await query.run(client);
} catch (error) {
// Catch invalid user id errors
type ErrorCasting = { message?: string };
// Catch invalid user id errors
if (
(error as ErrorCasting).message &&
(error as ErrorCasting).message?.includes(
`missing value for required link 'user'`
)
) {
throw new LuciaError("AUTH_INVALID_USER_ID");
}
throw error;
}
},
deleteSession: async (sessionId) => {
if (Session === null) {
throw new Error("Session table not defined");
}
const query = e.delete(
Session,
(sessionObj: GlobalDatabaseSessionAttributes) => ({
filter_single: e.op(sessionObj.id, "=", e.cast(e.uuid, sessionId)),
})
);
await query.run(client);
},
deleteSessionsByUserId: async (userId) => {
if (Session === null) {
throw new Error("Session table not defined");
}
const query = e.delete(
Session,
(sessionObj: GlobalDatabaseSessionAttributes) => ({
filter: e.op(sessionObj.user.id, "=", e.cast(e.uuid, userId)),
})
);
await query.run(client);
},
updateSession: async (sessionId, partialSession) => {
if (Session === null) {
throw new Error("Session table not defined");
}
const query = e.update(
Session,
(sessionObj: GlobalDatabaseSessionAttributes) => ({
filter_single: e.op(sessionObj.id, "=", e.cast(e.uuid, sessionId)),
set: partialSession,
})
);
await query.run(client);
},
getKey: async (keyId) => {
const query = e.select(Key, (keyObj: { key_id: string }) => ({
...Key["*"],
filter_single: e.op(keyObj.key_id, "=", e.cast(e.uuid, keyId)),
}));
return await query.run(client);
},
getKeysByUserId: async (userId) => {
const query = e.select(Key, (keyObj: { user: { id: string } }) => ({
...Key["*"],
filter: e.op(keyObj.user.id, "=", e.cast(e.uuid, userId)),
}));
return await query.run(client);
},
setKey: async (key) => {
try {
const { user_id, id: key_id, ...keyWithoutUserId } = key;
const query = e.insert(Key, {
key_id,
...keyWithoutUserId,
user: e.select(User, (userObj: GlobalDatabaseUserAttributes) => ({
filter_single: e.op(userObj.id, "=", e.cast(e.uuid, user_id)),
})),
});
await query.run(client);
} catch (error) {
type ErrorCasting = { message?: string };
// Catch duplicate key id error
if (
(error as ErrorCasting).message &&
(error as ErrorCasting).message?.includes(
`${modelNames.key}: key_id`
)
) {
throw new LuciaError("AUTH_DUPLICATE_KEY_ID");
}
// Catch invalid user id errors
if (
(error as ErrorCasting).message &&
(error as ErrorCasting).message?.includes(
`missing value for required link 'user'`
)
) {
throw new LuciaError("AUTH_INVALID_USER_ID");
}
throw error;
}
},
deleteKey: async (keyId) => {
const query = e.delete(Key, (keyObj: { key_id: string }) => ({
filter_single: e.op(keyObj.key_id, "=", e.cast(e.uuid, keyId)),
}));
await query.run(client);
},
deleteKeysByUserId: async (userId) => {
const query = e.delete(Key, (keyObj: { user: { id: string } }) => ({
filter: e.op(keyObj.user.id, "=", e.cast(e.uuid, userId)),
}));
await query.run(client);
},
updateKey: async (keyId, partialKey) => {
const query = e.update(Key, (keyObj: { key_id: string }) => ({
filter_single: e.op(keyObj.key_id, "=", e.cast(e.uuid, keyId)),
set: partialKey,
}));
await query.run(client);
},
getSessionAndUser: async (sessionId) => {
if (Session === null) {
throw new Error("Session table not defined");
}
const query = e.select(
Session,
(sessionObj: GlobalDatabaseSessionAttributes) => ({
...Session["*"],
user: {
...User["*"],
},
filter_single: e.op(sessionObj.id, "=", e.cast(e.uuid, sessionId)),
})
);
const result = await query.run(client);
if (!result) return [null, null];
const { user, ...session } = result;
return [session, user];
},
};
};
};