tinybase 1.0.4 → 1.1.0-beta.2

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.
@@ -7,6 +7,8 @@ const arrayLength = (array) => array.length;
7
7
  const arrayIsEmpty = (array) => arrayLength(array) == 0;
8
8
  const arrayReduce = (array, cb, initial) => array.reduce(cb, initial);
9
9
  const arrayFromSecond = (ids) => ids.slice(1);
10
+ const arrayPush = (array, value) => array.push(value);
11
+ const arrayPop = (array) => array.pop();
10
12
 
11
13
  const isUndefined = (thing) => thing == void 0;
12
14
  const ifNotUndefined = (value, then, otherwise) =>
@@ -52,6 +54,7 @@ const getDefinableFunctions = (store, getDefaultThing, validateRowValue) => {
52
54
  const storeListenerIds = mapNew();
53
55
  const getStore = () => store;
54
56
  const getThingIds = () => mapKeys(tableIds);
57
+ const forEachThing = (cb) => mapForEach(things, cb);
55
58
  const hasThing = (id) => collHas(things, id);
56
59
  const getTableId = (id) => mapGet(tableIds, id);
57
60
  const getThing = (id) => mapGet(things, id);
@@ -142,6 +145,7 @@ const getDefinableFunctions = (store, getDefaultThing, validateRowValue) => {
142
145
  return [
143
146
  getStore,
144
147
  getThingIds,
148
+ forEachThing,
145
149
  hasThing,
146
150
  getTableId,
147
151
  getThing,
@@ -194,7 +198,7 @@ const getListenerFunctions = (getThing) => {
194
198
  const allListeners = mapNew();
195
199
  const addListener = (listener, deepSet, idOrNulls = []) => {
196
200
  thing ??= getThing();
197
- const id = listenerPool.pop() ?? '' + nextId++;
201
+ const id = arrayPop(listenerPool) ?? '' + nextId++;
198
202
  mapSet(allListeners, id, [listener, deepSet, idOrNulls]);
199
203
  addDeepSet(deepSet, id, idOrNulls);
200
204
  return id;
@@ -215,7 +219,7 @@ const getListenerFunctions = (getThing) => {
215
219
  forDeepSet(collDel)(deepSet, id, ...idOrNulls);
216
220
  mapSet(allListeners, id);
217
221
  if (arrayLength(listenerPool) < 1e3) {
218
- listenerPool.push(id);
222
+ arrayPush(listenerPool, id);
219
223
  }
220
224
  return idOrNulls;
221
225
  },
@@ -249,6 +253,7 @@ const createRelationships = getCreateFunction((store) => {
249
253
  const [
250
254
  getStore,
251
255
  getRelationshipIds,
256
+ forEachRelationshipImpl,
252
257
  hasRelationship,
253
258
  getLocalTableId,
254
259
  getRelationship,
@@ -363,6 +368,12 @@ const createRelationships = getCreateFunction((store) => {
363
368
  );
364
369
  return relationships;
365
370
  };
371
+ const forEachRelationship = (relationshipCallback) =>
372
+ forEachRelationshipImpl((relationshipId) =>
373
+ relationshipCallback(relationshipId, (rowCallback) =>
374
+ store.forEachRow(getLocalTableId(relationshipId), rowCallback),
375
+ ),
376
+ );
366
377
  const delRelationshipDefinition = (relationshipId) => {
367
378
  mapSet(remoteTableIds, relationshipId);
368
379
  delDefinition(relationshipId);
@@ -403,6 +414,7 @@ const createRelationships = getCreateFunction((store) => {
403
414
  delRelationshipDefinition,
404
415
  getStore,
405
416
  getRelationshipIds,
417
+ forEachRelationship,
406
418
  hasRelationship,
407
419
  getLocalTableId,
408
420
  getRemoteTableId,
@@ -89,7 +89,7 @@ export type Row = {[cellId: Id]: Cell};
89
89
  export type Cell = string | number | boolean;
90
90
 
91
91
  /**
92
- * The TableCallback type describes a function that takes a Tables's Id and a
92
+ * The TableCallback type describes a function that takes a Table's Id and a
93
93
  * callback to loop over each Row within it.
94
94
  *
95
95
  * A TableCallback is provided when using the forEachTable method, so that you
@@ -332,6 +332,35 @@ export type CellListener = (
332
332
  getCellChange: GetCellChange | undefined,
333
333
  ) => void;
334
334
 
335
+ /**
336
+ * The InvalidCellListener type describes a function that is used to listen to
337
+ * attempts to set invalid data to a Cell.
338
+ *
339
+ * A InvalidCellListener is provided when using the addInvalidCellListener
340
+ * method. See that method for specific examples.
341
+ *
342
+ * When called, a InvalidCellListener is given a reference to the Store, the Id
343
+ * of the Table, the Id of the Row, and the Id of Cell that were being attempted
344
+ * to be changed. It is also given the invalid value of the Cell, which could
345
+ * have been of absolutely any type. Since there could have been multiple failed
346
+ * attempts to set the Cell within a single transaction, this is an array
347
+ * containing each attempt, chronologically.
348
+ *
349
+ * @param store A reference to the Store that was being changed.
350
+ * @param tableId The Id of the Table that was being changed.
351
+ * @param rowId The Id of the Row that was being changed.
352
+ * @param cellId The Id of the Cell that was being changed.
353
+ * @param invalidCells An array of the values of the Cell that were invalid.
354
+ * @category Listener
355
+ */
356
+ export type InvalidCellListener = (
357
+ store: Store,
358
+ tableId: Id,
359
+ rowId: Id,
360
+ cellId: Id,
361
+ invalidCells: any[],
362
+ ) => void;
363
+
335
364
  /**
336
365
  * The GetCellChange type describes a function that returns information about
337
366
  * any Cell's changes during a transaction.
@@ -545,6 +574,9 @@ export type StoreListenerStats = {
545
574
  * unique Id. And the setPartialRow method lets you update multiple Cell values
546
575
  * in a Row without affecting the others.
547
576
  *
577
+ * You can listen to attempts to write invalid data to a Cell with the
578
+ * addInvalidCellListener method.
579
+ *
548
580
  * The transaction method is used to wrap multiple changes to the Store so that
549
581
  * the relevant listeners only fire once.
550
582
  *
@@ -1631,7 +1663,7 @@ export interface Store {
1631
1663
  *
1632
1664
  * This method is useful for iterating over the Table structure of the Store
1633
1665
  * in a functional style. The `tableCallback` parameter is a TableCallback
1634
- * function that will called with the Id of each Table, and with a function
1666
+ * function that will be called with the Id of each Table, and with a function
1635
1667
  * that can then be used to iterate over each Row of the Table, should you
1636
1668
  * wish.
1637
1669
  *
@@ -1664,9 +1696,10 @@ export interface Store {
1664
1696
  *
1665
1697
  * This method is useful for iterating over the Row structure of the Table in
1666
1698
  * a functional style. The `rowCallback` parameter is a RowCallback function
1667
- * that will called with the Id of each Row, and with a function that can then
1668
- * be used to iterate over each Cell of the Row, should you wish.
1699
+ * that will be called with the Id of each Row, and with a function that can
1700
+ * then be used to iterate over each Cell of the Row, should you wish.
1669
1701
  *
1702
+ * @param tableId The Id of the Table to iterate over.
1670
1703
  * @param rowCallback The function that should be called for every Row.
1671
1704
  * @example
1672
1705
  * This example iterates over each Row in a Table, and lists each Cell Id
@@ -1698,8 +1731,10 @@ export interface Store {
1698
1731
  *
1699
1732
  * This method is useful for iterating over the Cell structure of the Row in a
1700
1733
  * functional style. The `cellCallback` parameter is a CellCallback function
1701
- * that will called with the Id and value of each Cell.
1734
+ * that will be called with the Id and value of each Cell.
1702
1735
  *
1736
+ * @param tableId The Id of the Table containing the Row to iterate over.
1737
+ * @param rowId The Id of the Row to iterate over.
1703
1738
  * @param cellCallback The function that should be called for every Cell.
1704
1739
  * @example
1705
1740
  * This example iterates over each Cell in a Row, and lists its value.
@@ -2368,6 +2403,235 @@ export interface Store {
2368
2403
  mutator?: boolean,
2369
2404
  ): Id;
2370
2405
 
2406
+ /**
2407
+ * The addInvalidCellListener method registers a listener function with the
2408
+ * Store that will be called whenever invalid data was attempted to be written
2409
+ * to a Cell.
2410
+ *
2411
+ * You can either listen to a single Cell (by specifying the Table Id, Row Id,
2412
+ * and Cell Id as the method's first three parameters) or invalid attempts to
2413
+ * change any Cell (by providing `null` wildcards).
2414
+ *
2415
+ * All, some, or none of the `tableId`, `rowId`, and `cellId` parameters can
2416
+ * be wildcarded with `null`. You can listen to a specific Cell in a specific
2417
+ * Row in a specific Table, any Cell in any Row in any Table, for example - or
2418
+ * every other combination of wildcards.
2419
+ *
2420
+ * The provided listener is an InvalidCellListener function, and will be
2421
+ * called with a reference to the Store, the Id of the Table, the Id of the
2422
+ * Row, and the Id of Cell that were being attempted to be changed. It is also
2423
+ * given the invalid value of the Cell, which could have been of absolutely
2424
+ * any type. Since there could have been multiple failed attempts to set the
2425
+ * Cell within a single transaction, this is an array containing each attempt,
2426
+ * chronologically.
2427
+ *
2428
+ * Use the optional mutator parameter to indicate that there is code in the
2429
+ * listener that will mutate Store data. If set to `false` (or omitted), such
2430
+ * mutations will be silently ignored. All relevant mutator listeners (with
2431
+ * this flag set to `true`) are called _before_ any non-mutator listeners
2432
+ * (since the latter may become relevant due to changes made in the former).
2433
+ * The changes made by mutator listeners do not fire other mutating listeners,
2434
+ * though they will fire non-mutator listeners.
2435
+ *
2436
+ * Special note should be made for how the listener will be called when a
2437
+ * Schema is present. The listener will be called:
2438
+ *
2439
+ * - if a Table is being updated that is not specified in the Schema
2440
+ * - if a Cell is of the wrong type specified in the Schema
2441
+ * - if a Cell is omitted and is not defaulted in the Schema
2442
+ * - if an empty Row is provided and there are no Cell defaults in the Schema
2443
+ *
2444
+ * The listener will not be called if Cell that is defaulted in the Schema is
2445
+ * not provided, as long as all of the Cells that are _not_ defaulted _are_
2446
+ * provided.
2447
+ *
2448
+ * To help understand all of these schema-based conditions, please see the
2449
+ * Schema example below.
2450
+ *
2451
+ * @param tableId The Id of the Table to listen to, or `null` as a wildcard.
2452
+ * @param rowId The Id of the Row to listen to, or `null` as a wildcard.
2453
+ * @param cellId The Id of the Cell to listen to, or `null` as a wildcard.
2454
+ * @param listener The function that will be called whenever an attempt to
2455
+ * write invalid data to the matching Cell was made.
2456
+ * @param mutator An optional boolean that indicates that the listener mutates
2457
+ * Store data.
2458
+ * @returns A unique Id for the listener that can later be used to call it
2459
+ * explicitly, or to remove it.
2460
+ * @example
2461
+ * This example registers a listener that responds to any invalid changes to a
2462
+ * specific Cell.
2463
+ *
2464
+ * ```js
2465
+ * const store = createStore().setTables({
2466
+ * pets: {fido: {species: 'dog', color: 'brown'}},
2467
+ * });
2468
+ * const listenerId = store.addInvalidCellListener(
2469
+ * 'pets',
2470
+ * 'fido',
2471
+ * 'color',
2472
+ * (store, tableId, rowId, cellId, invalidCells) => {
2473
+ * console.log('Invalid color cell in fido row in pets table');
2474
+ * console.log(invalidCells);
2475
+ * },
2476
+ * );
2477
+ *
2478
+ * store.setCell('pets', 'fido', 'color', {r: '96', g: '4B', b: '00'});
2479
+ * // -> 'Invalid color cell in fido row in pets table'
2480
+ * // -> [{r: '96', g: '4B', b: '00'}]
2481
+ *
2482
+ * store.delListener(listenerId);
2483
+ * ```
2484
+ * @example
2485
+ * This example registers a listener that responds to any invalid changes to
2486
+ * any Cell - in a Store _without_ a Schema. Note also how it then responds to
2487
+ * cases where an empty or invalid Row objects, or Table objects, or Tables
2488
+ * objects are provided.
2489
+ *
2490
+ * ```js
2491
+ * const store = createStore().setTables({
2492
+ * pets: {fido: {species: 'dog', color: 'brown'}},
2493
+ * });
2494
+ * const listenerId = store.addInvalidCellListener(
2495
+ * null,
2496
+ * null,
2497
+ * null,
2498
+ * (store, tableId, rowId, cellId) => {
2499
+ * console.log(
2500
+ * `Invalid ${cellId} cell in ${rowId} row in ${tableId} table`,
2501
+ * );
2502
+ * },
2503
+ * );
2504
+ *
2505
+ * store.setCell('pets', 'fido', 'color', {r: '96', g: '4B', b: '00'});
2506
+ * // -> 'Invalid color cell in fido row in pets table'
2507
+ * store.setTable('sales', {fido: {date: new Date()}});
2508
+ * // -> 'Invalid date cell in fido row in sales table'
2509
+ *
2510
+ * store.setRow('pets', 'felix', {});
2511
+ * // -> 'Invalid undefined cell in felix row in pets table'
2512
+ *
2513
+ * store.setRow('filter', 'name', /[a-z]?/);
2514
+ * // -> 'Invalid undefined cell in name row in filter table'
2515
+ *
2516
+ * store.setRow('sales', '2021', {forecast: undefined});
2517
+ * // -> 'Invalid forecast cell in 2021 row in sales table'
2518
+ *
2519
+ * store.addRow('filter', /[0-9]?/);
2520
+ * // -> 'Invalid undefined cell in undefined row in filter table'
2521
+ *
2522
+ * store.setTable('raw', {});
2523
+ * // -> 'Invalid undefined cell in undefined row in raw table'
2524
+ *
2525
+ * store.setTable('raw', ['row1', 'row2']);
2526
+ * // -> 'Invalid undefined cell in undefined row in raw table'
2527
+ *
2528
+ * store.setTables(['table1', 'table2']);
2529
+ * // -> 'Invalid undefined cell in undefined row in undefined table'
2530
+ *
2531
+ * store.delListener(listenerId);
2532
+ * ```
2533
+ * @example
2534
+ * This example registers a listener that responds to any invalid changes to
2535
+ * any Cell - in a Store _with_ a Schema. Note how it responds to cases where
2536
+ * missing parameters are provided for optional, and defaulted Cell values in
2537
+ * a Row.
2538
+ *
2539
+ * ```js
2540
+ * const store = createStore().setSchema({
2541
+ * pets: {
2542
+ * species: {type: 'string'},
2543
+ * color: {type: 'string', default: 'unknown'},
2544
+ * },
2545
+ * });
2546
+ *
2547
+ * const listenerId = store.addInvalidCellListener(
2548
+ * null,
2549
+ * null,
2550
+ * null,
2551
+ * (store, tableId, rowId, cellId) => {
2552
+ * console.log(
2553
+ * `Invalid ${cellId} cell in ${rowId} row in ${tableId} table`,
2554
+ * );
2555
+ * },
2556
+ * );
2557
+ *
2558
+ * store.setRow('sales', 'fido', {price: 5});
2559
+ * // -> 'Invalid price cell in fido row in sales table'
2560
+ * // The listener is called, because the sales Table is not in the schema
2561
+ *
2562
+ * store.setRow('pets', 'felix', {species: true});
2563
+ * // -> 'Invalid species cell in felix row in pets table'
2564
+ * // The listener is called, because species is invalid...
2565
+ * console.log(store.getRow('pets', 'felix'));
2566
+ * // -> {color: 'unknown'}
2567
+ * // ...even though a Row was set with the default value
2568
+ *
2569
+ * store.setRow('pets', 'fido', {color: 'brown'});
2570
+ * // -> 'Invalid species cell in fido row in pets table'
2571
+ * // The listener is called, because species is missing and not defaulted...
2572
+ * console.log(store.getRow('pets', 'fido'));
2573
+ * // -> {color: 'brown'}
2574
+ * // ...even though a Row was set
2575
+ *
2576
+ * store.setRow('pets', 'rex', {species: 'dog'});
2577
+ * console.log(store.getRow('pets', 'rex'));
2578
+ * // -> {species: 'dog', color: 'unknown'}
2579
+ * // The listener is not called, because color is defaulted
2580
+ *
2581
+ * store.delTables().setSchema({
2582
+ * pets: {
2583
+ * species: {type: 'string'},
2584
+ * color: {type: 'string'},
2585
+ * },
2586
+ * });
2587
+ *
2588
+ * store.setRow('pets', 'cujo', {});
2589
+ * // -> 'Invalid species cell in cujo row in pets table'
2590
+ * // -> 'Invalid color cell in cujo row in pets table'
2591
+ * // -> 'Invalid undefined cell in cujo row in pets table'
2592
+ * // The listener is called multiple times, because neither Cell is defaulted
2593
+ * // and the Row as a whole is empty
2594
+ *
2595
+ * store.delListener(listenerId);
2596
+ * ```
2597
+ * @example
2598
+ * This example registers a listener that responds to any changes to a
2599
+ * specific Cell, and which also mutates the Store itself.
2600
+ *
2601
+ * ```js
2602
+ * const store = createStore().setTables({
2603
+ * pets: {fido: {species: 'dog', color: 'brown'}},
2604
+ * });
2605
+ * const listenerId = store.addInvalidCellListener(
2606
+ * 'pets',
2607
+ * 'fido',
2608
+ * 'color',
2609
+ * (store, tableId, rowId, cellId, invalidCells) =>
2610
+ * store.setCell(
2611
+ * 'meta',
2612
+ * 'invalid_updates',
2613
+ * `${tableId}_${rowId}_${cellId}`,
2614
+ * JSON.stringify(invalidCells[0]),
2615
+ * ),
2616
+ * true,
2617
+ * );
2618
+ *
2619
+ * store.setCell('pets', 'fido', 'color', {r: '96', g: '4B', b: '00'});
2620
+ * console.log(store.getRow('meta', 'invalid_updates'));
2621
+ * // -> {'pets_fido_color': '{"r":"96","g":"4B","b":"00"}'}
2622
+ *
2623
+ * store.delListener(listenerId);
2624
+ * ```
2625
+ * @category Listener
2626
+ */
2627
+ addInvalidCellListener(
2628
+ tableId: IdOrNull,
2629
+ rowId: IdOrNull,
2630
+ cellId: IdOrNull,
2631
+ listener: InvalidCellListener,
2632
+ mutator?: boolean,
2633
+ ): Id;
2634
+
2371
2635
  /**
2372
2636
  * The callListener method provides a way for you to manually provoke a
2373
2637
  * listener to be called, even if the underlying data hasn't changed.