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