@koolbase/core 12.48.0 → 12.50.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.
@@ -188,11 +188,27 @@ export declare class KoolbaseProviderInvalidError extends KoolbaseDataError {
188
188
  export declare class KoolbaseRevisionMismatchError extends KoolbaseDataError {
189
189
  readonly expectedRevision?: number | undefined;
190
190
  readonly currentRevision?: number | undefined;
191
- /** The record as the server holds it now. */
191
+ /**
192
+ * The record as the server holds it now. Absent when the caller may not
193
+ * read it ({@link readable} false): a failed write never discloses it.
194
+ */
192
195
  readonly current?: Record<string, unknown> | undefined;
196
+ /**
197
+ * Whether the caller may read the record now. False: the server withheld
198
+ * it and said only the revisions. Undefined from an older API.
199
+ */
200
+ readonly readable?: boolean | undefined;
193
201
  constructor(message?: string, expectedRevision?: number | undefined, currentRevision?: number | undefined,
194
- /** The record as the server holds it now. */
195
- current?: Record<string, unknown> | undefined);
202
+ /**
203
+ * The record as the server holds it now. Absent when the caller may not
204
+ * read it ({@link readable} false): a failed write never discloses it.
205
+ */
206
+ current?: Record<string, unknown> | undefined,
207
+ /**
208
+ * Whether the caller may read the record now. False: the server withheld
209
+ * it and said only the revisions. Undefined from an older API.
210
+ */
211
+ readable?: boolean | undefined);
196
212
  }
197
213
  /**
198
214
  * The same idempotency key sent with different data.
@@ -253,12 +253,21 @@ class KoolbaseProviderInvalidError extends KoolbaseDataError {
253
253
  exports.KoolbaseProviderInvalidError = KoolbaseProviderInvalidError;
254
254
  class KoolbaseRevisionMismatchError extends KoolbaseDataError {
255
255
  constructor(message, expectedRevision, currentRevision,
256
- /** The record as the server holds it now. */
257
- current) {
256
+ /**
257
+ * The record as the server holds it now. Absent when the caller may not
258
+ * read it ({@link readable} false): a failed write never discloses it.
259
+ */
260
+ current,
261
+ /**
262
+ * Whether the caller may read the record now. False: the server withheld
263
+ * it and said only the revisions. Undefined from an older API.
264
+ */
265
+ readable) {
258
266
  super(message ?? 'The record has changed since you read it', 'revision_mismatch');
259
267
  this.expectedRevision = expectedRevision;
260
268
  this.currentRevision = currentRevision;
261
269
  this.current = current;
270
+ this.readable = readable;
262
271
  this.name = 'KoolbaseRevisionMismatchError';
263
272
  Object.setPrototypeOf(this, KoolbaseRevisionMismatchError.prototype);
264
273
  }
@@ -595,7 +604,7 @@ function koolbaseDataError(status, body, fallbackMessage = 'Request failed') {
595
604
  // exactly this. attach() still puts the whole bag on the error, so
596
605
  // nothing is lost for a caller reading it directly.
597
606
  const d = (body?.details ?? {});
598
- return attach(new KoolbaseRevisionMismatchError(message, d.expected_revision, d.current_revision, d.record));
607
+ return attach(new KoolbaseRevisionMismatchError(message, d.expected_revision, d.current_revision, d.record, typeof d.readable === 'boolean' ? d.readable : undefined));
599
608
  }
600
609
  // Two codes, one situation — the database and fiscal packages name it
601
610
  // differently and a caller should not have to know which spoke.
