@koolbase/core 12.47.0 → 12.49.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.
@@ -421,6 +421,16 @@ class KoolbaseDatabase {
421
421
  if (operations.length === 0) {
422
422
  throw new Error('batch requires at least one operation');
423
423
  }
424
+ // The collections an update or delete addresses by id, from the device's
425
+ // saved copies, read before the request (12.49.0): a delete leaves nothing
426
+ // to read afterwards.
427
+ const userIdBefore = this.getUserId() ?? 'anonymous';
428
+ const byId = new Map();
429
+ for (const op of operations) {
430
+ if (op.type === 'update' || op.type === 'delete') {
431
+ byId.set(op.recordId, (await (0, cache_store_js_1.getCachedRecord)(userIdBefore, op.recordId))?.collection);
432
+ }
433
+ }
424
434
  const body = await this.request('POST', '/v1/sdk/db/batch', {
425
435
  operations: operations.map(batchOpToWire),
426
436
  });
@@ -446,16 +456,35 @@ class KoolbaseDatabase {
446
456
  await (0, cache_store_js_1.cacheRecord)(userId, r.record.collection, r.record.id, r.record.data, r.record.revision);
447
457
  }
448
458
  }
459
+ // Every collection the batch wrote re-reads its live queries (12.49.0):
460
+ // an insert's or upsert's by its op, an update's by the record it returned
461
+ // or its saved copy, a delete's by its saved copy. One it cannot name --
462
+ // a record never read on this device -- re-runs every live query rather
463
+ // than a guess, as a single delete does.
449
464
  const touched = new Set();
450
- for (const op of operations) {
465
+ let unknown = false;
466
+ operations.forEach((op, i) => {
451
467
  if (op.type === 'insert' || op.type === 'upsert') {
452
468
  touched.add(op.collection);
469
+ return;
453
470
  }
471
+ const col = results[i]?.record?.collection ?? byId.get(op.recordId);
472
+ if (col)
473
+ touched.add(col);
474
+ else
475
+ unknown = true;
476
+ });
477
+ for (const op of operations) {
478
+ if (op.type === 'delete')
479
+ await (0, cache_store_js_1.removeCachedRecord)(userId, op.recordId);
454
480
  }
455
481
  for (const col of touched) {
456
482
  await (0, cache_store_js_3.invalidateCache)(userId, col);
457
- void (0, query_refresh_js_1.refreshCollectionQueries)(col);
483
+ if (!unknown)
484
+ void (0, query_refresh_js_1.refreshCollectionQueries)(col);
458
485
  }
486
+ if (unknown)
487
+ void (0, query_refresh_js_1.refreshCollectionQueries)(undefined);
459
488
  return results;
460
489
  }
461
490
  // ─── Get single record ──────────────────────────────────────────────────────
@@ -42,3 +42,4 @@ export { RestoreResult } from './types.js';
42
42
  export type { AuthStateListener, FetchLike, KoolbaseAuthStorage, ResendVerificationResult } from './types.js';
43
43
  export { setPlatform, getPlatform, memoryPlatform, type PlatformAdapter, type PlatformBroadcast, type PlatformStorage, type PlatformNetwork, type PlatformLifecycle, type PlatformLocks, type PlatformInfo, } from './platform.js';
44
44
  export * from './query-builder.js';
45
+ export { onCollectionWrite } from './query-refresh.js';
package/dist/cjs/index.js CHANGED
@@ -19,7 +19,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
19
19
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
20
20
  };
21
21
  Object.defineProperty(exports, "__esModule", { value: true });
22
- exports.memoryPlatform = exports.getPlatform = exports.setPlatform = exports.RestoreResult = exports.koolbaseSdkVersion = exports.getOrCreateDeviceId = exports.KoolbaseMessaging = exports.isPending = exports.isFiscalized = exports.decodeFiscalResponse = exports.KoolbaseFiscalError = exports.KoolbaseFiscal = exports.disabledAnalytics = exports.KoolbaseAnalytics = exports.initialUploadQueueState = exports.KoolbaseUploadQueue = exports.initialFileState = exports.KoolbaseFileController = exports.filesQueryKey = exports.initialFilesState = exports.KoolbaseFilesController = exports.KoolbaseUploadTask = exports.KoolbaseStorage = exports.KoolbaseConnectivity = exports.KoolbaseRealtime = exports.KoolbaseFunctions = exports.KoolbaseFlags = exports.initialRecordState = exports.KoolbaseRecordController = exports.initialCollectionState = exports.collectionQueryKey = exports.KoolbaseCollectionController = exports.KoolbaseDatabase = exports.KoolbaseAppAdmin = exports.KoolbaseAuth = void 0;
22
+ exports.onCollectionWrite = exports.memoryPlatform = exports.getPlatform = exports.setPlatform = exports.RestoreResult = exports.koolbaseSdkVersion = exports.getOrCreateDeviceId = exports.KoolbaseMessaging = exports.isPending = exports.isFiscalized = exports.decodeFiscalResponse = exports.KoolbaseFiscalError = exports.KoolbaseFiscal = exports.disabledAnalytics = exports.KoolbaseAnalytics = exports.initialUploadQueueState = exports.KoolbaseUploadQueue = exports.initialFileState = exports.KoolbaseFileController = exports.filesQueryKey = exports.initialFilesState = exports.KoolbaseFilesController = exports.KoolbaseUploadTask = exports.KoolbaseStorage = exports.KoolbaseConnectivity = exports.KoolbaseRealtime = exports.KoolbaseFunctions = exports.KoolbaseFlags = exports.initialRecordState = exports.KoolbaseRecordController = exports.initialCollectionState = exports.collectionQueryKey = exports.KoolbaseCollectionController = exports.KoolbaseDatabase = exports.KoolbaseAppAdmin = exports.KoolbaseAuth = void 0;
23
23
  __exportStar(require("./types.js"), exports);
