tinybase 6.1.0-beta.1 → 6.1.0-beta.3

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