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