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