tinybase 7.3.5 → 8.0.0-beta.1

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 (59) hide show
  1. package/@types/index.d.ts +1 -0
  2. package/@types/middleware/index.d.ts +1064 -0
  3. package/@types/middleware/with-schemas/index.d.ts +1355 -0
  4. package/@types/omni/index.d.ts +1 -0
  5. package/@types/omni/with-schemas/index.d.ts +1 -0
  6. package/@types/store/index.d.ts +0 -1
  7. package/@types/with-schemas/index.d.ts +1 -0
  8. package/agents.md +33 -11
  9. package/checkpoints/index.js +8 -9
  10. package/checkpoints/with-schemas/index.js +8 -9
  11. package/index.js +484 -153
  12. package/mergeable-store/index.js +371 -135
  13. package/mergeable-store/with-schemas/index.js +371 -135
  14. package/middleware/index.js +130 -0
  15. package/middleware/with-schemas/index.js +130 -0
  16. package/min/checkpoints/index.js +1 -1
  17. package/min/checkpoints/index.js.gz +0 -0
  18. package/min/checkpoints/with-schemas/index.js +1 -1
  19. package/min/checkpoints/with-schemas/index.js.gz +0 -0
  20. package/min/index.js +1 -1
  21. package/min/index.js.gz +0 -0
  22. package/min/mergeable-store/index.js +1 -1
  23. package/min/mergeable-store/index.js.gz +0 -0
  24. package/min/mergeable-store/with-schemas/index.js +1 -1
  25. package/min/mergeable-store/with-schemas/index.js.gz +0 -0
  26. package/min/middleware/index.js +1 -0
  27. package/min/middleware/index.js.gz +0 -0
  28. package/min/middleware/with-schemas/index.js +1 -0
  29. package/min/middleware/with-schemas/index.js.gz +0 -0
  30. package/min/omni/index.js +1 -1
  31. package/min/omni/index.js.gz +0 -0
  32. package/min/omni/with-schemas/index.js +1 -1
  33. package/min/omni/with-schemas/index.js.gz +0 -0
  34. package/min/queries/index.js +1 -1
  35. package/min/queries/index.js.gz +0 -0
  36. package/min/queries/with-schemas/index.js +1 -1
  37. package/min/queries/with-schemas/index.js.gz +0 -0
  38. package/min/store/index.js +1 -1
  39. package/min/store/index.js.gz +0 -0
  40. package/min/store/with-schemas/index.js +1 -1
  41. package/min/store/with-schemas/index.js.gz +0 -0
  42. package/min/ui-react-inspector/index.js +1 -1
  43. package/min/ui-react-inspector/index.js.gz +0 -0
  44. package/min/ui-react-inspector/with-schemas/index.js +1 -1
  45. package/min/ui-react-inspector/with-schemas/index.js.gz +0 -0
  46. package/min/with-schemas/index.js +1 -1
  47. package/min/with-schemas/index.js.gz +0 -0
  48. package/omni/index.js +492 -161
  49. package/omni/with-schemas/index.js +492 -161
  50. package/package.json +37 -1
  51. package/queries/index.js +1 -6
  52. package/queries/with-schemas/index.js +1 -6
  53. package/readme.md +14 -14
  54. package/releases.md +41 -41
  55. package/store/index.js +370 -134
  56. package/store/with-schemas/index.js +370 -134
  57. package/ui-react-inspector/index.js +349 -134
  58. package/ui-react-inspector/with-schemas/index.js +349 -134
  59. package/with-schemas/index.js +484 -153