@@ -33,9 +33,18 @@ function batchOpToWire(op) {
33
33
  case 'insert':
34
34
  return { type: 'insert', collection: op.collection, data: op.data };
35
35
  case 'update':
36
- return { type: 'update', record_id: op.recordId, data: op.data };
36
+ return {
37
+ type: 'update',
38
+ record_id: op.recordId,
39
+ data: op.data,
40
+ ...(op.expectedRevision !== undefined ? { expected_revision: op.expectedRevision } : {}),
41
+ };
37
42
  case 'delete':
38
- return { type: 'delete', record_id: op.recordId };
43
+ return {
44
+ type: 'delete',
45
+ record_id: op.recordId,
46
+ ...(op.expectedRevision !== undefined ? { expected_revision: op.expectedRevision } : {}),
47
+ };
39
48
  case 'upsert':
40
49
  return {
41
50
  type: 'upsert',
@@ -421,6 +430,16 @@ class KoolbaseDatabase {
421
430
  if (operations.length === 0) {
422
431
  throw new Error('batch requires at least one operation');
423
432
  }
433
+ // The collections an update or delete addresses by id, from the device's
434
+ // saved copies, read before the request (12.49.0): a delete leaves nothing
435
+ // to read afterwards.
436
+ const userIdBefore = this.getUserId() ?? 'anonymous';
437
+ const byId = new Map();
438
+ for (const op of operations) {
439
+ if (op.type === 'update' || op.type === 'delete') {
440
+ byId.set(op.recordId, (await (0, cache_store_js_1.getCachedRecord)(userIdBefore, op.recordId))?.collection);
441
+ }
442
+ }
424
443
  const body = await this.request('POST', '/v1/sdk/db/batch', {
425
444
  operations: operations.map(batchOpToWire),
426
445
  });
@@ -446,16 +465,35 @@ class KoolbaseDatabase {
446
465
  await (0, cache_store_js_1.cacheRecord)(userId, r.record.collection, r.record.id, r.record.data, r.record.revision);
447
466
  }
448
467
  }
468
+ // Every collection the batch wrote re-reads its live queries (12.49.0):
469
+ // an insert's or upsert's by its op, an update's by the record it returned
470
+ // or its saved copy, a delete's by its saved copy. One it cannot name --
471
+ // a record never read on this device -- re-runs every live query rather
472
+ // than a guess, as a single delete does.
449
473
  const touched = new Set();
450
- for (const op of operations) {
474
+ let unknown = false;
475
+ operations.forEach((op, i) => {
451
476
  if (op.type === 'insert' || op.type === 'upsert') {
452
477
  touched.add(op.collection);
478
+ return;
453
479
  }
480
+ const col = results[i]?.record?.collection ?? byId.get(op.recordId);
481
+ if (col)
482
+ touched.add(col);
483
+ else
484
+ unknown = true;
485
+ });
486
+ for (const op of operations) {
487
+ if (op.type === 'delete')
488
+ await (0, cache_store_js_1.removeCachedRecord)(userId, op.recordId);
454
489
  }
455
490
  for (const col of touched) {
456
491
  await (0, cache_store_js_3.invalidateCache)(userId, col);
457
- void (0, query_refresh_js_1.refreshCollectionQueries)(col);
492
+ if (!unknown)
493
+ void (0, query_refresh_js_1.refreshCollectionQueries)(col);
458
494
  }
495
+ if (unknown)
496
+ void (0, query_refresh_js_1.refreshCollectionQueries)(undefined);
459
497
  return results;
460
498
  }
461
499
  // ─── Get single record ──────────────────────────────────────────────────────
@@ -719,9 +719,13 @@ export type BatchOp = {
719
719
  type: 'update';
720
720
  recordId: string;
721
721
  data: Record<string, unknown>;
722
+ /** Conditional: applies only if the record still carries this revision; a mismatch rolls back the whole batch. */
723
+ expectedRevision?: number;
722
724
  } | {
723
725
  type: 'delete';
724
726
  recordId: string;
727
+ /** Conditional, as update's (needs the API with conditional batch deletes). */
728
+ expectedRevision?: number;
725
729
  } | {
726
730
  type: 'upsert';
727
731
  collection: string;
@@ -735,8 +739,12 @@ export type BatchOp = {
735
739
  */
736
740
  export declare const BatchOp: {
737
741
  insert: (collection: string, data: Record<string, unknown>) => BatchOp;
738
- update: (recordId: string, data: Record<string, unknown>) => BatchOp;
739
- delete: (recordId: string) => BatchOp;
742
+ update: (recordId: string, data: Record<string, unknown>, opts?: {
743
+ expectedRevision?: number;
744
+ }) => BatchOp;
745
+ delete: (recordId: string, opts?: {
746
+ expectedRevision?: number;
747
+ }) => BatchOp;
740
748
  upsert: (collection: string, opts: {
741
749
  match: Record<string, unknown>;
742
750
  data: Record<string, unknown>;
package/dist/cjs/types.js CHANGED
@@ -29,8 +29,17 @@ var FunctionRuntime;
29
29
  */
30
30
  exports.BatchOp = {
31
31
  insert: (collection, data) => ({ type: 'insert', collection, data }),
32
- update: (recordId, data) => ({ type: 'update', recordId, data }),
33
- delete: (recordId) => ({ type: 'delete', recordId }),
32
+ update: (recordId, data, opts) => ({
33
+ type: 'update',
34
+ recordId,
35
+ data,
36
+ ...(opts?.expectedRevision !== undefined ? { expectedRevision: opts.expectedRevision } : {}),
37
+ }),
38
+ delete: (recordId, opts) => ({
39
+ type: 'delete',
40
+ recordId,
41
+ ...(opts?.expectedRevision !== undefined ? { expectedRevision: opts.expectedRevision } : {}),
42
+ }),
34
43
  upsert: (collection, opts) => ({
35
44
  type: 'upsert',
36
45
  collection,
@@ -188,11 +188,27 @@ export declare class KoolbaseProviderInvalidError extends KoolbaseDataError {
188
188
  export declare class KoolbaseRevisionMismatchError extends KoolbaseDataError {
189
189
  readonly expectedRevision?: number | undefined;
190
190
  readonly currentRevision?: number | undefined;
191
- /** The record as the server holds it now. */
191
+ /**
192
+ * The record as the server holds it now. Absent when the caller may not
193
+ * read it ({@link readable} false): a failed write never discloses it.
194
+ */
192
195
  readonly current?: Record<string, unknown> | undefined;
196
+ /**
197
+ * Whether the caller may read the record now. False: the server withheld
198
+ * it and said only the revisions. Undefined from an older API.
199
+ */
200
+ readonly readable?: boolean | undefined;
193
201
  constructor(message?: string, expectedRevision?: number | undefined, currentRevision?: number | undefined,
194
- /** The record as the server holds it now. */
195
- current?: Record<string, unknown> | undefined);
202
+ /**
203
+ * The record as the server holds it now. Absent when the caller may not
204
+ * read it ({@link readable} false): a failed write never discloses it.
205
+ */
206
+ current?: Record<string, unknown> | undefined,
207
+ /**
208
+ * Whether the caller may read the record now. False: the server withheld
209
+ * it and said only the revisions. Undefined from an older API.
210
+ */
211
+ readable?: boolean | undefined);
196
212
  }
197
213
  /**
198
214
  * The same idempotency key sent with different data.
@@ -233,12 +233,21 @@ export class KoolbaseProviderInvalidError extends KoolbaseDataError {
233
233
  }
234
234
  export class KoolbaseRevisionMismatchError extends KoolbaseDataError {
235
235
  constructor(message, expectedRevision, currentRevision,
236
- /** The record as the server holds it now. */
237
- current) {
236
+ /**
237
+ * The record as the server holds it now. Absent when the caller may not
238
+ * read it ({@link readable} false): a failed write never discloses it.
239
+ */
240
+ current,
241
+ /**
242
+ * Whether the caller may read the record now. False: the server withheld
243
+ * it and said only the revisions. Undefined from an older API.
244
+ */
245
+ readable) {
238
246
  super(message ?? 'The record has changed since you read it', 'revision_mismatch');
239
247
  this.expectedRevision = expectedRevision;
240
248
  this.currentRevision = currentRevision;
241
249
  this.current = current;
250
+ this.readable = readable;
242
251
  this.name = 'KoolbaseRevisionMismatchError';
243
252
  Object.setPrototypeOf(this, KoolbaseRevisionMismatchError.prototype);
244
253
  }
@@ -557,7 +566,7 @@ export function koolbaseDataError(status, body, fallbackMessage = 'Request faile
557
566
  // exactly this. attach() still puts the whole bag on the error, so
558
567
  // nothing is lost for a caller reading it directly.
559
568
  const d = (body?.details ?? {});
560
- return attach(new KoolbaseRevisionMismatchError(message, d.expected_revision, d.current_revision, d.record));
569
+ return attach(new KoolbaseRevisionMismatchError(message, d.expected_revision, d.current_revision, d.record, typeof d.readable === 'boolean' ? d.readable : undefined));
561
570
  }
562
571
  // Two codes, one situation — the database and fiscal packages name it
563
572
  // differently and a caller should not have to know which spoke.
@@ -30,9 +30,18 @@ function batchOpToWire(op) {
30
30
  case 'insert':
31
31
  return { type: 'insert', collection: op.collection, data: op.data };
32
32
  case 'update':
33
- return { type: 'update', record_id: op.recordId, data: op.data };
33
+ return {
34
+ type: 'update',
35
+ record_id: op.recordId,
36
+ data: op.data,
37
+ ...(op.expectedRevision !== undefined ? { expected_revision: op.expectedRevision } : {}),
38
+ };
34
39
  case 'delete':
35
- return { type: 'delete', record_id: op.recordId };
40
+ return {
41
+ type: 'delete',
42
+ record_id: op.recordId,
43
+ ...(op.expectedRevision !== undefined ? { expected_revision: op.expectedRevision } : {}),
44
+ };
36
45
  case 'upsert':
37
46
  return {
38
47
  type: 'upsert',
@@ -418,6 +427,16 @@ export class KoolbaseDatabase {
418
427
  if (operations.length === 0) {
419
428
  throw new Error('batch requires at least one operation');
420
429
  }
430
+ // The collections an update or delete addresses by id, from the device's
431
+ // saved copies, read before the request (12.49.0): a delete leaves nothing
432
+ // to read afterwards.
433
+ const userIdBefore = this.getUserId() ?? 'anonymous';
434
+ const byId = new Map();
435
+ for (const op of operations) {
436
+ if (op.type === 'update' || op.type === 'delete') {
437
+ byId.set(op.recordId, (await getCachedRecord(userIdBefore, op.recordId))?.collection);
438
+ }
439
+ }
421
440
  const body = await this.request('POST', '/v1/sdk/db/batch', {
422
441
  operations: operations.map(batchOpToWire),
423
442
  });
@@ -443,16 +462,35 @@ export class KoolbaseDatabase {
443
462
  await cacheRecord(userId, r.record.collection, r.record.id, r.record.data, r.record.revision);
444
463
  }
445
464
  }
465
+ // Every collection the batch wrote re-reads its live queries (12.49.0):
466
+ // an insert's or upsert's by its op, an update's by the record it returned
467
+ // or its saved copy, a delete's by its saved copy. One it cannot name --
468
+ // a record never read on this device -- re-runs every live query rather
469
+ // than a guess, as a single delete does.
446
470
  const touched = new Set();
447
- for (const op of operations) {
471
+ let unknown = false;
472
+ operations.forEach((op, i) => {
448
473
  if (op.type === 'insert' || op.type === 'upsert') {
449
474
  touched.add(op.collection);
475
+ return;
450
476
  }
477
+ const col = results[i]?.record?.collection ?? byId.get(op.recordId);
478
+ if (col)
479
+ touched.add(col);
480
+ else
481
+ unknown = true;
482
+ });
483
+ for (const op of operations) {
484
+ if (op.type === 'delete')
485
+ await removeCachedRecord(userId, op.recordId);
451
486
  }
452
487
  for (const col of touched) {
453
488
  await invalidateCache(userId, col);
454
- void refreshCollectionQueries(col);
489
+ if (!unknown)
490
+ void refreshCollectionQueries(col);
455
491
  }
492
+ if (unknown)
493
+ void refreshCollectionQueries(undefined);
456
494
  return results;
457
495
  }
458
496
  // ─── Get single record ──────────────────────────────────────────────────────
@@ -719,9 +719,13 @@ export type BatchOp = {
719
719
  type: 'update';
720
720
  recordId: string;
721
721
  data: Record<string, unknown>;
722
+ /** Conditional: applies only if the record still carries this revision; a mismatch rolls back the whole batch. */
723
+ expectedRevision?: number;
722
724
  } | {
723
725
  type: 'delete';
724
726
  recordId: string;
727
+ /** Conditional, as update's (needs the API with conditional batch deletes). */
728
+ expectedRevision?: number;
725
729
  } | {
726
730
  type: 'upsert';
727
731
  collection: string;
@@ -735,8 +739,12 @@ export type BatchOp = {
735
739
  */
736
740
  export declare const BatchOp: {
737
741
  insert: (collection: string, data: Record<string, unknown>) => BatchOp;
738
- update: (recordId: string, data: Record<string, unknown>) => BatchOp;
739
- delete: (recordId: string) => BatchOp;
742
+ update: (recordId: string, data: Record<string, unknown>, opts?: {
743
+ expectedRevision?: number;
744
+ }) => BatchOp;
745
+ delete: (recordId: string, opts?: {
746
+ expectedRevision?: number;
747
+ }) => BatchOp;
740
748
  upsert: (collection: string, opts: {
741
749
  match: Record<string, unknown>;
742
750
  data: Record<string, unknown>;
package/dist/esm/types.js CHANGED
@@ -26,8 +26,17 @@ export var FunctionRuntime;
26
26
  */
27
27
  export const BatchOp = {
28
28
  insert: (collection, data) => ({ type: 'insert', collection, data }),
29
- update: (recordId, data) => ({ type: 'update', recordId, data }),
30
- delete: (recordId) => ({ type: 'delete', recordId }),
29
+ update: (recordId, data, opts) => ({
30
+ type: 'update',
31
+ recordId,
32
+ data,
33
+ ...(opts?.expectedRevision !== undefined ? { expectedRevision: opts.expectedRevision } : {}),
34
+ }),
35
+ delete: (recordId, opts) => ({
36
+ type: 'delete',
37
+ recordId,
38
+ ...(opts?.expectedRevision !== undefined ? { expectedRevision: opts.expectedRevision } : {}),
39
+ }),
31
40
  upsert: (collection, opts) => ({
32
41
  type: 'upsert',
33
42
  collection,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@koolbase/core",
3
- "version": "12.48.0",
3
+ "version": "12.50.0",
4
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
6
  "react-native": "./dist/esm/index.js",