@tanstack/db-sqlite-persistence-core 0.4.3 → 0.4.5

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.
@@ -358,6 +358,12 @@ export interface PersistedCollectionCoordinator {
358
358
  */
359
359
  requestEnsurePersistedIndex: (collectionId: string, signature: string, spec: PersistedIndexSpec, scopedAdapter?: HydrationPersistenceAdapter, localEnsureCompleted?: boolean) => Promise<void>;
360
360
  requestApplyLocalMutations?: (collectionId: string, mutations: Array<PersistedMutationEnvelope>) => Promise<ApplyLocalMutationsResponse>;
361
+ /**
362
+ * Applies a committed transaction through the supplied adapter when present.
363
+ * Wrappers must forward this leader-local loan: scheduling it again can wait
364
+ * on the hydration that is awaiting this call. Never serialize or retain the
365
+ * loan after the call; ordinary work uses the registered collection adapter.
366
+ */
361
367
  requestApplyCommittedTx: (collectionId: string, tx: PersistedTx, scopedAdapter?: HydrationPersistenceAdapter) => Promise<ApplyCommittedTxResponse>;
362
368
  /** The scoped adapter is leader-local and is never serialized to a follower. */
363
369
  pullSince?: (collectionId: string, fromRowVersion: number, scopedAdapter?: HydrationPersistenceAdapter) => Promise<PullSinceResponse>;
@@ -433,7 +439,7 @@ export declare class SingleProcessCoordinator implements PersistedCollectionCoor
433
439
  registerRemoteSubsetOwner(collectionId: string, owner: RemoteSubsetOwner): () => void;
434
440
  requestEnsurePersistedIndex(): Promise<void>;
435
441
  setAdapterForCollection(collectionId: string, adapter: PersistenceAdapter): void;
436
- requestApplyCommittedTx(collectionId: string, tx: PersistedTx): Promise<ApplyCommittedTxResponse>;
442
+ requestApplyCommittedTx(collectionId: string, tx: PersistedTx, scopedAdapter?: HydrationPersistenceAdapter): Promise<ApplyCommittedTxResponse>;
437
443
  pullSince(): Promise<PullSinceResponse>;
438
444
  }
439
445
  export declare function validatePersistedCollectionCoordinator(coordinator: PersistedCollectionCoordinator): void;