@@ -0,0 +1,1355 @@
1
+ /**
2
+ * The middleware module of the TinyBase project provides the ability to
3
+ * intercept and validate incoming writes to Store objects.
4
+ *
5
+ * The main entry point to this module is the createMiddleware function, which
6
+ * returns a new Middleware object. From there, you can register hooks that are
7
+ * called before data is written to the Store.
8
+ * @packageDocumentation
9
+ * @module middleware
10
+ * @since v8.0.0
11
+ */
12
+ import type {
13
+ CellIdFromSchema,
14
+ TableIdFromSchema,
15
+ ValueIdFromSchema,
16
+ } from '../../_internal/store/with-schemas/index.d.ts';
17
+ import type {Id} from '../../common/with-schemas/index.d.ts';
18
+ import type {
19
+ Cell,
20
+ Changes,
21
+ Content,
22
+ OptionalSchemas,
23
+ OptionalTablesSchema,
24
+ OptionalValuesSchema,
25
+ Row,
26
+ Store,
27
+ Table,
28
+ Tables,
29
+ Value,
30
+ Values,
31
+ } from '../../store/with-schemas/index.d.ts';
32
+
33
+ /**
34
+ * The WillSetContentCallback type describes a function that is called before
35
+ * Content is set in the Store.
36
+ *
37
+ * This has schema-based typing. The following is a simplified representation:
38
+ *
39
+ * ```ts override
40
+ * (content: Content) => Content | undefined;
41
+ * ```
42
+ *
43
+ * The callback receives the Content (a [Tables, Values] tuple) that is about
44
+ * to be set. It can return the Content (possibly transformed) to allow the
45
+ * write, or `undefined` to prevent the Content from being set.
46
+ *
47
+ * Multiple WillSetContentCallback functions can be registered and they will be
48
+ * called sequentially, the Content being updated successively. If any callback
49
+ * returns `undefined`, the chain short-circuits and the Content will not be
50
+ * set.
51
+ * @param content The Content about to be set.
52
+ * @returns The Content to use (possibly transformed), or `undefined` to
53
+ * prevent the write.
54
+ * @category Callback
55
+ * @since v8.0.0
56
+ */
57
+ export type WillSetContentCallback<Schemas extends OptionalSchemas> = (
58
+ content: Content<Schemas>,
59
+ ) => Content<Schemas> | undefined;
60
+
61
+ /**
62
+ * The WillSetTablesCallback type describes a function that is called before
63
+ * Tables are set in the Store.
64
+ *
65
+ * This has schema-based typing. The following is a simplified representation:
66
+ *
67
+ * ```ts override
68
+ * (tables: Tables) => Tables | undefined;
69
+ * ```
70
+ *
71
+ * The callback receives the Tables object that is about to be set. It can
72
+ * return the Tables (possibly transformed) to allow the write, or `undefined`
73
+ * to prevent the Tables from being set.
74
+ *
75
+ * Multiple WillSetTablesCallback functions can be registered and they will be
76
+ * called sequentially, the Tables being updated successively. If any callback
77
+ * returns `undefined`, the chain short-circuits and the Tables will not be
78
+ * set.
79
+ * @param tables The Tables object about to be set.
80
+ * @returns The Tables to use (possibly transformed), or `undefined` to prevent
81
+ * the write.
82
+ * @category Callback
83
+ * @since v8.0.0
84
+ */
85
+ export type WillSetTablesCallback<Schema extends OptionalTablesSchema> = (
86
+ tables: Tables<Schema>,
87
+ ) => Tables<Schema> | undefined;
88
+
89
+ /**
90
+ * The WillSetTableCallback type describes a function that is called before a
91
+ * Table is set in the Store.
92
+ *
93
+ * This has schema-based typing. The following is a simplified representation:
94
+ *
95
+ * ```ts override
96
+ * (
97
+ * tableId: Id,
98
+ * table: Table,
99
+ * ) => Table | undefined;
100
+ * ```
101
+ *
102
+ * The callback receives the table Id and the Table object that is about to be
103
+ * set. It can return the Table (possibly transformed) to allow the write, or
104
+ * `undefined` to prevent the Table from being set.
105
+ *
106
+ * Multiple WillSetTableCallback functions can be registered and they will be
107
+ * called sequentially, the Table being updated successively. If any callback
108
+ * returns `undefined`, the chain short-circuits and the Table will not be set.
109
+ * @param tableId The Id of the Table being set.
110
+ * @param table The Table object about to be set.
111
+ * @returns The Table to use (possibly transformed), or `undefined` to prevent
112
+ * the write.
113
+ * @category Callback
114
+ * @since v8.0.0
115
+ */
116
+ export type WillSetTableCallback<
117
+ Schema extends OptionalTablesSchema,
118
+ Params extends any[] = TableIdFromSchema<Schema> extends infer TableId
119
+ ? TableId extends TableIdFromSchema<Schema>
120
+ ? [tableId: TableId, table: Table<Schema, TableId>]
121
+ : never
122
+ : never,
123
+ > = (
124
+ ...params: Params | [tableId: never, table: never]
125
+ ) => Params[1] | undefined;
126
+
127
+ /**
128
+ * The WillSetRowCallback type describes a function that is called before a Row
129
+ * is set in the Store.
130
+ *
131
+ * This has schema-based typing. The following is a simplified representation:
132
+ *
133
+ * ```ts override
134
+ * (
135
+ * tableId: Id,
136
+ * rowId: Id,
137
+ * row: Row,
138
+ * ) => Row | undefined;
139
+ * ```
140
+ *
141
+ * The callback receives the table Id, row Id, and the Row object that is about
142
+ * to be set. It can return the Row (possibly transformed) to allow the write,
143
+ * or `undefined` to prevent the Row from being set.
144
+ *
145
+ * Multiple WillSetRowCallback functions can be registered and they will be
146
+ * called sequentially, the Row being updated successively. If any callback
147
+ * returns `undefined`, the chain short-circuits and the Row will not be set.
148
+ * @param tableId The Id of the Table being written to.
149
+ * @param rowId The Id of the Row being set.
150
+ * @param row The Row object about to be set.
151
+ * @returns The Row to use (possibly transformed), or `undefined` to prevent the
152
+ * write.
153
+ * @category Callback
154
+ * @since v8.0.0
155
+ */
156
+ export type WillSetRowCallback<
157
+ Schema extends OptionalTablesSchema,
158
+ Params extends any[] = TableIdFromSchema<Schema> extends infer TableId
159
+ ? TableId extends TableIdFromSchema<Schema>
160
+ ? [tableId: TableId, rowId: Id, row: Row<Schema, TableId>]
161
+ : never
162
+ : never,
163
+ > = (
164
+ ...params: Params | [tableId: never, rowId: never, row: never]
165
+ ) => Params[2] | undefined;
166
+
167
+ /**
168
+ * The WillSetCellCallback type describes a function that is called before a
169
+ * Cell is set in the Store.
170
+ *
171
+ * This has schema-based typing. The following is a simplified representation:
172
+ *
173
+ * ```ts override
174
+ * (
175
+ * tableId: Id,
176
+ * rowId: Id,
177
+ * cellId: Id,
178
+ * cell: Cell,
179
+ * ) => CellOrUndefined;
180
+ * ```
181
+ *
182
+ * The callback receives the table Id, row Id, cell Id, and the Cell value that
183
+ * is about to be set. It can return the Cell value (possibly transformed) to
184
+ * allow the write, or `undefined` to prevent the Cell from being set.
185
+ *
186
+ * Multiple WillSetCellCallback functions can be registered and they will be
187
+ * called sequentially, the Cell value being updated successively. If any
188
+ * callback returns `undefined`, the chain short-circuits and the Cell will not
189
+ * be set.
190
+ * @param tableId The Id of the Table being written to.
191
+ * @param rowId The Id of the Row being written to.
192
+ * @param cellId The Id of the Cell being set.
193
+ * @param cell The Cell value about to be set.
194
+ * @returns The Cell value to use (possibly transformed), or `undefined` to
195
+ * prevent the write.
196
+ * @category Callback
197
+ * @since v8.0.0
198
+ */
199
+ export type WillSetCellCallback<
200
+ Schema extends OptionalTablesSchema,
201
+ Params extends any[] = TableIdFromSchema<Schema> extends infer TableId
202
+ ? TableId extends TableIdFromSchema<Schema>
203
+ ? CellIdFromSchema<Schema, TableId> extends infer CellId
204
+ ? CellId extends CellIdFromSchema<Schema, TableId>
205
+ ? [
206
+ tableId: TableId,
207
+ rowId: Id,
208
+ cellId: CellId,
209
+ cell: Cell<Schema, TableId, CellId>,
210
+ ]
211
+ : never
212
+ : never
213
+ : never
214
+ : never,
215
+ > = (
216
+ ...params: Params | [tableId: never, rowId: never, cellId: never, cell: never]
217
+ ) => Params[3] | undefined;
218
+
219
+ /**
220
+ * The WillSetValuesCallback type describes a function that is called before
221
+ * Values are set in the Store.
222
+ *
223
+ * This has schema-based typing. The following is a simplified representation:
224
+ *
225
+ * ```ts override
226
+ * (values: Values) => Values | undefined;
227
+ * ```
228
+ *
229
+ * The callback receives the Values object that is about to be set. It can
230
+ * return the Values (possibly transformed) to allow the write, or `undefined`
231
+ * to prevent the Values from being set.
232
+ *
233
+ * Multiple WillSetValuesCallback functions can be registered and they will be
234
+ * called sequentially, the Values being updated successively. If any callback
235
+ * returns `undefined`, the chain short-circuits and the Values will not be set.
236
+ * @param values The Values object about to be set.
237
+ * @returns The Values to use (possibly transformed), or `undefined` to prevent
238
+ * the write.
239
+ * @category Callback
240
+ * @since v8.0.0
241
+ */
242
+ export type WillSetValuesCallback<Schema extends OptionalValuesSchema> = (
243
+ values: Values<Schema>,
244
+ ) => Values<Schema> | undefined;
245
+
246
+ /**
247
+ * The WillSetValueCallback type describes a function that is called before a
248
+ * Value is set in the Store.
249
+ *
250
+ * This has schema-based typing. The following is a simplified representation:
251
+ *
252
+ * ```ts override
253
+ * (
254
+ * valueId: Id,
255
+ * value: Value,
256
+ * ) => ValueOrUndefined;
257
+ * ```
258
+ *
259
+ * The callback receives the value Id and the Value that is about to be set. It
260
+ * can return the Value (possibly transformed) to allow the write, or
261
+ * `undefined` to prevent the Value from being set.
262
+ *
263
+ * Multiple WillSetValueCallback functions can be registered and they will be
264
+ * called sequentially, the Value being updated successively. If any callback
265
+ * returns `undefined`, the chain short-circuits and the Value will not be set.
266
+ * @param valueId The Id of the Value being set.
267
+ * @param value The Value about to be set.
268
+ * @returns The Value to use (possibly transformed), or `undefined` to prevent
269
+ * the write.
270
+ * @category Callback
271
+ * @since v8.0.0
272
+ */
273
+ export type WillSetValueCallback<
274
+ Schema extends OptionalValuesSchema,
275
+ Params extends any[] = ValueIdFromSchema<Schema> extends infer ValueId
276
+ ? ValueId extends ValueIdFromSchema<Schema>
277
+ ? [valueId: ValueId, value: Value<Schema, ValueId>]
278
+ : never
279
+ : never,
280
+ > = (
281
+ ...params: Params | [valueId: never, value: never]
282
+ ) => Params[1] | undefined;
283
+
284
+ /**
285
+ * The WillDelTablesCallback type describes a function that is called before all
286
+ * Tables are deleted from the Store.
287
+ *
288
+ * The callback takes no parameters. It returns `true` to allow the deletion, or
289
+ * `false` to prevent it.
290
+ *
291
+ * Multiple WillDelTablesCallback functions can be registered and they will be
292
+ * called sequentially. If any callback returns `false`, the chain
293
+ * short-circuits and the Tables will not be deleted.
294
+ * @returns `true` to allow the deletion, `false` to prevent it.
295
+ * @category Callback
296
+ * @since v8.0.0
297
+ */
298
+ export type WillDelTablesCallback = () => boolean;
299
+
300
+ /**
301
+ * The WillDelTableCallback type describes a function that is called before a
302
+ * Table is deleted from the Store.
303
+ *
304
+ * This has schema-based typing. The following is a simplified representation:
305
+ *
306
+ * ```ts override
307
+ * (tableId: Id) => boolean;
308
+ * ```
309
+ *
310
+ * The callback receives the table Id of the Table about to be deleted. It
311
+ * returns `true` to allow the deletion, or `false` to prevent it.
312
+ *
313
+ * Multiple WillDelTableCallback functions can be registered and they will be
314
+ * called sequentially. If any callback returns `false`, the chain
315
+ * short-circuits and the Table will not be deleted.
316
+ * @param tableId The Id of the Table being deleted.
317
+ * @returns `true` to allow the deletion, `false` to prevent it.
318
+ * @category Callback
319
+ * @since v8.0.0
320
+ */
321
+ export type WillDelTableCallback<
322
+ Schema extends OptionalTablesSchema,
323
+ Params extends any[] = TableIdFromSchema<Schema> extends infer TableId
324
+ ? TableId extends TableIdFromSchema<Schema>
325
+ ? [tableId: TableId]
326
+ : never
327
+ : never,
328
+ > = (...params: Params | [tableId: never]) => boolean;
329
+
330
+ /**
331
+ * The WillDelRowCallback type describes a function that is called before a
332
+ * Row is deleted from the Store.
333
+ *
334
+ * This has schema-based typing. The following is a simplified representation:
335
+ *
336
+ * ```ts override
337
+ * (tableId: Id, rowId: Id) => boolean;
338
+ * ```
339
+ *
340
+ * The callback receives the table Id and row Id of the Row about to be
341
+ * deleted. It returns `true` to allow the deletion, or `false` to prevent it.
342
+ *
343
+ * Multiple WillDelRowCallback functions can be registered and they will be
344
+ * called sequentially. If any callback returns `false`, the chain
345
+ * short-circuits and the Row will not be deleted.
346
+ * @param tableId The Id of the Table containing the Row.
347
+ * @param rowId The Id of the Row being deleted.
348
+ * @returns `true` to allow the deletion, `false` to prevent it.
349
+ * @category Callback
350
+ * @since v8.0.0
351
+ */
352
+ export type WillDelRowCallback<
353
+ Schema extends OptionalTablesSchema,
354
+ Params extends any[] = TableIdFromSchema<Schema> extends infer TableId
355
+ ? TableId extends TableIdFromSchema<Schema>
356
+ ? [tableId: TableId, rowId: Id]
357
+ : never
358
+ : never,
359
+ > = (...params: Params | [tableId: never, rowId: never]) => boolean;
360
+
361
+ /**
362
+ * The WillDelCellCallback type describes a function that is called before a
363
+ * Cell is deleted from the Store.
364
+ *
365
+ * This has schema-based typing. The following is a simplified representation:
366
+ *
367
+ * ```ts override
368
+ * (
369
+ * tableId: Id,
370
+ * rowId: Id,
371
+ * cellId: Id,
372
+ * ) => boolean;
373
+ * ```
374
+ *
375
+ * The callback receives the table Id, row Id, and cell Id of the Cell about to
376
+ * be deleted. It returns `true` to allow the deletion, or `false` to prevent
377
+ * it.
378
+ *
379
+ * Multiple WillDelCellCallback functions can be registered and they will be
380
+ * called sequentially. If any callback returns `false`, the chain
381
+ * short-circuits and the Cell will not be deleted.
382
+ * @param tableId The Id of the Table containing the Cell.
383
+ * @param rowId The Id of the Row containing the Cell.
384
+ * @param cellId The Id of the Cell being deleted.
385
+ * @returns `true` to allow the deletion, `false` to prevent it.
386
+ * @category Callback
387
+ * @since v8.0.0
388
+ */
389
+ export type WillDelCellCallback<
390
+ Schema extends OptionalTablesSchema,
391
+ Params extends any[] = TableIdFromSchema<Schema> extends infer TableId
392
+ ? TableId extends TableIdFromSchema<Schema>
393
+ ? CellIdFromSchema<Schema, TableId> extends infer CellId
394
+ ? CellId extends CellIdFromSchema<Schema, TableId>
395
+ ? [tableId: TableId, rowId: Id, cellId: CellId]
396
+ : never
397
+ : never
398
+ : never
399
+ : never,
400
+ > = (
401
+ ...params: Params | [tableId: never, rowId: never, cellId: never]
402
+ ) => boolean;
403
+
404
+ /**
405
+ * The WillDelValuesCallback type describes a function that is called before all
406
+ * Values are deleted from the Store.
407
+ *
408
+ * The callback takes no parameters. It returns `true` to allow the deletion, or
409
+ * `false` to prevent it.
410
+ *
411
+ * Multiple WillDelValuesCallback functions can be registered and they will be
412
+ * called sequentially. If any callback returns `false`, the chain
413
+ * short-circuits and the Values will not be deleted.
414
+ * @returns `true` to allow the deletion, `false` to prevent it.
415
+ * @category Callback
416
+ * @since v8.0.0
417
+ */
418
+ export type WillDelValuesCallback = () => boolean;
419
+
420
+ /**
421
+ * The WillDelValueCallback type describes a function that is called before a
422
+ * Value is deleted from the Store.
423
+ *
424
+ * This has schema-based typing. The following is a simplified representation:
425
+ *
426
+ * ```ts override
427
+ * (valueId: Id) => boolean;
428
+ * ```
429
+ *
430
+ * The callback receives the value Id of the Value about to be deleted. It
431
+ * returns `true` to allow the deletion, or `false` to prevent it.
432
+ *
433
+ * Multiple WillDelValueCallback functions can be registered and they will be
434
+ * called sequentially. If any callback returns `false`, the chain
435
+ * short-circuits and the Value will not be deleted.
436
+ * @param valueId The Id of the Value being deleted.
437
+ * @returns `true` to allow the deletion, `false` to prevent it.
438
+ * @category Callback
439
+ * @since v8.0.0
440
+ */
441
+ export type WillDelValueCallback<
442
+ Schema extends OptionalValuesSchema,
443
+ Params extends any[] = ValueIdFromSchema<Schema> extends infer ValueId
444
+ ? ValueId extends ValueIdFromSchema<Schema>
445
+ ? [valueId: ValueId]
446
+ : never
447
+ : never,
448
+ > = (...params: Params | [valueId: never]) => boolean;
449
+
450
+ /**
451
+ * The WillApplyChangesCallback type describes a function that is called before
452
+ * Changes are applied to the Store.
453
+ *
454
+ * This has schema-based typing. The following is a simplified representation:
455
+ *
456
+ * ```ts override
457
+ * (
458
+ * changes: Changes,
459
+ * ) => Changes | undefined;
460
+ * ```
461
+ *
462
+ * The callback receives the Changes object that is about to be applied. It can
463
+ * return the Changes (possibly transformed) to allow the write, or `undefined`
464
+ * to prevent the Changes from being applied.
465
+ *
466
+ * Multiple WillApplyChangesCallback functions can be registered and they will
467
+ * be called sequentially, the Changes being updated successively. If any
468
+ * callback returns `undefined`, the chain short-circuits and the Changes will
469
+ * not be applied.
470
+ * @param changes The Changes about to be applied.
471
+ * @returns The Changes to use (possibly transformed), or `undefined` to
472
+ * prevent the write.
473
+ * @category Callback
474
+ * @since v8.0.0
475
+ */
476
+ export type WillApplyChangesCallback<Schemas extends OptionalSchemas> = (
477
+ changes: Changes<Schemas>,
478
+ ) => Changes<Schemas> | undefined;
479
+
480
+ /**
481
+ * A Middleware object lets you intercept and validate writes to a Store.
482
+ *
483
+ * This is useful for enforcing business rules, data validation, or
484
+ * transformation logic before data is persisted in the Store.
485
+ *
486
+ * Create a Middleware object easily with the createMiddleware function.
487
+ * @example
488
+ * This example shows a very simple lifecycle of a Middleware object: from
489
+ * creation, to getting the Store reference, and then destroying it.
490
+ *
491
+ * ```js
492
+ * import {createMiddleware, createStore} from 'tinybase';
493
+ *
494
+ * const store = createStore();
495
+ * const middleware = createMiddleware(store);
496
+ *
497
+ * console.log(middleware.getStore() == store);
498
+ * // -> true
499
+ *
500
+ * middleware.destroy();
501
+ * ```
502
+ * @category Middleware
503
+ * @since v8.0.0
504
+ */
505
+ export interface Middleware<in out Schemas extends OptionalSchemas> {
506
+ /**
507
+ * The getStore method returns a reference to the underlying Store that is
508
+ * backing this Middleware object.
509
+ * @returns A reference to the Store.
510
+ * @example
511
+ * This example creates a Middleware object against a newly-created Store and
512
+ * then gets its reference in order to update its data.
513
+ *
514
+ * This has schema-based typing. The following is a simplified representation:
515
+ *
516
+ * ```ts override
517
+ * getStore(): Store;
518
+ * ```
519
+ *
520
+ * ```js
521
+ * import {createMiddleware, createStore} from 'tinybase';
522
+ *
523
+ * const middleware = createMiddleware(createStore());
524
+ * console.log(middleware.getStore().getTables());
525
+ * // -> {}
526
+ * middleware.destroy();
527
+ * ```
528
+ * @category Getter
529
+ * @since v8.0.0
530
+ */
531
+ getStore(): Store<Schemas>;
532
+
533
+ /**
534
+ * The addWillSetContentCallback method registers a
535
+ * WillSetContentCallback that will be called before Content is set in the
536
+ * Store.
537
+ *
538
+ * This has schema-based typing. The following is a simplified representation:
539
+ *
540
+ * ```ts override
541
+ * addWillSetContentCallback(callback: WillSetContentCallback): Middleware;
542
+ * ```
543
+ *
544
+ * The callback can transform the Content or return `undefined` to prevent
545
+ * the write. Multiple callbacks can be registered and they are called
546
+ * sequentially, each receiving the (possibly transformed) Content from
547
+ * the previous callback.
548
+ * @param callback The WillSetContentCallback to register.
549
+ * @returns A reference to the Middleware object, for chaining.
550
+ * @example
551
+ * This example registers a callback that prevents setting Content with
552
+ * empty Tables in the pet store.
553
+ *
554
+ * ```js
555
+ * import {createMiddleware, createStore} from 'tinybase';
556
+ *
557
+ * const store = createStore();
558
+ * const middleware = createMiddleware(store);
559
+ *
560
+ * middleware.addWillSetContentCallback(([tables, values]) =>
561
+ * Object.keys(tables).length > 0 ? [tables, values] : undefined,
562
+ * );
563
+ *
564
+ * store.setContent([{}, {open: true}]);
565
+ * console.log(store.getContent());
566
+ * // -> [{}, {}]
567
+ *
568
+ * store.setContent([{pets: {fido: {species: 'dog'}}}, {}]);
569
+ * console.log(store.getContent());
570
+ * // -> [{pets: {fido: {species: 'dog'}}}, {}]
571
+ *
572
+ * middleware.destroy();
573
+ * ```
574
+ * @category Configuration
575
+ * @since v8.0.0
576
+ */
577
+ addWillSetContentCallback(
578
+ callback: WillSetContentCallback<Schemas>,
579
+ ): Middleware<Schemas>;
580
+
581
+ /**
582
+ * The addWillSetTablesCallback method registers a WillSetTablesCallback that
583
+ * will be called before Tables are set in the Store.
584
+ *
585
+ * This has schema-based typing. The following is a simplified representation:
586
+ *
587
+ * ```ts override
588
+ * addWillSetTablesCallback(callback: WillSetTablesCallback): Middleware;
589
+ * ```
590
+ *
591
+ * The callback can transform the Tables or return `undefined` to prevent the
592
+ * write. Multiple callbacks can be registered and they are called
593
+ * sequentially, each receiving the (possibly transformed) tables from the
594
+ * previous callback.
595
+ * @param callback The WillSetTablesCallback to register.
596
+ * @returns A reference to the Middleware object, for chaining.
597
+ * @example
598
+ * This example registers a callback that upper-cases all string Cell values
599
+ * when entire Tables are set in the pet store.
600
+ *
601
+ * ```js
602
+ * import {createMiddleware, createStore} from 'tinybase';
603
+ *
604
+ * const store = createStore();
605
+ * const middleware = createMiddleware(store);
606
+ *
607
+ * middleware.addWillSetTablesCallback((tables) =>
608
+ * Object.fromEntries(
609
+ * Object.entries(tables).map(([tableId, table]) => [
610
+ * tableId,
611
+ * Object.fromEntries(
612
+ * Object.entries(table).map(([rowId, row]) => [
613
+ * rowId,
614
+ * Object.fromEntries(
615
+ * Object.entries(row).map(([k, v]) => [
616
+ * k,
617
+ * typeof v === 'string' ? v.toUpperCase() : v,
618
+ * ]),
619
+ * ),
620
+ * ]),
621
+ * ),
622
+ * ]),
623
+ * ),
624
+ * );
625
+ *
626
+ * store.setTables({pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}});
627
+ * console.log(store.getTables());
628
+ * // -> {pets: {fido: {species: 'DOG'}, felix: {species: 'CAT'}}}
629
+ *
630
+ * middleware.destroy();
631
+ * ```
632
+ * @example
633
+ * This example registers a callback that prevents setting any Tables that
634
+ * include a 'banned' table.
635
+ *
636
+ * ```js
637
+ * import {createMiddleware, createStore} from 'tinybase';
638
+ *
639
+ * const store = createStore();
640
+ * const middleware = createMiddleware(store);
641
+ *
642
+ * middleware.addWillSetTablesCallback((tables) =>
643
+ * 'banned' in tables ? undefined : tables,
644
+ * );
645
+ *
646
+ * store.setTables({banned: {r1: {c1: 1}}, pets: {fido: {species: 'dog'}}});
647
+ * console.log(store.getTables());
648
+ * // -> {}
649
+ *
650
+ * middleware.destroy();
651
+ * ```
652
+ * @category Configuration
653
+ * @since v8.0.0
654
+ */
655
+ addWillSetTablesCallback(
656
+ callback: WillSetTablesCallback<Schemas[0]>,
657
+ ): Middleware<Schemas>;
658
+
659
+ /**
660
+ * The addWillSetTableCallback method registers a WillSetTableCallback that
661
+ * will be called before any Table is set in the Store.
662
+ *
663
+ * This has schema-based typing. The following is a simplified representation:
664
+ *
665
+ * ```ts override
666
+ * addWillSetTableCallback(callback: WillSetTableCallback): Middleware;
667
+ * ```
668
+ *
669
+ * The callback can transform the Table or return `undefined` to prevent the
670
+ * write. Multiple callbacks can be registered and they are called
671
+ * sequentially, each receiving the (possibly transformed) table from the
672
+ * previous callback.
673
+ * @param callback The WillSetTableCallback to register.
674
+ * @returns A reference to the Middleware object, for chaining.
675
+ * @example
676
+ * This example registers a callback that upper-cases string Cell values in
677
+ * rows set to the 'pets' table.
678
+ *
679
+ * ```js
680
+ * import {createMiddleware, createStore} from 'tinybase';
681
+ *
682
+ * const store = createStore();
683
+ * const middleware = createMiddleware(store);
684
+ *
685
+ * middleware.addWillSetTableCallback((tableId, table) =>
686
+ * tableId === 'pets'
687
+ * ? Object.fromEntries(
688
+ * Object.entries(table).map(([rowId, row]) => [
689
+ * rowId,
690
+ * Object.fromEntries(
691
+ * Object.entries(row).map(([k, v]) => [
692
+ * k,
693
+ * typeof v === 'string' ? v.toUpperCase() : v,
694
+ * ]),
695
+ * ),
696
+ * ]),
697
+ * )
698
+ * : table,
699
+ * );
700
+ *
701
+ * store.setTable('pets', {fido: {species: 'dog'}, felix: {species: 'cat'}});
702
+ * console.log(store.getTable('pets'));
703
+ * // -> {fido: {species: 'DOG'}, felix: {species: 'CAT'}}
704
+ *
705
+ * middleware.destroy();
706
+ * ```
707
+ * @example
708
+ * This example registers a callback that prevents writes to the 'species'
709
+ * table.
710
+ *
711
+ * ```js
712
+ * import {createMiddleware, createStore} from 'tinybase';
713
+ *
714
+ * const store = createStore();
715
+ * const middleware = createMiddleware(store);
716
+ *
717
+ * middleware.addWillSetTableCallback((tableId, table) =>
718
+ * tableId === 'species' ? undefined : table,
719
+ * );
720
+ *
721
+ * store.setTable('species', {dog: {legs: 4, sound: 'woof'}});
722
+ * console.log(store.getTables());
723
+ * // -> {}
724
+ *
725
+ * middleware.destroy();
726
+ * ```
727
+ * @category Configuration
728
+ * @since v8.0.0
729
+ */
730
+ addWillSetTableCallback(
731
+ callback: WillSetTableCallback<Schemas[0]>,
732
+ ): Middleware<Schemas>;
733
+
734
+ /**
735
+ * The addWillSetRowCallback method registers a WillSetRowCallback that will
736
+ * be called before any Row is set in the Store.
737
+ *
738
+ * This has schema-based typing. The following is a simplified representation:
739
+ *
740
+ * ```ts override
741
+ * addWillSetRowCallback(callback: WillSetRowCallback): Middleware;
742
+ * ```
743
+ *
744
+ * The callback can transform the Row or return `undefined` to prevent the
745
+ * write. Multiple callbacks can be registered and they are called
746
+ * sequentially, each receiving the (possibly transformed) row from the
747
+ * previous callback.
748
+ * @param callback The WillSetRowCallback to register.
749
+ * @returns A reference to the Middleware object, for chaining.
750
+ * @example
751
+ * This example registers a callback that upper-cases string Cell values in
752
+ * rows set to the 'pets' table.
753
+ *
754
+ * ```js
755
+ * import {createMiddleware, createStore} from 'tinybase';
756
+ *
757
+ * const store = createStore();
758
+ * const middleware = createMiddleware(store);
759
+ *
760
+ * middleware.addWillSetRowCallback((tableId, _rowId, row) =>
761
+ * tableId === 'pets'
762
+ * ? Object.fromEntries(
763
+ * Object.entries(row).map(([k, v]) => [
764
+ * k,
765
+ * typeof v === 'string' ? v.toUpperCase() : v,
766
+ * ]),
767
+ * )
768
+ * : row,
769
+ * );
770
+ *
771
+ * store.setRow('pets', 'fido', {species: 'dog', legs: 4});
772
+ * console.log(store.getRow('pets', 'fido'));
773
+ * // -> {species: 'DOG', legs: 4}
774
+ *
775
+ * middleware.destroy();
776
+ * ```
777
+ * @example
778
+ * This example registers a callback that prevents writes to the 'species'
779
+ * table.
780
+ *
781
+ * ```js
782
+ * import {createMiddleware, createStore} from 'tinybase';
783
+ *
784
+ * const store = createStore();
785
+ * const middleware = createMiddleware(store);
786
+ *
787
+ * middleware.addWillSetRowCallback((tableId, _rowId, row) =>
788
+ * tableId === 'species' ? undefined : row,
789
+ * );
790
+ *
791
+ * store.setRow('species', 'dog', {legs: 4, sound: 'woof'});
792
+ * console.log(store.getTables());
793
+ * // -> {}
794
+ *
795
+ * middleware.destroy();
796
+ * ```
797
+ * @category Configuration
798
+ * @since v8.0.0
799
+ */
800
+ addWillSetRowCallback(
801
+ callback: WillSetRowCallback<Schemas[0]>,
802
+ ): Middleware<Schemas>;
803
+
804
+ /**
805
+ * The addWillSetCellCallback method registers a WillSetCellCallback that will
806
+ * be called before any Cell is set in the Store.
807
+ *
808
+ * This has schema-based typing. The following is a simplified representation:
809
+ *
810
+ * ```ts override
811
+ * addWillSetCellCallback(callback: WillSetCellCallback): Middleware;
812
+ * ```
813
+ *
814
+ * The callback can transform the Cell value or return `undefined` to prevent
815
+ * the write. Multiple callbacks can be registered and they are called
816
+ * sequentially, each receiving the (possibly transformed) value from the
817
+ * previous callback.
818
+ * @param callback The WillSetCellCallback to register.
819
+ * @returns A reference to the Middleware object, for chaining.
820
+ * @example
821
+ * This example registers a callback that upper-cases string Cell values in
822
+ * the 'pets' table.
823
+ *
824
+ * ```js
825
+ * import {createMiddleware, createStore} from 'tinybase';
826
+ *
827
+ * const store = createStore();
828
+ * const middleware = createMiddleware(store);
829
+ *
830
+ * middleware.addWillSetCellCallback((tableId, rowId, cellId, cell) =>
831
+ * tableId === 'pets' && typeof cell === 'string'
832
+ * ? cell.toUpperCase()
833
+ * : cell,
834
+ * );
835
+ *
836
+ * store.setCell('pets', 'fido', 'species', 'dog');
837
+ * console.log(store.getCell('pets', 'fido', 'species'));
838
+ * // -> 'DOG'
839
+ *
840
+ * middleware.destroy();
841
+ * ```
842
+ * @example
843
+ * This example registers a callback that prevents writes to the 'species'
844
+ * table.
845
+ *
846
+ * ```js
847
+ * import {createMiddleware, createStore} from 'tinybase';
848
+ *
849
+ * const store = createStore();
850
+ * const middleware = createMiddleware(store);
851
+ *
852
+ * middleware.addWillSetCellCallback((tableId, _rowId, _cellId, cell) =>
853
+ * tableId === 'species' ? undefined : cell,
854
+ * );
855
+ *
856
+ * store.setCell('species', 'dog', 'legs', 4);
857
+ * console.log(store.getTables());
858
+ * // -> {}
859
+ *
860
+ * middleware.destroy();
861
+ * ```
862
+ * @category Configuration
863
+ * @since v8.0.0
864
+ */
865
+ addWillSetCellCallback(
866
+ callback: WillSetCellCallback<Schemas[0]>,
867
+ ): Middleware<Schemas>;
868
+
869
+ /**
870
+ * The addWillSetValuesCallback method registers a WillSetValuesCallback that
871
+ * will be called before Values are set in the Store.
872
+ *
873
+ * This has schema-based typing. The following is a simplified representation:
874
+ *
875
+ * ```ts override
876
+ * addWillSetValuesCallback(callback: WillSetValuesCallback): Middleware;
877
+ * ```
878
+ *
879
+ * The callback can transform the Values or return `undefined` to prevent the
880
+ * write. Multiple callbacks can be registered and they are called
881
+ * sequentially, each receiving the (possibly transformed) values from the
882
+ * previous callback.
883
+ * @param callback The WillSetValuesCallback to register.
884
+ * @returns A reference to the Middleware object, for chaining.
885
+ * @example
886
+ * This example registers a callback that upper-cases all string Values in the
887
+ * pet store's settings.
888
+ *
889
+ * ```js
890
+ * import {createMiddleware, createStore} from 'tinybase';
891
+ *
892
+ * const store = createStore();
893
+ * const middleware = createMiddleware(store);
894
+ *
895
+ * middleware.addWillSetValuesCallback((values) =>
896
+ * Object.fromEntries(
897
+ * Object.entries(values).map(([k, v]) => [
898
+ * k,
899
+ * typeof v === 'string' ? v.toUpperCase() : v,
900
+ * ]),
901
+ * ),
902
+ * );
903
+ *
904
+ * store.setValues({storeName: 'happy pets', limit: 50});
905
+ * console.log(store.getValues());
906
+ * // -> {storeName: 'HAPPY PETS', limit: 50}
907
+ *
908
+ * middleware.destroy();
909
+ * ```
910
+ * @example
911
+ * This example registers a callback that prevents setting Values when the pet
912
+ * store is 'closed'.
913
+ *
914
+ * ```js
915
+ * import {createMiddleware, createStore} from 'tinybase';
916
+ *
917
+ * const store = createStore();
918
+ * const middleware = createMiddleware(store);
919
+ *
920
+ * middleware.addWillSetValuesCallback((values) =>
921
+ * 'closed' in values ? undefined : values,
922
+ * );
923
+ *
924
+ * store.setValues({closed: true, storeName: 'happy pets'});
925
+ * console.log(store.getValues());
926
+ * // -> {}
927
+ *
928
+ * middleware.destroy();
929
+ * ```
930
+ * @category Configuration
931
+ * @since v8.0.0
932
+ */
933
+ addWillSetValuesCallback(
934
+ callback: WillSetValuesCallback<Schemas[1]>,
935
+ ): Middleware<Schemas>;
936
+
937
+ /**
938
+ * The addWillSetValueCallback method registers a WillSetValueCallback that
939
+ * will be called before any Value is set in the Store.
940
+ *
941
+ * This has schema-based typing. The following is a simplified representation:
942
+ *
943
+ * ```ts override
944
+ * addWillSetValueCallback(callback: WillSetValueCallback): Middleware;
945
+ * ```
946
+ *
947
+ * The callback can transform the Value or return `undefined` to prevent the
948
+ * write. Multiple callbacks can be registered and they are called
949
+ * sequentially, each receiving the (possibly transformed) value from the
950
+ * previous callback.
951
+ * @param callback The WillSetValueCallback to register.
952
+ * @returns A reference to the Middleware object, for chaining.
953
+ * @example
954
+ * This example registers a callback that clamps the 'limit' Value to the
955
+ * maximum capacity of the pet store.
956
+ *
957
+ * ```js
958
+ * import {createMiddleware, createStore} from 'tinybase';
959
+ *
960
+ * const store = createStore();
961
+ * const middleware = createMiddleware(store);
962
+ *
963
+ * middleware.addWillSetValueCallback((valueId, value) =>
964
+ * valueId === 'limit' && typeof value === 'number'
965
+ * ? Math.min(50, Math.max(0, value))
966
+ * : value,
967
+ * );
968
+ *
969
+ * store.setValue('limit', 100);
970
+ * console.log(store.getValue('limit'));
971
+ * // -> 50
972
+ *
973
+ * middleware.destroy();
974
+ * ```
975
+ * @category Configuration
976
+ * @since v8.0.0
977
+ */
978
+ addWillSetValueCallback(
979
+ callback: WillSetValueCallback<Schemas[1]>,
980
+ ): Middleware<Schemas>;
981
+
982
+ /**
983
+ * The addWillDelTablesCallback method registers a WillDelTablesCallback that
984
+ * will be called before all Tables are deleted from the Store.
985
+ *
986
+ * This has schema-based typing. The following is a simplified representation:
987
+ *
988
+ * ```ts override
989
+ * addWillDelTablesCallback(callback: WillDelTablesCallback): Middleware;
990
+ * ```
991
+ *
992
+ * The callback returns `true` to allow the deletion or `false` to prevent
993
+ * it. Multiple callbacks can be registered and they are called sequentially.
994
+ * If any callback returns `false`, the deletion is prevented.
995
+ * @param callback The WillDelTablesCallback to register.
996
+ * @returns A reference to the Middleware object, for chaining.
997
+ * @example
998
+ * This example registers a callback that prevents deleting all Tables from
999
+ * the pet store.
1000
+ *
1001
+ * ```js
1002
+ * import {createMiddleware, createStore} from 'tinybase';
1003
+ *
1004
+ * const store = createStore();
1005
+ * const middleware = createMiddleware(store);
1006
+ *
1007
+ * store.setTables({pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}});
1008
+ *
1009
+ * middleware.addWillDelTablesCallback(() => false);
1010
+ *
1011
+ * store.delTables();
1012
+ * console.log(store.getTables());
1013
+ * // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
1014
+ *
1015
+ * middleware.destroy();
1016
+ * ```
1017
+ * @category Configuration
1018
+ * @since v8.0.0
1019
+ */
1020
+ addWillDelTablesCallback(
1021
+ callback: WillDelTablesCallback,
1022
+ ): Middleware<Schemas>;
1023
+
1024
+ /**
1025
+ * The addWillDelTableCallback method registers a WillDelTableCallback that
1026
+ * will be called before any Table is deleted from the Store.
1027
+ *
1028
+ * This has schema-based typing. The following is a simplified representation:
1029
+ *
1030
+ * ```ts override
1031
+ * addWillDelTableCallback(callback: WillDelTableCallback): Middleware;
1032
+ * ```
1033
+ *
1034
+ * The callback returns `true` to allow the deletion or `false` to prevent
1035
+ * it. Multiple callbacks can be registered and they are called sequentially.
1036
+ * If any callback returns `false`, the deletion is prevented.
1037
+ * @param callback The WillDelTableCallback to register.
1038
+ * @returns A reference to the Middleware object, for chaining.
1039
+ * @example
1040
+ * This example registers a callback that prevents deleting the 'pets' table
1041
+ * from the pet store.
1042
+ *
1043
+ * ```js
1044
+ * import {createMiddleware, createStore} from 'tinybase';
1045
+ *
1046
+ * const store = createStore();
1047
+ * const middleware = createMiddleware(store);
1048
+ *
1049
+ * store.setTable('pets', {fido: {species: 'dog'}, felix: {species: 'cat'}});
1050
+ *
1051
+ * middleware.addWillDelTableCallback((tableId) => tableId !== 'pets');
1052
+ *
1053
+ * store.delTable('pets');
1054
+ * console.log(store.getTable('pets'));
1055
+ * // -> {fido: {species: 'dog'}, felix: {species: 'cat'}}
1056
+ *
1057
+ * middleware.destroy();
1058
+ * ```
1059
+ * @category Configuration
1060
+ * @since v8.0.0
1061
+ */
1062
+ addWillDelTableCallback(
1063
+ callback: WillDelTableCallback<Schemas[0]>,
1064
+ ): Middleware<Schemas>;
1065
+
1066
+ /**
1067
+ * The addWillDelRowCallback method registers a WillDelRowCallback that will
1068
+ * be called before any Row is deleted from the Store.
1069
+ *
1070
+ * This has schema-based typing. The following is a simplified representation:
1071
+ *
1072
+ * ```ts override
1073
+ * addWillDelRowCallback(callback: WillDelRowCallback): Middleware;
1074
+ * ```
1075
+ *
1076
+ * The callback returns `true` to allow the deletion or `false` to prevent it.
1077
+ * Multiple callbacks can be registered and they are called sequentially. If
1078
+ * any callback returns `false`, the deletion is prevented.
1079
+ * @param callback The WillDelRowCallback to register.
1080
+ * @returns A reference to the Middleware object, for chaining.
1081
+ * @example
1082
+ * This example registers a callback that prevents deleting rows from the
1083
+ * 'pets' table.
1084
+ *
1085
+ * ```js
1086
+ * import {createMiddleware, createStore} from 'tinybase';
1087
+ *
1088
+ * const store = createStore();
1089
+ * const middleware = createMiddleware(store);
1090
+ *
1091
+ * store.setRow('pets', 'fido', {species: 'dog', legs: 4});
1092
+ *
1093
+ * middleware.addWillDelRowCallback((tableId) => tableId !== 'pets');
1094
+ *
1095
+ * store.delRow('pets', 'fido');
1096
+ * console.log(store.getRow('pets', 'fido'));
1097
+ * // -> {species: 'dog', legs: 4}
1098
+ *
1099
+ * middleware.destroy();
1100
+ * ```
1101
+ * @category Configuration
1102
+ * @since v8.0.0
1103
+ */
1104
+ addWillDelRowCallback(
1105
+ callback: WillDelRowCallback<Schemas[0]>,
1106
+ ): Middleware<Schemas>;
1107
+
1108
+ /**
1109
+ * The addWillDelCellCallback method registers a WillDelCellCallback that will
1110
+ * be called before any Cell is deleted from the Store.
1111
+ *
1112
+ * This has schema-based typing. The following is a simplified representation:
1113
+ *
1114
+ * ```ts override
1115
+ * addWillDelCellCallback(callback: WillDelCellCallback): Middleware;
1116
+ * ```
1117
+ *
1118
+ * The callback returns `true` to allow the deletion or `false` to prevent it.
1119
+ * Multiple callbacks can be registered and they are called sequentially. If
1120
+ * any callback returns `false`, the deletion is prevented.
1121
+ * @param callback The WillDelCellCallback to register.
1122
+ * @returns A reference to the Middleware object, for chaining.
1123
+ * @example
1124
+ * This example registers a callback that prevents deleting cells from the
1125
+ * 'pets' table.
1126
+ *
1127
+ * ```js
1128
+ * import {createMiddleware, createStore} from 'tinybase';
1129
+ *
1130
+ * const store = createStore();
1131
+ * const middleware = createMiddleware(store);
1132
+ *
1133
+ * store.setCell('pets', 'fido', 'species', 'dog');
1134
+ *
1135
+ * middleware.addWillDelCellCallback((tableId) => tableId !== 'pets');
1136
+ *
1137
+ * store.delCell('pets', 'fido', 'species', true);
1138
+ * console.log(store.getCell('pets', 'fido', 'species'));
1139
+ * // -> 'dog'
1140
+ *
1141
+ * middleware.destroy();
1142
+ * ```
1143
+ * @category Configuration
1144
+ * @since v8.0.0
1145
+ */
1146
+ addWillDelCellCallback(
1147
+ callback: WillDelCellCallback<Schemas[0]>,
1148
+ ): Middleware<Schemas>;
1149
+
1150
+ /**
1151
+ * The addWillDelValuesCallback method registers a WillDelValuesCallback that
1152
+ * will be called before all Values are deleted from the Store.
1153
+ *
1154
+ * This has schema-based typing. The following is a simplified representation:
1155
+ *
1156
+ * ```ts override
1157
+ * addWillDelValuesCallback(callback: WillDelValuesCallback): Middleware;
1158
+ * ```
1159
+ *
1160
+ * The callback returns `true` to allow the deletion or `false` to prevent it.
1161
+ * Multiple callbacks can be registered and they are called sequentially. If
1162
+ * any callback returns `false`, the deletion is prevented.
1163
+ * @param callback The WillDelValuesCallback to register.
1164
+ * @returns A reference to the Middleware object, for chaining.
1165
+ * @example
1166
+ * This example registers a callback that prevents deleting all Values from
1167
+ * the pet store.
1168
+ *
1169
+ * ```js
1170
+ * import {createMiddleware, createStore} from 'tinybase';
1171
+ *
1172
+ * const store = createStore();
1173
+ * const middleware = createMiddleware(store);
1174
+ *
1175
+ * store.setValues({storeName: 'happy pets', limit: 50});
1176
+ *
1177
+ * middleware.addWillDelValuesCallback(() => false);
1178
+ *
1179
+ * store.delValues();
1180
+ * console.log(store.getValues());
1181
+ * // -> {storeName: 'happy pets', limit: 50}
1182
+ *
1183
+ * middleware.destroy();
1184
+ * ```
1185
+ * @category Configuration
1186
+ * @since v8.0.0
1187
+ */
1188
+ addWillDelValuesCallback(
1189
+ callback: WillDelValuesCallback,
1190
+ ): Middleware<Schemas>;
1191
+
1192
+ /**
1193
+ * The addWillDelValueCallback method registers a WillDelValueCallback that
1194
+ * will be called before any Value is deleted from the Store.
1195
+ *
1196
+ * This has schema-based typing. The following is a simplified representation:
1197
+ *
1198
+ * ```ts override
1199
+ * addWillDelValueCallback(callback: WillDelValueCallback): Middleware;
1200
+ * ```
1201
+ *
1202
+ * The callback returns `true` to allow the deletion or `false` to prevent it.
1203
+ * Multiple callbacks can be registered and they are called sequentially. If
1204
+ * any callback returns `false`, the deletion is prevented.
1205
+ * @param callback The WillDelValueCallback to register.
1206
+ * @returns A reference to the Middleware object, for chaining.
1207
+ * @example
1208
+ * This example registers a callback that prevents deleting the 'storeName'
1209
+ * Value from the pet store.
1210
+ *
1211
+ * ```js
1212
+ * import {createMiddleware, createStore} from 'tinybase';
1213
+ *
1214
+ * const store = createStore();
1215
+ * const middleware = createMiddleware(store);
1216
+ *
1217
+ * store.setValue('storeName', 'happy pets');
1218
+ *
1219
+ * middleware.addWillDelValueCallback((valueId) => valueId !== 'storeName');
1220
+ *
1221
+ * store.delValue('storeName');
1222
+ * console.log(store.getValue('storeName'));
1223
+ * // -> 'happy pets'
1224
+ *
1225
+ * middleware.destroy();
1226
+ * ```
1227
+ * @category Configuration
1228
+ * @since v8.0.0
1229
+ */
1230
+ addWillDelValueCallback(
1231
+ callback: WillDelValueCallback<Schemas[1]>,
1232
+ ): Middleware<Schemas>;
1233
+
1234
+ /**
1235
+ * The addWillApplyChangesCallback method registers a
1236
+ * WillApplyChangesCallback that will be called before Changes are applied to
1237
+ * the Store via the applyChanges method.
1238
+ *
1239
+ * This has schema-based typing. The following is a simplified representation:
1240
+ *
1241
+ * ```ts override
1242
+ * addWillApplyChangesCallback(callback: WillApplyChangesCallback): Middleware;
1243
+ * ```
1244
+ *
1245
+ * This callback receives the Changes object and can return it (to allow),
1246
+ * return a modified Changes object (to transform), or return `undefined` (to
1247
+ * reject). Multiple callbacks can be registered and they are called
1248
+ * sequentially, each receiving the output of the previous. If any callback
1249
+ * returns `undefined`, all remaining callbacks are skipped and the changes
1250
+ * are rejected.
1251
+ *
1252
+ * This fires when applyChanges is called directly, or indirectly via
1253
+ * applyMergeableChanges, setMergeableContent, or merge on a MergeableStore.
1254
+ * @param callback The WillApplyChangesCallback to register.
1255
+ * @returns A reference to the Middleware object, for chaining.
1256
+ * @example
1257
+ * This example registers a callback that rejects changes containing a
1258
+ * 'secret' table.
1259
+ *
1260
+ * ```js
1261
+ * import {createMiddleware, createStore} from 'tinybase';
1262
+ *
1263
+ * const store = createStore();
1264
+ * const middleware = createMiddleware(store);
1265
+ *
1266
+ * middleware.addWillApplyChangesCallback(([changedTables, changedValues]) =>
1267
+ * changedTables['secret'] != null
1268
+ * ? undefined
1269
+ * : [changedTables, changedValues, 1],
1270
+ * );
1271
+ *
1272
+ * store.applyChanges([{pets: {fido: {species: 'dog'}}}, {}, 1]);
1273
+ * console.log(store.getRow('pets', 'fido'));
1274
+ * // -> {species: 'dog'}
1275
+ *
1276
+ * store.applyChanges([{secret: {r1: {c1: 'v1'}}}, {}, 1]);
1277
+ * console.log(store.getTable('secret'));
1278
+ * // -> {}
1279
+ *
1280
+ * middleware.destroy();
1281
+ * ```
1282
+ * @category Configuration
1283
+ * @since v8.0.0
1284
+ */
1285
+ addWillApplyChangesCallback(
1286
+ callback: WillApplyChangesCallback<Schemas>,
1287
+ ): Middleware<Schemas>;
1288
+
1289
+ /**
1290
+ * The destroy method should be called when this Middleware object is no
1291
+ * longer used. It removes all hooks and listeners from the Store, and
1292
+ * unregisters the Middleware from the Store.
1293
+ * @example
1294
+ * This example creates a Middleware object against a newly-created Store and
1295
+ * then destroys it.
1296
+ *
1297
+ * ```js
1298
+ * import {createMiddleware, createStore} from 'tinybase';
1299
+ *
1300
+ * const store = createStore();
1301
+ * const middleware = createMiddleware(store);
1302
+ * middleware.destroy();
1303
+ * ```
1304
+ * @category Lifecycle
1305
+ * @since v8.0.0
1306
+ */
1307
+ destroy(): void;
1308
+ }
1309
+
1310
+ /**
1311
+ * The createMiddleware function creates a Middleware object, and is the main
1312
+ * entry point into the middleware module.
1313
+ *
1314
+ * This has schema-based typing. The following is a simplified representation:
1315
+ *
1316
+ * ```ts override
1317
+ * createMiddleware(store: Store): Middleware;
1318
+ * ```
1319
+ *
1320
+ * A given Store can only have one Middleware object associated with it. If you
1321
+ * call this function twice on the same Store, your second call will return a
1322
+ * reference to the Middleware object created by the first.
1323
+ * @param store The Store for which to register the Middleware.
1324
+ * @returns A reference to the new Middleware object.
1325
+ * @example
1326
+ * This example creates a Middleware object.
1327
+ *
1328
+ * ```js
1329
+ * import {createMiddleware, createStore} from 'tinybase';
1330
+ *
1331
+ * const store = createStore();
1332
+ * const middleware = createMiddleware(store);
1333
+ * console.log(middleware.getStore() == store);
1334
+ * // -> true
1335
+ * middleware.destroy();
1336
+ * ```
1337
+ * @example
1338
+ * This example creates a Middleware object, and calls the method a second time
1339
+ * for the same Store to return the same object.
1340
+ *
1341
+ * ```js
1342
+ * import {createMiddleware, createStore} from 'tinybase';
1343
+ *
1344
+ * const store = createStore();
1345
+ * const middleware = createMiddleware(store);
1346
+ * console.log(middleware === createMiddleware(store));
1347
+ * // -> true
1348
+ * middleware.destroy();
1349
+ * ```
1350
+ * @category Creation
1351
+ * @since v8.0.0
1352
+ */
1353
+ export function createMiddleware<Schemas extends OptionalSchemas>(
1354
+ store: Store<Schemas>,
1355
+ ): Middleware<Schemas>;