tinybase 1.0.2 → 1.1.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/lib/checkpoints.d.ts +945 -0
  2. package/lib/checkpoints.js +1 -0
  3. package/lib/checkpoints.js.gz +0 -0
  4. package/lib/common.d.ts +115 -0
  5. package/lib/common.js +1 -0
  6. package/lib/common.js.gz +0 -0
  7. package/lib/debug/checkpoints.d.ts +66 -0
  8. package/lib/debug/checkpoints.js +332 -0
  9. package/lib/debug/common.js +3 -0
  10. package/lib/debug/indexes.d.ts +167 -6
  11. package/lib/debug/indexes.js +431 -0
  12. package/lib/debug/metrics.d.ts +72 -0
  13. package/lib/debug/metrics.js +401 -0
  14. package/lib/debug/persisters.js +191 -0
  15. package/lib/debug/relationships.d.ts +86 -1
  16. package/lib/debug/relationships.js +434 -0
  17. package/lib/debug/store.d.ts +187 -5
  18. package/lib/debug/store.js +783 -0
  19. package/lib/debug/tinybase.js +144 -39
  20. package/lib/indexes.d.ts +939 -0
  21. package/lib/indexes.js +1 -0
  22. package/lib/indexes.js.gz +0 -0
  23. package/lib/metrics.d.ts +829 -0
  24. package/lib/metrics.js +1 -0
  25. package/lib/metrics.js.gz +0 -0
  26. package/lib/persisters.d.ts +711 -0
  27. package/lib/persisters.js +1 -0
  28. package/lib/persisters.js.gz +0 -0
  29. package/lib/relationships.d.ts +1201 -0
  30. package/lib/relationships.js +1 -0
  31. package/lib/relationships.js.gz +0 -0
  32. package/lib/store.d.ts +2688 -0
  33. package/lib/store.js +1 -0
  34. package/lib/store.js.gz +0 -0
  35. package/lib/tinybase.d.ts +13 -0
  36. package/lib/tinybase.js +1 -0
  37. package/lib/tinybase.js.gz +0 -0
  38. package/lib/ui-react.d.ts +7185 -0
  39. package/lib/ui-react.js +1 -0
  40. package/lib/ui-react.js.gz +0 -0
  41. package/lib/umd/checkpoints.js +1 -0
  42. package/lib/umd/checkpoints.js.gz +0 -0
  43. package/lib/umd/common.js +1 -0
  44. package/lib/umd/common.js.gz +0 -0
  45. package/lib/umd/indexes.js +1 -0
  46. package/lib/umd/indexes.js.gz +0 -0
  47. package/lib/umd/metrics.js +1 -0
  48. package/lib/umd/metrics.js.gz +0 -0
  49. package/lib/umd/persisters.js +1 -0
  50. package/lib/umd/persisters.js.gz +0 -0
  51. package/lib/umd/relationships.js +1 -0
  52. package/lib/umd/relationships.js.gz +0 -0
  53. package/lib/umd/store.js +1 -0
  54. package/lib/umd/store.js.gz +0 -0
  55. package/lib/umd/tinybase.js +1 -0
  56. package/lib/umd/tinybase.js.gz +0 -0
  57. package/lib/umd/ui-react.js +1 -0
  58. package/lib/umd/ui-react.js.gz +0 -0
  59. package/package.json +14 -14
  60. package/readme.md +2 -2