24
24
  __exportStar(require("./errors.js"), exports);
25
25
  __exportStar(require("./network.js"), exports);
@@ -91,3 +91,6 @@ Object.defineProperty(exports, "setPlatform", { enumerable: true, get: function
91
91
  Object.defineProperty(exports, "getPlatform", { enumerable: true, get: function () { return platform_js_1.getPlatform; } });
92
92
  Object.defineProperty(exports, "memoryPlatform", { enumerable: true, get: function () { return platform_js_1.memoryPlatform; } });
93
93
  __exportStar(require("./query-builder.js"), exports);
94
+ // Re-read after the app's own writes (12.48.0): counts, totals, charts.
95
+ var query_refresh_js_1 = require("./query-refresh.js");
96
+ Object.defineProperty(exports, "onCollectionWrite", { enumerable: true, get: function () { return query_refresh_js_1.onCollectionWrite; } });
@@ -6,5 +6,13 @@ export declare function registerQueryRefresher(collection: string, refresh: () =
6
6
  * this device). One after another; a failure in one never stops the others.
7
7
  */
8
8
  export declare function refreshCollectionQueries(collection?: string): Promise<void>;
9
+ /**
10
+ * Runs [run] after every write this app makes to [collection] -- an insert,
11
+ * update, upsert or delete, a batch, a synced offline write -- as a list on
12
+ * screen re-reads its records. For anything else drawn from the collection:
13
+ * a count, a total, a chart. Returns the stop. A failure in [run] never
14
+ * stops the others, and never fails the write.
15
+ */
16
+ export declare function onCollectionWrite(collection: string, run: () => void | Promise<void>): () => void;
9
17
  /** Tests only. */
10
18
  export declare function clearQueryRefreshers(): void;
@@ -6,6 +6,7 @@
6
6
  Object.defineProperty(exports, "__esModule", { value: true });
7
7
  exports.registerQueryRefresher = registerQueryRefresher;
8
8
  exports.refreshCollectionQueries = refreshCollectionQueries;
9
+ exports.onCollectionWrite = onCollectionWrite;
9
10
  exports.clearQueryRefreshers = clearQueryRefreshers;
10
11
  const refreshers = new Map();
11
12
  let next = 0;
@@ -35,6 +36,18 @@ async function refreshCollectionQueries(collection) {
35
36
  }
36
37
  }
37
38
  }
39
+ /**
40
+ * Runs [run] after every write this app makes to [collection] -- an insert,
41
+ * update, upsert or delete, a batch, a synced offline write -- as a list on
42
+ * screen re-reads its records. For anything else drawn from the collection:
43
+ * a count, a total, a chart. Returns the stop. A failure in [run] never
44
+ * stops the others, and never fails the write.
45
+ */
46
+ function onCollectionWrite(collection, run) {
47
+ return registerQueryRefresher(collection, async () => {
48
+ await run();
49
+ });
50
+ }
38
51
  /** Tests only. */
