easy-ping 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.
Files changed (115) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +374 -0
  3. package/dist/adapter-f0OR2DhY.d.cts +200 -0
  4. package/dist/adapter-f0OR2DhY.d.ts +200 -0
  5. package/dist/adapters/drizzle.cjs +440 -0
  6. package/dist/adapters/drizzle.cjs.map +1 -0
  7. package/dist/adapters/drizzle.d.cts +991 -0
  8. package/dist/adapters/drizzle.d.ts +991 -0
  9. package/dist/adapters/drizzle.js +409 -0
  10. package/dist/adapters/drizzle.js.map +1 -0
  11. package/dist/adapters/mongodb.cjs +458 -0
  12. package/dist/adapters/mongodb.cjs.map +1 -0
  13. package/dist/adapters/mongodb.d.cts +32 -0
  14. package/dist/adapters/mongodb.d.ts +32 -0
  15. package/dist/adapters/mongodb.js +376 -0
  16. package/dist/adapters/mongodb.js.map +1 -0
  17. package/dist/browser.cjs +96 -0
  18. package/dist/browser.cjs.map +1 -0
  19. package/dist/browser.d.cts +45 -0
  20. package/dist/browser.d.ts +45 -0
  21. package/dist/browser.js +89 -0
  22. package/dist/browser.js.map +1 -0
  23. package/dist/chunk-BV3U542P.js +11 -0
  24. package/dist/chunk-BV3U542P.js.map +1 -0
  25. package/dist/chunk-NYDVNMKG.js +62 -0
  26. package/dist/chunk-NYDVNMKG.js.map +1 -0
  27. package/dist/chunk-OZSOIX23.js +120 -0
  28. package/dist/chunk-OZSOIX23.js.map +1 -0
  29. package/dist/chunk-PTGHMCCG.js +23 -0
  30. package/dist/chunk-PTGHMCCG.js.map +1 -0
  31. package/dist/chunk-W3BMEPPQ.js +50 -0
  32. package/dist/chunk-W3BMEPPQ.js.map +1 -0
  33. package/dist/client.cjs +160 -0
  34. package/dist/client.cjs.map +1 -0
  35. package/dist/client.d.cts +42 -0
  36. package/dist/client.d.ts +42 -0
  37. package/dist/client.js +158 -0
  38. package/dist/client.js.map +1 -0
  39. package/dist/index.cjs +920 -0
  40. package/dist/index.cjs.map +1 -0
  41. package/dist/index.d.cts +203 -0
  42. package/dist/index.d.ts +203 -0
  43. package/dist/index.js +723 -0
  44. package/dist/index.js.map +1 -0
  45. package/dist/node.cjs +49 -0
  46. package/dist/node.cjs.map +1 -0
  47. package/dist/node.d.cts +8 -0
  48. package/dist/node.d.ts +8 -0
  49. package/dist/node.js +46 -0
  50. package/dist/node.js.map +1 -0
  51. package/dist/plugin-Bqj9xAOS.d.cts +217 -0
  52. package/dist/plugin-bTJKmnlq.d.ts +217 -0
  53. package/dist/plugins/digests.cjs +206 -0
  54. package/dist/plugins/digests.cjs.map +1 -0
  55. package/dist/plugins/digests.d.cts +68 -0
  56. package/dist/plugins/digests.d.ts +68 -0
  57. package/dist/plugins/digests.js +201 -0
  58. package/dist/plugins/digests.js.map +1 -0
  59. package/dist/plugins/preferences-client.cjs +23 -0
  60. package/dist/plugins/preferences-client.cjs.map +1 -0
  61. package/dist/plugins/preferences-client.d.cts +29 -0
  62. package/dist/plugins/preferences-client.d.ts +29 -0
  63. package/dist/plugins/preferences-client.js +18 -0
  64. package/dist/plugins/preferences-client.js.map +1 -0
  65. package/dist/plugins/preferences-react.cjs +125 -0
  66. package/dist/plugins/preferences-react.cjs.map +1 -0
  67. package/dist/plugins/preferences-react.d.cts +40 -0
  68. package/dist/plugins/preferences-react.d.ts +40 -0
  69. package/dist/plugins/preferences-react.js +101 -0
  70. package/dist/plugins/preferences-react.js.map +1 -0
  71. package/dist/plugins/preferences.cjs +147 -0
  72. package/dist/plugins/preferences.cjs.map +1 -0
  73. package/dist/plugins/preferences.d.cts +22 -0
  74. package/dist/plugins/preferences.d.ts +22 -0
  75. package/dist/plugins/preferences.js +119 -0
  76. package/dist/plugins/preferences.js.map +1 -0
  77. package/dist/plugins/push.cjs +146 -0
  78. package/dist/plugins/push.cjs.map +1 -0
  79. package/dist/plugins/push.d.cts +60 -0
  80. package/dist/plugins/push.d.ts +60 -0
  81. package/dist/plugins/push.js +144 -0
  82. package/dist/plugins/push.js.map +1 -0
  83. package/dist/provider-CA45WVqt.d.cts +23 -0
  84. package/dist/provider-CA45WVqt.d.ts +23 -0
  85. package/dist/providers/resend.cjs +69 -0
  86. package/dist/providers/resend.cjs.map +1 -0
  87. package/dist/providers/resend.d.cts +20 -0
  88. package/dist/providers/resend.d.ts +20 -0
  89. package/dist/providers/resend.js +66 -0
  90. package/dist/providers/resend.js.map +1 -0
  91. package/dist/providers/web-push.cjs +211 -0
  92. package/dist/providers/web-push.cjs.map +1 -0
  93. package/dist/providers/web-push.d.cts +33 -0
  94. package/dist/providers/web-push.d.ts +33 -0
  95. package/dist/providers/web-push.js +193 -0
  96. package/dist/providers/web-push.js.map +1 -0
  97. package/dist/react.cjs +218 -0
  98. package/dist/react.cjs.map +1 -0
  99. package/dist/react.d.cts +57 -0
  100. package/dist/react.d.ts +57 -0
  101. package/dist/react.js +193 -0
  102. package/dist/react.js.map +1 -0
  103. package/dist/schema.cjs +263 -0
  104. package/dist/schema.cjs.map +1 -0
  105. package/dist/schema.d.cts +180 -0
  106. package/dist/schema.d.ts +180 -0
  107. package/dist/schema.js +200 -0
  108. package/dist/schema.js.map +1 -0
  109. package/dist/testing.cjs +342 -0
  110. package/dist/testing.cjs.map +1 -0
  111. package/dist/testing.d.cts +38 -0
  112. package/dist/testing.d.ts +38 -0
  113. package/dist/testing.js +340 -0
  114. package/dist/testing.js.map +1 -0
  115. package/package.json +168 -0
