@nxgt/janus-mongo 0.1.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/LICENSE +21 -0
- package/README.md +106 -0
- package/dist/collections.d.ts +75 -0
- package/dist/collections.d.ts.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +480 -0
- package/dist/index.js.map +13 -0
- package/dist/relations.d.ts +54 -0
- package/dist/relations.d.ts.map +1 -0
- package/dist/stores.d.ts +37 -0
- package/dist/stores.d.ts.map +1 -0
- package/dist/translate.d.ts +58 -0
- package/dist/translate.d.ts.map +1 -0
- package/docs/README.md +11 -0
- package/docs/guide/sync.md +122 -0
- package/docs/guide/wiring.md +159 -0
- package/docs/roadmap.md +54 -0
- package/docs/troubleshooting.md +193 -0
- package/package.json +65 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,480 @@
|
|
|
1
|
+
// src/collections.ts
|
|
2
|
+
import { defineCollection } from "@nxgt/mongo";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
var jsonObject = z.record(z.string(), z.unknown());
|
|
5
|
+
var at = z.date();
|
|
6
|
+
var users = defineCollection({
|
|
7
|
+
name: "users",
|
|
8
|
+
schema: z.object({
|
|
9
|
+
_id: z.string(),
|
|
10
|
+
type: z.string(),
|
|
11
|
+
schemaVersion: z.string(),
|
|
12
|
+
active: z.boolean(),
|
|
13
|
+
fields: jsonObject,
|
|
14
|
+
logins: z.array(z.string()),
|
|
15
|
+
password: z.object({ hash: z.string(), updatedAt: at }).nullable(),
|
|
16
|
+
emailVerifiedAt: at.nullable(),
|
|
17
|
+
version: z.int().nonnegative(),
|
|
18
|
+
createdAt: at,
|
|
19
|
+
updatedAt: at
|
|
20
|
+
}),
|
|
21
|
+
indexes: [
|
|
22
|
+
{
|
|
23
|
+
key: { type: 1, logins: 1 },
|
|
24
|
+
name: "loginUnique",
|
|
25
|
+
unique: true,
|
|
26
|
+
partialFilterExpression: { logins: { $type: "string" } }
|
|
27
|
+
},
|
|
28
|
+
{ key: { type: 1, _id: 1 }, name: "typeId" }
|
|
29
|
+
]
|
|
30
|
+
});
|
|
31
|
+
var sessions = defineCollection({
|
|
32
|
+
name: "sessions",
|
|
33
|
+
schema: z.object({
|
|
34
|
+
_id: z.string(),
|
|
35
|
+
tokenHash: z.string(),
|
|
36
|
+
userId: z.string(),
|
|
37
|
+
authenticatedAt: at,
|
|
38
|
+
expiresAt: at,
|
|
39
|
+
revokedAt: at.nullable(),
|
|
40
|
+
createdAt: at
|
|
41
|
+
}),
|
|
42
|
+
indexes: [
|
|
43
|
+
{
|
|
44
|
+
key: { tokenHash: 1 },
|
|
45
|
+
name: "tokenHashUnique",
|
|
46
|
+
unique: true
|
|
47
|
+
},
|
|
48
|
+
{ key: { userId: 1 }, name: "userId" },
|
|
49
|
+
{
|
|
50
|
+
key: { expiresAt: 1 },
|
|
51
|
+
name: "expiry",
|
|
52
|
+
expireAfterSeconds: 0
|
|
53
|
+
}
|
|
54
|
+
]
|
|
55
|
+
});
|
|
56
|
+
var tokens = defineCollection({
|
|
57
|
+
name: "tokens",
|
|
58
|
+
schema: z.object({
|
|
59
|
+
_id: z.string(),
|
|
60
|
+
kind: z.enum(["verifyEmail", "resetPassword"]),
|
|
61
|
+
userId: z.string(),
|
|
62
|
+
address: z.string(),
|
|
63
|
+
expiresAt: at,
|
|
64
|
+
spentAt: at.nullable(),
|
|
65
|
+
createdAt: at
|
|
66
|
+
}),
|
|
67
|
+
indexes: [
|
|
68
|
+
{ key: { expiresAt: 1 }, name: "expiry", expireAfterSeconds: 0 },
|
|
69
|
+
{ key: { userId: 1 }, name: "userId" }
|
|
70
|
+
]
|
|
71
|
+
});
|
|
72
|
+
var janusCollections = [users, sessions, tokens];
|
|
73
|
+
// src/relations.ts
|
|
74
|
+
import {
|
|
75
|
+
isSubjectSet
|
|
76
|
+
} from "@nxgt/janus";
|
|
77
|
+
import {
|
|
78
|
+
defineCollection as defineCollection2,
|
|
79
|
+
getCollection,
|
|
80
|
+
syncCollections
|
|
81
|
+
} from "@nxgt/mongo";
|
|
82
|
+
import { z as z2 } from "zod";
|
|
83
|
+
|
|
84
|
+
// src/translate.ts
|
|
85
|
+
import { JanusError, StoreConflict, StoreFailure } from "@nxgt/janus";
|
|
86
|
+
async function run(slot, operation, body) {
|
|
87
|
+
try {
|
|
88
|
+
return await body();
|
|
89
|
+
} catch (error) {
|
|
90
|
+
if (error instanceof JanusError)
|
|
91
|
+
throw error;
|
|
92
|
+
throw new StoreFailure(`${slot}.${operation}: the store could not answer`, {
|
|
93
|
+
slot,
|
|
94
|
+
operation,
|
|
95
|
+
cause: error
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
function isDuplicateKey(error) {
|
|
100
|
+
return typeof error === "object" && error !== null && error.code === 11000;
|
|
101
|
+
}
|
|
102
|
+
function settle(write) {
|
|
103
|
+
return write.then((written) => ({ written }), (error) => {
|
|
104
|
+
if (isDuplicateKey(error))
|
|
105
|
+
return { duplicate: error };
|
|
106
|
+
throw error;
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
function takenLogin(duplicate) {
|
|
110
|
+
const type = duplicate.keyValue?.type;
|
|
111
|
+
const login = duplicate.keyValue?.logins;
|
|
112
|
+
if (typeof type !== "string" || typeof login !== "string")
|
|
113
|
+
return null;
|
|
114
|
+
return { type, login };
|
|
115
|
+
}
|
|
116
|
+
function loginTaken(operation, { type, login }, cause) {
|
|
117
|
+
return new StoreConflict("login", `${operation}: the login "${login}" is taken by another ${type}`, { login, userType: type, operation, cause });
|
|
118
|
+
}
|
|
119
|
+
function unexpectedDuplicate(slot, operation, duplicate) {
|
|
120
|
+
return new StoreFailure(`${slot}.${operation}: a duplicate key on ${Object.keys(duplicate.keyPattern ?? {}).join(", ") || "an unknown index"}, which this adapter never writes on purpose`, { slot, operation, cause: duplicate });
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// src/relations.ts
|
|
124
|
+
var relations = defineCollection2({
|
|
125
|
+
name: "relations",
|
|
126
|
+
schema: z2.object({
|
|
127
|
+
_id: z2.object({
|
|
128
|
+
object: z2.object({ type: z2.string(), id: z2.string() }),
|
|
129
|
+
relation: z2.string(),
|
|
130
|
+
subject: z2.object({
|
|
131
|
+
type: z2.string(),
|
|
132
|
+
id: z2.string(),
|
|
133
|
+
relation: z2.string().optional()
|
|
134
|
+
})
|
|
135
|
+
})
|
|
136
|
+
}),
|
|
137
|
+
indexes: [
|
|
138
|
+
{
|
|
139
|
+
key: { "_id.object.type": 1, "_id.object.id": 1, "_id.relation": 1 },
|
|
140
|
+
name: "objectRelation"
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
key: {
|
|
144
|
+
"_id.subject.type": 1,
|
|
145
|
+
"_id.subject.id": 1,
|
|
146
|
+
"_id.subject.relation": 1,
|
|
147
|
+
"_id.relation": 1,
|
|
148
|
+
"_id.object.type": 1,
|
|
149
|
+
"_id.object.id": 1
|
|
150
|
+
},
|
|
151
|
+
name: "subjectObjects"
|
|
152
|
+
}
|
|
153
|
+
]
|
|
154
|
+
});
|
|
155
|
+
function createMongoRelations(db) {
|
|
156
|
+
const collection = getCollection(db, relations).raw;
|
|
157
|
+
const run$ = (operation, body) => run("relations", operation, body);
|
|
158
|
+
const holding = async (object, relation, sets) => (await collection.find({
|
|
159
|
+
"_id.object.type": object.type,
|
|
160
|
+
"_id.object.id": object.id,
|
|
161
|
+
"_id.relation": relation,
|
|
162
|
+
"_id.subject.relation": { $exists: sets }
|
|
163
|
+
}).toArray()).map((document) => document._id.subject);
|
|
164
|
+
return {
|
|
165
|
+
write: ({ add = [], remove = [] }) => run$("write", async () => {
|
|
166
|
+
const removals = remove.map(keyOf);
|
|
167
|
+
const additions = add.map(keyOf);
|
|
168
|
+
const apply = async (session) => {
|
|
169
|
+
if (removals.length > 0) {
|
|
170
|
+
await collection.deleteMany({ _id: { $in: removals } }, session === undefined ? {} : { session });
|
|
171
|
+
}
|
|
172
|
+
if (additions.length > 0) {
|
|
173
|
+
await collection.bulkWrite(additions.map((key) => ({
|
|
174
|
+
replaceOne: {
|
|
175
|
+
filter: { _id: key },
|
|
176
|
+
replacement: { _id: key },
|
|
177
|
+
upsert: true
|
|
178
|
+
}
|
|
179
|
+
})), session === undefined ? {} : { session });
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
if (removals.length + additions.length <= 1)
|
|
183
|
+
return apply();
|
|
184
|
+
const session = db.client.startSession();
|
|
185
|
+
try {
|
|
186
|
+
session.startTransaction();
|
|
187
|
+
await apply(session);
|
|
188
|
+
await session.commitTransaction();
|
|
189
|
+
} finally {
|
|
190
|
+
await session.endSession();
|
|
191
|
+
}
|
|
192
|
+
}),
|
|
193
|
+
has: (tuple) => run$("has", async () => await collection.findOne({ _id: keyOf(tuple) }, { projection: { _id: 1 } }) !== null),
|
|
194
|
+
findSubjectSets: (object, relation) => run$("findSubjectSets", async () => (await holding(object, relation, true)).map((subject) => ({
|
|
195
|
+
type: subject.type,
|
|
196
|
+
id: subject.id,
|
|
197
|
+
relation: subject.relation
|
|
198
|
+
}))),
|
|
199
|
+
findEntities: (object, relation) => run$("findEntities", async () => (await holding(object, relation, false)).map((subject) => ({ type: subject.type, id: subject.id }))),
|
|
200
|
+
findObjects: ({ type, relation, subject, after, limit }) => run$("findObjects", async () => {
|
|
201
|
+
const filter = {
|
|
202
|
+
"_id.subject.type": subject.type,
|
|
203
|
+
"_id.subject.id": subject.id,
|
|
204
|
+
"_id.subject.relation": isSubjectSet(subject) ? subject.relation : null,
|
|
205
|
+
"_id.relation": relation,
|
|
206
|
+
"_id.object.type": type,
|
|
207
|
+
...after === null ? {} : { "_id.object.id": { $gt: after } }
|
|
208
|
+
};
|
|
209
|
+
const found = await collection.find(filter, { projection: { "_id.object.id": 1 } }).sort({ "_id.object.id": 1 }).limit(limit + 1).toArray();
|
|
210
|
+
const items = found.slice(0, limit).map((document) => document._id.object.id);
|
|
211
|
+
const last = items.at(-1);
|
|
212
|
+
return {
|
|
213
|
+
items,
|
|
214
|
+
nextCursor: found.length > limit && last !== undefined ? last : null
|
|
215
|
+
};
|
|
216
|
+
}),
|
|
217
|
+
deleteEntity: (entity) => run$("deleteEntity", async () => {
|
|
218
|
+
const result = await collection.deleteMany({
|
|
219
|
+
$or: [
|
|
220
|
+
{ "_id.object.type": entity.type, "_id.object.id": entity.id },
|
|
221
|
+
{ "_id.subject.type": entity.type, "_id.subject.id": entity.id }
|
|
222
|
+
]
|
|
223
|
+
});
|
|
224
|
+
return result.deletedCount;
|
|
225
|
+
})
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
function syncMongoRelations(db, options) {
|
|
229
|
+
return syncCollections(db, [relations], options);
|
|
230
|
+
}
|
|
231
|
+
function keyOf(tuple) {
|
|
232
|
+
return {
|
|
233
|
+
object: { type: tuple.object.type, id: tuple.object.id },
|
|
234
|
+
relation: tuple.relation,
|
|
235
|
+
subject: subjectKey(tuple.subject)
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
function subjectKey(subject) {
|
|
239
|
+
return isSubjectSet(subject) ? { type: subject.type, id: subject.id, relation: subject.relation } : { type: subject.type, id: subject.id };
|
|
240
|
+
}
|
|
241
|
+
// src/stores.ts
|
|
242
|
+
import { NotFoundError, StoreConflict as StoreConflict2 } from "@nxgt/janus";
|
|
243
|
+
import {
|
|
244
|
+
getCollection as getCollection2,
|
|
245
|
+
syncCollections as syncCollections2
|
|
246
|
+
} from "@nxgt/mongo";
|
|
247
|
+
function createMongoStores(db) {
|
|
248
|
+
return {
|
|
249
|
+
users: userStore(db),
|
|
250
|
+
sessions: sessionStore(db),
|
|
251
|
+
tokens: tokenStore(db)
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
function syncMongoStores(db, options) {
|
|
255
|
+
return syncCollections2(db, janusCollections, options);
|
|
256
|
+
}
|
|
257
|
+
function userStore(db) {
|
|
258
|
+
const collection = getCollection2(db, users);
|
|
259
|
+
const run$ = (operation, body) => run("users", operation, body);
|
|
260
|
+
const find = async (id) => {
|
|
261
|
+
const found = await collection.findById(id);
|
|
262
|
+
return found === undefined ? null : toUser(found);
|
|
263
|
+
};
|
|
264
|
+
const refuse = (operation, duplicate) => {
|
|
265
|
+
const taken = takenLogin(duplicate);
|
|
266
|
+
return taken === null ? unexpectedDuplicate("users", operation, duplicate) : loginTaken(operation, taken, duplicate);
|
|
267
|
+
};
|
|
268
|
+
return {
|
|
269
|
+
insertUser: (record) => run$("insertUser", async () => {
|
|
270
|
+
const document = toUserDocument(record);
|
|
271
|
+
const outcome = await settle(collection.raw.insertOne(document));
|
|
272
|
+
if (!("duplicate" in outcome))
|
|
273
|
+
return toUser(document);
|
|
274
|
+
const stored = await find(record.id);
|
|
275
|
+
if (stored !== null)
|
|
276
|
+
return stored;
|
|
277
|
+
throw refuse("insertUser", outcome.duplicate);
|
|
278
|
+
}),
|
|
279
|
+
findUser: (id) => run$("findUser", () => find(id)),
|
|
280
|
+
findUserByLogin: (type, login) => run$("findUserByLogin", async () => {
|
|
281
|
+
const found = await collection.findFirst({ type, logins: login });
|
|
282
|
+
return found === undefined ? null : toUser(found);
|
|
283
|
+
}),
|
|
284
|
+
listUsers: ({ type, after, limit }) => run$("listUsers", async () => {
|
|
285
|
+
const found = await collection.findMany({
|
|
286
|
+
filter: after === null ? { type } : { type, _id: { $gt: after } },
|
|
287
|
+
sort: { _id: 1 },
|
|
288
|
+
limit: limit + 1
|
|
289
|
+
});
|
|
290
|
+
const items = found.slice(0, limit).map(toUser);
|
|
291
|
+
const last = items.at(-1);
|
|
292
|
+
return {
|
|
293
|
+
items,
|
|
294
|
+
nextCursor: found.length > limit && last !== undefined ? last.id : null
|
|
295
|
+
};
|
|
296
|
+
}),
|
|
297
|
+
updateUser: (id, patch, ifVersion) => run$("updateUser", async () => {
|
|
298
|
+
const outcome = await settle(collection.raw.findOneAndUpdate({ _id: id, version: ifVersion }, { $set: toUserSet(patch), $inc: { version: 1 } }, { returnDocument: "after" }));
|
|
299
|
+
if ("duplicate" in outcome) {
|
|
300
|
+
throw refuse("updateUser", outcome.duplicate);
|
|
301
|
+
}
|
|
302
|
+
if (outcome.written !== null)
|
|
303
|
+
return toUser(outcome.written);
|
|
304
|
+
const stored = await find(id);
|
|
305
|
+
if (stored === null) {
|
|
306
|
+
throw new NotFoundError("updateUser: no user has this id", {
|
|
307
|
+
userId: id,
|
|
308
|
+
operation: "updateUser"
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
throw new StoreConflict2("version", `updateUser: expected version ${ifVersion}, found ${stored.version}`, {
|
|
312
|
+
userId: id,
|
|
313
|
+
expectedVersion: ifVersion,
|
|
314
|
+
actualVersion: stored.version,
|
|
315
|
+
operation: "updateUser"
|
|
316
|
+
});
|
|
317
|
+
}),
|
|
318
|
+
deleteUser: (id) => run$("deleteUser", async () => {
|
|
319
|
+
const result = await collection.raw.deleteOne({ _id: id });
|
|
320
|
+
return result.deletedCount === 1;
|
|
321
|
+
})
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
function sessionStore(db) {
|
|
325
|
+
const collection = getCollection2(db, sessions);
|
|
326
|
+
const run$ = (operation, body) => run("sessions", operation, body);
|
|
327
|
+
return {
|
|
328
|
+
insertSession: (record) => run$("insertSession", async () => {
|
|
329
|
+
const outcome = await settle(collection.raw.insertOne(toSessionDocument(record)));
|
|
330
|
+
if (!("duplicate" in outcome))
|
|
331
|
+
return;
|
|
332
|
+
if (await collection.findById(record.id) !== undefined)
|
|
333
|
+
return;
|
|
334
|
+
throw unexpectedDuplicate("sessions", "insertSession", outcome.duplicate);
|
|
335
|
+
}),
|
|
336
|
+
findSessionByTokenHash: (tokenHash) => run$("findSessionByTokenHash", async () => {
|
|
337
|
+
const found = await collection.findFirst({ tokenHash });
|
|
338
|
+
return found === undefined ? null : toSession(found);
|
|
339
|
+
}),
|
|
340
|
+
extendSession: (id, expiresAt) => run$("extendSession", async () => {
|
|
341
|
+
const written = await collection.raw.findOneAndUpdate({ _id: id, revokedAt: null }, { $set: { expiresAt } }, { returnDocument: "after" });
|
|
342
|
+
return written === null ? null : toSession(written);
|
|
343
|
+
}),
|
|
344
|
+
revokeSession: (id, at) => run$("revokeSession", async () => {
|
|
345
|
+
const result = await collection.raw.updateOne({ _id: id }, [
|
|
346
|
+
{ $set: { revokedAt: { $ifNull: ["$revokedAt", at] } } }
|
|
347
|
+
]);
|
|
348
|
+
return result.matchedCount === 1;
|
|
349
|
+
}),
|
|
350
|
+
revokeUserSessions: (userId, at, except) => run$("revokeUserSessions", async () => {
|
|
351
|
+
const result = await collection.raw.updateMany(except === undefined ? { userId, revokedAt: null } : { userId, revokedAt: null, _id: { $ne: except } }, { $set: { revokedAt: at } });
|
|
352
|
+
return result.modifiedCount;
|
|
353
|
+
}),
|
|
354
|
+
deleteUserSessions: (userId) => run$("deleteUserSessions", async () => {
|
|
355
|
+
const result = await collection.raw.deleteMany({ userId });
|
|
356
|
+
return result.deletedCount;
|
|
357
|
+
})
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
function tokenStore(db) {
|
|
361
|
+
const collection = getCollection2(db, tokens);
|
|
362
|
+
const run$ = (operation, body) => run("tokens", operation, body);
|
|
363
|
+
return {
|
|
364
|
+
insertToken: (record) => run$("insertToken", async () => {
|
|
365
|
+
await settle(collection.raw.insertOne(toTokenDocument(record)));
|
|
366
|
+
}),
|
|
367
|
+
consumeToken: (tokenHash, kind, at) => run$("consumeToken", async () => {
|
|
368
|
+
const before = await collection.raw.findOneAndUpdate({ _id: tokenHash, kind }, [{ $set: { spentAt: { $ifNull: ["$spentAt", at] } } }], { returnDocument: "before" });
|
|
369
|
+
return before === null ? null : toToken(before);
|
|
370
|
+
}),
|
|
371
|
+
deleteUserTokens: (userId) => run$("deleteUserTokens", async () => {
|
|
372
|
+
const result = await collection.raw.deleteMany({ userId });
|
|
373
|
+
return result.deletedCount;
|
|
374
|
+
})
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
function toUserDocument(record) {
|
|
378
|
+
return structuredClone({
|
|
379
|
+
_id: record.id,
|
|
380
|
+
type: record.type,
|
|
381
|
+
schemaVersion: record.schemaVersion,
|
|
382
|
+
active: record.active,
|
|
383
|
+
fields: record.fields,
|
|
384
|
+
logins: [...record.logins],
|
|
385
|
+
password: record.password,
|
|
386
|
+
emailVerifiedAt: record.emailVerifiedAt,
|
|
387
|
+
version: record.version,
|
|
388
|
+
createdAt: record.createdAt,
|
|
389
|
+
updatedAt: record.updatedAt
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
function toUserSet(patch) {
|
|
393
|
+
const set = { updatedAt: patch.updatedAt };
|
|
394
|
+
for (const field of [
|
|
395
|
+
"schemaVersion",
|
|
396
|
+
"active",
|
|
397
|
+
"fields",
|
|
398
|
+
"logins",
|
|
399
|
+
"password",
|
|
400
|
+
"emailVerifiedAt"
|
|
401
|
+
]) {
|
|
402
|
+
if (patch[field] !== undefined)
|
|
403
|
+
set[field] = patch[field];
|
|
404
|
+
}
|
|
405
|
+
return structuredClone(set);
|
|
406
|
+
}
|
|
407
|
+
function toUser(document) {
|
|
408
|
+
const password = document.password;
|
|
409
|
+
return {
|
|
410
|
+
id: document._id,
|
|
411
|
+
type: document.type,
|
|
412
|
+
schemaVersion: document.schemaVersion,
|
|
413
|
+
active: document.active,
|
|
414
|
+
fields: document.fields,
|
|
415
|
+
logins: [...document.logins],
|
|
416
|
+
password: password === null ? null : { hash: password.hash, updatedAt: password.updatedAt },
|
|
417
|
+
emailVerifiedAt: document.emailVerifiedAt,
|
|
418
|
+
version: document.version,
|
|
419
|
+
createdAt: document.createdAt,
|
|
420
|
+
updatedAt: document.updatedAt
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
function toSessionDocument(record) {
|
|
424
|
+
return {
|
|
425
|
+
_id: record.id,
|
|
426
|
+
tokenHash: record.tokenHash,
|
|
427
|
+
userId: record.userId,
|
|
428
|
+
authenticatedAt: record.authenticatedAt,
|
|
429
|
+
expiresAt: record.expiresAt,
|
|
430
|
+
revokedAt: record.revokedAt,
|
|
431
|
+
createdAt: record.createdAt
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
function toSession(document) {
|
|
435
|
+
return {
|
|
436
|
+
id: document._id,
|
|
437
|
+
tokenHash: document.tokenHash,
|
|
438
|
+
userId: document.userId,
|
|
439
|
+
authenticatedAt: document.authenticatedAt,
|
|
440
|
+
expiresAt: document.expiresAt,
|
|
441
|
+
revokedAt: document.revokedAt,
|
|
442
|
+
createdAt: document.createdAt
|
|
443
|
+
};
|
|
444
|
+
}
|
|
445
|
+
function toTokenDocument(record) {
|
|
446
|
+
return {
|
|
447
|
+
_id: record.tokenHash,
|
|
448
|
+
kind: record.kind,
|
|
449
|
+
userId: record.userId,
|
|
450
|
+
address: record.address,
|
|
451
|
+
expiresAt: record.expiresAt,
|
|
452
|
+
spentAt: record.spentAt,
|
|
453
|
+
createdAt: record.createdAt
|
|
454
|
+
};
|
|
455
|
+
}
|
|
456
|
+
function toToken(document) {
|
|
457
|
+
return {
|
|
458
|
+
tokenHash: document._id,
|
|
459
|
+
kind: document.kind,
|
|
460
|
+
userId: document.userId,
|
|
461
|
+
address: document.address,
|
|
462
|
+
expiresAt: document.expiresAt,
|
|
463
|
+
spentAt: document.spentAt,
|
|
464
|
+
createdAt: document.createdAt
|
|
465
|
+
};
|
|
466
|
+
}
|
|
467
|
+
export {
|
|
468
|
+
createMongoRelations,
|
|
469
|
+
createMongoStores,
|
|
470
|
+
janusCollections,
|
|
471
|
+
relations,
|
|
472
|
+
sessions,
|
|
473
|
+
syncMongoRelations,
|
|
474
|
+
syncMongoStores,
|
|
475
|
+
tokens,
|
|
476
|
+
users
|
|
477
|
+
};
|
|
478
|
+
|
|
479
|
+
//# debugId=8620B43C28476F8F64756E2164756E21
|
|
480
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/collections.ts", "../src/relations.ts", "../src/translate.ts", "../src/stores.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"import { defineCollection } from '@nxgt/mongo';\nimport { z } from 'zod';\n\n/**\n * The three collections, as `@nxgt/mongo` definitions.\n *\n * **What the database holds is the port's record**, with the id under `_id`.\n * Nothing else is renamed and nothing is encoded, so a record comes back byte\n * for byte (rule 4) and a document read in a shell reads like the record in\n * the code.\n *\n * Defining them registers them with `@nxgt/mongo`, so an application that\n * deploys with `syncAll(db)` creates these three with the rest of its own.\n *\n * `@nxgt/mongo`'s optimistic lock and soft delete stay off. The port keeps its\n * own `version`, because it must tell `NOT_FOUND` from `VERSION_CONFLICT`,\n * which takes a second read the lock does not make; and the port has no\n * deletion to soften.\n */\n\n/** Anything JSON. The core validated it against the application's schema already. */\nconst jsonObject = z.record(z.string(), z.unknown());\nconst at = z.date();\n\nexport const users = defineCollection({\n\tname: 'users',\n\tschema: z.object({\n\t\t/** The UUIDv7 the core minted. A string, never an `ObjectId`: the store mints nothing. */\n\t\t_id: z.string(),\n\t\ttype: z.string(),\n\t\tschemaVersion: z.string(),\n\t\tactive: z.boolean(),\n\t\tfields: jsonObject,\n\t\tlogins: z.array(z.string()),\n\t\tpassword: z.object({ hash: z.string(), updatedAt: at }).nullable(),\n\t\temailVerifiedAt: at.nullable(),\n\t\tversion: z.int().nonnegative(),\n\t\tcreatedAt: at,\n\t\tupdatedAt: at,\n\t}),\n\tindexes: [\n\t\t/**\n\t\t * A login is unique **per type**: a scalar `type` and a flat array of\n\t\t * strings, so the multikey semantics are the plain ones, no collation is\n\t\t * needed, and the driver's `keyValue` names both the type and the login\n\t\t * that collided.\n\t\t */\n\t\t{\n\t\t\tkey: { type: 1, logins: 1 },\n\t\t\tname: 'loginUnique',\n\t\t\tunique: true,\n\t\t\t// Without it, two users holding no login at all collide: an empty\n\t\t\t// array is indexed as one missing value, and a unique index admits\n\t\t\t// one of those per type.\n\t\t\tpartialFilterExpression: { logins: { $type: 'string' } },\n\t\t},\n\t\t/** `listUsers` reads one type in `_id` order. */\n\t\t{ key: { type: 1, _id: 1 }, name: 'typeId' },\n\t],\n});\n\nexport const sessions = defineCollection({\n\tname: 'sessions',\n\tschema: z.object({\n\t\t_id: z.string(),\n\t\ttokenHash: z.string(),\n\t\tuserId: z.string(),\n\t\tauthenticatedAt: at,\n\t\texpiresAt: at,\n\t\trevokedAt: at.nullable(),\n\t\tcreatedAt: at,\n\t}),\n\tindexes: [\n\t\t{\n\t\t\tkey: { tokenHash: 1 },\n\t\t\tname: 'tokenHashUnique',\n\t\t\tunique: true,\n\t\t},\n\t\t{ key: { userId: 1 }, name: 'userId' },\n\t\t/**\n\t\t * Storage hygiene, **not** the expiry: MongoDB's TTL monitor runs every\n\t\t * sixty seconds, so a lapsed session stays readable for up to a minute.\n\t\t * The core compares `expiresAt` on every read, which is what expires it.\n\t\t */\n\t\t{\n\t\t\tkey: { expiresAt: 1 },\n\t\t\tname: 'expiry',\n\t\t\texpireAfterSeconds: 0,\n\t\t},\n\t],\n});\n\nexport const tokens = defineCollection({\n\tname: 'tokens',\n\tschema: z.object({\n\t\t/** The token's `sha256`. Unique by construction, so it is the key itself. */\n\t\t_id: z.string(),\n\t\tkind: z.enum(['verifyEmail', 'resetPassword']),\n\t\tuserId: z.string(),\n\t\taddress: z.string(),\n\t\texpiresAt: at,\n\t\tspentAt: at.nullable(),\n\t\tcreatedAt: at,\n\t}),\n\tindexes: [\n\t\t// The same hygiene as sessions'. A token the monitor dropped is answered\n\t\t// `null`, which the port allows: a lapsed token is refused either way.\n\t\t{ key: { expiresAt: 1 }, name: 'expiry', expireAfterSeconds: 0 },\n\t\t/** `deleteUserTokens` reads one user's tokens. */\n\t\t{ key: { userId: 1 }, name: 'userId' },\n\t],\n});\n\n/** The three definitions, in the order `syncMongoStores` syncs them. */\nexport const janusCollections = [users, sessions, tokens] as const;\n",
|
|
6
|
+
"import {\n\ttype Entity,\n\tisSubjectSet,\n\ttype RelationTuple,\n\ttype Subject,\n\ttype SubjectSet,\n} from '@nxgt/janus';\nimport type { RelationStore } from '@nxgt/janus/permissions';\nimport {\n\ttype DocumentOf,\n\tdefineCollection,\n\tgetCollection,\n\ttype SyncOptions,\n\ttype SyncReport,\n\tsyncCollections,\n} from '@nxgt/mongo';\nimport type { ClientSession, Db, Filter } from 'mongodb';\nimport { z } from 'zod';\nimport { run } from './translate';\n\n/**\n * The tuples, one document each, **keyed by the tuple itself**:\n * `{ _id: { object: { type, id }, relation, subject: { type, id, relation? } } }`.\n *\n * The key is the port's `RelationTuple`, as written, so uniqueness is the\n * `_id` index every collection has — a constraint, never a read — an insert\n * of a stored tuple is a retry, and a document read in a shell is the tuple\n * in the code. A subject set carries `relation`, an entity does not, so\n * `team:t1#member` and `team:t1` are two keys.\n *\n * The key is always built by {@link keyOf}, in one field order: MongoDB\n * compares embedded documents field by field, in order.\n */\nexport const relations = defineCollection({\n\tname: 'relations',\n\tschema: z.object({\n\t\t_id: z.object({\n\t\t\tobject: z.object({ type: z.string(), id: z.string() }),\n\t\t\trelation: z.string(),\n\t\t\tsubject: z.object({\n\t\t\t\ttype: z.string(),\n\t\t\t\tid: z.string(),\n\t\t\t\trelation: z.string().optional(),\n\t\t\t}),\n\t\t}),\n\t}),\n\tindexes: [\n\t\t/** One hop forwards: `findSubjectSets`, `findEntities`, and `deleteEntity` as the object. */\n\t\t{\n\t\t\tkey: { '_id.object.type': 1, '_id.object.id': 1, '_id.relation': 1 },\n\t\t\tname: 'objectRelation',\n\t\t},\n\t\t/**\n\t\t * The reverse index `list()` walks, in the order `findObjects` pages it:\n\t\t * every equality first, the object id last. Its prefix is `deleteEntity`\n\t\t * as a subject. An entity has no `subject.relation`, which the index\n\t\t * holds as `null` and `findObjects` asks for as `null`.\n\t\t */\n\t\t{\n\t\t\tkey: {\n\t\t\t\t'_id.subject.type': 1,\n\t\t\t\t'_id.subject.id': 1,\n\t\t\t\t'_id.subject.relation': 1,\n\t\t\t\t'_id.relation': 1,\n\t\t\t\t'_id.object.type': 1,\n\t\t\t\t'_id.object.id': 1,\n\t\t\t},\n\t\t\tname: 'subjectObjects',\n\t\t},\n\t],\n});\n\ntype RelationDocument = DocumentOf<typeof relations>;\ntype Key = RelationDocument['_id'];\n\n/**\n * The relation store `permissions()` takes, over one MongoDB database — and\n * the one `janus({ relations })` deletes a user's tuples from.\n *\n * ```ts\n * await syncMongoRelations(db); // a deployment step: creates the indexes\n * const access = permissions({ model, store: createMongoRelations(db) });\n * ```\n *\n * **A write of more than one tuple is a transaction**, which MongoDB runs on\n * a replica set only — as every production deployment is. `grant` and\n * `revoke` write one tuple, and run anywhere. The transaction is not retried\n * here: the driver's `withTransaction` would retry an outage for two minutes\n * before answering, and a write is idempotent, so the caller retries.\n */\nexport function createMongoRelations(db: Db): RelationStore {\n\tconst collection = getCollection(db, relations).raw;\n\tconst run$ = <T>(operation: string, body: () => Promise<T>) =>\n\t\trun('relations', operation, body);\n\n\t/** One hop from an object: its subject sets, or its entities. */\n\tconst holding = async (object: Entity, relation: string, sets: boolean) =>\n\t\t(\n\t\t\tawait collection\n\t\t\t\t.find({\n\t\t\t\t\t'_id.object.type': object.type,\n\t\t\t\t\t'_id.object.id': object.id,\n\t\t\t\t\t'_id.relation': relation,\n\t\t\t\t\t'_id.subject.relation': { $exists: sets },\n\t\t\t\t})\n\t\t\t\t.toArray()\n\t\t).map((document) => document._id.subject);\n\n\treturn {\n\t\twrite: ({ add = [], remove = [] }) =>\n\t\t\trun$('write', async () => {\n\t\t\t\tconst removals = remove.map(keyOf);\n\t\t\t\tconst additions = add.map(keyOf);\n\t\t\t\tconst apply = async (session?: ClientSession) => {\n\t\t\t\t\tif (removals.length > 0) {\n\t\t\t\t\t\tawait collection.deleteMany(\n\t\t\t\t\t\t\t{ _id: { $in: removals } },\n\t\t\t\t\t\t\tsession === undefined ? {} : { session },\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t\tif (additions.length > 0) {\n\t\t\t\t\t\t// A replace of a document by itself: a stored tuple is a no-op,\n\t\t\t\t\t\t// an absent one is inserted, and concurrent writers of one\n\t\t\t\t\t\t// tuple meet on the `_id` index, which the server retries.\n\t\t\t\t\t\tawait collection.bulkWrite(\n\t\t\t\t\t\t\tadditions.map((key) => ({\n\t\t\t\t\t\t\t\treplaceOne: {\n\t\t\t\t\t\t\t\t\tfilter: { _id: key },\n\t\t\t\t\t\t\t\t\treplacement: { _id: key },\n\t\t\t\t\t\t\t\t\tupsert: true,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t})),\n\t\t\t\t\t\t\tsession === undefined ? {} : { session },\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t};\n\n\t\t\t\tif (removals.length + additions.length <= 1) return apply();\n\n\t\t\t\tconst session = db.client.startSession();\n\t\t\t\ttry {\n\t\t\t\t\tsession.startTransaction();\n\t\t\t\t\tawait apply(session);\n\t\t\t\t\tawait session.commitTransaction();\n\t\t\t\t} finally {\n\t\t\t\t\t// Ends — and so aborts — a transaction that did not commit.\n\t\t\t\t\tawait session.endSession();\n\t\t\t\t}\n\t\t\t}),\n\n\t\thas: (tuple) =>\n\t\t\trun$(\n\t\t\t\t'has',\n\t\t\t\tasync () =>\n\t\t\t\t\t(await collection.findOne(\n\t\t\t\t\t\t{ _id: keyOf(tuple) },\n\t\t\t\t\t\t{ projection: { _id: 1 } },\n\t\t\t\t\t)) !== null,\n\t\t\t),\n\n\t\tfindSubjectSets: (object, relation) =>\n\t\t\trun$('findSubjectSets', async () =>\n\t\t\t\t(await holding(object, relation, true)).map(\n\t\t\t\t\t(subject): SubjectSet => ({\n\t\t\t\t\t\ttype: subject.type,\n\t\t\t\t\t\tid: subject.id,\n\t\t\t\t\t\trelation: subject.relation as string,\n\t\t\t\t\t}),\n\t\t\t\t),\n\t\t\t),\n\n\t\tfindEntities: (object, relation) =>\n\t\t\trun$('findEntities', async () =>\n\t\t\t\t(await holding(object, relation, false)).map(\n\t\t\t\t\t(subject): Entity => ({ type: subject.type, id: subject.id }),\n\t\t\t\t),\n\t\t\t),\n\n\t\tfindObjects: ({ type, relation, subject, after, limit }) =>\n\t\t\trun$('findObjects', async () => {\n\t\t\t\tconst filter: Filter<RelationDocument> = {\n\t\t\t\t\t'_id.subject.type': subject.type,\n\t\t\t\t\t'_id.subject.id': subject.id,\n\t\t\t\t\t// `null` matches the missing field: an entity, not a set.\n\t\t\t\t\t'_id.subject.relation': isSubjectSet(subject)\n\t\t\t\t\t\t? subject.relation\n\t\t\t\t\t\t: null,\n\t\t\t\t\t'_id.relation': relation,\n\t\t\t\t\t'_id.object.type': type,\n\t\t\t\t\t...(after === null ? {} : { '_id.object.id': { $gt: after } }),\n\t\t\t\t};\n\t\t\t\tconst found = await collection\n\t\t\t\t\t.find(filter, { projection: { '_id.object.id': 1 } })\n\t\t\t\t\t.sort({ '_id.object.id': 1 })\n\t\t\t\t\t.limit(limit + 1)\n\t\t\t\t\t.toArray();\n\t\t\t\tconst items = found\n\t\t\t\t\t.slice(0, limit)\n\t\t\t\t\t.map((document) => document._id.object.id);\n\t\t\t\tconst last = items.at(-1);\n\t\t\t\treturn {\n\t\t\t\t\titems,\n\t\t\t\t\tnextCursor: found.length > limit && last !== undefined ? last : null,\n\t\t\t\t};\n\t\t\t}),\n\n\t\tdeleteEntity: (entity) =>\n\t\t\trun$('deleteEntity', async () => {\n\t\t\t\tconst result = await collection.deleteMany({\n\t\t\t\t\t$or: [\n\t\t\t\t\t\t{ '_id.object.type': entity.type, '_id.object.id': entity.id },\n\t\t\t\t\t\t{ '_id.subject.type': entity.type, '_id.subject.id': entity.id },\n\t\t\t\t\t],\n\t\t\t\t});\n\t\t\t\treturn result.deletedCount;\n\t\t\t}),\n\t};\n}\n\n/**\n * Creates the collection and its indexes, and says what it changed. A\n * deployment step, as `syncMongoStores` is — and a separate one, because an\n * application that only authenticates keeps no tuples.\n */\nexport function syncMongoRelations(\n\tdb: Db,\n\toptions?: SyncOptions,\n): Promise<SyncReport[]> {\n\treturn syncCollections(db, [relations], options);\n}\n\n/** The `_id` of a tuple, in the one field order MongoDB compares. */\nfunction keyOf(tuple: RelationTuple): Key {\n\treturn {\n\t\tobject: { type: tuple.object.type, id: tuple.object.id },\n\t\trelation: tuple.relation,\n\t\tsubject: subjectKey(tuple.subject),\n\t};\n}\n\nfunction subjectKey(subject: Subject): Key['subject'] {\n\treturn isSubjectSet(subject)\n\t\t? { type: subject.type, id: subject.id, relation: subject.relation }\n\t\t: { type: subject.type, id: subject.id };\n}\n",
|
|
7
|
+
"import { JanusError, StoreConflict, StoreFailure } from '@nxgt/janus';\n\n/**\n * **The driver's errors, as the port's.** One file, and one default.\n *\n * | What happens | Becomes |\n * | --- | --- |\n * | A duplicate key on `(type, logins)` | `StoreConflict('login')`, naming the login and the type |\n * | A duplicate key on `_id` | nothing — the insert is a retry, and answers what is stored |\n * | A duplicate key on **any other index** | **`StoreFailure`** |\n * | A `$jsonSchema` validation failure (121) | **`StoreFailure`** |\n * | Anything else, absolutely | `StoreFailure`, with the driver error as `cause` |\n *\n * The two bold rows go against the obvious reading. A duplicate on an index\n * the adapter did not mean to collide on is an adapter bug: reporting it as\n * `LOGIN_TAKEN` would tell somebody their login is taken when it is\n * not. And the core has validated every record before it reaches a store, so a\n * validator refusing one means the adapter and the port disagree — never the\n * caller's fault, never a 400.\n *\n * **The default is a failure, not an absence.** That branch is what keeps the\n * port's first rule: nothing here answers `null` for an error.\n */\n\n/** Runs one port method, and makes every rejection one the port allows. */\nexport async function run<T>(\n\tslot: 'users' | 'sessions' | 'tokens' | 'relations',\n\toperation: string,\n\tbody: () => Promise<T>,\n): Promise<T> {\n\ttry {\n\t\treturn await body();\n\t} catch (error) {\n\t\t// Already the port's — a conflict, a NOT_FOUND after a read — or a\n\t\t// failure raised below with its own message.\n\t\tif (error instanceof JanusError) throw error;\n\t\tthrow new StoreFailure(`${slot}.${operation}: the store could not answer`, {\n\t\t\tslot,\n\t\t\toperation,\n\t\t\tcause: error,\n\t\t});\n\t}\n}\n\n/** The shape of the driver's `E11000`, as far as this file reads it. */\nexport interface DuplicateKey {\n\treadonly code: 11000;\n\treadonly keyPattern?: Readonly<Record<string, unknown>>;\n\treadonly keyValue?: Readonly<Record<string, unknown>>;\n}\n\nexport function isDuplicateKey(error: unknown): error is DuplicateKey {\n\treturn (\n\t\ttypeof error === 'object' &&\n\t\terror !== null &&\n\t\t(error as { code?: unknown }).code === 11000\n\t);\n}\n\n/**\n * Settles a write: what it answered when it landed, or the duplicate-key error\n * when an index refused it. **Any other rejection is rethrown**, so a caller\n * of this is only ever told \"written\" or \"duplicate\", and never \"nothing\".\n */\nexport function settle<T>(\n\twrite: Promise<T>,\n): Promise<\n\t| { readonly written: T }\n\t| { readonly written?: never; duplicate: DuplicateKey }\n> {\n\treturn write.then(\n\t\t(written) => ({ written }),\n\t\t(error: unknown) => {\n\t\t\tif (isDuplicateKey(error)) return { duplicate: error };\n\t\t\tthrow error;\n\t\t},\n\t);\n}\n\n/** A login one type holds. */\nexport interface TypedLogin {\n\treadonly type: string;\n\treadonly login: string;\n}\n\n/**\n * The login a duplicate names, when it is the login index that refused the\n * write — and `null` for every other index, which the caller then reports as\n * a failure.\n */\nexport function takenLogin(duplicate: DuplicateKey): TypedLogin | null {\n\tconst type = duplicate.keyValue?.type;\n\tconst login = duplicate.keyValue?.logins;\n\tif (typeof type !== 'string' || typeof login !== 'string') return null;\n\treturn { type, login };\n}\n\n/** The refusal the port promises for a taken login. */\nexport function loginTaken(\n\toperation: string,\n\t{ type, login }: TypedLogin,\n\tcause: unknown,\n): StoreConflict {\n\treturn new StoreConflict(\n\t\t'login',\n\t\t`${operation}: the login \"${login}\" is taken by another ${type}`,\n\t\t{ login, userType: type, operation, cause },\n\t);\n}\n\n/** A duplicate on an index the adapter never meant to collide on: a bug, reported as one. */\nexport function unexpectedDuplicate(\n\tslot: 'users' | 'sessions' | 'tokens',\n\toperation: string,\n\tduplicate: DuplicateKey,\n): StoreFailure {\n\treturn new StoreFailure(\n\t\t`${slot}.${operation}: a duplicate key on ${Object.keys(duplicate.keyPattern ?? {}).join(', ') || 'an unknown index'}, which this adapter never writes on purpose`,\n\t\t{ slot, operation, cause: duplicate },\n\t);\n}\n",
|
|
8
|
+
"import type {\n\tJanusStores,\n\tSessionRecord,\n\tSessionStore,\n\tTokenRecord,\n\tTokenStore,\n\tUserPatch,\n\tUserRecord,\n\tUserStore,\n} from '@nxgt/janus';\nimport { type Id, NotFoundError, StoreConflict } from '@nxgt/janus';\nimport {\n\ttype DocumentOf,\n\tgetCollection,\n\ttype SyncOptions,\n\ttype SyncReport,\n\tsyncCollections,\n} from '@nxgt/mongo';\nimport type { Db } from 'mongodb';\nimport { janusCollections, sessions, tokens, users } from './collections';\nimport {\n\tloginTaken,\n\trun,\n\tsettle,\n\ttakenLogin,\n\tunexpectedDuplicate,\n} from './translate';\n\ntype UserDocument = DocumentOf<typeof users>;\ntype SessionDocument = DocumentOf<typeof sessions>;\ntype TokenDocument = DocumentOf<typeof tokens>;\n\n/**\n * The three stores `janus()` takes, over one MongoDB database.\n *\n * ```ts\n * const db = (await connectMongo(process.env.MONGO_URI)).db;\n * await syncMongoStores(db); // a deployment step: creates the indexes\n * const auth = janus({ user, password: { login: 'email' }, store: createMongoStores(db), hasher });\n * ```\n *\n * **Reads go through `@nxgt/mongo`'s `find*`, never its `get*`.** `findById`\n * answers `undefined` for an absence, so an absence arrives as a *value*,\n * turned into `null` here, and everything that arrives as a rejection is a\n * failure. There is no path on which an absence and an outage travel the same\n * channel, so there is none on which a `catch` could confuse them.\n *\n * **Every conditional write is one driver call** on `raw`, the driver's own\n * collection: `@nxgt/mongo` names no update conditioned on anything but its\n * own lock, and the port needs `version`, `revokedAt` and `kind`.\n *\n * `sessions.deleteExpiredSessions` is not implemented: a TTL index drops lapsed\n * sessions, so `sessions.collectExpired()` answers `UNSUPPORTED` — which is\n * what the port asks of a store with its own expiry.\n */\nexport function createMongoStores(db: Db): JanusStores {\n\treturn {\n\t\tusers: userStore(db),\n\t\tsessions: sessionStore(db),\n\t\ttokens: tokenStore(db),\n\t};\n}\n\n/**\n * Creates the three collections, their validators and their indexes, and says\n * what it changed. Run it twice and the second run sends nothing.\n *\n * A deployment step, never a request-time one: it needs the `dbAdmin` role,\n * and the core never calls it (rule 6). An application that deploys with\n * `@nxgt/mongo`'s `syncAll(db)` already syncs these three with its own.\n */\nexport function syncMongoStores(\n\tdb: Db,\n\toptions?: SyncOptions,\n): Promise<SyncReport[]> {\n\treturn syncCollections(db, janusCollections, options);\n}\n\nfunction userStore(db: Db): UserStore {\n\tconst collection = getCollection(db, users);\n\tconst run$ = <T>(operation: string, body: () => Promise<T>) =>\n\t\trun('users', operation, body);\n\n\tconst find = async (id: string) => {\n\t\tconst found = await collection.findById(id);\n\t\treturn found === undefined ? null : toUser(found);\n\t};\n\n\t/** A duplicate on the login index as the port's conflict, and any other as a bug. */\n\tconst refuse = (\n\t\toperation: string,\n\t\tduplicate: Parameters<typeof takenLogin>[0],\n\t) => {\n\t\tconst taken = takenLogin(duplicate);\n\t\treturn taken === null\n\t\t\t? unexpectedDuplicate('users', operation, duplicate)\n\t\t\t: loginTaken(operation, taken, duplicate);\n\t};\n\n\treturn {\n\t\tinsertUser: (record) =>\n\t\t\trun$('insertUser', async () => {\n\t\t\t\tconst document = toUserDocument(record);\n\t\t\t\tconst outcome = await settle(collection.raw.insertOne(document));\n\t\t\t\tif (!('duplicate' in outcome)) return toUser(document);\n\n\t\t\t\t// A retry whose first attempt landed: whichever index reported it,\n\t\t\t\t// the user with this id is there, and is the answer. That includes\n\t\t\t\t// their own logins, which are not taken by them.\n\t\t\t\tconst stored = await find(record.id);\n\t\t\t\tif (stored !== null) return stored;\n\t\t\t\tthrow refuse('insertUser', outcome.duplicate);\n\t\t\t}),\n\n\t\tfindUser: (id) => run$('findUser', () => find(id)),\n\n\t\tfindUserByLogin: (type, login) =>\n\t\t\trun$('findUserByLogin', async () => {\n\t\t\t\tconst found = await collection.findFirst({ type, logins: login });\n\t\t\t\treturn found === undefined ? null : toUser(found);\n\t\t\t}),\n\n\t\tlistUsers: ({ type, after, limit }) =>\n\t\t\trun$('listUsers', async () => {\n\t\t\t\t// One more than the page, to know whether another follows.\n\t\t\t\tconst found = await collection.findMany({\n\t\t\t\t\tfilter: after === null ? { type } : { type, _id: { $gt: after } },\n\t\t\t\t\tsort: { _id: 1 },\n\t\t\t\t\tlimit: limit + 1,\n\t\t\t\t});\n\t\t\t\tconst items = found.slice(0, limit).map(toUser);\n\t\t\t\tconst last = items.at(-1);\n\n\t\t\t\treturn {\n\t\t\t\t\titems,\n\t\t\t\t\tnextCursor:\n\t\t\t\t\t\tfound.length > limit && last !== undefined ? last.id : null,\n\t\t\t\t};\n\t\t\t}),\n\n\t\tupdateUser: (id, patch, ifVersion) =>\n\t\t\trun$('updateUser', async () => {\n\t\t\t\tconst outcome = await settle(\n\t\t\t\t\tcollection.raw.findOneAndUpdate(\n\t\t\t\t\t\t{ _id: id, version: ifVersion },\n\t\t\t\t\t\t{ $set: toUserSet(patch), $inc: { version: 1 } },\n\t\t\t\t\t\t{ returnDocument: 'after' },\n\t\t\t\t\t),\n\t\t\t\t);\n\n\t\t\t\tif ('duplicate' in outcome) {\n\t\t\t\t\tthrow refuse('updateUser', outcome.duplicate);\n\t\t\t\t}\n\t\t\t\tif (outcome.written !== null) return toUser(outcome.written);\n\n\t\t\t\t// The write matched nothing, which alone cannot tell an unknown id\n\t\t\t\t// from a moved version: read again, and say which.\n\t\t\t\tconst stored = await find(id);\n\t\t\t\tif (stored === null) {\n\t\t\t\t\tthrow new NotFoundError('updateUser: no user has this id', {\n\t\t\t\t\t\tuserId: id,\n\t\t\t\t\t\toperation: 'updateUser',\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t\tthrow new StoreConflict(\n\t\t\t\t\t'version',\n\t\t\t\t\t`updateUser: expected version ${ifVersion}, found ${stored.version}`,\n\t\t\t\t\t{\n\t\t\t\t\t\tuserId: id,\n\t\t\t\t\t\texpectedVersion: ifVersion,\n\t\t\t\t\t\tactualVersion: stored.version,\n\t\t\t\t\t\toperation: 'updateUser',\n\t\t\t\t\t},\n\t\t\t\t);\n\t\t\t}),\n\n\t\tdeleteUser: (id) =>\n\t\t\trun$('deleteUser', async () => {\n\t\t\t\tconst result = await collection.raw.deleteOne({ _id: id });\n\t\t\t\treturn result.deletedCount === 1;\n\t\t\t}),\n\t};\n}\n\nfunction sessionStore(db: Db): SessionStore {\n\tconst collection = getCollection(db, sessions);\n\tconst run$ = <T>(operation: string, body: () => Promise<T>) =>\n\t\trun('sessions', operation, body);\n\n\treturn {\n\t\tinsertSession: (record) =>\n\t\t\trun$('insertSession', async () => {\n\t\t\t\tconst outcome = await settle(\n\t\t\t\t\tcollection.raw.insertOne(toSessionDocument(record)),\n\t\t\t\t);\n\t\t\t\tif (!('duplicate' in outcome)) return;\n\n\t\t\t\t// Idempotent under retry: a session with this id is already there.\n\t\t\t\tif ((await collection.findById(record.id)) !== undefined) return;\n\t\t\t\tthrow unexpectedDuplicate(\n\t\t\t\t\t'sessions',\n\t\t\t\t\t'insertSession',\n\t\t\t\t\toutcome.duplicate,\n\t\t\t\t);\n\t\t\t}),\n\n\t\tfindSessionByTokenHash: (tokenHash) =>\n\t\t\trun$('findSessionByTokenHash', async () => {\n\t\t\t\tconst found = await collection.findFirst({ tokenHash });\n\t\t\t\treturn found === undefined ? null : toSession(found);\n\t\t\t}),\n\n\t\textendSession: (id, expiresAt) =>\n\t\t\trun$('extendSession', async () => {\n\t\t\t\t// `revokedAt: null` in the filter: an extension racing a revocation\n\t\t\t\t// matches nothing, and never brings the session back.\n\t\t\t\tconst written = await collection.raw.findOneAndUpdate(\n\t\t\t\t\t{ _id: id, revokedAt: null },\n\t\t\t\t\t{ $set: { expiresAt } },\n\t\t\t\t\t{ returnDocument: 'after' },\n\t\t\t\t);\n\t\t\t\treturn written === null ? null : toSession(written);\n\t\t\t}),\n\n\t\trevokeSession: (id, at) =>\n\t\t\trun$('revokeSession', async () => {\n\t\t\t\t// A pipeline, so a session already revoked keeps its first\n\t\t\t\t// `revokedAt` and still counts as matched.\n\t\t\t\tconst result = await collection.raw.updateOne({ _id: id }, [\n\t\t\t\t\t{ $set: { revokedAt: { $ifNull: ['$revokedAt', at] } } },\n\t\t\t\t]);\n\t\t\t\treturn result.matchedCount === 1;\n\t\t\t}),\n\n\t\trevokeUserSessions: (userId, at, except) =>\n\t\t\trun$('revokeUserSessions', async () => {\n\t\t\t\tconst result = await collection.raw.updateMany(\n\t\t\t\t\texcept === undefined\n\t\t\t\t\t\t? { userId, revokedAt: null }\n\t\t\t\t\t\t: { userId, revokedAt: null, _id: { $ne: except } },\n\t\t\t\t\t{ $set: { revokedAt: at } },\n\t\t\t\t);\n\t\t\t\treturn result.modifiedCount;\n\t\t\t}),\n\n\t\tdeleteUserSessions: (userId) =>\n\t\t\trun$('deleteUserSessions', async () => {\n\t\t\t\tconst result = await collection.raw.deleteMany({ userId });\n\t\t\t\treturn result.deletedCount;\n\t\t\t}),\n\t};\n}\n\nfunction tokenStore(db: Db): TokenStore {\n\tconst collection = getCollection(db, tokens);\n\tconst run$ = <T>(operation: string, body: () => Promise<T>) =>\n\t\trun('tokens', operation, body);\n\n\treturn {\n\t\tinsertToken: (record) =>\n\t\t\trun$('insertToken', async () => {\n\t\t\t\t// `_id` is the hash, the only unique index: a duplicate is a retry.\n\t\t\t\tawait settle(collection.raw.insertOne(toTokenDocument(record)));\n\t\t\t}),\n\n\t\tconsumeToken: (tokenHash, kind, at) =>\n\t\t\trun$('consumeToken', async () => {\n\t\t\t\t// **One conditional write**, answering the document as it was\n\t\t\t\t// before it. The pipeline keeps a first `spentAt`, so exactly one\n\t\t\t\t// caller ever reads `spentAt: null`.\n\t\t\t\tconst before = await collection.raw.findOneAndUpdate(\n\t\t\t\t\t{ _id: tokenHash, kind },\n\t\t\t\t\t[{ $set: { spentAt: { $ifNull: ['$spentAt', at] } } }],\n\t\t\t\t\t{ returnDocument: 'before' },\n\t\t\t\t);\n\t\t\t\treturn before === null ? null : toToken(before);\n\t\t\t}),\n\n\t\tdeleteUserTokens: (userId) =>\n\t\t\trun$('deleteUserTokens', async () => {\n\t\t\t\tconst result = await collection.raw.deleteMany({ userId });\n\t\t\t\treturn result.deletedCount;\n\t\t\t}),\n\t};\n}\n\n// ─── Documents and records ────────────────────────────────────────────────\n//\n// Each record is rebuilt field by field, never spread from a document: a\n// spread would hand the core `_id` and the `id` getter `@nxgt/mongo` adds,\n// and the port's records are exactly what it declares.\n\nfunction toUserDocument(record: UserRecord): UserDocument {\n\treturn structuredClone({\n\t\t_id: record.id,\n\t\ttype: record.type,\n\t\tschemaVersion: record.schemaVersion,\n\t\tactive: record.active,\n\t\tfields: record.fields,\n\t\tlogins: [...record.logins],\n\t\tpassword: record.password,\n\t\temailVerifiedAt: record.emailVerifiedAt,\n\t\tversion: record.version,\n\t\tcreatedAt: record.createdAt,\n\t\tupdatedAt: record.updatedAt,\n\t});\n}\n\n/**\n * The fields a patch names, as one `$set`. A key present with `undefined` is\n * absent, as the port says — never an erasure. `password: null` is named, and\n * removes the password.\n */\nfunction toUserSet(patch: UserPatch): Record<string, unknown> {\n\tconst set: Record<string, unknown> = { updatedAt: patch.updatedAt };\n\n\tfor (const field of [\n\t\t'schemaVersion',\n\t\t'active',\n\t\t'fields',\n\t\t'logins',\n\t\t'password',\n\t\t'emailVerifiedAt',\n\t] as const) {\n\t\tif (patch[field] !== undefined) set[field] = patch[field];\n\t}\n\n\treturn structuredClone(set);\n}\n\nfunction toUser(document: UserDocument): UserRecord {\n\tconst password = document.password;\n\treturn {\n\t\tid: document._id as Id,\n\t\ttype: document.type,\n\t\tschemaVersion: document.schemaVersion,\n\t\tactive: document.active,\n\t\tfields: document.fields as UserRecord['fields'],\n\t\tlogins: [...document.logins],\n\t\tpassword:\n\t\t\tpassword === null\n\t\t\t\t? null\n\t\t\t\t: { hash: password.hash, updatedAt: password.updatedAt },\n\t\temailVerifiedAt: document.emailVerifiedAt,\n\t\tversion: document.version,\n\t\tcreatedAt: document.createdAt,\n\t\tupdatedAt: document.updatedAt,\n\t};\n}\n\nfunction toSessionDocument(record: SessionRecord): SessionDocument {\n\treturn {\n\t\t_id: record.id,\n\t\ttokenHash: record.tokenHash,\n\t\tuserId: record.userId,\n\t\tauthenticatedAt: record.authenticatedAt,\n\t\texpiresAt: record.expiresAt,\n\t\trevokedAt: record.revokedAt,\n\t\tcreatedAt: record.createdAt,\n\t};\n}\n\nfunction toSession(document: SessionDocument): SessionRecord {\n\treturn {\n\t\tid: document._id,\n\t\ttokenHash: document.tokenHash,\n\t\tuserId: document.userId as Id,\n\t\tauthenticatedAt: document.authenticatedAt,\n\t\texpiresAt: document.expiresAt,\n\t\trevokedAt: document.revokedAt,\n\t\tcreatedAt: document.createdAt,\n\t};\n}\n\nfunction toTokenDocument(record: TokenRecord): TokenDocument {\n\treturn {\n\t\t_id: record.tokenHash,\n\t\tkind: record.kind,\n\t\tuserId: record.userId,\n\t\taddress: record.address,\n\t\texpiresAt: record.expiresAt,\n\t\tspentAt: record.spentAt,\n\t\tcreatedAt: record.createdAt,\n\t};\n}\n\nfunction toToken(document: TokenDocument): TokenRecord {\n\treturn {\n\t\ttokenHash: document._id,\n\t\tkind: document.kind,\n\t\tuserId: document.userId as Id,\n\t\taddress: document.address,\n\t\texpiresAt: document.expiresAt,\n\t\tspentAt: document.spentAt,\n\t\tcreatedAt: document.createdAt,\n\t};\n}\n"
|
|
9
|
+
],
|
|
10
|
+
"mappings": ";AAAA;AACA;AAoBA,IAAM,aAAa,EAAE,OAAO,EAAE,OAAO,GAAG,EAAE,QAAQ,CAAC;AACnD,IAAM,KAAK,EAAE,KAAK;AAEX,IAAM,QAAQ,iBAAiB;AAAA,EACrC,MAAM;AAAA,EACN,QAAQ,EAAE,OAAO;AAAA,IAEhB,KAAK,EAAE,OAAO;AAAA,IACd,MAAM,EAAE,OAAO;AAAA,IACf,eAAe,EAAE,OAAO;AAAA,IACxB,QAAQ,EAAE,QAAQ;A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AClB,QAAQ,OAAO;AAAA,IACf,iBAAiB,OAAO;AAAA,IACxB,WAAW,OAAO;AAAA,IAClB,WAAW,OAAO;AAAA,IAClB,WAAW,OAAO;AAAA,EACnB;AAAA;AAGD,SAAS,SAAS,CAAC,UAA0C;AAAA,EAC5D,OAAO;AAAA,IACN,IAAI,SAAS;AAAA,IACb,WAAW,SAAS;AAAA,IACpB,QAAQ,SAAS;AAAA,IACjB,iBAAiB,SAAS;AAAA,IAC1B,WAAW,SAAS;AAAA,IACpB,WAAW,SAAS;AAAA,IACpB,WAAW,SAAS;AAAA,EACrB;AAAA;AAGD,SAAS,eAAe,CAAC,QAAoC;AAAA,EAC5D,OAAO;AAAA,IACN,KAAK,OAAO;AAAA,IACZ,MAAM,OAAO;AAAA,IACb,QAAQ,OAAO;AAAA,IACf,SAAS,OAAO;AAAA,IAChB,WAAW,OAAO;AAAA,IAClB,SAAS,OAAO;AAAA,IAChB,WAAW,OAAO;AAAA,EACnB;AAAA;AAGD,SAAS,OAAO,CAAC,UAAsC;AAAA,EACtD,OAAO;AAAA,IACN,WAAW,SAAS;AAAA,IACpB,MAAM,SAAS;AAAA,IACf,QAAQ,SAAS;AAAA,IACjB,SAAS,SAAS;AAAA,IAClB,WAAW,SAAS;AAAA,IACpB,SAAS,SAAS;AAAA,IAClB,WAAW,SAAS;AAAA,EACrB;AAAA;",
|
|
11
|
+
"debugId": "8620B43C28476F8F64756E2164756E21",
|
|
12
|
+
"names": []
|
|
13
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { RelationStore } from '@nxgt/janus/permissions';
|
|
2
|
+
import { type SyncOptions, type SyncReport } from '@nxgt/mongo';
|
|
3
|
+
import type { Db } from 'mongodb';
|
|
4
|
+
import { z } from 'zod';
|
|
5
|
+
/**
|
|
6
|
+
* The tuples, one document each, **keyed by the tuple itself**:
|
|
7
|
+
* `{ _id: { object: { type, id }, relation, subject: { type, id, relation? } } }`.
|
|
8
|
+
*
|
|
9
|
+
* The key is the port's `RelationTuple`, as written, so uniqueness is the
|
|
10
|
+
* `_id` index every collection has — a constraint, never a read — an insert
|
|
11
|
+
* of a stored tuple is a retry, and a document read in a shell is the tuple
|
|
12
|
+
* in the code. A subject set carries `relation`, an entity does not, so
|
|
13
|
+
* `team:t1#member` and `team:t1` are two keys.
|
|
14
|
+
*
|
|
15
|
+
* The key is always built by {@link keyOf}, in one field order: MongoDB
|
|
16
|
+
* compares embedded documents field by field, in order.
|
|
17
|
+
*/
|
|
18
|
+
export declare const relations: import("@nxgt/mongo").CollectionDefinition<import("@nxgt/mongo").StampedSchema<{
|
|
19
|
+
_id: z.ZodObject<{
|
|
20
|
+
object: z.ZodObject<{
|
|
21
|
+
type: z.ZodString;
|
|
22
|
+
id: z.ZodString;
|
|
23
|
+
}, z.core.$strip>;
|
|
24
|
+
relation: z.ZodString;
|
|
25
|
+
subject: z.ZodObject<{
|
|
26
|
+
type: z.ZodString;
|
|
27
|
+
id: z.ZodString;
|
|
28
|
+
relation: z.ZodOptional<z.ZodString>;
|
|
29
|
+
}, z.core.$strip>;
|
|
30
|
+
}, z.core.$strip>;
|
|
31
|
+
}, false, false, false, false>, import("@nxgt/mongo").StampNamesOf<false, false, false, false>>;
|
|
32
|
+
/**
|
|
33
|
+
* The relation store `permissions()` takes, over one MongoDB database — and
|
|
34
|
+
* the one `janus({ relations })` deletes a user's tuples from.
|
|
35
|
+
*
|
|
36
|
+
* ```ts
|
|
37
|
+
* await syncMongoRelations(db); // a deployment step: creates the indexes
|
|
38
|
+
* const access = permissions({ model, store: createMongoRelations(db) });
|
|
39
|
+
* ```
|
|
40
|
+
*
|
|
41
|
+
* **A write of more than one tuple is a transaction**, which MongoDB runs on
|
|
42
|
+
* a replica set only — as every production deployment is. `grant` and
|
|
43
|
+
* `revoke` write one tuple, and run anywhere. The transaction is not retried
|
|
44
|
+
* here: the driver's `withTransaction` would retry an outage for two minutes
|
|
45
|
+
* before answering, and a write is idempotent, so the caller retries.
|
|
46
|
+
*/
|
|
47
|
+
export declare function createMongoRelations(db: Db): RelationStore;
|
|
48
|
+
/**
|
|
49
|
+
* Creates the collection and its indexes, and says what it changed. A
|
|
50
|
+
* deployment step, as `syncMongoStores` is — and a separate one, because an
|
|
51
|
+
* application that only authenticates keeps no tuples.
|
|
52
|
+
*/
|
|
53
|
+
export declare function syncMongoRelations(db: Db, options?: SyncOptions): Promise<SyncReport[]>;
|
|
54
|
+
//# sourceMappingURL=relations.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"relations.d.ts","sourceRoot":"","sources":["../src/relations.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AAC7D,OAAO,EAIN,KAAK,WAAW,EAChB,KAAK,UAAU,EAEf,MAAM,aAAa,CAAC;AACrB,OAAO,KAAK,EAAiB,EAAE,EAAU,MAAM,SAAS,CAAC;AACzD,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAGxB;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,SAAS;;;;;;;;;;;;;+FAqCpB,CAAC;AAKH;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,oBAAoB,CAAC,EAAE,EAAE,EAAE,GAAG,aAAa,CA+H1D;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CACjC,EAAE,EAAE,EAAE,EACN,OAAO,CAAC,EAAE,WAAW,GACnB,OAAO,CAAC,UAAU,EAAE,CAAC,CAEvB"}
|