39
52
  function clearQueryRefreshers() {
40
53
  refreshers.clear();
@@ -418,6 +418,16 @@ export class KoolbaseDatabase {
418
418
  if (operations.length === 0) {
419
419
  throw new Error('batch requires at least one operation');
420
420
  }
421
+ // The collections an update or delete addresses by id, from the device's
422
+ // saved copies, read before the request (12.49.0): a delete leaves nothing
423
+ // to read afterwards.
424
+ const userIdBefore = this.getUserId() ?? 'anonymous';
425
+ const byId = new Map();
426
+ for (const op of operations) {
427
+ if (op.type === 'update' || op.type === 'delete') {
428
+ byId.set(op.recordId, (await getCachedRecord(userIdBefore, op.recordId))?.collection);
429
+ }
430
+ }
421
431
  const body = await this.request('POST', '/v1/sdk/db/batch', {
422
432
  operations: operations.map(batchOpToWire),
423
433
  });
@@ -443,16 +453,35 @@ export class KoolbaseDatabase {
443
453
  await cacheRecord(userId, r.record.collection, r.record.id, r.record.data, r.record.revision);
444
454
  }
445
455
  }
456
+ // Every collection the batch wrote re-reads its live queries (12.49.0):
457
+ // an insert's or upsert's by its op, an update's by the record it returned
458
+ // or its saved copy, a delete's by its saved copy. One it cannot name --
459
+ // a record never read on this device -- re-runs every live query rather
460
+ // than a guess, as a single delete does.
446
461
  const touched = new Set();
447
- for (const op of operations) {
462
+ let unknown = false;
463
+ operations.forEach((op, i) => {
448
464
  if (op.type === 'insert' || op.type === 'upsert') {
449
465
  touched.add(op.collection);
466
+ return;
450
467
  }
468
+ const col = results[i]?.record?.collection ?? byId.get(op.recordId);
469
+ if (col)
470
+ touched.add(col);
471
+ else
472
+ unknown = true;
473
+ });
474
+ for (const op of operations) {
475
+ if (op.type === 'delete')
476
+ await removeCachedRecord(userId, op.recordId);
451
477
  }
452
478
  for (const col of touched) {
453
479
  await invalidateCache(userId, col);
454
- void refreshCollectionQueries(col);
480
+ if (!unknown)
481
+ void refreshCollectionQueries(col);
455
482
  }
483
+ if (unknown)
484
+ void refreshCollectionQueries(undefined);
456
485
  return results;
457
486
  }
458
487
  // ─── Get single record ──────────────────────────────────────────────────────
@@ -42,3 +42,4 @@ export { RestoreResult } from './types.js';
42
42
  export type { AuthStateListener, FetchLike, KoolbaseAuthStorage, ResendVerificationResult } from './types.js';
43
43
  export { setPlatform, getPlatform, memoryPlatform, type PlatformAdapter, type PlatformBroadcast, type PlatformStorage, type PlatformNetwork, type PlatformLifecycle, type PlatformLocks, type PlatformInfo, } from './platform.js';
44
44
  export * from './query-builder.js';
45
+ export { onCollectionWrite } from './query-refresh.js';
package/dist/esm/index.js CHANGED
@@ -39,3 +39,5 @@ export { koolbaseSdkVersion } from './device-metadata.js';
39
39
  export { RestoreResult } from './types.js';
40
40
  export { setPlatform, getPlatform, memoryPlatform, } from './platform.js';
41
41
  export * from './query-builder.js';
42
+ // Re-read after the app's own writes (12.48.0): counts, totals, charts.
43
+ export { onCollectionWrite } from './query-refresh.js';
@@ -6,5 +6,13 @@ export declare function registerQueryRefresher(collection: string, refresh: () =
6
6
  * this device). One after another; a failure in one never stops the others.
7
7
  */
8
8
  export declare function refreshCollectionQueries(collection?: string): Promise<void>;
9
+ /**
10
+ * Runs [run] after every write this app makes to [collection] -- an insert,
11
+ * update, upsert or delete, a batch, a synced offline write -- as a list on
12
+ * screen re-reads its records. For anything else drawn from the collection:
13
+ * a count, a total, a chart. Returns the stop. A failure in [run] never
14
+ * stops the others, and never fails the write.
15
+ */
16
+ export declare function onCollectionWrite(collection: string, run: () => void | Promise<void>): () => void;
9
17
  /** Tests only. */
10
18
  export declare function clearQueryRefreshers(): void;
@@ -30,6 +30,18 @@ export async function refreshCollectionQueries(collection) {
30
30
  }
31
31
  }
32
32
  }
33
+ /**
34
+ * Runs [run] after every write this app makes to [collection] -- an insert,
35
+ * update, upsert or delete, a batch, a synced offline write -- as a list on
36
+ * screen re-reads its records. For anything else drawn from the collection:
37
+ * a count, a total, a chart. Returns the stop. A failure in [run] never
38
+ * stops the others, and never fails the write.
39
+ */
40
+ export function onCollectionWrite(collection, run) {
41
+ return registerQueryRefresher(collection, async () => {
42
+ await run();
43
+ });
44
+ }
33
45
  /** Tests only. */
34
46
  export function clearQueryRefreshers() {
35
47
  refreshers.clear();
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "@koolbase/core",
3
- "version": "12.47.0",
4
- "description": "Koolbase SDK core \u2014 shared behaviour behind @koolbase/react-native and @koolbase/js. Install one of those, not this.",
3
+ "version": "12.49.0",
4
+ "description": "Koolbase SDK core — shared behaviour behind @koolbase/react-native and @koolbase/js. Install one of those, not this.",
5
5
  "main": "./dist/cjs/index.js",
6
+ "react-native": "./dist/esm/index.js",
6
7
  "types": "./dist/esm/index.d.ts",
7
8
  "files": [
8
9
  "dist",
@@ -2,6 +2,7 @@
2
2
  "//": "For resolvers that ignore \"exports\" (TypeScript node10, older bundlers): the ./react entry.",
3
3
  "main": "../dist/cjs/react/index.js",
4
4
  "module": "../dist/esm/react/index.js",
5
+ "react-native": "../dist/esm/react/index.js",
5
6
  "types": "../dist/cjs/react/index.d.ts",
6
7
  "sideEffects": false
7
8
  }