@@ -0,0 +1,458 @@
1
+ 'use strict';
2
+
3
+ // src/core/base64url.ts
4
+ function encodeBase64Url(input) {
5
+ const bytes = typeof input === "string" ? new TextEncoder().encode(input) : input;
6
+ let binary = "";
7
+ for (const byte of bytes) binary += String.fromCharCode(byte);
8
+ return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
9
+ }
10
+ function decodeBase64UrlBytes(value) {
11
+ try {
12
+ const binary = atob(value.replace(/-/g, "+").replace(/_/g, "/"));
13
+ return Uint8Array.from(binary, (char) => char.charCodeAt(0));
14
+ } catch {
15
+ return null;
16
+ }
17
+ }
18
+ function decodeBase64Url(value) {
19
+ const bytes = decodeBase64UrlBytes(value);
20
+ return bytes ? new TextDecoder().decode(bytes) : null;
21
+ }
22
+
23
+ // src/schema/declaration.ts
24
+ var coreSchema = {
25
+ notification: {
26
+ tableName: "notification",
27
+ fields: {
28
+ id: { type: "string", required: true },
29
+ userId: { type: "string", required: true },
30
+ type: { type: "string", required: true },
31
+ payload: { type: "json", required: true },
32
+ actorId: { type: "string" },
33
+ groupKey: { type: "string" },
34
+ dedupeKey: { type: "string" },
35
+ seenAt: { type: "date" },
36
+ readAt: { type: "date" },
37
+ archivedAt: { type: "date" },
38
+ createdAt: { type: "date", required: true, defaultNow: true }
39
+ },
40
+ primaryKey: ["id"],
41
+ indexes: [
42
+ { on: ["userId", "createdAt"], name: "notification_feed_idx" },
43
+ { on: ["userId", "groupKey"], name: "notification_group_idx" },
44
+ { on: ["userId", "dedupeKey"], unique: true, name: "notification_dedupe_idx" }
45
+ ]
46
+ },
47
+ notificationDelivery: {
48
+ tableName: "notification_delivery",
49
+ fields: {
50
+ id: { type: "string", required: true },
51
+ notificationId: { type: "string", required: true },
52
+ channel: { type: "string", required: true },
53
+ status: { type: "string", required: true, default: "pending" },
54
+ attempts: { type: "number", required: true, default: 0 },
55
+ maxAttempts: { type: "number", required: true, default: 5 },
56
+ notBefore: { type: "date", required: true, defaultNow: true },
57
+ claimedAt: { type: "date" },
58
+ claimedBy: { type: "string" },
59
+ lastError: { type: "string" },
60
+ updatedAt: { type: "date", required: true, defaultNow: true }
61
+ },
62
+ primaryKey: ["id"],
63
+ indexes: [
64
+ { on: ["status", "notBefore"], name: "delivery_claim_idx" },
65
+ { on: ["notificationId"], name: "delivery_notification_idx" }
66
+ ]
67
+ },
68
+ notificationPreference: {
69
+ tableName: "notification_preference",
70
+ fields: {
71
+ userId: { type: "string", required: true },
72
+ type: { type: "string", required: true },
73
+ channel: { type: "string", required: true },
74
+ enabled: { type: "boolean", required: true, default: true },
75
+ frequency: { type: "string", required: true, default: "instant" }
76
+ },
77
+ primaryKey: ["userId", "type", "channel"]
78
+ }
79
+ };
80
+
81
+ // src/core/store.ts
82
+ var OPERATORS = ["in", "lt", "lte", "gt", "gte", "not"];
83
+ var isOperator = (value) => typeof value === "object" && value !== null && !(value instanceof Date) && OPERATORS.some((op) => op in value);
84
+
85
+ // src/adapters/mongodb/adapter.ts
86
+ var str = (value) => String(value);
87
+ var nullableStr = (value) => value == null ? null : String(value);
88
+ var num = (value) => Number(value);
89
+ var date = (value) => value instanceof Date ? value : new Date(String(value));
90
+ var nullableDate = (value) => value == null ? null : value instanceof Date ? value : new Date(String(value));
91
+ var encodeCursor = (createdAt, id) => encodeBase64Url(`${createdAt.toISOString()}|${id}`);
92
+ function decodeCursor(cursor) {
93
+ const decoded = decodeBase64Url(cursor);
94
+ if (!decoded) return null;
95
+ const [iso, id] = decoded.split("|");
96
+ if (!iso || !id) return null;
97
+ const createdAt = new Date(iso);
98
+ return Number.isNaN(createdAt.getTime()) ? null : { createdAt, id };
99
+ }
100
+ function toNotification(doc) {
101
+ return {
102
+ id: str(doc._id),
103
+ userId: str(doc.userId),
104
+ type: str(doc.type),
105
+ payload: doc.payload,
106
+ actorId: nullableStr(doc.actorId),
107
+ groupKey: nullableStr(doc.groupKey),
108
+ dedupeKey: nullableStr(doc.dedupeKey),
109
+ seenAt: nullableDate(doc.seenAt),
110
+ readAt: nullableDate(doc.readAt),
111
+ archivedAt: nullableDate(doc.archivedAt),
112
+ createdAt: date(doc.createdAt)
113
+ };
114
+ }
115
+ function toDelivery(doc) {
116
+ return {
117
+ id: str(doc._id),
118
+ notificationId: str(doc.notificationId),
119
+ channel: str(doc.channel),
120
+ status: str(doc.status),
121
+ attempts: num(doc.attempts),
122
+ maxAttempts: num(doc.maxAttempts),
123
+ notBefore: date(doc.notBefore),
124
+ claimedAt: nullableDate(doc.claimedAt),
125
+ claimedBy: nullableStr(doc.claimedBy),
126
+ lastError: nullableStr(doc.lastError),
127
+ updatedAt: date(doc.updatedAt)
128
+ };
129
+ }
130
+ var toMongoField = (field) => field === "id" ? "_id" : field;
131
+ function toFilter(where) {
132
+ const filter = {};
133
+ for (const [field, condition] of Object.entries(where)) {
134
+ const key = toMongoField(field);
135
+ if (condition === null) {
136
+ filter[key] = null;
137
+ continue;
138
+ }
139
+ if (!isOperator(condition)) {
140
+ filter[key] = condition;
141
+ continue;
142
+ }
143
+ const operator = condition;
144
+ if ("in" in operator) filter[key] = { $in: operator.in };
145
+ else if ("lt" in operator) filter[key] = { $lt: operator.lt };
146
+ else if ("lte" in operator) filter[key] = { $lte: operator.lte };
147
+ else if ("gt" in operator) filter[key] = { $gt: operator.gt };
148
+ else if ("gte" in operator) filter[key] = { $gte: operator.gte };
149
+ else if ("not" in operator) {
150
+ filter[key] = { $ne: operator.not };
151
+ }
152
+ }
153
+ return filter;
154
+ }
155
+ var isDuplicateKey = (error) => {
156
+ const code = error?.code;
157
+ if (code === 11e3) return true;
158
+ const writeErrors = error?.writeErrors;
159
+ return Array.isArray(writeErrors) && writeErrors.some((entry) => entry?.code === 11e3);
160
+ };
161
+ function mongoAdapter(db, options = {}) {
162
+ const database = db;
163
+ const prefix = options.prefix ?? "";
164
+ const client = options.client;
165
+ const notifications = () => database.collection(`${prefix}notification`);
166
+ const deliveries = () => database.collection(`${prefix}notification_delivery`);
167
+ async function atomically(fn) {
168
+ if (!client) return fn();
169
+ const session = client.startSession();
170
+ try {
171
+ return await session.withTransaction(() => fn(session));
172
+ } finally {
173
+ await session.endSession();
174
+ }
175
+ }
176
+ return {
177
+ name: "mongodb",
178
+ // Documents keep the declared camelCase, and store objects natively.
179
+ naming: "preserve",
180
+ serializesJson: false,
181
+ async createNotifications(input) {
182
+ if (input.length === 0) return { created: [], deduped: [] };
183
+ const created = [];
184
+ const deduped = [];
185
+ for (const row of input) {
186
+ const now = /* @__PURE__ */ new Date();
187
+ try {
188
+ await atomically(async (session) => {
189
+ const options2 = session ? { session } : void 0;
190
+ await notifications().insertOne(
191
+ {
192
+ _id: row.id,
193
+ userId: row.userId,
194
+ type: row.type,
195
+ payload: row.payload ?? null,
196
+ actorId: row.actorId ?? null,
197
+ groupKey: row.groupKey ?? null,
198
+ dedupeKey: row.dedupeKey ?? null,
199
+ seenAt: null,
200
+ readAt: null,
201
+ archivedAt: null,
202
+ createdAt: now
203
+ },
204
+ options2
205
+ );
206
+ if (row.deliveries.length > 0) {
207
+ await deliveries().insertMany(
208
+ row.deliveries.map((delivery) => ({
209
+ _id: delivery.id,
210
+ notificationId: row.id,
211
+ channel: delivery.channel,
212
+ status: "pending",
213
+ attempts: 0,
214
+ maxAttempts: delivery.maxAttempts,
215
+ notBefore: delivery.notBefore,
216
+ claimedAt: null,
217
+ claimedBy: null,
218
+ lastError: null,
219
+ updatedAt: now
220
+ })),
221
+ options2
222
+ );
223
+ }
224
+ });
225
+ created.push(row.id);
226
+ } catch (error) {
227
+ if (!isDuplicateKey(error)) throw error;
228
+ deduped.push(row.id);
229
+ }
230
+ }
231
+ return { created, deduped };
232
+ },
233
+ async claimPendingDeliveries(args) {
234
+ const now = args.now ?? /* @__PURE__ */ new Date();
235
+ const staleBefore = new Date(now.getTime() - args.leaseMs);
236
+ const eligible = {
237
+ $and: [
238
+ {
239
+ $or: [{ status: "pending" }, { status: "claimed", claimedAt: { $lt: staleBefore } }]
240
+ },
241
+ { notBefore: { $lte: now } },
242
+ { $expr: { $lt: ["$attempts", "$maxAttempts"] } }
243
+ ]
244
+ };
245
+ if (args.channels && args.channels.length > 0) {
246
+ eligible.$and.push({ channel: { $in: [...args.channels] } });
247
+ }
248
+ if (args.ids && args.ids.length > 0) {
249
+ eligible.$and.push({ _id: { $in: [...args.ids] } });
250
+ }
251
+ const claimed = [];
252
+ for (let taken = 0; taken < args.limit; taken += 1) {
253
+ const doc = await deliveries().findOneAndUpdate(
254
+ eligible,
255
+ { $set: { status: "claimed", claimedAt: now, claimedBy: args.claimToken } },
256
+ { sort: { notBefore: 1, _id: 1 }, returnDocument: "after" }
257
+ );
258
+ if (!doc) break;
259
+ claimed.push(doc);
260
+ }
261
+ if (claimed.length === 0) return [];
262
+ const parents = await notifications().find({ _id: { $in: claimed.map((doc) => doc.notificationId) } }).toArray();
263
+ const byId = new Map(parents.map((doc) => [str(doc._id), doc]));
264
+ return claimed.flatMap((doc) => {
265
+ const parent = byId.get(str(doc.notificationId));
266
+ if (!parent) return [];
267
+ return [
268
+ {
269
+ id: str(doc._id),
270
+ notificationId: str(doc.notificationId),
271
+ channel: str(doc.channel),
272
+ attempts: num(doc.attempts),
273
+ maxAttempts: num(doc.maxAttempts),
274
+ notification: {
275
+ userId: str(parent.userId),
276
+ type: str(parent.type),
277
+ payload: parent.payload,
278
+ actorId: nullableStr(parent.actorId)
279
+ }
280
+ }
281
+ ];
282
+ });
283
+ },
284
+ async releaseDeliveries(releases) {
285
+ if (releases.length === 0) return;
286
+ const now = /* @__PURE__ */ new Date();
287
+ for (const { id, outcome, nextAttemptAt } of releases) {
288
+ const failed = outcome.result === "failed";
289
+ const retryable = failed && outcome.retryable;
290
+ await deliveries().updateOne({ _id: id }, [
291
+ {
292
+ $set: {
293
+ attempts: failed ? { $add: ["$attempts", 1] } : "$attempts",
294
+ status: failed ? {
295
+ $cond: [
296
+ { $and: [retryable, { $lt: [{ $add: ["$attempts", 1] }, "$maxAttempts"] }] },
297
+ "pending",
298
+ "failed"
299
+ ]
300
+ } : "sent",
301
+ // $literal: an error message starting with "$" would otherwise
302
+ // be read as a field path.
303
+ lastError: failed ? { $literal: outcome.error.slice(0, 2e3) } : null,
304
+ notBefore: nextAttemptAt ?? "$notBefore",
305
+ // Written unconditionally: if the lease expired and another
306
+ // worker re-sent, the duplicate already happened and the true
307
+ // terminal state beats leaving the row wedged in "claimed".
308
+ claimedAt: null,
309
+ claimedBy: null,
310
+ updatedAt: now
311
+ }
312
+ }
313
+ ]);
314
+ }
315
+ },
316
+ async listNotifications(query) {
317
+ const cursor = query.cursor ? decodeCursor(query.cursor) : null;
318
+ const filter = { userId: query.userId, archivedAt: null };
319
+ if (query.unreadOnly) filter.readAt = null;
320
+ if (cursor) {
321
+ filter.$or = [
322
+ { createdAt: { $lt: cursor.createdAt } },
323
+ { createdAt: cursor.createdAt, _id: { $lt: cursor.id } }
324
+ ];
325
+ }
326
+ const docs = await notifications().find(filter, { sort: { createdAt: -1, _id: -1 }, limit: query.limit + 1 }).toArray();
327
+ const page = docs.slice(0, query.limit).map(toNotification);
328
+ const last = page.at(-1);
329
+ return {
330
+ notifications: page,
331
+ nextCursor: docs.length > query.limit && last ? encodeCursor(last.createdAt, last.id) : null
332
+ };
333
+ },
334
+ async countUnseen(userId) {
335
+ return notifications().countDocuments({ userId, seenAt: null, archivedAt: null });
336
+ },
337
+ async markSeen(userId, before) {
338
+ await notifications().updateMany(
339
+ { userId, seenAt: null, createdAt: { $lte: before } },
340
+ { $set: { seenAt: /* @__PURE__ */ new Date() } }
341
+ );
342
+ },
343
+ async markRead(userId, notificationIds) {
344
+ if (notificationIds.length === 0) return 0;
345
+ const filter = { userId, _id: { $in: [...notificationIds] } };
346
+ await notifications().updateMany(
347
+ { ...filter, readAt: null },
348
+ {
349
+ $set: { readAt: /* @__PURE__ */ new Date() }
350
+ }
351
+ );
352
+ return notifications().countDocuments(filter);
353
+ },
354
+ async markAllRead(userId) {
355
+ const result = await notifications().updateMany(
356
+ { userId, readAt: null },
357
+ { $set: { readAt: /* @__PURE__ */ new Date() } }
358
+ );
359
+ return result.modifiedCount;
360
+ },
361
+ async getFailedDeliveries(args) {
362
+ const docs = await deliveries().find(
363
+ { status: "failed", updatedAt: { $gte: args.since } },
364
+ { sort: { updatedAt: -1 }, limit: args.limit }
365
+ ).toArray();
366
+ return docs.map(toDelivery);
367
+ },
368
+ async queryTable(table, where, options2) {
369
+ const find = {};
370
+ if (options2.limit) find.limit = options2.limit;
371
+ if (options2.orderBy) {
372
+ const direction = options2.orderBy.direction === "desc" ? -1 : 1;
373
+ find.sort = { [toMongoField(options2.orderBy.field)]: direction };
374
+ }
375
+ const docs = await database.collection(table).find(toFilter(where), find).toArray();
376
+ return docs.map(({ _id, ...rest }) => _id === void 0 ? rest : { id: _id, ...rest });
377
+ },
378
+ async insertRows(table, rows, onConflict) {
379
+ if (rows.length === 0) return 0;
380
+ const documents = rows.map(
381
+ ({ id, ...rest }) => id === void 0 ? { ...rest } : { _id: id, ...rest }
382
+ );
383
+ if (!onConflict || onConflict.length === 0) {
384
+ await database.collection(table).insertMany(documents);
385
+ return documents.length;
386
+ }
387
+ let written = 0;
388
+ for (const document of documents) {
389
+ const key = {};
390
+ for (const field of onConflict) key[toMongoField(field)] = document[toMongoField(field)];
391
+ const { _id, ...updatable } = document;
392
+ const update = { $set: updatable };
393
+ if (_id !== void 0) update.$setOnInsert = { _id };
394
+ const result = await database.collection(table).updateOne(key, update, { upsert: true });
395
+ written += result.matchedCount + (result.upsertedCount ?? 0);
396
+ }
397
+ return written;
398
+ },
399
+ async updateRows(table, where, set) {
400
+ const assignments = {};
401
+ for (const [field, value] of Object.entries(set)) assignments[toMongoField(field)] = value;
402
+ if (Object.keys(assignments).length === 0) return 0;
403
+ const result = await database.collection(table).updateMany(toFilter(where), { $set: assignments });
404
+ return result.matchedCount;
405
+ },
406
+ async deleteRows(table, where) {
407
+ const result = await database.collection(table).deleteMany(toFilter(where));
408
+ return result.deletedCount;
409
+ }
410
+ };
411
+ }
412
+
413
+ // src/adapters/mongodb/indexes.ts
414
+ var BSON_TYPE = {
415
+ string: "string",
416
+ number: "number",
417
+ boolean: "bool",
418
+ date: "date",
419
+ json: "object"
420
+ };
421
+ async function createPluginIndexes(db, schema, prefix = "") {
422
+ const database = db;
423
+ for (const table of Object.values(schema)) {
424
+ const collection = database.collection(prefix + table.tableName);
425
+ if (table.primaryKey && table.primaryKey.length > 0 && table.primaryKey[0] !== "id") {
426
+ const spec = {};
427
+ for (const field of table.primaryKey) spec[field] = 1;
428
+ await collection.createIndex(spec, {
429
+ name: `${table.tableName}_pk`,
430
+ unique: true
431
+ });
432
+ }
433
+ for (const declaration of table.indexes ?? []) {
434
+ const spec = {};
435
+ for (const field of declaration.on) spec[field] = 1;
436
+ const options = {
437
+ name: prefix + (declaration.name ?? `${table.tableName}_${declaration.on.join("_")}_idx`)
438
+ };
439
+ if (declaration.unique) {
440
+ options.unique = true;
441
+ const partial = {};
442
+ for (const field of declaration.on) {
443
+ const spec2 = table.fields[field];
444
+ if (spec2 && !spec2.required) partial[field] = { $type: BSON_TYPE[spec2.type] };
445
+ }
446
+ if (Object.keys(partial).length > 0) options.partialFilterExpression = partial;
447
+ }
448
+ await collection.createIndex(spec, options);
449
+ }
450
+ }
451
+ }
452
+ var createMongoIndexes = (db, options = {}) => createPluginIndexes(db, coreSchema, options.prefix ?? "");
453
+
454
+ exports.createMongoIndexes = createMongoIndexes;
455
+ exports.createPluginIndexes = createPluginIndexes;
456
+ exports.mongoAdapter = mongoAdapter;
457
+ //# sourceMappingURL=mongodb.cjs.map
458
+ //# sourceMappingURL=mongodb.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/core/base64url.ts","../../src/schema/declaration.ts","../../src/core/store.ts","../../src/adapters/mongodb/adapter.ts","../../src/adapters/mongodb/indexes.ts"],"names":["options","spec"],"mappings":";;;AAEO,SAAS,gBAAgB,KAAA,EAAoC;AAClE,EAAA,MAAM,KAAA,GAAQ,OAAO,KAAA,KAAU,QAAA,GAAW,IAAI,WAAA,EAAY,CAAE,MAAA,CAAO,KAAK,CAAA,GAAI,KAAA;AAC5E,EAAA,IAAI,MAAA,GAAS,EAAA;AACb,EAAA,KAAA,MAAW,IAAA,IAAQ,KAAA,EAAO,MAAA,IAAU,MAAA,CAAO,aAAa,IAAI,CAAA;AAC5D,EAAA,OAAO,IAAA,CAAK,MAAM,CAAA,CAAE,OAAA,CAAQ,KAAA,EAAO,GAAG,CAAA,CAAE,OAAA,CAAQ,KAAA,EAAO,GAAG,CAAA,CAAE,OAAA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AAW,EAAC,EAAG;AAC7C,MAAA,MAAM,OAAkB,EAAC;AACzB,MAAA,KAAA,MAAW,KAAA,IAAS,WAAA,CAAY,EAAA,EAAI,IAAA,CAAK,KAAK,CAAA,GAAI,CAAA;AAElD,MAAA,MAAM,OAAA,GAAwB;AAAA,QAC5B,IAAA,EAAM,MAAA,IAAU,WAAA,CAAY,IAAA,IAAQ,CAAA,EAAG,KAAA,CAAM,SAAS,CAAA,CAAA,EAAI,WAAA,CAAY,EAAA,CAAG,IAAA,CAAK,GAAG,CAAC,CAAA,IAAA,CAAA;AAAA,OACpF;AAEA,MAAA,IAAI,YAAY,MAAA,EAAQ;AACtB,QAAA,OAAA,CAAQ,MAAA,GAAS,IAAA;AAEjB,QAAA,MAAM,UAAmC,EAAC;AAC1C,QAAA,KAAA,MAAW,KAAA,IAAS,YAAY,EAAA,EAAI;AAClC,UAAA,MAAMC,KAAAA,GAAO,KAAA,CAAM,MAAA,CAAO,KAAK,CAAA;AAC/B,UAAA,IAAIA,KAAAA,IAAQ,CAACA,KAAAA,CAAK,QAAA,EAAU,OAAA,CAAQ,KAAK,CAAA,GAAI,EAAE,KAAA,EAAO,SAAA,CAAUA,KAAAA,CAAK,IAAI,CAAA,EAAE;AAAA,QAC7E;AACA,QAAA,IAAI,OAAO,IAAA,CAAK,OAAO,EAAE,MAAA,GAAS,CAAA,UAAW,uBAAA,GAA0B,OAAA;AAAA,MACzE;AAEA,MAAA,MAAM,UAAA,CAAW,WAAA,CAAY,IAAA,EAAM,OAAO,CAAA;AAAA,IAC5C;AAAA,EACF;AACF;AAGO,IAAM,kBAAA,GAAqB,CAAC,EAAA,EAAa,OAAA,GAA+B,EAAC,KAC9E,mBAAA,CAAoB,EAAA,EAAI,UAAA,EAAY,OAAA,CAAQ,MAAA,IAAU,EAAE","file":"mongodb.cjs","sourcesContent":["// Web APIs, not Buffer: this runs on Workers and Vercel Edge.\n\nexport function encodeBase64Url(input: string | Uint8Array): string {\n const bytes = typeof input === \"string\" ? new TextEncoder().encode(input) : input;\n let binary = \"\";\n for (const byte of bytes) binary += String.fromCharCode(byte);\n return btoa(binary).replace(/\\+/g, \"-\").replace(/\\//g, \"_\").replace(/=+$/, \"\");\n}\n\n// Uint8Array<ArrayBuffer>: bare Uint8Array widens and is not a BufferSource.\nexport function decodeBase64UrlBytes(value: string): Uint8Array<ArrayBuffer> | null {\n try {\n const binary = atob(value.replace(/-/g, \"+\").replace(/_/g, \"/\"));\n return Uint8Array.from(binary, (char) => char.charCodeAt(0));\n } catch {\n return null;\n }\n}\n\nexport function decodeBase64Url(value: string): string | null {\n const bytes = decodeBase64UrlBytes(value);\n return bytes ? new TextDecoder().decode(bytes) : null;\n}\n","import type { SchemaDeclaration } from \"../core/plugin\";\n\nexport const toSnakeCase = (value: string) => value.replace(/[A-Z]/g, (c) => `_${c.toLowerCase()}`);\n\n/**\n * Core tables in the format plugins use. Less expressive than the hand-written\n * Drizzle schema (no partial indexes, ordering or cascade) — a drift test\n * asserts the two agree on the fields they share.\n */\nexport const coreSchema = {\n notification: {\n tableName: \"notification\",\n fields: {\n id: { type: \"string\", required: true },\n userId: { type: \"string\", required: true },\n type: { type: \"string\", required: true },\n payload: { type: \"json\", required: true },\n actorId: { type: \"string\" },\n groupKey: { type: \"string\" },\n dedupeKey: { type: \"string\" },\n seenAt: { type: \"date\" },\n readAt: { type: \"date\" },\n archivedAt: { type: \"date\" },\n createdAt: { type: \"date\", required: true, defaultNow: true },\n },\n primaryKey: [\"id\"],\n indexes: [\n { on: [\"userId\", \"createdAt\"], name: \"notification_feed_idx\" },\n { on: [\"userId\", \"groupKey\"], name: \"notification_group_idx\" },\n { on: [\"userId\", \"dedupeKey\"], unique: true, name: \"notification_dedupe_idx\" },\n ],\n },\n\n notificationDelivery: {\n tableName: \"notification_delivery\",\n fields: {\n id: { type: \"string\", required: true },\n notificationId: { type: \"string\", required: true },\n channel: { type: \"string\", required: true },\n status: { type: \"string\", required: true, default: \"pending\" },\n attempts: { type: \"number\", required: true, default: 0 },\n maxAttempts: { type: \"number\", required: true, default: 5 },\n notBefore: { type: \"date\", required: true, defaultNow: true },\n claimedAt: { type: \"date\" },\n claimedBy: { type: \"string\" },\n lastError: { type: \"string\" },\n updatedAt: { type: \"date\", required: true, defaultNow: true },\n },\n primaryKey: [\"id\"],\n indexes: [\n { on: [\"status\", \"notBefore\"], name: \"delivery_claim_idx\" },\n { on: [\"notificationId\"], name: \"delivery_notification_idx\" },\n ],\n },\n\n notificationPreference: {\n tableName: \"notification_preference\",\n fields: {\n userId: { type: \"string\", required: true },\n type: { type: \"string\", required: true },\n channel: { type: \"string\", required: true },\n enabled: { type: \"boolean\", required: true, default: true },\n frequency: { type: \"string\", required: true, default: \"instant\" },\n },\n primaryKey: [\"userId\", \"type\", \"channel\"],\n },\n} satisfies SchemaDeclaration;\n","import { toSnakeCase } from \"../schema/declaration\";\nimport { ConfigError } from \"./errors\";\nimport type { SchemaDeclaration, TableDeclaration } from \"./plugin\";\n\nexport type Scalar = string | number | boolean | Date | null;\n\n/**\n * One operator per condition. Operators are tagged objects so a bare value is\n * always an equality test; json columns therefore cannot be filtered, since an\n * object value would be ambiguous.\n */\nexport type WhereCondition =\n | Scalar\n | { in: readonly (string | number)[] }\n | { lt: Date | number }\n | { lte: Date | number }\n | { gt: Date | number }\n | { gte: Date | number }\n | { not: Scalar };\n\nexport type WhereClause = Record<string, WhereCondition>;\n\nexport type QueryOptions = {\n limit?: number;\n orderBy?: { field: string; direction: \"asc\" | \"desc\" };\n};\n\nexport type UpsertOptions = { onConflict: readonly string[] };\n\n/**\n * Table access for plugins, scoped to the tables that plugin declared.\n *\n * Plugins could always declare tables via schema() but had no way to read\n * them, so preferences had to bolt its queries onto the core adapter. That\n * does not generalise to digests or push.\n */\nexport type PluginStore = {\n find<T = Record<string, unknown>>(\n table: string,\n where?: WhereClause,\n options?: QueryOptions,\n ): Promise<T[]>;\n insert(table: string, rows: readonly Record<string, unknown>[]): Promise<number>;\n upsert(\n table: string,\n rows: readonly Record<string, unknown>[],\n options: UpsertOptions,\n ): Promise<number>;\n update(table: string, where: WhereClause, set: Record<string, unknown>): Promise<number>;\n remove(table: string, where: WhereClause): Promise<number>;\n};\n\nexport type TableStorage = {\n readonly naming?: \"snake_case\" | \"preserve\";\n readonly serializesJson?: boolean;\n\n queryTable(\n table: string,\n where: WhereClause,\n options: QueryOptions,\n ): Promise<Record<string, unknown>[]>;\n insertRows(\n table: string,\n rows: readonly Record<string, unknown>[],\n onConflict?: readonly string[],\n ): Promise<number>;\n updateRows(table: string, where: WhereClause, set: Record<string, unknown>): Promise<number>;\n deleteRows(table: string, where: WhereClause): Promise<number>;\n};\n\nconst OPERATORS = [\"in\", \"lt\", \"lte\", \"gt\", \"gte\", \"not\"] as const;\n\nexport const isOperator = (value: unknown): boolean =>\n typeof value === \"object\" &&\n value !== null &&\n !(value instanceof Date) &&\n OPERATORS.some((op) => op in value);\n\n/**\n * Every table and column a plugin touches is checked against its own\n * declaration. Without this a plugin could read the notification table, and\n * unvalidated identifiers would reach the SQL builder.\n */\nexport function createPluginStore(\n pluginId: string,\n schema: SchemaDeclaration | undefined,\n storage: TableStorage,\n prefix: string,\n): PluginStore {\n const byTableName = new Map<string, TableDeclaration>();\n for (const declaration of Object.values(schema ?? {})) {\n byTableName.set(declaration.tableName, declaration);\n }\n\n function resolve(table: string): TableDeclaration {\n const declaration = byTableName.get(table);\n if (!declaration) {\n throw new ConfigError(\n `plugin \"${pluginId}\" accessed table \"${table}\", which it does not declare in schema().`,\n );\n }\n return declaration;\n }\n\n function checkFields(table: TableDeclaration, fields: Iterable<string>, context: string) {\n for (const field of fields) {\n const spec = table.fields[field];\n if (!spec) {\n throw new ConfigError(\n `plugin \"${pluginId}\" used unknown field \"${field}\" on \"${table.tableName}\" (${context}).`,\n );\n }\n if (context === \"where\" && spec.type === \"json\") {\n throw new ConfigError(\n `plugin \"${pluginId}\" cannot filter on json field \"${field}\"; ` +\n \"a bare object value is indistinguishable from an operator.\",\n );\n }\n }\n }\n\n const qualified = (table: TableDeclaration) => prefix + table.tableName;\n\n // A document store keeps the declared names; a SQL adapter wants columns.\n const column = (field: string) => (storage.naming === \"preserve\" ? field : toSnakeCase(field));\n\n /**\n * json columns must be handed to the driver as text; postgres-js cannot bind\n * a plain object and the insert fails outright.\n */\n const serialize = (table: TableDeclaration, row: Record<string, unknown>) => {\n if (storage.serializesJson === false) return row;\n\n const out: Record<string, unknown> = {};\n for (const [field, value] of Object.entries(row)) {\n out[field] =\n table.fields[field]?.type === \"json\" && value !== null && value !== undefined\n ? JSON.stringify(value)\n : value;\n }\n return out;\n };\n\n return {\n async find(table, where = {}, options = {}) {\n const declaration = resolve(table);\n checkFields(declaration, Object.keys(where), \"where\");\n if (options.orderBy) checkFields(declaration, [options.orderBy.field], \"orderBy\");\n\n const rows = await storage.queryTable(qualified(declaration), where, options);\n\n // SELECT * returns snake_case columns; plugins declare camelCase fields\n // and type their reads that way. Without this every property is\n // undefined at runtime while typechecking perfectly.\n return rows.map((row) => {\n const mapped: Record<string, unknown> = {};\n for (const field of Object.keys(declaration.fields)) {\n const value = row[column(field)];\n // Drivers differ: some hand back parsed jsonb, some raw text.\n mapped[field] =\n declaration.fields[field]?.type === \"json\" && typeof value === \"string\"\n ? JSON.parse(value)\n : value;\n }\n return mapped;\n }) as never;\n },\n\n async insert(table, rows) {\n const declaration = resolve(table);\n for (const row of rows) checkFields(declaration, Object.keys(row), \"insert\");\n return storage.insertRows(\n qualified(declaration),\n rows.map((row) => serialize(declaration, row)),\n );\n },\n\n async upsert(table, rows, options) {\n const declaration = resolve(table);\n for (const row of rows) checkFields(declaration, Object.keys(row), \"upsert\");\n checkFields(declaration, options.onConflict, \"onConflict\");\n return storage.insertRows(\n qualified(declaration),\n rows.map((row) => serialize(declaration, row)),\n options.onConflict,\n );\n },\n\n async update(table, where, set) {\n const declaration = resolve(table);\n checkFields(declaration, Object.keys(where), \"where\");\n checkFields(declaration, Object.keys(set), \"update\");\n return storage.updateRows(qualified(declaration), where, serialize(declaration, set));\n },\n\n async remove(table, where) {\n const declaration = resolve(table);\n checkFields(declaration, Object.keys(where), \"where\");\n return storage.deleteRows(qualified(declaration), where);\n },\n };\n}\n","import type {\n ClaimArgs,\n ClaimedDelivery,\n DatabaseAdapter,\n DeliveryRelease,\n FeedPage,\n FeedQuery,\n InsertNotification,\n} from \"../../core/adapter\";\nimport { decodeBase64Url, encodeBase64Url } from \"../../core/base64url\";\nimport { isOperator, type QueryOptions, type WhereClause } from \"../../core/store\";\nimport type { Channel, DeliveryRecord, DeliveryStatus, NotificationRecord } from \"../../core/types\";\n\ntype Document = Record<string, unknown>;\n\n/**\n * Minimal structural view of the pieces of the Mongo driver this uses.\n *\n * Typed structurally rather than against the real `Db` so `mongodb` stays an\n * optional peer dependency: an app on Postgres never installs it, and its\n * types are not needed to typecheck this file.\n */\ntype MongoCollection = {\n insertMany(docs: Document[], options?: unknown): Promise<unknown>;\n insertOne(doc: Document, options?: unknown): Promise<unknown>;\n find(filter: Document, options?: Document): { toArray(): Promise<Document[]> };\n findOne(filter: Document, options?: Document): Promise<Document | null>;\n findOneAndUpdate(\n filter: Document,\n update: Document | Document[],\n options?: Document,\n ): Promise<Document | null>;\n updateOne(\n filter: Document,\n update: Document | Document[],\n options?: Document,\n ): Promise<{\n matchedCount: number;\n modifiedCount: number;\n upsertedCount?: number;\n }>;\n updateMany(\n filter: Document,\n update: Document | Document[],\n options?: Document,\n ): Promise<{\n matchedCount: number;\n modifiedCount: number;\n }>;\n deleteMany(filter: Document, options?: Document): Promise<{ deletedCount: number }>;\n countDocuments(filter: Document, options?: Document): Promise<number>;\n createIndex(spec: Document, options?: Document): Promise<string>;\n};\n\ntype MongoDb = {\n collection(name: string): MongoCollection;\n};\n\ntype MongoSession = {\n withTransaction<T>(fn: () => Promise<T>): Promise<T>;\n endSession(): Promise<void>;\n};\n\ntype MongoClient = {\n startSession(): MongoSession;\n};\n\nexport type MongoAdapterOptions = {\n /** Prefixes every collection name, matching the SQL adapter's table prefix. */\n prefix?: string;\n /**\n * The MongoClient the db came from. Supplying it makes createNotifications\n * atomic; without it a crash between the two writes can leave a notification\n * with no deliveries. Transactions need a replica set, so it is opt-in — a\n * standalone mongod rejects the session outright.\n */\n client?: unknown;\n};\n\nconst str = (value: unknown): string => String(value);\nconst nullableStr = (value: unknown): string | null => (value == null ? null : String(value));\nconst num = (value: unknown): number => Number(value);\nconst date = (value: unknown): Date => (value instanceof Date ? value : new Date(String(value)));\nconst nullableDate = (value: unknown): Date | null =>\n value == null ? null : value instanceof Date ? value : new Date(String(value));\n\nconst encodeCursor = (createdAt: Date, id: string) =>\n encodeBase64Url(`${createdAt.toISOString()}|${id}`);\n\nfunction decodeCursor(cursor: string): { createdAt: Date; id: string } | null {\n const decoded = decodeBase64Url(cursor);\n if (!decoded) return null;\n\n const [iso, id] = decoded.split(\"|\");\n if (!iso || !id) return null;\n\n const createdAt = new Date(iso);\n return Number.isNaN(createdAt.getTime()) ? null : { createdAt, id };\n}\n\n/** Documents carry the id as `_id`; the rest of the library never sees that. */\nfunction toNotification(doc: Document): NotificationRecord {\n return {\n id: str(doc._id),\n userId: str(doc.userId),\n type: str(doc.type),\n payload: doc.payload,\n actorId: nullableStr(doc.actorId),\n groupKey: nullableStr(doc.groupKey),\n dedupeKey: nullableStr(doc.dedupeKey),\n seenAt: nullableDate(doc.seenAt),\n readAt: nullableDate(doc.readAt),\n archivedAt: nullableDate(doc.archivedAt),\n createdAt: date(doc.createdAt),\n };\n}\n\nfunction toDelivery(doc: Document): DeliveryRecord {\n return {\n id: str(doc._id),\n notificationId: str(doc.notificationId),\n channel: str(doc.channel) as Channel,\n status: str(doc.status) as DeliveryStatus,\n attempts: num(doc.attempts),\n maxAttempts: num(doc.maxAttempts),\n notBefore: date(doc.notBefore),\n claimedAt: nullableDate(doc.claimedAt),\n claimedBy: nullableStr(doc.claimedBy),\n lastError: nullableStr(doc.lastError),\n updatedAt: date(doc.updatedAt),\n };\n}\n\n/** The store hands `id`; documents key on `_id`. */\nconst toMongoField = (field: string) => (field === \"id\" ? \"_id\" : field);\n\n/** Translates a WhereClause into a Mongo filter. */\nfunction toFilter(where: WhereClause): Document {\n const filter: Document = {};\n\n for (const [field, condition] of Object.entries(where)) {\n const key = toMongoField(field);\n\n if (condition === null) {\n filter[key] = null;\n continue;\n }\n\n if (!isOperator(condition)) {\n filter[key] = condition;\n continue;\n }\n\n const operator = condition as Record<string, unknown>;\n if (\"in\" in operator) filter[key] = { $in: operator.in };\n else if (\"lt\" in operator) filter[key] = { $lt: operator.lt };\n else if (\"lte\" in operator) filter[key] = { $lte: operator.lte };\n else if (\"gt\" in operator) filter[key] = { $gt: operator.gt };\n else if (\"gte\" in operator) filter[key] = { $gte: operator.gte };\n else if (\"not\" in operator) {\n // $ne null also matches a missing field, which is what \"not null\" means.\n filter[key] = { $ne: operator.not };\n }\n }\n\n return filter;\n}\n\n/** A duplicate on a unique index, rather than a real write failure. */\nconst isDuplicateKey = (error: unknown): boolean => {\n const code = (error as { code?: number; writeErrors?: { code?: number }[] })?.code;\n if (code === 11000) return true;\n const writeErrors = (error as { writeErrors?: { code?: number }[] })?.writeErrors;\n return Array.isArray(writeErrors) && writeErrors.some((entry) => entry?.code === 11000);\n};\n\nexport function mongoAdapter(db: unknown, options: MongoAdapterOptions = {}): DatabaseAdapter {\n const database = db as MongoDb;\n const prefix = options.prefix ?? \"\";\n\n const client = options.client as MongoClient | undefined;\n\n const notifications = () => database.collection(`${prefix}notification`);\n const deliveries = () => database.collection(`${prefix}notification_delivery`);\n\n /**\n * Runs `fn` in a transaction when a client was supplied, and plainly\n * otherwise. The session must reach every command inside, so `fn` receives\n * it — a write that omits it silently runs outside the transaction.\n *\n * One transaction per notification, not per batch: a duplicate key aborts\n * the transaction it happens in, and dedupe is the expected path here, so a\n * batch-wide transaction would throw away the notifications around it.\n */\n async function atomically<T>(fn: (session?: MongoSession) => Promise<T>): Promise<T> {\n if (!client) return fn();\n\n const session = client.startSession();\n try {\n return await session.withTransaction(() => fn(session));\n } finally {\n await session.endSession();\n }\n }\n\n return {\n name: \"mongodb\",\n // Documents keep the declared camelCase, and store objects natively.\n naming: \"preserve\",\n serializesJson: false,\n\n async createNotifications(input: readonly InsertNotification[]) {\n if (input.length === 0) return { created: [], deduped: [] };\n\n const created: string[] = [];\n const deduped: string[] = [];\n\n for (const row of input) {\n const now = new Date();\n\n try {\n await atomically(async (session) => {\n const options = session ? { session } : undefined;\n\n // The notification goes in first: it carries the unique dedupe\n // index, so a duplicate is rejected before any delivery is written.\n await notifications().insertOne(\n {\n _id: row.id,\n userId: row.userId,\n type: row.type,\n payload: row.payload ?? null,\n actorId: row.actorId ?? null,\n groupKey: row.groupKey ?? null,\n dedupeKey: row.dedupeKey ?? null,\n seenAt: null,\n readAt: null,\n archivedAt: null,\n createdAt: now,\n },\n options,\n );\n\n if (row.deliveries.length > 0) {\n await deliveries().insertMany(\n row.deliveries.map((delivery) => ({\n _id: delivery.id,\n notificationId: row.id,\n channel: delivery.channel,\n status: \"pending\",\n attempts: 0,\n maxAttempts: delivery.maxAttempts,\n notBefore: delivery.notBefore,\n claimedAt: null,\n claimedBy: null,\n lastError: null,\n updatedAt: now,\n })),\n options,\n );\n }\n });\n\n created.push(row.id);\n } catch (error) {\n if (!isDuplicateKey(error)) throw error;\n deduped.push(row.id);\n }\n }\n\n return { created, deduped };\n },\n\n async claimPendingDeliveries(args: ClaimArgs): Promise<readonly ClaimedDelivery[]> {\n const now = args.now ?? new Date();\n const staleBefore = new Date(now.getTime() - args.leaseMs);\n\n const eligible: Document = {\n $and: [\n {\n $or: [{ status: \"pending\" }, { status: \"claimed\", claimedAt: { $lt: staleBefore } }],\n },\n { notBefore: { $lte: now } },\n { $expr: { $lt: [\"$attempts\", \"$maxAttempts\"] } },\n ],\n };\n\n if (args.channels && args.channels.length > 0) {\n (eligible.$and as Document[]).push({ channel: { $in: [...args.channels] } });\n }\n if (args.ids && args.ids.length > 0) {\n (eligible.$and as Document[]).push({ _id: { $in: [...args.ids] } });\n }\n\n // findOneAndUpdate is atomic per document, so two concurrent callers\n // cannot be handed the same delivery. updateMany would be one round trip\n // but gives no way to learn which documents this call actually won.\n const claimed: Document[] = [];\n for (let taken = 0; taken < args.limit; taken += 1) {\n const doc = await deliveries().findOneAndUpdate(\n eligible,\n { $set: { status: \"claimed\", claimedAt: now, claimedBy: args.claimToken } },\n { sort: { notBefore: 1, _id: 1 }, returnDocument: \"after\" },\n );\n if (!doc) break;\n claimed.push(doc);\n }\n\n if (claimed.length === 0) return [];\n\n // One lookup for the batch rather than one per delivery.\n const parents = await notifications()\n .find({ _id: { $in: claimed.map((doc) => doc.notificationId) } })\n .toArray();\n const byId = new Map(parents.map((doc) => [str(doc._id), doc]));\n\n return claimed.flatMap((doc) => {\n const parent = byId.get(str(doc.notificationId));\n if (!parent) return [];\n\n return [\n {\n id: str(doc._id),\n notificationId: str(doc.notificationId),\n channel: str(doc.channel) as Channel,\n attempts: num(doc.attempts),\n maxAttempts: num(doc.maxAttempts),\n notification: {\n userId: str(parent.userId),\n type: str(parent.type),\n payload: parent.payload,\n actorId: nullableStr(parent.actorId),\n },\n },\n ];\n });\n },\n\n async releaseDeliveries(releases: readonly DeliveryRelease[]) {\n if (releases.length === 0) return;\n const now = new Date();\n\n for (const { id, outcome, nextAttemptAt } of releases) {\n const failed = outcome.result === \"failed\";\n const retryable = failed && outcome.retryable;\n\n // An aggregation-pipeline update so the terminal-vs-retry decision\n // reads maxAttempts from the document itself, as the SQL CASE does.\n await deliveries().updateOne({ _id: id }, [\n {\n $set: {\n attempts: failed ? { $add: [\"$attempts\", 1] } : \"$attempts\",\n status: failed\n ? {\n $cond: [\n { $and: [retryable, { $lt: [{ $add: [\"$attempts\", 1] }, \"$maxAttempts\"] }] },\n \"pending\",\n \"failed\",\n ],\n }\n : \"sent\",\n // $literal: an error message starting with \"$\" would otherwise\n // be read as a field path.\n lastError: failed ? { $literal: outcome.error.slice(0, 2000) } : null,\n notBefore: nextAttemptAt ?? \"$notBefore\",\n // Written unconditionally: if the lease expired and another\n // worker re-sent, the duplicate already happened and the true\n // terminal state beats leaving the row wedged in \"claimed\".\n claimedAt: null,\n claimedBy: null,\n updatedAt: now,\n },\n },\n ]);\n }\n },\n\n async listNotifications(query: FeedQuery): Promise<FeedPage> {\n const cursor = query.cursor ? decodeCursor(query.cursor) : null;\n\n const filter: Document = { userId: query.userId, archivedAt: null };\n if (query.unreadOnly) filter.readAt = null;\n\n if (cursor) {\n // Matches the SQL row comparison (createdAt, id) < (cursor, cursorId).\n filter.$or = [\n { createdAt: { $lt: cursor.createdAt } },\n { createdAt: cursor.createdAt, _id: { $lt: cursor.id } },\n ];\n }\n\n const docs = await notifications()\n .find(filter, { sort: { createdAt: -1, _id: -1 }, limit: query.limit + 1 })\n .toArray();\n\n const page = docs.slice(0, query.limit).map(toNotification);\n const last = page.at(-1);\n\n return {\n notifications: page,\n nextCursor:\n docs.length > query.limit && last ? encodeCursor(last.createdAt, last.id) : null,\n };\n },\n\n async countUnseen(userId: string) {\n return notifications().countDocuments({ userId, seenAt: null, archivedAt: null });\n },\n\n async markSeen(userId: string, before: Date) {\n await notifications().updateMany(\n { userId, seenAt: null, createdAt: { $lte: before } },\n { $set: { seenAt: new Date() } },\n );\n },\n\n async markRead(userId: string, notificationIds: readonly string[]) {\n if (notificationIds.length === 0) return 0;\n\n // Scoped by userId as well as id, and idempotent: re-marking a read\n // notification reports success rather than a 404.\n const filter = { userId, _id: { $in: [...notificationIds] } };\n await notifications().updateMany(\n { ...filter, readAt: null },\n {\n $set: { readAt: new Date() },\n },\n );\n\n return notifications().countDocuments(filter);\n },\n\n async markAllRead(userId: string) {\n const result = await notifications().updateMany(\n { userId, readAt: null },\n { $set: { readAt: new Date() } },\n );\n return result.modifiedCount;\n },\n\n async getFailedDeliveries(args: { since: Date; limit: number }) {\n const docs = await deliveries()\n .find(\n { status: \"failed\", updatedAt: { $gte: args.since } },\n { sort: { updatedAt: -1 }, limit: args.limit },\n )\n .toArray();\n\n return docs.map(toDelivery);\n },\n\n async queryTable(table: string, where: WhereClause, options: QueryOptions) {\n const find: Document = {};\n if (options.limit) find.limit = options.limit;\n if (options.orderBy) {\n const direction = options.orderBy.direction === \"desc\" ? -1 : 1;\n find.sort = { [toMongoField(options.orderBy.field)]: direction };\n }\n\n const docs = await database.collection(table).find(toFilter(where), find).toArray();\n\n // `_id` is a storage detail; plugins declared the field as `id`.\n return docs.map(({ _id, ...rest }) => (_id === undefined ? rest : { id: _id, ...rest }));\n },\n\n async insertRows(\n table: string,\n rows: readonly Record<string, unknown>[],\n onConflict?: readonly string[],\n ) {\n if (rows.length === 0) return 0;\n\n const documents = rows.map(({ id, ...rest }) =>\n id === undefined ? { ...rest } : { _id: id, ...rest },\n );\n\n if (!onConflict || onConflict.length === 0) {\n await database.collection(table).insertMany(documents as Document[]);\n return documents.length;\n }\n\n // Upsert keyed on the conflict fields, matching ON CONFLICT DO UPDATE.\n let written = 0;\n for (const document of documents as Document[]) {\n const key: Document = {};\n for (const field of onConflict) key[toMongoField(field)] = document[toMongoField(field)];\n\n // _id goes in $setOnInsert, never $set: Mongo rejects an update that\n // would modify it, so a conflicting upsert must leave it alone.\n const { _id, ...updatable } = document;\n const update: Document = { $set: updatable };\n if (_id !== undefined) update.$setOnInsert = { _id };\n\n const result = await database.collection(table).updateOne(key, update, { upsert: true });\n written += result.matchedCount + (result.upsertedCount ?? 0);\n }\n return written;\n },\n\n async updateRows(table: string, where: WhereClause, set: Record<string, unknown>) {\n const assignments: Document = {};\n for (const [field, value] of Object.entries(set)) assignments[toMongoField(field)] = value;\n if (Object.keys(assignments).length === 0) return 0;\n\n const result = await database\n .collection(table)\n .updateMany(toFilter(where), { $set: assignments });\n return result.matchedCount;\n },\n\n async deleteRows(table: string, where: WhereClause) {\n const result = await database.collection(table).deleteMany(toFilter(where));\n return result.deletedCount;\n },\n };\n}\n","import type { FieldDeclaration, SchemaDeclaration } from \"../../core/plugin\";\nimport { coreSchema } from \"../../schema/declaration\";\n\ntype IndexSpec = Record<string, 1 | -1>;\n\ntype IndexOptions = {\n name: string;\n unique?: boolean;\n partialFilterExpression?: Record<string, unknown>;\n};\n\ntype IndexableDb = {\n collection(name: string): {\n createIndex(spec: IndexSpec, options: IndexOptions): Promise<string>;\n };\n};\n\nconst BSON_TYPE: Record<FieldDeclaration[\"type\"], string> = {\n string: \"string\",\n number: \"number\",\n boolean: \"bool\",\n date: \"date\",\n json: \"object\",\n};\n\n/**\n * Applies a SchemaDeclaration's indexes to Mongo. The document counterpart of\n * renderPostgresDdl — there are no tables to create, only indexes, and the\n * declaration is the same source of truth.\n *\n * A unique index over a nullable field gets a partial filter. Postgres treats\n * every NULL as distinct, so unlimited rows may leave dedupeKey unset; a plain\n * Mongo unique index treats them as one value and would allow exactly one.\n */\nexport async function createPluginIndexes(\n db: unknown,\n schema: SchemaDeclaration,\n prefix = \"\",\n): Promise<void> {\n const database = db as IndexableDb;\n\n for (const table of Object.values(schema)) {\n const collection = database.collection(prefix + table.tableName);\n\n // A single \"id\" key is stored as _id, which is already unique.\n if (table.primaryKey && table.primaryKey.length > 0 && table.primaryKey[0] !== \"id\") {\n const spec: IndexSpec = {};\n for (const field of table.primaryKey) spec[field] = 1;\n await collection.createIndex(spec, {\n name: `${table.tableName}_pk`,\n unique: true,\n });\n }\n\n for (const declaration of table.indexes ?? []) {\n const spec: IndexSpec = {};\n for (const field of declaration.on) spec[field] = 1;\n\n const options: IndexOptions = {\n name: prefix + (declaration.name ?? `${table.tableName}_${declaration.on.join(\"_\")}_idx`),\n };\n\n if (declaration.unique) {\n options.unique = true;\n\n const partial: Record<string, unknown> = {};\n for (const field of declaration.on) {\n const spec = table.fields[field];\n if (spec && !spec.required) partial[field] = { $type: BSON_TYPE[spec.type] };\n }\n if (Object.keys(partial).length > 0) options.partialFilterExpression = partial;\n }\n\n await collection.createIndex(spec, options);\n }\n }\n}\n\n/** Creates the indexes the core tables need. Call once at startup. */\nexport const createMongoIndexes = (db: unknown, options: { prefix?: string } = {}) =>\n createPluginIndexes(db, coreSchema, options.prefix ?? \"\");\n"]}
@@ -0,0 +1,32 @@
1
+ import { D as DatabaseAdapter } from '../adapter-f0OR2DhY.cjs';
2
+ import { S as SchemaDeclaration } from '../plugin-Bqj9xAOS.cjs';
3
+
4
+ type MongoAdapterOptions = {
5
+ /** Prefixes every collection name, matching the SQL adapter's table prefix. */
6
+ prefix?: string;
7
+ /**
8
+ * The MongoClient the db came from. Supplying it makes createNotifications
9
+ * atomic; without it a crash between the two writes can leave a notification
10
+ * with no deliveries. Transactions need a replica set, so it is opt-in — a
11
+ * standalone mongod rejects the session outright.
12
+ */
13
+ client?: unknown;
14
+ };
15
+ declare function mongoAdapter(db: unknown, options?: MongoAdapterOptions): DatabaseAdapter;
16
+
17
+ /**
18
+ * Applies a SchemaDeclaration's indexes to Mongo. The document counterpart of
19
+ * renderPostgresDdl — there are no tables to create, only indexes, and the
20
+ * declaration is the same source of truth.
21
+ *
22
+ * A unique index over a nullable field gets a partial filter. Postgres treats
23
+ * every NULL as distinct, so unlimited rows may leave dedupeKey unset; a plain
24
+ * Mongo unique index treats them as one value and would allow exactly one.
25
+ */
26
+ declare function createPluginIndexes(db: unknown, schema: SchemaDeclaration, prefix?: string): Promise<void>;
27
+ /** Creates the indexes the core tables need. Call once at startup. */
28
+ declare const createMongoIndexes: (db: unknown, options?: {
29
+ prefix?: string;
30
+ }) => Promise<void>;
31
+
32
+ export { type MongoAdapterOptions, createMongoIndexes, createPluginIndexes, mongoAdapter };
@@ -0,0 +1,32 @@
1
+ import { D as DatabaseAdapter } from '../adapter-f0OR2DhY.js';
2
+ import { S as SchemaDeclaration } from '../plugin-bTJKmnlq.js';
3
+
4
+ type MongoAdapterOptions = {
5
+ /** Prefixes every collection name, matching the SQL adapter's table prefix. */
6
+ prefix?: string;
7
+ /**
8
+ * The MongoClient the db came from. Supplying it makes createNotifications
9
+ * atomic; without it a crash between the two writes can leave a notification
10
+ * with no deliveries. Transactions need a replica set, so it is opt-in — a
11
+ * standalone mongod rejects the session outright.
12
+ */
13
+ client?: unknown;
14
+ };
15
+ declare function mongoAdapter(db: unknown, options?: MongoAdapterOptions): DatabaseAdapter;
16
+
17
+ /**
18
+ * Applies a SchemaDeclaration's indexes to Mongo. The document counterpart of
19
+ * renderPostgresDdl — there are no tables to create, only indexes, and the
20
+ * declaration is the same source of truth.
21
+ *
22
+ * A unique index over a nullable field gets a partial filter. Postgres treats
23
+ * every NULL as distinct, so unlimited rows may leave dedupeKey unset; a plain
24
+ * Mongo unique index treats them as one value and would allow exactly one.
25
+ */
26
+ declare function createPluginIndexes(db: unknown, schema: SchemaDeclaration, prefix?: string): Promise<void>;
27
+ /** Creates the indexes the core tables need. Call once at startup. */
28
+ declare const createMongoIndexes: (db: unknown, options?: {
29
+ prefix?: string;
30
+ }) => Promise<void>;
31
+
32
+ export { type MongoAdapterOptions, createMongoIndexes, createPluginIndexes, mongoAdapter };