@@ -24,6 +24,12 @@ const typedArrayConstructors = [
24
24
  const NativeDate = Date;
25
25
  const identifierPattern = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
26
26
  function toTransportedLoadSubsetOptions(options) {
27
+ return visitLoadSubsetOptions(options, false);
28
+ }
29
+ function validateRemoteSubsetOptions(options) {
30
+ visitLoadSubsetOptions(options, true);
31
+ }
32
+ function visitLoadSubsetOptions(options, validateOnly) {
27
33
  assertPlainContainer(options, `options`);
28
34
  assertAllowedProperties(options, `options`, [
29
35
  `where`,
@@ -35,6 +41,7 @@ function toTransportedLoadSubsetOptions(options) {
35
41
  `subscription`
36
42
  ]);
37
43
  const state = {
44
+ validateOnly,
38
45
  expressions: /* @__PURE__ */ new WeakMap(),
39
46
  expressionArrays: /* @__PURE__ */ new WeakMap(),
40
47
  stringArrays: /* @__PURE__ */ new WeakMap(),
@@ -44,26 +51,31 @@ function toTransportedLoadSubsetOptions(options) {
44
51
  cursors: /* @__PURE__ */ new WeakMap(),
45
52
  wireValues: /* @__PURE__ */ new WeakMap()
46
53
  };
47
- const projected = {};
54
+ const projected = validateOnly ? options : {};
48
55
  const where = readDataProperty(options, `where`, `options.where`);
49
56
  const orderBy = readDataProperty(options, `orderBy`, `options.orderBy`);
50
57
  const limit = readDataProperty(options, `limit`, `options.limit`);
51
58
  const cursor = readDataProperty(options, `cursor`, `options.cursor`);
52
59
  const offset = readDataProperty(options, `offset`, `options.offset`);
53
60
  if (where.present && where.value !== void 0) {
54
- projected.where = projectExpression(where.value, `options.where`, state);
61
+ const result = projectExpression(where.value, `options.where`, state);
62
+ if (!state.validateOnly) projected.where = result;
55
63
  }
56
64
  if (orderBy.present && orderBy.value !== void 0) {
57
- projected.orderBy = projectOrderBy(orderBy.value, `options.orderBy`, state);
65
+ const result = projectOrderBy(orderBy.value, `options.orderBy`, state);
66
+ if (!state.validateOnly) projected.orderBy = result;
58
67
  }
59
68
  if (limit.present && limit.value !== void 0) {
60
- projected.limit = projectNumber(limit.value, `options.limit`);
69
+ const result = projectNumber(limit.value, `options.limit`);
70
+ if (!state.validateOnly) projected.limit = result;
61
71
  }
62
72
  if (cursor.present && cursor.value !== void 0) {
63
- projected.cursor = projectCursor(cursor.value, `options.cursor`, state);
73
+ const result = projectCursor(cursor.value, `options.cursor`, state);
74
+ if (!state.validateOnly) projected.cursor = result;
64
75
  }
65
76
  if (offset.present && offset.value !== void 0) {
66
- projected.offset = projectNumber(offset.value, `options.offset`);
77
+ const result = projectNumber(offset.value, `options.offset`);
78
+ if (!state.validateOnly) projected.offset = result;
67
79
  }
68
80
  return projected;
69
81
  }
@@ -97,65 +109,58 @@ function projectExpression(value, path, state) {
97
109
  case `ref`: {
98
110
  assertExpressionPrototype(object, path, db.IR.PropRef.prototype);
99
111
  assertAllowedProperties(object, path, [`type`, `path`, `sourceAlias`]);
100
- const projected = {
101
- type: `ref`,
102
- path: []
103
- };
112
+ const projected = state.validateOnly ? object : { type: `ref`, path: [] };
104
113
  state.expressions.set(object, projected);
105
114
  const sourcePath = readRequiredDataProperty(
106
115
  object,
107
116
  `path`,
108
117
  `${path}.path`
109
118
  );
110
- projected.path = projectStringArray(sourcePath, `${path}.path`, state);
119
+ const result = projectStringArray(sourcePath, `${path}.path`, state);
120
+ if (!state.validateOnly) projected.path = result;
111
121
  const sourceAlias = readDataProperty(
112
122
  object,
113
123
  `sourceAlias`,
114
124
  `${path}.sourceAlias`
115
125
  );
116
126
  if (sourceAlias.present) {
117
- if (typeof sourceAlias.value !== `string` || projected.path[0] !== sourceAlias.value) {
127
+ if (typeof sourceAlias.value !== `string` || result[0] !== sourceAlias.value) {
118
128
  throw new RemoteSubsetWireValueError(
119
129
  `${path}.sourceAlias`,
120
130
  `source alias must match the first path segment`
121
131
  );
122
132
  }
123
- projected.sourceAlias = sourceAlias.value;
133
+ if (!state.validateOnly) projected.sourceAlias = sourceAlias.value;
124
134
  }
125
135
  return projected;
126
136
  }
127
137
  case `val`: {
128
138
  assertExpressionPrototype(object, path, db.IR.Value.prototype);
129
139
  assertAllowedProperties(object, path, [`type`, `value`]);
130
- const projected = {
131
- type: `val`,
132
- value: void 0
133
- };
140
+ const projected = state.validateOnly ? object : { type: `val`, value: void 0 };
134
141
  state.expressions.set(object, projected);
135
142
  const sourceValue = readRequiredDataProperty(
136
143
  object,
137
144
  `value`,
138
145
  `${path}.value`
139
146
  );
140
- projected.value = projectWireValue(sourceValue, `${path}.value`, state);
147
+ const result = projectWireValue(sourceValue, `${path}.value`, state);
148
+ if (!state.validateOnly) projected.value = result;
141
149
  return projected;
142
150
  }
143
151
  case `func`: {
144
152
  assertExpressionPrototype(object, path, db.IR.Func.prototype);
145
153
  assertAllowedProperties(object, path, [`type`, `name`, `args`]);
146
- const projected = {
147
- type: `func`,
148
- name: ``,
149
- args: []
150
- };
154
+ const projected = state.validateOnly ? object : { type: `func`, name: ``, args: [] };
151
155
  state.expressions.set(object, projected);
152
156
  const name = readRequiredDataProperty(object, `name`, `${path}.name`);
153
157
  if (typeof name !== `string`) {
154
158
  throw new RemoteSubsetWireValueError(`${path}.name`, describe(name));
155
159
  }
156
- projected.name = name;
160
+ if (!state.validateOnly) projected.name = name;
157
161
  const args = readRequiredDataProperty(object, `args`, `${path}.args`);
158
- projected.args = projectExpressionArray(args, `${path}.args`, state);
162
+ const result = projectExpressionArray(args, `${path}.args`, state);
163
+ if (!state.validateOnly) projected.args = result;
159
164
  return projected;
160
165
  }
161
166
  default:
@@ -167,7 +172,7 @@ function projectExpressionArray(value, path, state) {
167
172
  const existing = state.expressionArrays.get(array);
168
173
  if (existing) return existing;
169
174
  assertArrayShape(array, path);
170
- const projected = new Array(array.length);
175
+ const projected = state.validateOnly ? array : new Array(array.length);
171
176
  state.expressionArrays.set(array, projected);
172
177
  for (let index = 0; index < array.length; index++) {
173
178
  if (!(index in array)) {
@@ -176,11 +181,8 @@ function projectExpressionArray(value, path, state) {
176
181
  `missing expression`
177
182
  );
178
183
  }
179
- projected[index] = projectExpression(
180
- array[index],
181
- `${path}[${index}]`,
182
- state
183
- );
184
+ const result = projectExpression(array[index], `${path}[${index}]`, state);
185
+ if (!state.validateOnly) projected[index] = result;
184
186
  }
185
187
  return projected;
186
188
  }
@@ -189,14 +191,14 @@ function projectStringArray(value, path, state) {
189
191
  const existing = state.stringArrays.get(array);
190
192
  if (existing) return existing;
191
193
  assertArrayShape(array, path);
192
- const projected = new Array(array.length);
194
+ const projected = state.validateOnly ? array : new Array(array.length);
193
195
  state.stringArrays.set(array, projected);
194
196
  for (let index = 0; index < array.length; index++) {
195
197
  const entry = array[index];
196
198
  if (!(index in array) || typeof entry !== `string`) {
197
199
  throw new RemoteSubsetWireValueError(`${path}[${index}]`, describe(entry));
198
200
  }
199
- projected[index] = entry;
201
+ if (!state.validateOnly) projected[index] = entry;
200
202
  }
201
203
  return projected;
202
204
  }
@@ -205,27 +207,27 @@ function projectOrderBy(value, path, state) {
205
207
  const existing = state.orderByArrays.get(array);
206
208
  if (existing) return existing;
207
209
  assertArrayShape(array, path);
208
- const projected = new Array(array.length);
210
+ const projected = state.validateOnly ? array : new Array(array.length);
209
211
  state.orderByArrays.set(array, projected);
210
212
  for (let index = 0; index < array.length; index++) {
211
213
  const clausePath = `${path}[${index}]`;
212
214
  const clause = requirePlainRecord(array[index], clausePath);
213
215
  const existingClause = state.orderByClauses.get(clause);
214
216
  if (existingClause) {
215
- projected[index] = existingClause;
217
+ if (!state.validateOnly) projected[index] = existingClause;
216
218
  continue;
217
219
  }
218
220
  assertAllowedProperties(clause, clausePath, [
219
221
  `expression`,
220
222
  `compareOptions`
221
223
  ]);
222
- const projectedClause = {
224
+ const projectedClause = state.validateOnly ? clause : {
223
225
  expression: void 0,
224
226
  compareOptions: void 0
225
227
  };
226
- projected[index] = projectedClause;
228
+ if (!state.validateOnly) projected[index] = projectedClause;
227
229
  state.orderByClauses.set(clause, projectedClause);
228
- projectedClause.expression = projectExpression(
230
+ const expression = projectExpression(
229
231
  readRequiredDataProperty(
230
232
  clause,
231
233
  `expression`,
@@ -234,7 +236,7 @@ function projectOrderBy(value, path, state) {
234
236
  `${clausePath}.expression`,
235
237
  state
236
238
  );
237
- projectedClause.compareOptions = projectCompareOptions(
239
+ const compareOptions = projectCompareOptions(
238
240
  readRequiredDataProperty(
239
241
  clause,
240
242
  `compareOptions`,
@@ -243,6 +245,10 @@ function projectOrderBy(value, path, state) {
243
245
  `${clausePath}.compareOptions`,
244
246
  state
245
247
  );
248
+ if (!state.validateOnly) {
249
+ projectedClause.expression = expression;
250
+ projectedClause.compareOptions = compareOptions;
251
+ }
246
252
  }
247
253
  return projected;
248
254
  }
@@ -257,7 +263,7 @@ function projectCompareOptions(value, path, state) {
257
263
  `locale`,
258
264
  `localeOptions`
259
265
  ]);
260
- const projected = {};
266
+ const projected = state.validateOnly ? source : {};
261
267
  const direction = readRequiredDataProperty(
262
268
  source,
263
269
  `direction`,
@@ -269,12 +275,12 @@ function projectCompareOptions(value, path, state) {
269
275
  describe(direction)
270
276
  );
271
277
  }
272
- projected.direction = direction;
278
+ if (!state.validateOnly) projected.direction = direction;
273
279
  const nulls = readRequiredDataProperty(source, `nulls`, `${path}.nulls`);
274
280
  if (nulls !== `first` && nulls !== `last`) {
275
281
  throw new RemoteSubsetWireValueError(`${path}.nulls`, describe(nulls));
276
282
  }
277
- projected.nulls = nulls;
283
+ if (!state.validateOnly) projected.nulls = nulls;
278
284
  const stringSort = readDataProperty(
279
285
  source,
280
286
  `stringSort`,
@@ -287,7 +293,7 @@ function projectCompareOptions(value, path, state) {
287
293
  describe(stringSort.value)
288
294
  );
289
295
  }
290
- projected.stringSort = stringSort.value;
296
+ if (!state.validateOnly) projected.stringSort = stringSort.value;
291
297
  }
292
298
  if (projected.stringSort !== `lexical`) {
293
299
  const locale = readDataProperty(source, `locale`, `${path}.locale`);
@@ -298,7 +304,7 @@ function projectCompareOptions(value, path, state) {
298
304
  describe(locale.value)
299
305
  );
300
306
  }
301
- projected.locale = locale.value;
307
+ if (!state.validateOnly) projected.locale = locale.value;
302
308
  }
303
309
  const localeOptions = readDataProperty(
304
310
  source,
@@ -306,11 +312,12 @@ function projectCompareOptions(value, path, state) {
306
312
  `${path}.localeOptions`
307
313
  );
308
314
  if (localeOptions.present && localeOptions.value !== void 0) {
309
- projected.localeOptions = projectWireRecord(
315
+ const result2 = projectWireRecord(
310
316
  localeOptions.value,
311
317
  `${path}.localeOptions`,
312
318
  state
313
319
  );
320
+ if (!state.validateOnly) projected.localeOptions = result2;
314
321
  }
315
322
  }
316
323
  const result = projected;
@@ -326,21 +333,25 @@ function projectCursor(value, path, state) {
326
333
  `whereCurrent`,
327
334
  `lastKey`
328
335
  ]);
329
- const projected = {
336
+ const projected = state.validateOnly ? source : {
330
337
  whereFrom: void 0,
331
338
  whereCurrent: void 0
332
339
  };
333
340
  state.cursors.set(source, projected);
334
- projected.whereFrom = projectExpression(
341
+ const whereFrom = projectExpression(
335
342
  readRequiredDataProperty(source, `whereFrom`, `${path}.whereFrom`),
336
343
  `${path}.whereFrom`,
337
344
  state
338
345
  );
339
- projected.whereCurrent = projectExpression(
346
+ const whereCurrent = projectExpression(
340
347
  readRequiredDataProperty(source, `whereCurrent`, `${path}.whereCurrent`),
341
348
  `${path}.whereCurrent`,
342
349
  state
343
350
  );
351
+ if (!state.validateOnly) {
352
+ projected.whereFrom = whereFrom;
353
+ projected.whereCurrent = whereCurrent;
354
+ }
344
355
  const lastKey = readDataProperty(source, `lastKey`, `${path}.lastKey`);
345
356
  if (lastKey.present && lastKey.value !== void 0) {
346
357
  if (typeof lastKey.value !== `string` && typeof lastKey.value !== `number`) {
@@ -349,7 +360,7 @@ function projectCursor(value, path, state) {
349
360
  describe(lastKey.value)
350
361
  );
351
362
  }
352
- projected.lastKey = lastKey.value;
363
+ if (!state.validateOnly) projected.lastKey = lastKey.value;
353
364
  }
354
365
  return projected;
355
366
  }
@@ -368,14 +379,16 @@ function projectWireValue(value, path, state) {
368
379
  if (value instanceof NativeDate) {
369
380
  assertExactPrototype(value, NativeDate.prototype, path);
370
381
  assertNoOwnProperties(value, path);
371
- const projected = new NativeDate(value.getTime());
382
+ const time = value.getTime();
383
+ const projected = state.validateOnly ? value : new NativeDate(time);
372
384
  state.wireValues.set(value, projected);
373
385
  return projected;
374
386
  }
375
387
  if (value instanceof RegExp) {
376
388
  assertExactPrototype(value, RegExp.prototype, path);
377
389
  assertRegExpShape(value, path);
378
- const projected = new RegExp(value.source, value.flags);
390
+ const { source, flags } = value;
391
+ const projected = state.validateOnly ? value : new RegExp(source, flags);
379
392
  state.wireValues.set(value, projected);
380
393
  return projected;
381
394
  }
@@ -385,7 +398,12 @@ function projectWireValue(value, path, state) {
385
398
  assertFixedArrayBuffer(value, path);
386
399
  let projected;
387
400
  try {
388
- projected = value.slice(0);
401
+ if (state.validateOnly) {
402
+ new Uint8Array(value, 0, 0);
403
+ projected = value;
404
+ } else {
405
+ projected = value.slice(0);
406
+ }
389
407
  } catch {
390
408
  throw new RemoteSubsetWireValueError(path, `detached ArrayBuffer`);
391
409
  }
@@ -409,7 +427,7 @@ function projectWireValue(value, path, state) {
409
427
  if (!(projectedBuffer instanceof ArrayBuffer)) {
410
428
  throw new RemoteSubsetWireValueError(path, `shared DataView buffer`);
411
429
  }
412
- const projected = new DataView(projectedBuffer, byteOffset, byteLength);
430
+ const projected = state.validateOnly ? value : new DataView(projectedBuffer, byteOffset, byteLength);
413
431
  state.wireValues.set(value, projected);
414
432
  return projected;
415
433
  }
@@ -436,7 +454,7 @@ function projectWireValue(value, path, state) {
436
454
  }
437
455
  let projected;
438
456
  try {
439
- projected = new typedArrayConstructor(projectedBuffer, byteOffset, length);
457
+ projected = state.validateOnly ? typedArray : new typedArrayConstructor(projectedBuffer, byteOffset, length);
440
458
  } catch {
441
459
  throw new RemoteSubsetWireValueError(path, `detached typed array`);
442
460
  }
@@ -453,15 +471,12 @@ function projectWireValue(value, path, state) {
453
471
  }
454
472
  function projectWireArray(value, path, state) {
455
473
  assertArrayShape(value, path);
456
- const projected = new Array(value.length);
474
+ const projected = state.validateOnly ? value : new Array(value.length);
457
475
  state.wireValues.set(value, projected);
458
476
  for (let index = 0; index < value.length; index++) {
459
477
  if (index in value) {
460
- projected[index] = projectWireValue(
461
- value[index],
462
- `${path}[${index}]`,
463
- state
464
- );
478
+ const result = projectWireValue(value[index], `${path}[${index}]`, state);
479
+ if (!state.validateOnly) projected[index] = result;
465
480
  }
466
481
  }
467
482
  return projected;
@@ -469,14 +484,21 @@ function projectWireArray(value, path, state) {
469
484
  function projectWireMap(value, path, state) {
470
485
  assertExactPrototype(value, Map.prototype, path);
471
486
  assertNoOwnProperties(value, path);
472
- const projected = /* @__PURE__ */ new Map();
487
+ const projected = state.validateOnly ? value : /* @__PURE__ */ new Map();
473
488
  state.wireValues.set(value, projected);
474
489
  let index = 0;
475
490
  for (const [key, entry] of value) {
476
- projected.set(
477
- projectWireValue(key, `${path}.entries[${index}].key`, state),
478
- projectWireValue(entry, `${path}.entries[${index}].value`, state)
491
+ const projectedKey = projectWireValue(
492
+ key,
493
+ `${path}.entries[${index}].key`,
494
+ state
479
495
  );
496
+ const projectedValue = projectWireValue(
497
+ entry,
498
+ `${path}.entries[${index}].value`,
499
+ state
500
+ );
501
+ if (!state.validateOnly) projected.set(projectedKey, projectedValue);
480
502
  index++;
481
503
  }
482
504
  return projected;
@@ -484,11 +506,12 @@ function projectWireMap(value, path, state) {
484
506
  function projectWireSet(value, path, state) {
485
507
  assertExactPrototype(value, Set.prototype, path);
486
508
  assertNoOwnProperties(value, path);
487
- const projected = /* @__PURE__ */ new Set();
509
+ const projected = state.validateOnly ? value : /* @__PURE__ */ new Set();
488
510
  state.wireValues.set(value, projected);
489
511
  let index = 0;
490
512
  for (const entry of value) {
491
- projected.add(projectWireValue(entry, `${path}.values[${index}]`, state));
513
+ const result = projectWireValue(entry, `${path}.values[${index}]`, state);
514
+ if (!state.validateOnly) projected.add(result);
492
515
  index++;
493
516
  }
494
517
  return projected;
@@ -497,7 +520,7 @@ function projectWireRecord(value, path, state) {
497
520
  const source = requirePlainRecord(value, path);
498
521
  const existing = state.wireValues.get(source);
499
522
  if (existing) return existing;
500
- const projected = {};
523
+ const projected = state.validateOnly ? source : {};
501
524
  state.wireValues.set(source, projected);
502
525
  for (const key of ownKeys(source, path)) {
503
526
  const childPath = propertyPath(path, key);
@@ -508,12 +531,15 @@ function projectWireRecord(value, path, state) {
508
531
  if (!descriptor || !descriptor.enumerable || !(`value` in descriptor)) {
509
532
  throw new RemoteSubsetWireValueError(childPath, `non-data property`);
510
533
  }
511
- Object.defineProperty(projected, key, {
512
- value: projectWireValue(descriptor.value, childPath, state),
513
- enumerable: true,
514
- writable: true,
515
- configurable: true
516
- });
534
+ const result = projectWireValue(descriptor.value, childPath, state);
535
+ if (!state.validateOnly) {
536
+ Object.defineProperty(projected, key, {
537
+ value: result,
538
+ enumerable: true,
539
+ writable: true,
540
+ configurable: true
541
+ });
542
+ }
517
543
  }
518
544
  return projected;
519
545
  }
@@ -678,4 +704,5 @@ function describe(value) {
678
704
  exports.RemoteSubsetWireValueError = RemoteSubsetWireValueError;
679
705
  exports.toProcessLocalLoadSubsetOptions = toProcessLocalLoadSubsetOptions;
680
706
  exports.toTransportedLoadSubsetOptions = toTransportedLoadSubsetOptions;
707
+ exports.validateRemoteSubsetOptions = validateRemoteSubsetOptions;
681
708
  //# sourceMappingURL=remote-subset-wire.cjs.map