@@ -0,0 +1,939 @@
1
+ /**
2
+ * The indexes module of the TinyBase project provides the ability to create
3
+ * and track indexes of the data in Store objects.
4
+ *
5
+ * The main entry point to this module is the createIndexes function, which
6
+ * returns a new Indexes object. From there, you can create new Index
7
+ * definitions, access the contents of those Indexes directly, and register
8
+ * listeners for when they change.
9
+ *
10
+ * @packageDocumentation
11
+ * @module indexes
12
+ */
13
+
14
+ import {GetCell, RowCallback, Store} from './store.d';
15
+ import {Id, IdOrNull, Ids, SortKey} from './common.d';
16
+
17
+ /**
18
+ * The Index type represents the concept of a map of Slice objects, keyed by Id.
19
+ *
20
+ * The Ids in a Slice represent Row objects from a Table that all have a derived
21
+ * string value in common, as described by the setIndexDefinition method. Those
22
+ * values are used as the key for each Slice in the overall Index object.
23
+ *
24
+ * Note that the Index type is not actually used in the API, and you instead
25
+ * enumerate and access its structure with the getSliceIds method and
26
+ * getSliceRowIds method.
27
+ *
28
+ * @category Concept
29
+ */
30
+ export type Index = {[sliceId: Id]: Slice};
31
+
32
+ /**
33
+ * The Slice type represents the concept of a set of Row objects that comprise
34
+ * part of an Index.
35
+ *
36
+ * The Ids in a Slice represent Row objects from a Table that all have a derived
37
+ * string value in common, as described by the setIndexDefinition method.
38
+ *
39
+ * Note that the Slice type is not actually used in the API, and you instead get
40
+ * Row Ids directly with the getSliceRowIds method.
41
+ *
42
+ * @category Concept
43
+ */
44
+ export type Slice = Ids;
45
+
46
+ /**
47
+ * The IndexCallback type describes a function that takes an Index's Id and a
48
+ * callback to loop over each Slice within it.
49
+ *
50
+ * A IndexCallback is provided when using the forEachIndex method, so that you
51
+ * can do something based on every Index in the Indexes object. See that method
52
+ * for specific examples.
53
+ *
54
+ * @param indexId The Id of the Index that the callback can operate on.
55
+ * @param forEachRow A function that will let you iterate over the Slice objects
56
+ * in this Index.
57
+ * @category Callback
58
+ */
59
+ export type IndexCallback = (
60
+ indexId: Id,
61
+ forEachSlice: (sliceCallback: SliceCallback) => void,
62
+ ) => void;
63
+
64
+ /**
65
+ * The SliceCallback type describes a function that takes a Slice's Id and a
66
+ * callback to loop over each Row within it.
67
+ *
68
+ * A SliceCallback is provided when using the forEachSlice method, so that you
69
+ * can do something based on every Slice in an Index. See that method for
70
+ * specific examples.
71
+ *
72
+ * @param sliceId The Id of the Slice that the callback can operate on.
73
+ * @param forEachRow A function that will let you iterate over the Row objects
74
+ * in this Slice.
75
+ * @category Callback
76
+ */
77
+ export type SliceCallback = (
78
+ sliceId: Id,
79
+ forEachRow: (rowCallback: RowCallback) => void,
80
+ ) => void;
81
+
82
+ /**
83
+ * The SliceIdsListener type describes a function that is used to listen to
84
+ * changes to the Slice Ids in an Index.
85
+ *
86
+ * A SliceIdsListener is provided when using the addSliceIdsListener method. See
87
+ * that method for specific examples.
88
+ *
89
+ * When called, a SliceIdsListener is given a reference to the Indexes object,
90
+ * and the Id of the Index that changed.
91
+ *
92
+ * @param indexes A reference to the Indexes object that changed.
93
+ * @param indexId The Id of the Index that changed.
94
+ * @category Listener
95
+ */
96
+ export type SliceIdsListener = (indexes: Indexes, indexId: Id) => void;
97
+
98
+ /**
99
+ * The SliceRowIdsListener type describes a function that is used to listen to
100
+ * changes to the Row Ids in a Slice.
101
+ *
102
+ * A SliceRowIdsListener is provided when using the addSliceRowIdsListener
103
+ * method. See that method for specific examples.
104
+ *
105
+ * When called, a SliceRowIdsListener is given a reference to the Indexes
106
+ * object, the Id of the Index that changed, and the Id of the Slice whose Row
107
+ * Ids changed.
108
+ *
109
+ * @param indexes A reference to the Indexes object that changed.
110
+ * @param indexId The Id of the Index that changed.
111
+ * @param sliceId The Id of the Slice that changed.
112
+ * @category Listener
113
+ */
114
+ export type SliceRowIdsListener = (
115
+ indexes: Indexes,
116
+ indexId: Id,
117
+ sliceId: Id,
118
+ ) => void;
119
+
120
+ /**
121
+ * The IndexesListenerStats type describes the number of listeners registered
122
+ * with the Indexes object, and can be used for debugging purposes.
123
+ *
124
+ * A IndexesListenerStats object is returned from the getListenerStats method,
125
+ * and is only populated in a debug build.
126
+ *
127
+ * @category Development
128
+ */
129
+ export type IndexesListenerStats = {
130
+ /**
131
+ * The number of SlideIdsListeners registered with the Indexes object.
132
+ */
133
+ sliceIds?: number;
134
+ /**
135
+ * The number of SliceRowIdsListeners registered with the Indexes object.
136
+ */
137
+ sliceRowIds?: number;
138
+ };
139
+
140
+ /**
141
+ * An Indexes object lets you look up all the Row objects in a Table that have a
142
+ * certain Cell value.
143
+ *
144
+ * This is useful for creating filtered views of a Table, or simple search
145
+ * functionality.
146
+ *
147
+ * Create an Indexes object easily with the createIndexes function. From there,
148
+ * you can add new Index definitions (with the setIndexDefinition method), query
149
+ * their contents (with the getSliceIds method and getSliceRowIds method), and
150
+ * add listeners for when they change (with the addSliceIdsListener method and
151
+ * addSliceRowIdsListener method).
152
+ *
153
+ * This module defaults to indexing Row objects by one of their Cell values.
154
+ * However, far more complex indexes can be configured with a custom function.
155
+ *
156
+ * @example
157
+ * This example shows a very simple lifecycle of an Indexes object: from
158
+ * creation, to adding a definition, getting its contents, and then registering
159
+ * and removing a listener for it.
160
+ *
161
+ * ```js
162
+ * const store = createStore().setTable('pets', {
163
+ * fido: {species: 'dog'},
164
+ * felix: {species: 'cat'},
165
+ * cujo: {species: 'dog'},
166
+ * });
167
+ *
168
+ * const indexes = createIndexes(store);
169
+ * indexes.setIndexDefinition(
170
+ * 'bySpecies', // indexId
171
+ * 'pets', // tableId to index
172
+ * 'species', // cellId to index
173
+ * );
174
+ *
175
+ * console.log(indexes.getSliceIds('bySpecies'));
176
+ * // -> ['dog', 'cat']
177
+ * console.log(indexes.getSliceRowIds('bySpecies', 'dog'));
178
+ * // -> ['fido', 'cujo']
179
+ *
180
+ * const listenerId = indexes.addSliceIdsListener('bySpecies', () => {
181
+ * console.log(indexes.getSliceIds('bySpecies'));
182
+ * });
183
+ * store.setRow('pets', 'lowly', {species: 'worm'});
184
+ * // -> ['dog', 'cat', 'worm']
185
+ *
186
+ * indexes.delListener(listenerId);
187
+ * indexes.destroy();
188
+ * ```
189
+ * @see Metrics And Indexes guides
190
+ * @see Rolling Dice demos
191
+ * @see Country demo
192
+ * @see Todo App demos
193
+ * @category Indexes
194
+ */
195
+ export interface Indexes {
196
+ /**
197
+ * The setIndexDefinition method lets you set the definition of an Index.
198
+ *
199
+ * Every Index definition is identified by a unique Id, and if you re-use an
200
+ * existing Id with this method, the previous definition is overwritten.
201
+ *
202
+ * An Index is a keyed map of Slice objects, each of which is a list of Row
203
+ * Ids from a given Table. Therefore the definition must specify the Table (by
204
+ * its Id) to be indexed.
205
+ *
206
+ * The Ids in a Slice represent Row objects from a Table that all have a
207
+ * derived string value in common, as described by this method. Those values
208
+ * are used as the key for each Slice in the overall Index object.
209
+ *
210
+ * Without the third `getSliceId` parameter, the Index will simply have a
211
+ * single Slice, keyed by an empty string. But more often you will specify a
212
+ * Cell value containing the Slice Id that the Row should belong to.
213
+ * Alternatively, a custom function can be provided that produces your own
214
+ * Slice Id from the local Row as a whole.
215
+ *
216
+ * The fourth `getSortKey` parameter specifies a Cell Id to get a value (or a
217
+ * function that processes a whole Row to get a value) that is used to sort
218
+ * the Row Ids within each Slice in the Index.
219
+ *
220
+ * The fifth parameter, `sliceIdSorter`, lets you specify a way to sort the
221
+ * Slice Ids when you access the Index, which may be useful if you are trying
222
+ * to create an alphabetic Index of Row entries. If not specified, the order
223
+ * of the Slice Ids will match the order of Row insertion.
224
+ *
225
+ * The final parameter, `rowIdSorter`, lets you specify a way to sort the Row
226
+ * Ids within each Slice, based on the `getSortKey` parameter. This may be
227
+ * useful if you are trying to keep Rows in a determined order relative to
228
+ * each other in the Index. If omitted, the Row Ids are sorted alphabetically,
229
+ * based on the `getSortKey` parameter.
230
+ *
231
+ * The two 'sorter' parameters, `sliceIdSorter` and `rowIdSorter`, are
232
+ * functions that take two values and return a positive or negative number for
233
+ * when they are in the wrong or right order, respectively. This is exactly
234
+ * the same as the 'compareFunction' that is used in the standard JavaScript
235
+ * array `sort` method, with the addition that `rowIdSorter` also takes the
236
+ * Slice Id parameter, in case you want to sort Row Ids differently in each
237
+ * Slice. You can use the convenient defaultSorter function to default this to
238
+ * be alphanumeric.
239
+ *
240
+ * @param indexId The Id of the Index to define.
241
+ * @param tableId The Id of the Table the Index will be generated from.
242
+ * @param getSliceId Either the Id of the Cell containing, or a function that
243
+ * produces, the Id that is used to indicate which Slice in the Index the Row
244
+ * Id should be in. Defaults to a function that returns `''` (meaning that if
245
+ * this `getSliceId` parameter is omitted, the Index will simply contain a
246
+ * single Slice containing all the Row Ids in the Table).
247
+ * @param getSortKey Either the Id of the Cell containing, or a function that
248
+ * produces, the value that is used to sort the Row Ids in each Slice.
249
+ * @param sliceIdSorter A function that takes two Slice Id values and returns
250
+ * a positive or negative number to indicate how they should be sorted.
251
+ * @param rowIdSorter A function that takes two Row Id values (and a slice Id)
252
+ * and returns a positive or negative number to indicate how they should be
253
+ * sorted.
254
+ * @returns A reference to the Indexes object.
255
+ * @example
256
+ * This example creates a Store, creates an Indexes object, and defines a
257
+ * simple Index based on the values in the `species` Cell.
258
+ *
259
+ * ```js
260
+ * const store = createStore().setTable('pets', {
261
+ * fido: {species: 'dog'},
262
+ * felix: {species: 'cat'},
263
+ * cujo: {species: 'dog'},
264
+ * });
265
+ *
266
+ * const indexes = createIndexes(store);
267
+ * indexes.setIndexDefinition('bySpecies', 'pets', 'species');
268
+ *
269
+ * console.log(indexes.getSliceIds('bySpecies'));
270
+ * // -> ['dog', 'cat']
271
+ * console.log(indexes.getSliceRowIds('bySpecies', 'dog'));
272
+ * // -> ['fido', 'cujo']
273
+ * ```
274
+ * @example
275
+ * This example creates a Store, creates an Indexes object, and defines an
276
+ * Index based on the first letter of the pets' names.
277
+ *
278
+ * ```js
279
+ * const store = createStore().setTable('pets', {
280
+ * fido: {species: 'dog'},
281
+ * felix: {species: 'cat'},
282
+ * cujo: {species: 'dog'},
283
+ * });
284
+ *
285
+ * const indexes = createIndexes(store);
286
+ * indexes.setIndexDefinition('byFirst', 'pets', (_, rowId) => rowId[0]);
287
+ *
288
+ * console.log(indexes.getSliceIds('byFirst'));
289
+ * // -> ['f', 'c']
290
+ * console.log(indexes.getSliceRowIds('byFirst', 'f'));
291
+ * // -> ['fido', 'felix']
292
+ * ```
293
+ * @example
294
+ * This example creates a Store, creates an Indexes object, and defines an
295
+ * Index based on the first letter of the pets' names. The Slice Ids (and Row
296
+ * Ids within them) are alphabetically sorted.
297
+ *
298
+ * ```js
299
+ * const store = createStore().setTable('pets', {
300
+ * fido: {species: 'dog'},
301
+ * felix: {species: 'cat'},
302
+ * cujo: {species: 'dog'},
303
+ * });
304
+ *
305
+ * const indexes = createIndexes(store);
306
+ * indexes.setIndexDefinition(
307
+ * 'byFirst', // indexId
308
+ * 'pets', // tableId
309
+ * (_, rowId) => rowId[0], // each Row's sliceId
310
+ * (_, rowId) => rowId, // each Row's sort key
311
+ * defaultSorter, // sort Slice Ids
312
+ * defaultSorter, // sort Row Ids
313
+ * );
314
+ *
315
+ * console.log(indexes.getSliceIds('byFirst'));
316
+ * // -> ['c', 'f']
317
+ * console.log(indexes.getSliceRowIds('byFirst', 'f'));
318
+ * // -> ['felix', 'fido']
319
+ * ```
320
+ * @category Configuration
321
+ */
322
+ setIndexDefinition(
323
+ indexId: Id,
324
+ tableId: Id,
325
+ getSliceId?: Id | ((getCell: GetCell, rowId: Id) => Id),
326
+ getSortKey?: Id | ((getCell: GetCell, rowId: Id) => SortKey),
327
+ sliceIdSorter?: (sliceId1: Id, sliceId2: Id) => number,
328
+ rowIdSorter?: (sortKey1: SortKey, sortKey2: SortKey, sliceId: Id) => number,
329
+ ): Indexes;
330
+
331
+ /**
332
+ * The delIndexDefinition method removes an existing Index definition.
333
+ *
334
+ * @param indexId The Id of the Index to remove.
335
+ * @returns A reference to the Indexes object.
336
+ * @example
337
+ * This example creates a Store, creates an Indexes object, defines a simple
338
+ * Index, and then removes it.
339
+ *
340
+ * ```js
341
+ * const store = createStore().setTable('pets', {
342
+ * fido: {species: 'dog'},
343
+ * felix: {species: 'cat'},
344
+ * cujo: {species: 'dog'},
345
+ * });
346
+ *
347
+ * const indexes = createIndexes(store);
348
+ * indexes.setIndexDefinition('bySpecies', 'pets', 'species');
349
+ * console.log(indexes.getIndexIds());
350
+ * // -> ['bySpecies']
351
+ *
352
+ * indexes.delIndexDefinition('bySpecies');
353
+ * console.log(indexes.getIndexIds());
354
+ * // -> []
355
+ * ```
356
+ * @category Configuration
357
+ */
358
+ delIndexDefinition(indexId: Id): Indexes;
359
+
360
+ /**
361
+ * The getStore method returns a reference to the underlying Store that is
362
+ * backing this Indexes object.
363
+ *
364
+ * @returns A reference to the Store.
365
+ * @example
366
+ * This example creates an Indexes object against a newly-created Store and
367
+ * then gets its reference in order to update its data.
368
+ *
369
+ * ```js
370
+ * const indexes = createIndexes(createStore());
371
+ * indexes.setIndexDefinition('bySpecies', 'pets', 'species');
372
+ * indexes.getStore().setCell('pets', 'fido', 'species', 'dog');
373
+ * console.log(indexes.getSliceIds('bySpecies'));
374
+ * // -> ['dog']
375
+ * ```
376
+ * @category Getter
377
+ */
378
+ getStore(): Store;
379
+
380
+ /**
381
+ * The getIndexIds method returns an array of the Index Ids registered with
382
+ * this Indexes object.
383
+ *
384
+ * @returns An array of Ids.
385
+ * @example
386
+ * This example creates an Indexes object with two definitions, and then gets
387
+ * the Ids of the definitions.
388
+ *
389
+ * ```js
390
+ * const indexes = createIndexes(createStore())
391
+ * .setIndexDefinition('bySpecies', 'pets', 'species')
392
+ * .setIndexDefinition('byColor', 'pets', 'color');
393
+ *
394
+ * console.log(indexes.getIndexIds());
395
+ * // -> ['bySpecies', 'byColor']
396
+ * ```
397
+ * @category Getter
398
+ */
399
+ getIndexIds(): Ids;
400
+
401
+ /**
402
+ * The forEachIndex method takes a function that it will then call for each
403
+ * Index in a specified Indexes object.
404
+ *
405
+ * This method is useful for iterating over the structure of the Indexes
406
+ * object in a functional style. The `indexCallback` parameter is a
407
+ * IndexCallback function that will be called with the Id of each Index, and
408
+ * with a function that can then be used to iterate over each Slice of the
409
+ * Index, should you wish.
410
+ *
411
+ * @param indexCallback The function that should be called for every Index.
412
+ * @example
413
+ * This example iterates over each Index in an Indexes object, and lists each
414
+ * Slice Id within them.
415
+ *
416
+ * ```js
417
+ * const store = createStore().setTable('pets', {
418
+ * fido: {species: 'dog', color: 'brown'},
419
+ * felix: {species: 'cat', color: 'black'},
420
+ * cujo: {species: 'dog', color: 'black'},
421
+ * });
422
+ * const indexes = createIndexes(store)
423
+ * .setIndexDefinition('bySpecies', 'pets', 'species')
424
+ * .setIndexDefinition('byColor', 'pets', 'color');
425
+ *
426
+ * indexes.forEachIndex((indexId, forEachSlice) => {
427
+ * console.log(indexId);
428
+ * forEachSlice((sliceId) => console.log(`- ${sliceId}`));
429
+ * });
430
+ * // -> 'bySpecies'
431
+ * // -> '- dog'
432
+ * // -> '- cat'
433
+ * // -> 'byColor'
434
+ * // -> '- brown'
435
+ * // -> '- black'
436
+ * ```
437
+ * @category Iterator
438
+ */
439
+ forEachIndex(indexCallback: IndexCallback): void;
440
+
441
+ /**
442
+ * The forEachSlice method takes a function that it will then call for each
443
+ * Slice in a specified Index.
444
+ *
445
+ * This method is useful for iterating over the Slice structure of the Index
446
+ * in a functional style. The `rowCallback` parameter is a RowCallback
447
+ * function that will be called with the Id and value of each Row in the
448
+ * Slice.
449
+ *
450
+ * @param indexId The Id of the Index to iterate over.
451
+ * @param sliceCallback The function that should be called for every Slice.
452
+ * @example
453
+ * This example iterates over each Row in a Slice, and lists its Id.
454
+ *
455
+ * ```js
456
+ * const store = createStore().setTable('pets', {
457
+ * fido: {species: 'dog'},
458
+ * felix: {species: 'cat'},
459
+ * cujo: {species: 'dog'},
460
+ * });
461
+ * const indexes = createIndexes(store);
462
+ * indexes.setIndexDefinition('bySpecies', 'pets', 'species');
463
+ *
464
+ * indexes.forEachSlice('bySpecies', (sliceId, forEachRow) => {
465
+ * console.log(sliceId);
466
+ * forEachRow((rowId) => console.log(`- ${rowId}`));
467
+ * });
468
+ * // -> 'dog'
469
+ * // -> '- fido'
470
+ * // -> '- cujo'
471
+ * // -> 'cat'
472
+ * // -> '- felix'
473
+ * ```
474
+ * @category Iterator
475
+ */
476
+ forEachSlice(indexId: Id, sliceCallback: SliceCallback): void;
477
+
478
+ /**
479
+ * The hasIndex method returns a boolean indicating whether a given Index
480
+ * exists in the Indexes object.
481
+ *
482
+ * @param indexId The Id of a possible Index in the Indexes object.
483
+ * @returns Whether an Index with that Id exists.
484
+ * @example
485
+ * This example shows two simple Index existence checks.
486
+ *
487
+ * ```js
488
+ * const indexes = createIndexes(createStore());
489
+ * indexes.setIndexDefinition('bySpecies', 'pets', 'species');
490
+ * console.log(indexes.hasIndex('bySpecies'));
491
+ * // -> true
492
+ * console.log(indexes.hasIndex('byColor'));
493
+ * // -> false
494
+ * ```
495
+ * @category Getter
496
+ */
497
+ hasIndex(indexId: Id): boolean;
498
+
499
+ /**
500
+ * The hasSlice method returns a boolean indicating whether a given Slice
501
+ * exists in the Indexes object.
502
+ *
503
+ * @param indexId The Id of a possible Index in the Indexes object.
504
+ * @param sliceId The Id of a possible Slice in the Index.
505
+ * @returns Whether a Slice with that Id exists.
506
+ * @example
507
+ * This example shows two simple Index existence checks.
508
+ *
509
+ * ```js
510
+ * const store = createStore().setTable('pets', {
511
+ * fido: {species: 'dog'},
512
+ * felix: {species: 'cat'},
513
+ * cujo: {species: 'dog'},
514
+ * });
515
+ * const indexes = createIndexes(store);
516
+ * indexes.setIndexDefinition('bySpecies', 'pets', 'species');
517
+ * console.log(indexes.hasSlice('bySpecies', 'dog'));
518
+ * // -> true
519
+ * console.log(indexes.hasSlice('bySpecies', 'worm'));
520
+ * // -> false
521
+ * ```
522
+ * @category Getter
523
+ */
524
+ hasSlice(indexId: Id, sliceId: Id): boolean;
525
+
526
+ /**
527
+ * The getTableId method returns the Id of the underlying Table that is
528
+ * backing an Index.
529
+ *
530
+ * If the Index Id is invalid, the method returns `undefined`.
531
+ *
532
+ * @param indexId The Id of an Index.
533
+ * @returns The Id of the Table backing the Index, or `undefined`.
534
+ * @example
535
+ * This example creates an Indexes object, a single Index definition, and then
536
+ * queries it (and a non-existent definition) to get the underlying Table Id.
537
+ *
538
+ * ```js
539
+ * const indexes = createIndexes(createStore());
540
+ * indexes.setIndexDefinition('bySpecies', 'pets', 'species');
541
+ *
542
+ * console.log(indexes.getTableId('bySpecies'));
543
+ * // -> 'pets'
544
+ * console.log(indexes.getTableId('byColor'));
545
+ * // -> undefined
546
+ * ```
547
+ * @category Getter
548
+ */
549
+ getTableId(indexId: Id): Id;
550
+
551
+ /**
552
+ * The getSliceIds method gets the list of Slice Ids in an Index.
553
+ *
554
+ * If the identified Index does not exist (or if the definition references a
555
+ * Table that does not exist) then an empty array is returned.
556
+ *
557
+ * @param indexId The Id of the Index.
558
+ * @returns The Slice Ids in the Index, or an empty array.
559
+ * @example
560
+ * This example creates a Store, creates an Indexes object, and defines a
561
+ * simple Index. It then uses getSliceIds to see the available Slice Ids in
562
+ * the Index (and also the Slice Ids in an Index that has not been defined).
563
+ *
564
+ * ```js
565
+ * const store = createStore().setTable('pets', {
566
+ * fido: {species: 'dog'},
567
+ * felix: {species: 'cat'},
568
+ * cujo: {species: 'dog'},
569
+ * });
570
+ *
571
+ * const indexes = createIndexes(store);
572
+ * indexes.setIndexDefinition('bySpecies', 'pets', 'species');
573
+ *
574
+ * console.log(indexes.getSliceIds('bySpecies'));
575
+ * // -> ['dog', 'cat']
576
+ * console.log(indexes.getSliceIds('byColor'));
577
+ * // -> []
578
+ * ```
579
+ * @category Getter
580
+ */
581
+ getSliceIds(indexId: Id): Ids;
582
+
583
+ /**
584
+ * The getSliceRowIds method gets the list of Row Ids in a given Slice, within
585
+ * a given Index.
586
+ *
587
+ * If the identified Index or Slice do not exist (or if the definition
588
+ * references a Table that does not exist) then an empty array is returned.
589
+ *
590
+ * @param indexId The Id of the Index.
591
+ * @param sliceId The Id of the Slice in the Index.
592
+ * @returns The Row Ids in the Slice, or an empty array.
593
+ * @example
594
+ * This example creates a Store, creates an Indexes object, and defines a
595
+ * simple Index. It then uses getSliceRowIds to see the Row Ids in the Slice
596
+ * (and also the Row Ids in Slices that do not exist).
597
+ *
598
+ * ```js
599
+ * const store = createStore().setTable('pets', {
600
+ * fido: {species: 'dog'},
601
+ * felix: {species: 'cat'},
602
+ * cujo: {species: 'dog'},
603
+ * });
604
+ *
605
+ * const indexes = createIndexes(store);
606
+ * indexes.setIndexDefinition('bySpecies', 'pets', 'species');
607
+ *
608
+ * console.log(indexes.getSliceRowIds('bySpecies', 'dog'));
609
+ * // -> ['fido', 'cujo']
610
+ * console.log(indexes.getSliceRowIds('bySpecies', 'worm'));
611
+ * // -> []
612
+ * console.log(indexes.getSliceRowIds('byColor', 'brown'));
613
+ * // -> []
614
+ * ```
615
+ * @category Getter
616
+ */
617
+ getSliceRowIds(indexId: Id, sliceId: Id): Ids;
618
+
619
+ /**
620
+ * The addSliceIdsListener method registers a listener function with the
621
+ * Indexes object that will be called whenever the Slice Ids in an Index
622
+ * change.
623
+ *
624
+ * You can either listen to a single Index (by specifying the Index Id as the
625
+ * method's first parameter), or changes to any Index (by providing a `null`
626
+ * wildcard).
627
+ *
628
+ * The provided listener is a SliceIdsListener function, and will be called
629
+ * with a reference to the Indexes object, and the Id of the Index that
630
+ * changed.
631
+ *
632
+ * @param indexId The Id of the Index to listen to, or `null` as a wildcard.
633
+ * @param listener The function that will be called whenever the Slice Ids in
634
+ * the Index change.
635
+ * @returns A unique Id for the listener that can later be used to remove it.
636
+ * @example
637
+ * This example creates a Store, an Indexes object, and then registers a
638
+ * listener that responds to any changes to a specific Index.
639
+ *
640
+ * ```js
641
+ * const store = createStore().setTable('pets', {
642
+ * fido: {species: 'dog'},
643
+ * felix: {species: 'cat'},
644
+ * cujo: {species: 'dog'},
645
+ * });
646
+ *
647
+ * const indexes = createIndexes(store);
648
+ * indexes.setIndexDefinition('bySpecies', 'pets', 'species');
649
+ *
650
+ * const listenerId = indexes.addSliceIdsListener(
651
+ * 'bySpecies',
652
+ * (indexes, indexId) => {
653
+ * console.log('Slice Ids for bySpecies index changed');
654
+ * console.log(indexes.getSliceIds('bySpecies'));
655
+ * },
656
+ * );
657
+ *
658
+ * store.setRow('pets', 'lowly', {species: 'worm'});
659
+ * // -> 'Slice Ids for bySpecies index changed'
660
+ * // -> ['dog', 'cat', 'worm']
661
+ *
662
+ * indexes.delListener(listenerId);
663
+ * ```
664
+ * @example
665
+ * This example creates a Store, an Indexes object, and then registers a
666
+ * listener that responds to any changes to any Index.
667
+ *
668
+ * ```js
669
+ * const store = createStore().setTable('pets', {
670
+ * fido: {species: 'dog', color: 'brown'},
671
+ * felix: {species: 'cat', color: 'black'},
672
+ * cujo: {species: 'dog', color: 'brown'},
673
+ * });
674
+ *
675
+ * const indexes = createIndexes(store)
676
+ * .setIndexDefinition('bySpecies', 'pets', 'species')
677
+ * .setIndexDefinition('byColor', 'pets', 'color');
678
+ *
679
+ * const listenerId = indexes.addSliceIdsListener(
680
+ * null,
681
+ * (indexes, indexId) => {
682
+ * console.log(`Slice Ids for ${indexId} index changed`);
683
+ * console.log(indexes.getSliceIds(indexId));
684
+ * },
685
+ * );
686
+ *
687
+ * store.setRow('pets', 'lowly', {species: 'worm', color: 'pink'});
688
+ * // -> 'Slice Ids for bySpecies index changed'
689
+ * // -> ['dog', 'cat', 'worm']
690
+ * // -> 'Slice Ids for byColor index changed'
691
+ * // -> ['brown', 'black', 'pink']
692
+ *
693
+ * indexes.delListener(listenerId);
694
+ * ```
695
+ * @category Listener
696
+ */
697
+ addSliceIdsListener(indexId: IdOrNull, listener: SliceIdsListener): Id;
698
+
699
+ /**
700
+ * The addSliceRowIdsListener method registers a listener function with the
701
+ * Indexes object that will be called whenever the Row Ids in a Slice change.
702
+ *
703
+ * You can either listen to a single Slice (by specifying the Index Id and
704
+ * Slice Id as the method's first two parameters), or changes to any Slice (by
705
+ * providing `null` wildcards).
706
+ *
707
+ * Both, either, or neither of the `indexId` and `sliceId` parameters can be
708
+ * wildcarded with `null`. You can listen to a specific Slice in a specific
709
+ * Index, any Slice in a specific Index, a specific Slice in any Index, or any
710
+ * Slice in any Index.
711
+ *
712
+ * The provided listener is a SliceRowIdsListener function, and will be called
713
+ * with a reference to the Indexes object, the Id of the Index, and the Id of
714
+ * the Slice that changed.
715
+ *
716
+ * @param indexId The Id of the Index to listen to, or `null` as a wildcard.
717
+ * @param sliceId The Id of the Slice to listen to, or `null` as a wildcard.
718
+ * @param listener The function that will be called whenever the Row Ids in
719
+ * the Slice change.
720
+ * @returns A unique Id for the listener that can later be used to remove it.
721
+ * @example
722
+ * This example creates a Store, an Indexes object, and then registers a
723
+ * listener that responds to any changes to a specific Slice.
724
+ *
725
+ * ```js
726
+ * const store = createStore().setTable('pets', {
727
+ * fido: {species: 'dog'},
728
+ * felix: {species: 'cat'},
729
+ * cujo: {species: 'dog'},
730
+ * });
731
+ *
732
+ * const indexes = createIndexes(store);
733
+ * indexes.setIndexDefinition('bySpecies', 'pets', 'species');
734
+ *
735
+ * const listenerId = indexes.addSliceRowIdsListener(
736
+ * 'bySpecies',
737
+ * 'dog',
738
+ * (indexes, indexId, sliceId) => {
739
+ * console.log('Row Ids for dog slice in bySpecies index changed');
740
+ * console.log(indexes.getSliceRowIds('bySpecies', 'dog'));
741
+ * },
742
+ * );
743
+ *
744
+ * store.setRow('pets', 'toto', {species: 'dog'});
745
+ * // -> 'Row Ids for dog slice in bySpecies index changed'
746
+ * // -> ['fido', 'cujo', 'toto']
747
+ *
748
+ * indexes.delListener(listenerId);
749
+ * ```
750
+ * @example
751
+ * This example creates a Store, an Indexes object, and then registers a
752
+ * listener that responds to any changes to any Slice.
753
+ *
754
+ * ```js
755
+ * const store = createStore().setTable('pets', {
756
+ * fido: {species: 'dog', color: 'brown'},
757
+ * felix: {species: 'cat', color: 'black'},
758
+ * cujo: {species: 'dog', color: 'black'},
759
+ * });
760
+ *
761
+ * const indexes = createIndexes(store)
762
+ * .setIndexDefinition('bySpecies', 'pets', 'species')
763
+ * .setIndexDefinition('byColor', 'pets', 'color');
764
+ *
765
+ * const listenerId = indexes.addSliceRowIdsListener(
766
+ * null,
767
+ * null,
768
+ * (indexes, indexId, sliceId) => {
769
+ * console.log(
770
+ * `Row Ids for ${sliceId} slice in ${indexId} index changed`,
771
+ * );
772
+ * console.log(indexes.getSliceRowIds(indexId, sliceId));
773
+ * },
774
+ * );
775
+ *
776
+ * store.setRow('pets', 'toto', {species: 'dog', color: 'brown'});
777
+ * // -> 'Row Ids for dog slice in bySpecies index changed'
778
+ * // -> ['fido', 'cujo', 'toto']
779
+ * // -> 'Row Ids for brown slice in byColor index changed'
780
+ * // -> ['fido', 'toto']
781
+ *
782
+ * indexes.delListener(listenerId);
783
+ * ```
784
+ * @category Listener
785
+ */
786
+ addSliceRowIdsListener(
787
+ indexId: IdOrNull,
788
+ sliceId: IdOrNull,
789
+ listener: SliceRowIdsListener,
790
+ ): Id;
791
+
792
+ /**
793
+ * The delListener method removes a listener that was previously added to the
794
+ * Indexes object.
795
+ *
796
+ * Use the Id returned by whichever method was used to add the listener. Note
797
+ * that the Indexes object may re-use this Id for future listeners added to
798
+ * it.
799
+ *
800
+ * @param listenerId The Id of the listener to remove.
801
+ * @returns A reference to the Indexes object.
802
+ * @example
803
+ * This example creates a Store, an Indexes object, registers a listener, and
804
+ * then removes it.
805
+ *
806
+ * ```js
807
+ * const store = createStore().setTable('pets', {
808
+ * fido: {species: 'dog'},
809
+ * felix: {species: 'cat'},
810
+ * cujo: {species: 'dog'},
811
+ * });
812
+ *
813
+ * const indexes = createIndexes(store);
814
+ * indexes.setIndexDefinition('bySpecies', 'pets', 'species');
815
+ *
816
+ * const listenerId = indexes.addSliceIdsListener(
817
+ * 'bySpecies',
818
+ * (indexes, indexId) => {
819
+ * console.log('Slice Ids for bySpecies index changed');
820
+ * },
821
+ * );
822
+ *
823
+ * store.setRow('pets', 'lowly', {species: 'worm'});
824
+ * // -> 'Slice Ids for bySpecies index changed'
825
+ *
826
+ * indexes.delListener(listenerId);
827
+ *
828
+ * store.setRow('pets', 'toto', {species: 'dog'});
829
+ * // -> undefined
830
+ * // The listener is not called.
831
+ * ```
832
+ * @category Listener
833
+ */
834
+ delListener(listenerId: Id): Indexes;
835
+
836
+ /**
837
+ * The destroy method should be called when this Indexes object is no longer
838
+ * used.
839
+ *
840
+ * This guarantees that all of the listeners that the object registered with
841
+ * the underlying Store are removed and it can be correctly garbage collected.
842
+ *
843
+ * @example
844
+ * This example creates a Store, adds an Indexes object with a
845
+ * definition (that registers a RowListener with the underlying Store),
846
+ * and then destroys it again, removing the listener.
847
+ *
848
+ * ```js
849
+ * const store = createStore().setTable('pets', {
850
+ * fido: {species: 'dog'},
851
+ * felix: {species: 'cat'},
852
+ * cujo: {species: 'dog'},
853
+ * });
854
+ *
855
+ * const indexes = createIndexes(store);
856
+ * indexes.setIndexDefinition('bySpecies', 'pets', 'species');
857
+ * console.log(store.getListenerStats().row);
858
+ * // -> 1
859
+ *
860
+ * indexes.destroy();
861
+ *
862
+ * console.log(store.getListenerStats().row);
863
+ * // -> 0
864
+ * ```
865
+ * @category Lifecycle
866
+ */
867
+ destroy(): void;
868
+
869
+ /**
870
+ * The getListenerStats method provides a set of statistics about the
871
+ * listeners registered with the Indexes object, and is used for debugging
872
+ * purposes.
873
+ *
874
+ * The IndexesListenerStats object contains a breakdown of the different types
875
+ * of listener.
876
+ *
877
+ * The statistics are only populated in a debug build: production builds
878
+ * return an empty object. The method is intended to be used during
879
+ * development to ensure your application is not leaking listener
880
+ * registrations, for example.
881
+ *
882
+ * @returns A IndexesListenerStats object containing Indexes listener
883
+ * statistics.
884
+ * @example
885
+ * This example gets the listener statistics of an Indexes object.
886
+ *
887
+ * ```js
888
+ * const store = createStore();
889
+ * const indexes = createIndexes(store);
890
+ * indexes.addSliceIdsListener(null, () => {
891
+ * console.log('Slice Ids changed');
892
+ * });
893
+ * indexes.addSliceRowIdsListener(null, null, () => {
894
+ * console.log('Slice Row Ids changed');
895
+ * });
896
+ *
897
+ * console.log(indexes.getListenerStats());
898
+ * // -> {sliceIds: 1, sliceRowIds: 1}
899
+ * ```
900
+ * @category Development
901
+ */
902
+ getListenerStats(): IndexesListenerStats;
903
+ }
904
+
905
+ /**
906
+ * The createIndexes function creates an Indexes object, and is the main entry
907
+ * point into the indexes module.
908
+ *
909
+ * It is trivially simple.
910
+ *
911
+ * A given Store can only have one Indexes object associated with it. If you
912
+ * call this function twice on the same Store, your second call will return a
913
+ * reference to the Indexes object created by the first.
914
+ *
915
+ * @param store The Store for which to register Index definitions.
916
+ * @returns A reference to the new Indexes object.
917
+ * @example
918
+ * This example creates an Indexes object.
919
+ *
920
+ * ```js
921
+ * const store = createStore();
922
+ * const indexes = createIndexes(store);
923
+ * console.log(indexes.getIndexIds());
924
+ * // -> []
925
+ * ```
926
+ * @example
927
+ * This example creates an Indexes object, and calls the method a second time
928
+ * for the same Store to return the same object.
929
+ *
930
+ * ```js
931
+ * const store = createStore();
932
+ * const indexes1 = createIndexes(store);
933
+ * const indexes2 = createIndexes(store);
934
+ * console.log(indexes1 === indexes2);
935
+ * // -> true
936
+ * ```
937
+ * @category Creation
938
+ */
939
+ export function createIndexes(store: Store): Indexes;