@spacelr/sdk 0.10.6 → 0.10.7

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/dist/index.d.mts CHANGED
@@ -1227,6 +1227,31 @@ interface SubscribeWithSnapshotOptions<T> {
1227
1227
  sort?: Record<string, 1 | -1>;
1228
1228
  limit?: number;
1229
1229
  select?: string[];
1230
+ /**
1231
+ * #699 P1 — opt-in full-backlog catch-up. When set, the snapshot (both the
1232
+ * initial one and every gap-recovery one) drains **all** matching documents
1233
+ * via keyset pagination instead of returning a single bounded page, then
1234
+ * delivers them to `onSnapshot` in one call. This is what makes a long
1235
+ * offline window (backlog larger than one page / the Redis stream window)
1236
+ * fully recoverable under a document-dependent `.read`.
1237
+ *
1238
+ * Ordering is forced to `_id` ascending (the order keyset pagination
1239
+ * requires); `limit` becomes the per-page size. Bounded by `maxPages` /
1240
+ * `maxDocs` so a pathological backlog cannot drain unbounded — on reaching
1241
+ * either cap the snapshot is delivered partial. A server `SCAN_LIMIT_EXCEEDED`
1242
+ * (sparse `.filter` scan) surfaces via `onSnapshotError`.
1243
+ *
1244
+ * **Requires 24-hex ObjectId `_id`s** — the drain uses keyset `.after()`
1245
+ * pagination, which only supports ObjectId cursors (see `.after()`/`.before()`).
1246
+ * On a collection with custom string `_id`s the drain degrades (an HTTP 400
1247
+ * mid-drain or a partial result); do not enable `catchUp` there.
1248
+ *
1249
+ * When omitted, behavior is unchanged: one page honoring `limit`/`sort`.
1250
+ */
1251
+ catchUp?: {
1252
+ maxPages?: number;
1253
+ maxDocs?: number;
1254
+ };
1230
1255
  /**
1231
1256
  * Called with the full snapshot:
1232
1257
  * - once after the initial find()
@@ -1322,7 +1347,17 @@ interface SubscribeEventsHandlers<T = Record<string, unknown>> {
1322
1347
  _id: string;
1323
1348
  _eventId: string;
1324
1349
  }) => void | Promise<void>;
1325
- /** Called on deletes. `document` may be absent on tombstones — only documentId + eventId are guaranteed. */
1350
+ /**
1351
+ * Called on deletes. `document` may be absent on tombstones — only documentId
1352
+ * + eventId are guaranteed.
1353
+ *
1354
+ * #699 P2: on a **stream-mode** collection a *document-less* delete tombstone
1355
+ * (e.g. a TTL-index expiry) is now dropped fail-closed by the gateway and
1356
+ * will **not** reach `onDelete` at all — previously it arrived with just
1357
+ * documentId+eventId. Consumers that relied on TTL-eviction firing `onDelete`
1358
+ * should not expect it; opt-in delete delivery via change-stream pre-images
1359
+ * is tracked in #710. Document-bearing deletes are unaffected.
1360
+ */
1326
1361
  onDelete?: (documentId: string, eventId: string) => void | Promise<void>;
1327
1362
  /** Called when the server emits an event-gap. See GapReason for the four codes. */
1328
1363
  onGap?: (info: StreamGapInfo) => void;
@@ -1574,6 +1609,17 @@ declare class CollectionRef<T = Record<string, unknown>> {
1574
1609
  * aborts the request and silences the AbortError. Gap-recovery state
1575
1610
  * machine lands in Task 6.
1576
1611
  */
1612
+ /**
1613
+ * #699 P1 — resolve the snapshot documents for `subscribeWithSnapshot`.
1614
+ * Without `catchUp`, one bounded page honoring `limit`/`sort` (unchanged
1615
+ * behavior). With `catchUp`, keyset-drain ALL matching pages (`_id`
1616
+ * ascending) up to `maxPages`/`maxDocs` and return the union. All pages are
1617
+ * gathered before the caller delivers `onSnapshot`, so the existing
1618
+ * snapshot-ordering buffer only unblocks live `onChange` after the full
1619
+ * drain. A thrown error (e.g. `SCAN_LIMIT_EXCEEDED`, abort) propagates to the
1620
+ * caller's existing snapshot try/catch.
1621
+ */
1622
+ private resolveSnapshotDocs;
1577
1623
  subscribeWithSnapshot(opts: SubscribeWithSnapshotOptions<T>): Promise<() => void>;
1578
1624
  }
1579
1625
  declare class DatabaseModule {
package/dist/index.d.ts CHANGED
@@ -1227,6 +1227,31 @@ interface SubscribeWithSnapshotOptions<T> {
1227
1227
  sort?: Record<string, 1 | -1>;
1228
1228
  limit?: number;
1229
1229
  select?: string[];
1230
+ /**
1231
+ * #699 P1 — opt-in full-backlog catch-up. When set, the snapshot (both the
1232
+ * initial one and every gap-recovery one) drains **all** matching documents
1233
+ * via keyset pagination instead of returning a single bounded page, then
1234
+ * delivers them to `onSnapshot` in one call. This is what makes a long
1235
+ * offline window (backlog larger than one page / the Redis stream window)
1236
+ * fully recoverable under a document-dependent `.read`.
1237
+ *
1238
+ * Ordering is forced to `_id` ascending (the order keyset pagination
1239
+ * requires); `limit` becomes the per-page size. Bounded by `maxPages` /
1240
+ * `maxDocs` so a pathological backlog cannot drain unbounded — on reaching
1241
+ * either cap the snapshot is delivered partial. A server `SCAN_LIMIT_EXCEEDED`
1242
+ * (sparse `.filter` scan) surfaces via `onSnapshotError`.
1243
+ *
1244
+ * **Requires 24-hex ObjectId `_id`s** — the drain uses keyset `.after()`
1245
+ * pagination, which only supports ObjectId cursors (see `.after()`/`.before()`).
1246
+ * On a collection with custom string `_id`s the drain degrades (an HTTP 400
1247
+ * mid-drain or a partial result); do not enable `catchUp` there.
1248
+ *
1249
+ * When omitted, behavior is unchanged: one page honoring `limit`/`sort`.
1250
+ */
1251
+ catchUp?: {
1252
+ maxPages?: number;
1253
+ maxDocs?: number;
1254
+ };
1230
1255
  /**
1231
1256
  * Called with the full snapshot:
1232
1257
  * - once after the initial find()
@@ -1322,7 +1347,17 @@ interface SubscribeEventsHandlers<T = Record<string, unknown>> {
1322
1347
  _id: string;
1323
1348
  _eventId: string;
1324
1349
  }) => void | Promise<void>;
1325
- /** Called on deletes. `document` may be absent on tombstones — only documentId + eventId are guaranteed. */
1350
+ /**
1351
+ * Called on deletes. `document` may be absent on tombstones — only documentId
1352
+ * + eventId are guaranteed.
1353
+ *
1354
+ * #699 P2: on a **stream-mode** collection a *document-less* delete tombstone
1355
+ * (e.g. a TTL-index expiry) is now dropped fail-closed by the gateway and
1356
+ * will **not** reach `onDelete` at all — previously it arrived with just
1357
+ * documentId+eventId. Consumers that relied on TTL-eviction firing `onDelete`
1358
+ * should not expect it; opt-in delete delivery via change-stream pre-images
1359
+ * is tracked in #710. Document-bearing deletes are unaffected.
1360
+ */
1326
1361
  onDelete?: (documentId: string, eventId: string) => void | Promise<void>;
1327
1362
  /** Called when the server emits an event-gap. See GapReason for the four codes. */
1328
1363
  onGap?: (info: StreamGapInfo) => void;
@@ -1574,6 +1609,17 @@ declare class CollectionRef<T = Record<string, unknown>> {
1574
1609
  * aborts the request and silences the AbortError. Gap-recovery state
1575
1610
  * machine lands in Task 6.
1576
1611
  */
1612
+ /**
1613
+ * #699 P1 — resolve the snapshot documents for `subscribeWithSnapshot`.
1614
+ * Without `catchUp`, one bounded page honoring `limit`/`sort` (unchanged
1615
+ * behavior). With `catchUp`, keyset-drain ALL matching pages (`_id`
1616
+ * ascending) up to `maxPages`/`maxDocs` and return the union. All pages are
1617
+ * gathered before the caller delivers `onSnapshot`, so the existing
1618
+ * snapshot-ordering buffer only unblocks live `onChange` after the full
1619
+ * drain. A thrown error (e.g. `SCAN_LIMIT_EXCEEDED`, abort) propagates to the
1620
+ * caller's existing snapshot try/catch.
1621
+ */
1622
+ private resolveSnapshotDocs;
1577
1623
  subscribeWithSnapshot(opts: SubscribeWithSnapshotOptions<T>): Promise<() => void>;
1578
1624
  }
1579
1625
  declare class DatabaseModule {
package/dist/index.js CHANGED
@@ -2321,6 +2321,9 @@ function isTimelineSourceStats(value) {
2321
2321
  }
2322
2322
 
2323
2323
  // libs/sdk/src/modules/database.module.ts
2324
+ var DEFAULT_CATCHUP_MAX_PAGES = 1e3;
2325
+ var DEFAULT_CATCHUP_MAX_DOCS = 5e4;
2326
+ var DEFAULT_CATCHUP_PAGE_SIZE = 100;
2324
2327
  var Paginator = class {
2325
2328
  constructor(http, basePath, opts) {
2326
2329
  this.http = http;
@@ -2758,6 +2761,54 @@ var CollectionRef = class {
2758
2761
  * aborts the request and silences the AbortError. Gap-recovery state
2759
2762
  * machine lands in Task 6.
2760
2763
  */
2764
+ /**
2765
+ * #699 P1 — resolve the snapshot documents for `subscribeWithSnapshot`.
2766
+ * Without `catchUp`, one bounded page honoring `limit`/`sort` (unchanged
2767
+ * behavior). With `catchUp`, keyset-drain ALL matching pages (`_id`
2768
+ * ascending) up to `maxPages`/`maxDocs` and return the union. All pages are
2769
+ * gathered before the caller delivers `onSnapshot`, so the existing
2770
+ * snapshot-ordering buffer only unblocks live `onChange` after the full
2771
+ * drain. A thrown error (e.g. `SCAN_LIMIT_EXCEEDED`, abort) propagates to the
2772
+ * caller's existing snapshot try/catch.
2773
+ */
2774
+ async resolveSnapshotDocs(opts, signal) {
2775
+ if (!opts.catchUp) {
2776
+ const builder = this.find(opts.where);
2777
+ if (opts.sort) builder.sort(opts.sort);
2778
+ if (opts.limit !== void 0) builder.limit(opts.limit);
2779
+ if (opts.select) builder.select(opts.select);
2780
+ const result = await builder.execute(signal);
2781
+ return result.documents;
2782
+ }
2783
+ const asCap = (v, def) => typeof v === "number" && Number.isFinite(v) && v >= 0 ? Math.floor(v) : def;
2784
+ const asPageSize = (v, def) => typeof v === "number" && Number.isFinite(v) && v >= 1 ? Math.floor(v) : def;
2785
+ const maxPages = asCap(opts.catchUp.maxPages, DEFAULT_CATCHUP_MAX_PAGES);
2786
+ const maxDocs = asCap(opts.catchUp.maxDocs, DEFAULT_CATCHUP_MAX_DOCS);
2787
+ const pageSize = asPageSize(opts.limit, DEFAULT_CATCHUP_PAGE_SIZE);
2788
+ const all = [];
2789
+ let cursor;
2790
+ for (let page = 0; page < maxPages && all.length < maxDocs; page++) {
2791
+ const builder = this.find(opts.where).sort({ _id: 1 }).limit(pageSize);
2792
+ if (opts.select) builder.select(opts.select);
2793
+ const result = cursor === void 0 ? await builder.execute(signal) : await builder.after(cursor).execute(signal);
2794
+ const docs = result.documents;
2795
+ if (docs.length === 0) break;
2796
+ const room = maxDocs - all.length;
2797
+ if (docs.length >= room) {
2798
+ all.push(...docs.slice(0, room));
2799
+ break;
2800
+ }
2801
+ all.push(...docs);
2802
+ const more = result.mode === "cursor" ? result.hasMore : result.hasMore ?? docs.length >= pageSize;
2803
+ if (!more) break;
2804
+ const nextCursor = docs[docs.length - 1]._id;
2805
+ if (nextCursor === void 0 || nextCursor === null || cursor !== void 0 && nextCursor <= cursor) {
2806
+ break;
2807
+ }
2808
+ cursor = nextCursor;
2809
+ }
2810
+ return all;
2811
+ }
2761
2812
  async subscribeWithSnapshot(opts) {
2762
2813
  if (!this.realtime) {
2763
2814
  throw new Error("Realtime not available: no RealtimeClient configured");
@@ -2829,12 +2880,7 @@ var CollectionRef = class {
2829
2880
  snapshotAbort = new AbortController();
2830
2881
  let recoveryDocs;
2831
2882
  try {
2832
- const builder = this.find(opts.where);
2833
- if (opts.sort) builder.sort(opts.sort);
2834
- if (opts.limit !== void 0) builder.limit(opts.limit);
2835
- if (opts.select) builder.select(opts.select);
2836
- const result = await builder.execute(snapshotAbort.signal);
2837
- recoveryDocs = result.documents;
2883
+ recoveryDocs = await this.resolveSnapshotDocs(opts, snapshotAbort.signal);
2838
2884
  } catch (err) {
2839
2885
  if (!isActive) return;
2840
2886
  if (err instanceof DOMException && err.name === "AbortError") return;
@@ -2977,12 +3023,7 @@ var CollectionRef = class {
2977
3023
  currentUnsubscribe = initial.unsubscribe;
2978
3024
  let snapshotDocs;
2979
3025
  try {
2980
- const builder = this.find(opts.where);
2981
- if (opts.sort) builder.sort(opts.sort);
2982
- if (opts.limit !== void 0) builder.limit(opts.limit);
2983
- if (opts.select) builder.select(opts.select);
2984
- const result = await builder.execute(snapshotAbort.signal);
2985
- snapshotDocs = result.documents;
3026
+ snapshotDocs = await this.resolveSnapshotDocs(opts, snapshotAbort.signal);
2986
3027
  } catch (err) {
2987
3028
  currentUnsubscribe?.();
2988
3029
  isActive = false;