tinybase 3.0.1 → 3.0.2

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 (68) hide show
  1. package/lib/cjs/checkpoints.cjs +1 -0
  2. package/lib/cjs/checkpoints.cjs.gz +0 -0
  3. package/lib/cjs/checkpoints.d.ts +959 -0
  4. package/lib/cjs/common.cjs +1 -0
  5. package/lib/cjs/common.cjs.gz +0 -0
  6. package/lib/cjs/common.d.ts +115 -0
  7. package/lib/cjs/indexes.cjs +1 -0
  8. package/lib/cjs/indexes.cjs.gz +0 -0
  9. package/lib/cjs/indexes.d.ts +966 -0
  10. package/lib/cjs/metrics.cjs +1 -0
  11. package/lib/cjs/metrics.cjs.gz +0 -0
  12. package/lib/cjs/metrics.d.ts +827 -0
  13. package/lib/cjs/persisters.cjs +1 -0
  14. package/lib/cjs/persisters.cjs.gz +0 -0
  15. package/lib/cjs/persisters.d.ts +727 -0
  16. package/lib/cjs/queries.cjs +1 -0
  17. package/lib/cjs/queries.cjs.gz +0 -0
  18. package/lib/cjs/queries.d.ts +3024 -0
  19. package/lib/cjs/relationships.cjs +1 -0
  20. package/lib/cjs/relationships.cjs.gz +0 -0
  21. package/lib/cjs/relationships.d.ts +1201 -0
  22. package/lib/cjs/store.cjs +1 -0
  23. package/lib/cjs/store.cjs.gz +0 -0
  24. package/lib/cjs/store.d.ts +5244 -0
  25. package/lib/cjs/tinybase.cjs +1 -0
  26. package/lib/cjs/tinybase.cjs.gz +0 -0
  27. package/lib/cjs/tinybase.d.ts +14 -0
  28. package/lib/cjs/tools.cjs +1 -0
  29. package/lib/cjs/tools.cjs.gz +0 -0
  30. package/lib/cjs/tools.d.ts +512 -0
  31. package/lib/cjs/ui-react.cjs +1 -0
  32. package/lib/cjs/ui-react.cjs.gz +0 -0
  33. package/lib/cjs/ui-react.d.ts +10921 -0
  34. package/lib/cjs-es6/checkpoints.cjs +1 -0
  35. package/lib/cjs-es6/checkpoints.cjs.gz +0 -0
  36. package/lib/cjs-es6/checkpoints.d.ts +959 -0
  37. package/lib/cjs-es6/common.cjs +1 -0
  38. package/lib/cjs-es6/common.cjs.gz +0 -0
  39. package/lib/cjs-es6/common.d.ts +115 -0
  40. package/lib/cjs-es6/indexes.cjs +1 -0
  41. package/lib/cjs-es6/indexes.cjs.gz +0 -0
  42. package/lib/cjs-es6/indexes.d.ts +966 -0
  43. package/lib/cjs-es6/metrics.cjs +1 -0
  44. package/lib/cjs-es6/metrics.cjs.gz +0 -0
  45. package/lib/cjs-es6/metrics.d.ts +827 -0
  46. package/lib/cjs-es6/persisters.cjs +1 -0
  47. package/lib/cjs-es6/persisters.cjs.gz +0 -0
  48. package/lib/cjs-es6/persisters.d.ts +727 -0
  49. package/lib/cjs-es6/queries.cjs +1 -0
  50. package/lib/cjs-es6/queries.cjs.gz +0 -0
  51. package/lib/cjs-es6/queries.d.ts +3024 -0
  52. package/lib/cjs-es6/relationships.cjs +1 -0
  53. package/lib/cjs-es6/relationships.cjs.gz +0 -0
  54. package/lib/cjs-es6/relationships.d.ts +1201 -0
  55. package/lib/cjs-es6/store.cjs +1 -0
  56. package/lib/cjs-es6/store.cjs.gz +0 -0
  57. package/lib/cjs-es6/store.d.ts +5244 -0
  58. package/lib/cjs-es6/tinybase.cjs +1 -0
  59. package/lib/cjs-es6/tinybase.cjs.gz +0 -0
  60. package/lib/cjs-es6/tinybase.d.ts +14 -0
  61. package/lib/cjs-es6/tools.cjs +1 -0
  62. package/lib/cjs-es6/tools.cjs.gz +0 -0
  63. package/lib/cjs-es6/tools.d.ts +512 -0
  64. package/lib/cjs-es6/ui-react.cjs +1 -0
  65. package/lib/cjs-es6/ui-react.cjs.gz +0 -0
  66. package/lib/cjs-es6/ui-react.d.ts +10921 -0
  67. package/package.json +27 -19
  68. package/readme.md +1 -1
@@ -0,0 +1,3024 @@
1
+ /**
2
+ * The queries module of the TinyBase project provides the ability to create and
3
+ * track queries of the data in Store objects.
4
+ *
5
+ * The main entry point to using the queries module is the createQueries
6
+ * function, which returns a new Queries object. That object in turn has methods
7
+ * that let you create new query definitions, access their results directly, and
8
+ * register listeners for when those results change.
9
+ *
10
+ * @packageDocumentation
11
+ * @module queries
12
+ * @since v2.0.0
13
+ */
14
+
15
+ import {
16
+ Cell,
17
+ CellCallback,
18
+ CellOrUndefined,
19
+ GetCell,
20
+ GetCellChange,
21
+ Row,
22
+ RowCallback,
23
+ Store,
24
+ Table,
25
+ TableCallback,
26
+ } from './store.d';
27
+ import {Id, IdOrNull, Ids} from './common.d';
28
+
29
+ /**
30
+ * The Aggregate type describes a custom function that takes an array of Cell
31
+ * values and returns an aggregate of them.
32
+ *
33
+ * There are a number of common predefined aggregators, such as for counting,
34
+ * summing, and averaging values. This type is instead used for when you wish to
35
+ * use a more complex aggregation of your own devising.
36
+ *
37
+ * @param cells The array of Cell values to be aggregated.
38
+ * @param length The length of the array of Cell values to be aggregated.
39
+ * @returns The value of the aggregation.
40
+ * @category Aggregators
41
+ * @since v2.0.0
42
+ */
43
+ export type Aggregate = (cells: Cell[], length: number) => Cell;
44
+
45
+ /**
46
+ * The AggregateAdd type describes a function that can be used to optimize a
47
+ * custom Aggregate by providing a shortcut for when a single value is added to
48
+ * the input values.
49
+ *
50
+ * Some aggregation functions do not need to recalculate the aggregation of the
51
+ * whole set when one value changes. For example, when adding a new number to a
52
+ * series, the new sum of the series is the new value added to the previous sum.
53
+ *
54
+ * If it is not possible to shortcut the aggregation based on just one value
55
+ * being added, return `undefined` and the aggregation will be completely
56
+ * recalculated.
57
+ *
58
+ * Where possible, if you are providing a custom Aggregate, seek an
59
+ * implementation of an AggregateAdd function that can reduce the complexity
60
+ * cost of growing the input data set.
61
+ *
62
+ * @param current The current value of the aggregation.
63
+ * @param add The Cell value being added to the aggregation.
64
+ * @param length The length of the array of Cell values in the aggregation.
65
+ * @returns The new value of the aggregation.
66
+ * @category Aggregators
67
+ * @since v2.0.0
68
+ */
69
+ export type AggregateAdd = (
70
+ current: Cell,
71
+ add: Cell,
72
+ length: number,
73
+ ) => Cell | undefined;
74
+
75
+ /**
76
+ * The AggregateRemove type describes a function that can be used to optimize a
77
+ * custom Aggregate by providing a shortcut for when a single value is removed
78
+ * from the input values.
79
+ *
80
+ * Some aggregation functions do not need to recalculate the aggregation of the
81
+ * whole set when one value changes. For example, when removing a number from a
82
+ * series, the new sum of the series is the new value subtracted from the
83
+ * previous sum.
84
+ *
85
+ * If it is not possible to shortcut the aggregation based on just one value
86
+ * being removed, return `undefined` and the aggregation will be completely
87
+ * recalculated. One example might be if you were taking the minimum of the
88
+ * values, and the previous minimum is being removed. The whole of the rest of
89
+ * the list will need to be re-scanned to find a new minimum.
90
+ *
91
+ * Where possible, if you are providing a custom Aggregate, seek an
92
+ * implementation of an AggregateRemove function that can reduce the complexity
93
+ * cost of shrinking the input data set.
94
+ *
95
+ * @param current The current value of the aggregation.
96
+ * @param remove The Cell value being removed from the aggregation.
97
+ * @param length The length of the array of Cell values in the aggregation.
98
+ * @returns The new value of the aggregation.
99
+ * @category Aggregators
100
+ * @since v2.0.0
101
+ */
102
+ export type AggregateRemove = (
103
+ current: Cell,
104
+ remove: Cell,
105
+ length: number,
106
+ ) => Cell | undefined;
107
+
108
+ /**
109
+ * The AggregateReplace type describes a function that can be used to optimize a
110
+ * custom Aggregate by providing a shortcut for when a single value in the input
111
+ * values is replaced with another.
112
+ *
113
+ * Some aggregation functions do not need to recalculate the aggregation of the
114
+ * whole set when one value changes. For example, when replacing a number in a
115
+ * series, the new sum of the series is the previous sum, plus the new value,
116
+ * minus the old value.
117
+ *
118
+ * If it is not possible to shortcut the aggregation based on just one value
119
+ * changing, return `undefined` and the aggregation will be completely
120
+ * recalculated.
121
+ *
122
+ * Where possible, if you are providing a custom Aggregate, seek an
123
+ * implementation of an AggregateReplace function that can reduce the complexity
124
+ * cost of changing the input data set in place.
125
+ *
126
+ * @param current The current value of the aggregation.
127
+ * @param add The Cell value being added to the aggregation.
128
+ * @param remove The Cell value being removed from the aggregation.
129
+ * @param length The length of the array of Cell values in the aggregation.
130
+ * @returns The new value of the aggregation.
131
+ * @category Aggregators
132
+ * @since v2.0.0
133
+ */
134
+ export type AggregateReplace = (
135
+ current: Cell,
136
+ add: Cell,
137
+ remove: Cell,
138
+ length: number,
139
+ ) => Cell | undefined;
140
+
141
+ /**
142
+ * The QueryCallback type describes a function that takes a query's Id.
143
+ *
144
+ * A QueryCallback is provided when using the forEachQuery method, so that you
145
+ * can do something based on every query in the Queries object. See that method
146
+ * for specific examples.
147
+ *
148
+ * @param queryId The Id of the query that the callback can operate on.
149
+ * @category Callback
150
+ * @since v2.0.0
151
+ */
152
+ export type QueryCallback = (queryId: Id) => void;
153
+
154
+ /**
155
+ * The ResultTableListener type describes a function that is used to listen to
156
+ * changes to a query's result Table.
157
+ *
158
+ * A ResultTableListener is provided when using the addResultTableListener
159
+ * method. See that method for specific examples.
160
+ *
161
+ * When called, a ResultTableListener is given a reference to the Queries
162
+ * object, the Id of the Table that changed (which is the same as the query Id),
163
+ * and a GetCellChange function that can be used to query Cell values before and
164
+ * after the change.
165
+ *
166
+ * @param queries A reference to the Queries object that changed.
167
+ * @param tableId The Id of the Table that changed, which is also the query Id.
168
+ * @param getCellChange A function that returns information about any Cell's
169
+ * changes.
170
+ * @category Listener
171
+ * @since v2.0.0
172
+ */
173
+ export type ResultTableListener = (
174
+ queries: Queries,
175
+ tableId: Id,
176
+ getCellChange: GetCellChange | undefined,
177
+ ) => void;
178
+
179
+ /**
180
+ * The ResultRowIdsListener type describes a function that is used to listen to
181
+ * changes to the Row Ids in a query's result Table.
182
+ *
183
+ * A ResultRowIdsListener is provided when using the addResultRowIdsListener
184
+ * method. See that method for specific examples.
185
+ *
186
+ * When called, a ResultRowIdsListener is given a reference to the Queries
187
+ * object, and the Id of the Table whose Row Ids changed (which is the same as
188
+ * the query Id).
189
+ *
190
+ * @param queries A reference to the Queries object that changed.
191
+ * @param tableId The Id of the Table that changed, which is also the query Id.
192
+ * @category Listener
193
+ * @since v2.0.0
194
+ */
195
+ export type ResultRowIdsListener = (queries: Queries, tableId: Id) => void;
196
+
197
+ /**
198
+ * The ResultSortedRowIdsListener type describes a function that is used to
199
+ * listen to changes to the sorted Row Ids in a query's result Table.
200
+ *
201
+ * A ResultSortedRowIdsListener is provided when using the
202
+ * addResultSortedRowIdsListener method. See that method for specific examples.
203
+ *
204
+ * When called, a ResultSortedRowIdsListener is given a reference to the Queries
205
+ * object, the Id of the Table whose Row Ids changed (which is the same as the
206
+ * query Id), the Cell Id being used to sort them, whether descending or not,
207
+ * and the offset and limit of the number of Ids returned, for pagination
208
+ * purposes. It also receives the sorted array of Ids itself, so that you can
209
+ * use them in the listener without the additional cost of an explicit call to
210
+ * getResultSortedRowIds.
211
+ *
212
+ * @param queries A reference to the Queries object that changed.
213
+ * @param tableId The Id of the Table that changed, which is also the query Id.
214
+ * @param cellId The Id of the Cell whose values were used for the sorting.
215
+ * @param descending Whether the sorting was in descending order.
216
+ * @param offset The number of Row Ids skipped.
217
+ * @param limit The maximum number of Row Ids returned.
218
+ * @param sortedRowIds The sorted Row Ids themselves.
219
+ * @category Listener
220
+ * @since v2.0.0
221
+ */
222
+ export type ResultSortedRowIdsListener = (
223
+ queries: Queries,
224
+ tableId: Id,
225
+ cellId: Id | undefined,
226
+ descending: boolean,
227
+ offset: number,
228
+ limit: number | undefined,
229
+ sortedRowIds: Ids,
230
+ ) => void;
231
+
232
+ /**
233
+ * The ResultRowListener type describes a function that is used to listen to
234
+ * changes to a Row in a query's result Table.
235
+ *
236
+ * A ResultRowListener is provided when using the addResultRowListener method.
237
+ * See that method for specific examples.
238
+ *
239
+ * When called, a ResultRowListener is given a reference to the Queries object,
240
+ * the Id of the Table that changed (which is the same as the query Id), the Id
241
+ * of the Row that changed, and a GetCellChange function that can be used to
242
+ * query Cell values before and after the change.
243
+ *
244
+ * @param queries A reference to the Queries object that changed.
245
+ * @param tableId The Id of the Table that changed, which is also the query Id.
246
+ * @param rowId The Id of the Row that changed.
247
+ * @param getCellChange A function that returns information about any Cell's
248
+ * changes.
249
+ * @category Listener
250
+ * @since v2.0.0
251
+ */
252
+ export type ResultRowListener = (
253
+ queries: Queries,
254
+ tableId: Id,
255
+ rowId: Id,
256
+ getCellChange: GetCellChange | undefined,
257
+ ) => void;
258
+
259
+ /**
260
+ * The ResultCellIdsListener type describes a function that is used to listen to
261
+ * changes to the Cell Ids in a Row in a query's result Table.
262
+ *
263
+ * A ResultCellIdsListener is provided when using the addResultCellIdsListener
264
+ * method. See that method for specific examples.
265
+ *
266
+ * When called, a ResultCellIdsListener is given a reference to the Queries
267
+ * object, the Id of the Table that changed (which is the same as the query Id),
268
+ * and the Id of the Row whose Cell Ids changed.
269
+ *
270
+ * @param queries A reference to the Queries object that changed.
271
+ * @param tableId The Id of the Table that changed, which is also the query Id.
272
+ * @param rowId The Id of the Row that changed.
273
+ * @category Listener
274
+ * @since v2.0.0
275
+ */
276
+ export type ResultCellIdsListener = (
277
+ queries: Queries,
278
+ tableId: Id,
279
+ rowId: Id,
280
+ ) => void;
281
+
282
+ /**
283
+ * The ResultCellListener type describes a function that is used to listen to
284
+ * changes to a Cell in a query's result Table.
285
+ *
286
+ * A ResultCellListener is provided when using the addResultCellListener method.
287
+ * See that method for specific examples.
288
+ *
289
+ * When called, a ResultCellListener is given a reference to the Queries object,
290
+ * the Id of the Table that changed (which is the same as the query Id), the Id
291
+ * of the Row that changed, and the Id of Cell that changed. It is also given
292
+ * the new value of the Cell, the old value of the Cell, and a GetCellChange
293
+ * function that can be used to query Cell values before and after the change.
294
+ *
295
+ * @param queries A reference to the Queries object that changed.
296
+ * @param tableId The Id of the Table that changed, which is also the query Id.
297
+ * @param rowId The Id of the Row that changed.
298
+ * @param cellId The Id of the Cell that changed.
299
+ * @param newCell The new value of the Cell that changed.
300
+ * @param oldCell The old value of the Cell that changed.
301
+ * @param getCellChange A function that returns information about any Cell's
302
+ * changes.
303
+ * @category Listener
304
+ * @since v2.0.0
305
+ */
306
+ export type ResultCellListener = (
307
+ queries: Queries,
308
+ tableId: Id,
309
+ rowId: Id,
310
+ cellId: Id,
311
+ newCell: Cell,
312
+ oldCell: Cell,
313
+ getCellChange: GetCellChange | undefined,
314
+ ) => void;
315
+
316
+ /**
317
+ * The QueriesListenerStats type describes the number of listeners registered
318
+ * with the Queries object, and can be used for debugging purposes.
319
+ *
320
+ * A QueriesListenerStats object is returned from the getListenerStats method,
321
+ * and is only populated in a debug build.
322
+ *
323
+ * @category Development
324
+ * @since v2.0.0
325
+ */
326
+ export type QueriesListenerStats = {
327
+ /**
328
+ * The number of ResultTableListener functions registered with the Store.
329
+ */
330
+ table?: number;
331
+ /**
332
+ * The number of ResultRowIdsListener functions registered with the Store.
333
+ */
334
+ rowIds?: number;
335
+ /**
336
+ * The number of ResultRowListener functions registered with the Store.
337
+ */
338
+ row?: number;
339
+ /**
340
+ * The number of ResultCellIdsListener functions registered with the Store.
341
+ */
342
+ cellIds?: number;
343
+ /**
344
+ * The number of ResultCellListener functions registered with the Store.
345
+ */
346
+ cell?: number;
347
+ };
348
+
349
+ /**
350
+ * The GetTableCell type describes a function that takes a Id and returns the
351
+ * Cell value for a particular Row, optionally in a joined Table.
352
+ *
353
+ * A GetTableCell can be provided when setting query definitions, specifically
354
+ * in the Select and Where clauses when you want to create or filter on
355
+ * calculated values. See those methods for specific examples.
356
+ *
357
+ * @category Callback
358
+ * @since v2.0.0
359
+ */
360
+ export type GetTableCell = {
361
+ /**
362
+ * When called with one parameter, this function will return the value of the
363
+ * specified Cell from the query's main Table for the Row being selected or
364
+ * filtered.
365
+ *
366
+ * @param cellId The Id of the Cell to fetch the value for.
367
+ * @returns A Cell value or `undefined`.
368
+ */
369
+ (cellId: Id): CellOrUndefined;
370
+ /**
371
+ * When called with two parameters, this function will return the value of the
372
+ * specified Cell from a Table that has been joined in the query, for the Row
373
+ * being selected or filtered.
374
+ *
375
+ * @param joinedTableId The Id of the Table to fetch the value from. If the
376
+ * underlying Table was joined 'as' a different Id, that should instead be
377
+ * used.
378
+ * @param joinedCellId The Id of the Cell to fetch the value for.
379
+ * @returns A Cell value or `undefined`.
380
+ */
381
+ (joinedTableId: Id, joinedCellId: Id): CellOrUndefined;
382
+ };
383
+
384
+ /**
385
+ * The Select type describes a function that lets you specify a Cell or
386
+ * calculated value for including into the query's result.
387
+ *
388
+ * The Select function is provided to the third `query` parameter of the
389
+ * setQueryDefinition method. A query definition must call the Select function
390
+ * at least once, otherwise it will be meaningless and return no data.
391
+ *
392
+ * @example
393
+ * This example shows a query that selects two Cells from the main query Table.
394
+ *
395
+ * ```js
396
+ * const store = createStore().setTable('pets', {
397
+ * fido: {species: 'dog', color: 'brown', legs: 4},
398
+ * felix: {species: 'cat', color: 'black', legs: 4},
399
+ * cujo: {species: 'dog', color: 'black', legs: 4},
400
+ * });
401
+ *
402
+ * const queries = createQueries(store);
403
+ * queries.setQueryDefinition('query', 'pets', ({select}) => {
404
+ * select('species');
405
+ * select('color');
406
+ * });
407
+ *
408
+ * queries.forEachResultRow('query', (rowId) => {
409
+ * console.log({[rowId]: queries.getResultRow('query', rowId)});
410
+ * });
411
+ * // -> {fido: {species: 'dog', color: 'brown'}}
412
+ * // -> {felix: {species: 'cat', color: 'black'}}
413
+ * // -> {cujo: {species: 'dog', color: 'black'}}
414
+ * ```
415
+ * @example
416
+ * This example shows a query that selects two Cells, one from a joined Table.
417
+ *
418
+ * ```js
419
+ * const store = createStore()
420
+ * .setTable('pets', {
421
+ * fido: {species: 'dog', ownerId: '1'},
422
+ * felix: {species: 'cat', ownerId: '2'},
423
+ * cujo: {species: 'dog', ownerId: '3'},
424
+ * })
425
+ * .setTable('owners', {
426
+ * '1': {name: 'Alice'},
427
+ * '2': {name: 'Bob'},
428
+ * '3': {name: 'Carol'},
429
+ * });
430
+ *
431
+ * const queries = createQueries(store);
432
+ * queries.setQueryDefinition('query', 'pets', ({select, join}) => {
433
+ * select('species');
434
+ * select('owners', 'name');
435
+ * // from pets
436
+ * join('owners', 'ownerId');
437
+ * });
438
+ *
439
+ * queries.forEachResultRow('query', (rowId) => {
440
+ * console.log({[rowId]: queries.getResultRow('query', rowId)});
441
+ * });
442
+ * // -> {fido: {species: 'dog', name: 'Alice'}}
443
+ * // -> {felix: {species: 'cat', name: 'Bob'}}
444
+ * // -> {cujo: {species: 'dog', name: 'Carol'}}
445
+ * ```
446
+ * @example
447
+ * This example shows a query that calculates a value from two underlying Cells.
448
+ *
449
+ * ```js
450
+ * const store = createStore()
451
+ * .setTable('pets', {
452
+ * fido: {species: 'dog', ownerId: '1'},
453
+ * felix: {species: 'cat', ownerId: '2'},
454
+ * cujo: {species: 'dog', ownerId: '3'},
455
+ * })
456
+ * .setTable('owners', {
457
+ * '1': {name: 'Alice'},
458
+ * '2': {name: 'Bob'},
459
+ * '3': {name: 'Carol'},
460
+ * });
461
+ *
462
+ * const queries = createQueries(store);
463
+ * queries.setQueryDefinition('query', 'pets', ({select, join}) => {
464
+ * select(
465
+ * (getTableCell, rowId) =>
466
+ * `${getTableCell('species')} for ${getTableCell('owners', 'name')}`,
467
+ * ).as('description');
468
+ * join('owners', 'ownerId');
469
+ * });
470
+ *
471
+ * queries.forEachResultRow('query', (rowId) => {
472
+ * console.log({[rowId]: queries.getResultRow('query', rowId)});
473
+ * });
474
+ * // -> {fido: {description: 'dog for Alice'}}
475
+ * // -> {felix: {description: 'cat for Bob'}}
476
+ * // -> {cujo: {description: 'dog for Carol'}}
477
+ * ```
478
+ * @category Definition
479
+ * @since v2.0.0
480
+ */
481
+ export type Select = {
482
+ /**
483
+ * Calling this function with one Id parameter will indicate that the query
484
+ * should select the value of the specified Cell from the query's main Table.
485
+ *
486
+ * @param cellId The Id of the Cell to fetch the value for.
487
+ * @returns A SelectedAs object so that the selected Cell Id can be optionally
488
+ * aliased.
489
+ */
490
+ (cellId: Id): SelectedAs;
491
+ /**
492
+ * Calling this function with two parameters will indicate that the query
493
+ * should select the value of the specified Cell from a Table that has been
494
+ * joined in the query.
495
+ *
496
+ * @param joinedTableId The Id of the Table to fetch the value from. If the
497
+ * underlying Table was joined 'as' a different Id, that should instead be
498
+ * used.
499
+ * @param joinedCellId The Id of the Cell to fetch the value for.
500
+ * @returns A SelectedAs object so that the selected Cell Id can be optionally
501
+ * aliased.
502
+ */
503
+ (joinedTableId: Id, joinedCellId: Id): SelectedAs;
504
+ /**
505
+ * Calling this function with one callback parameter will indicate that the
506
+ * query should select a calculated value, based on one or more Cell values in
507
+ * the main Table or a joined Table, or on the main Table's Row Id.
508
+ *
509
+ * @param getCell A callback that takes a GetTableCell function and the main
510
+ * Table's Row Id. These can be used to programmatically create a calculated
511
+ * value from multiple Cell values and the Row Id.
512
+ * @returns A SelectedAs object so that the selected Cell Id can be optionally
513
+ * aliased.
514
+ */
515
+ (
516
+ getCell: (getTableCell: GetTableCell, rowId: Id) => CellOrUndefined,
517
+ ): SelectedAs;
518
+ };
519
+ /**
520
+ * The SelectedAs type describes an object returned from calling a Select
521
+ * function so that the selected Cell Id can be optionally aliased.
522
+ *
523
+ * If you are using a callback in the Select cause, it is highly recommended to
524
+ * use the 'as' function, since otherwise a machine-generated column name will
525
+ * be used.
526
+ *
527
+ * Note that if two Select clauses are both aliased to the same name (or if two
528
+ * columns with the same underlying name are selected, both _without_ aliases),
529
+ * only the latter of two will be used in the query.
530
+ *
531
+ * @example
532
+ * This example shows a query that selects two Cells, one from a joined Table.
533
+ * Both are aliased with the 'as' function:
534
+ *
535
+ * ```js
536
+ * const store = createStore()
537
+ * .setTable('pets', {
538
+ * fido: {species: 'dog', ownerId: '1'},
539
+ * felix: {species: 'cat', ownerId: '2'},
540
+ * cujo: {species: 'dog', ownerId: '3'},
541
+ * })
542
+ * .setTable('owners', {
543
+ * '1': {name: 'Alice'},
544
+ * '2': {name: 'Bob'},
545
+ * '3': {name: 'Carol'},
546
+ * });
547
+ *
548
+ * const queries = createQueries(store);
549
+ * queries.setQueryDefinition('query', 'pets', ({select, join}) => {
550
+ * select('species').as('petSpecies');
551
+ * select('owners', 'name').as('ownerName');
552
+ * // from pets
553
+ * join('owners', 'ownerId');
554
+ * });
555
+ *
556
+ * queries.forEachResultRow('query', (rowId) => {
557
+ * console.log({[rowId]: queries.getResultRow('query', rowId)});
558
+ * });
559
+ * // -> {fido: {petSpecies: 'dog', ownerName: 'Alice'}}
560
+ * // -> {felix: {petSpecies: 'cat', ownerName: 'Bob'}}
561
+ * // -> {cujo: {petSpecies: 'dog', ownerName: 'Carol'}}
562
+ * ```
563
+ * @category Definition
564
+ * @since v2.0.0
565
+ */
566
+ export type SelectedAs = {
567
+ /**
568
+ * A function that lets you specify an alias for the Cell Id.
569
+ */
570
+ as: (selectedCellId: Id) => void;
571
+ };
572
+
573
+ /**
574
+ * The Join type describes a function that lets you specify a Cell or calculated
575
+ * value to join the main query Table to other Tables, by their Row Id.
576
+ *
577
+ * The Join function is provided to the third `query` parameter of the
578
+ * setQueryDefinition method.
579
+ *
580
+ * You can join zero, one, or many Tables. You can join the same underlying
581
+ * Table multiple times, but in that case you will need to use the 'as' function
582
+ * to distinguish them from each other.
583
+ *
584
+ * By default, each join is made from the main query Table to the joined table,
585
+ * but it is also possible to connect via an intermediate join Table to a more
586
+ * distant join Table.
587
+ *
588
+ * Because a Join clause is used to identify which unique Row Id of the joined
589
+ * Table will be joined to each Row of the main Table, queries follow the 'left
590
+ * join' semantics you may be familiar with from SQL. This means that an
591
+ * unfiltered query will only ever return the same number of Rows as the main
592
+ * Table being queried, and indeed the resulting table (assuming it has not been
593
+ * aggregated) will even preserve the main Table's original Row Ids.
594
+ *
595
+ * @example
596
+ * This example shows a query that joins a single Table by using an Id present
597
+ * in the main query Table.
598
+ *
599
+ * ```js
600
+ * const store = createStore()
601
+ * .setTable('pets', {
602
+ * fido: {species: 'dog', ownerId: '1'},
603
+ * felix: {species: 'cat', ownerId: '2'},
604
+ * cujo: {species: 'dog', ownerId: '3'},
605
+ * })
606
+ * .setTable('owners', {
607
+ * '1': {name: 'Alice'},
608
+ * '2': {name: 'Bob'},
609
+ * '3': {name: 'Carol'},
610
+ * });
611
+ *
612
+ * const queries = createQueries(store);
613
+ * queries.setQueryDefinition('query', 'pets', ({select, join}) => {
614
+ * select('species');
615
+ * select('owners', 'name');
616
+ * // from pets
617
+ * join('owners', 'ownerId');
618
+ * });
619
+ *
620
+ * queries.forEachResultRow('query', (rowId) => {
621
+ * console.log({[rowId]: queries.getResultRow('query', rowId)});
622
+ * });
623
+ * // -> {fido: {species: 'dog', name: 'Alice'}}
624
+ * // -> {felix: {species: 'cat', name: 'Bob'}}
625
+ * // -> {cujo: {species: 'dog', name: 'Carol'}}
626
+ * ```
627
+ * @example
628
+ * This example shows a query that joins the same underlying Table twice, and
629
+ * aliases them (and the selected Cell Ids). Note the left-join semantics: Felix
630
+ * the cat was bought, but the seller was unknown. The record still exists in
631
+ * the result Table.
632
+ *
633
+ * ```js
634
+ * const store = createStore()
635
+ * .setTable('pets', {
636
+ * fido: {species: 'dog', buyerId: '1', sellerId: '2'},
637
+ * felix: {species: 'cat', buyerId: '2'},
638
+ * cujo: {species: 'dog', buyerId: '3', sellerId: '1'},
639
+ * })
640
+ * .setTable('humans', {
641
+ * '1': {name: 'Alice'},
642
+ * '2': {name: 'Bob'},
643
+ * '3': {name: 'Carol'},
644
+ * });
645
+ *
646
+ * const queries = createQueries(store);
647
+ * queries.setQueryDefinition('query', 'pets', ({select, join}) => {
648
+ * select('buyers', 'name').as('buyer');
649
+ * select('sellers', 'name').as('seller');
650
+ * // from pets
651
+ * join('humans', 'buyerId').as('buyers');
652
+ * join('humans', 'sellerId').as('sellers');
653
+ * });
654
+ *
655
+ * queries.forEachResultRow('query', (rowId) => {
656
+ * console.log({[rowId]: queries.getResultRow('query', rowId)});
657
+ * });
658
+ * // -> {fido: {buyer: 'Alice', seller: 'Bob'}}
659
+ * // -> {felix: {buyer: 'Bob'}}
660
+ * // -> {cujo: {buyer: 'Carol', seller: 'Alice'}}
661
+ * ```
662
+ * @example
663
+ * This example shows a query that calculates the Id of the joined Table based
664
+ * from multiple values in the main Table rather than a single Cell.
665
+ *
666
+ * ```js
667
+ * const store = createStore()
668
+ * .setTable('pets', {
669
+ * fido: {species: 'dog', color: 'brown'},
670
+ * felix: {species: 'cat', color: 'black'},
671
+ * cujo: {species: 'dog', color: 'black'},
672
+ * })
673
+ * .setTable('colorSpecies', {
674
+ * 'brown-dog': {price: 6},
675
+ * 'black-dog': {price: 5},
676
+ * 'brown-cat': {price: 4},
677
+ * 'black-cat': {price: 3},
678
+ * });
679
+ *
680
+ * const queries = createQueries(store);
681
+ * queries.setQueryDefinition('query', 'pets', ({select, join}) => {
682
+ * select('colorSpecies', 'price');
683
+ * // from pets
684
+ * join(
685
+ * 'colorSpecies',
686
+ * (getCell) => `${getCell('color')}-${getCell('species')}`,
687
+ * );
688
+ * });
689
+ *
690
+ * queries.forEachResultRow('query', (rowId) => {
691
+ * console.log({[rowId]: queries.getResultRow('query', rowId)});
692
+ * });
693
+ * // -> {fido: {price: 6}}
694
+ * // -> {felix: {price: 3}}
695
+ * // -> {cujo: {price: 5}}
696
+ * ```
697
+ * @example
698
+ * This example shows a query that joins two Tables, one through the
699
+ * intermediate other.
700
+ *
701
+ * ```js
702
+ * const store = createStore()
703
+ * .setTable('pets', {
704
+ * fido: {species: 'dog', ownerId: '1'},
705
+ * felix: {species: 'cat', ownerId: '2'},
706
+ * cujo: {species: 'dog', ownerId: '3'},
707
+ * })
708
+ * .setTable('owners', {
709
+ * '1': {name: 'Alice', state: 'CA'},
710
+ * '2': {name: 'Bob', state: 'CA'},
711
+ * '3': {name: 'Carol', state: 'WA'},
712
+ * })
713
+ * .setTable('states', {
714
+ * CA: {name: 'California'},
715
+ * WA: {name: 'Washington'},
716
+ * });
717
+ *
718
+ * const queries = createQueries(store);
719
+ * queries.setQueryDefinition('query', 'pets', ({select, join}) => {
720
+ * select(
721
+ * (getTableCell, rowId) =>
722
+ * `${getTableCell('species')} in ${getTableCell('states', 'name')}`,
723
+ * ).as('description');
724
+ * // from pets
725
+ * join('owners', 'ownerId');
726
+ * join('states', 'owners', 'state');
727
+ * });
728
+ *
729
+ * queries.forEachResultRow('query', (rowId) => {
730
+ * console.log({[rowId]: queries.getResultRow('query', rowId)});
731
+ * });
732
+ * // -> {fido: {description: 'dog in California'}}
733
+ * // -> {felix: {description: 'cat in California'}}
734
+ * // -> {cujo: {description: 'dog in Washington'}}
735
+ * ```
736
+ * @category Definition
737
+ * @since v2.0.0
738
+ */
739
+ export type Join = {
740
+ /**
741
+ * Calling this function with two Id parameters will indicate that the join to
742
+ * a Row in an adjacent Table is made by finding its Id in a Cell of the
743
+ * query's main Table.
744
+ *
745
+ * @param joinedTableId The Id of the Table to join to.
746
+ * @param on The Id of the Cell in the main Table that contains the joined
747
+ * Table's Row Id.
748
+ * @returns A JoinedAs object so that the joined Table Id can be optionally
749
+ * aliased.
750
+ */
751
+ (joinedTableId: Id, on: Id): JoinedAs;
752
+ /**
753
+ * Calling this function with two parameters (where the second is a function)
754
+ * will indicate that the join to a Row in an adjacent Table is made by
755
+ * calculating its Id from the Cells and the Row Id of the query's main Table.
756
+ *
757
+ * @param joinedTableId The Id of the Table to join to.
758
+ * @param on A callback that takes a GetCell function and the main Table's Row
759
+ * Id. These can be used to programmatically calculate the joined Table's Row
760
+ * Id.
761
+ * @returns A JoinedAs object so that the joined Table Id can be optionally
762
+ * aliased.
763
+ */
764
+ (
765
+ joinedTableId: Id,
766
+ on: (getCell: GetCell, rowId: Id) => Id | undefined,
767
+ ): JoinedAs;
768
+ /**
769
+ * Calling this function with three Id parameters will indicate that the join
770
+ * to a Row in distant Table is made by finding its Id in a Cell of an
771
+ * intermediately joined Table.
772
+ *
773
+ * @param joinedTableId The Id of the distant Table to join to.
774
+ * @param fromIntermediateJoinedTableId The Id of an intermediate Table (which
775
+ * should have been in turn joined to the main query table via other Join
776
+ * clauses).
777
+ * @param on The Id of the Cell in the intermediate Table that contains the
778
+ * joined Table's Row Id.
779
+ * @returns A JoinedAs object so that the joined Table Id can be optionally
780
+ * aliased.
781
+ */
782
+ (joinedTableId: Id, fromIntermediateJoinedTableId: Id, on: Id): JoinedAs;
783
+ /**
784
+ * Calling this function with three parameters (where the third is a function)
785
+ * will indicate that the join to a Row in distant Table is made by
786
+ * calculating its Id from the Cells and the Row Id of an intermediately
787
+ * joined Table.
788
+ *
789
+ * @param joinedTableId The Id of the Table to join to.
790
+ * @param fromIntermediateJoinedTableId The Id of an intermediate Table (which
791
+ * should have been in turn joined to the main query table via other Join
792
+ * clauses).
793
+ * @param on A callback that takes a GetCell function and the intermediate
794
+ * Table's Row Id. These can be used to programmatically calculate the joined
795
+ * Table's Row Id.
796
+ * @returns A JoinedAs object so that the joined Table Id can be optionally
797
+ * aliased.
798
+ */
799
+ (
800
+ joinedTableId: Id,
801
+ fromIntermediateJoinedTableId: Id,
802
+ on: (
803
+ getIntermediateJoinedCell: GetCell,
804
+ intermediateJoinedTableRowId: Id,
805
+ ) => Id | undefined,
806
+ ): JoinedAs;
807
+ };
808
+ /**
809
+ * The JoinedAs type describes an object returned from calling a Join function
810
+ * so that the joined Table Id can be optionally aliased.
811
+ *
812
+ * Note that if two Join clauses are both aliased to the same name (or if you
813
+ * create two joins to the same underlying Table, both _without_ aliases), only
814
+ * the latter of two will be used in the query.
815
+ *
816
+ * @example
817
+ * This example shows a query that joins the same underlying Table twice, for
818
+ * different purposes. Both joins are aliased with the 'as' function to
819
+ * disambiguate them. Note that the selected Cells are also aliased.
820
+ *
821
+ * ```js
822
+ * const store = createStore()
823
+ * .setTable('pets', {
824
+ * fido: {species: 'dog', buyerId: '1', sellerId: '2'},
825
+ * felix: {species: 'cat', buyerId: '2'},
826
+ * cujo: {species: 'dog', buyerId: '3', sellerId: '1'},
827
+ * })
828
+ * .setTable('humans', {
829
+ * '1': {name: 'Alice'},
830
+ * '2': {name: 'Bob'},
831
+ * '3': {name: 'Carol'},
832
+ * });
833
+ *
834
+ * const queries = createQueries(store);
835
+ * queries.setQueryDefinition('query', 'pets', ({select, join}) => {
836
+ * select('buyers', 'name').as('buyer');
837
+ * select('sellers', 'name').as('seller');
838
+ * // from pets
839
+ * join('humans', 'buyerId').as('buyers');
840
+ * join('humans', 'sellerId').as('sellers');
841
+ * });
842
+ *
843
+ * queries.forEachResultRow('query', (rowId) => {
844
+ * console.log({[rowId]: queries.getResultRow('query', rowId)});
845
+ * });
846
+ * // -> {fido: {buyer: 'Alice', seller: 'Bob'}}
847
+ * // -> {felix: {buyer: 'Bob'}}
848
+ * // -> {cujo: {buyer: 'Carol', seller: 'Alice'}}
849
+ * ```
850
+ * @category Definition
851
+ * @since v2.0.0
852
+ */
853
+ export type JoinedAs = {as: (joinedTableId: Id) => void};
854
+
855
+ /**
856
+ * The Where type describes a function that lets you specify conditions to
857
+ * filter results, based on the underlying Cells of the main or joined Tables.
858
+ *
859
+ * The Where function is provided to the third `query` parameter of the
860
+ * setQueryDefinition method.
861
+ *
862
+ * If you do not specify a Where clause, you should expect every non-empty Row
863
+ * of the main Table to appear in the query's results.
864
+ *
865
+ * A Where condition has to be true for a Row to be included in the results.
866
+ * Each Where class is additive, as though combined with a logical 'and'. If you
867
+ * wish to create an 'or' expression, use the single parameter version of the
868
+ * type that allows arbitrary programmatic conditions.
869
+ *
870
+ * The Where keyword differs from the Having keyword in that the former
871
+ * describes conditions that should be met by underlying Cell values (whether
872
+ * selected or not), and the latter describes conditions based on calculated and
873
+ * aggregated values - after Group clauses have been applied.
874
+ *
875
+ * @example
876
+ * This example shows a query that filters the results from a single Table by
877
+ * comparing an underlying Cell from it with a value.
878
+ *
879
+ * ```js
880
+ * const store = createStore().setTable('pets', {
881
+ * fido: {species: 'dog'},
882
+ * felix: {species: 'cat'},
883
+ * cujo: {species: 'dog'},
884
+ * });
885
+ *
886
+ * const queries = createQueries(store);
887
+ * queries.setQueryDefinition('query', 'pets', ({select, where}) => {
888
+ * select('species');
889
+ * where('species', 'dog');
890
+ * });
891
+ *
892
+ * queries.forEachResultRow('query', (rowId) => {
893
+ * console.log({[rowId]: queries.getResultRow('query', rowId)});
894
+ * });
895
+ * // -> {fido: {species: 'dog'}}
896
+ * // -> {cujo: {species: 'dog'}}
897
+ * ```
898
+ * @example
899
+ * This example shows a query that filters the results of a query by comparing
900
+ * an underlying Cell from a joined Table with a value. Note that the joined
901
+ * table has also been aliased, and so its alias is used in the Where clause.
902
+ *
903
+ * ```js
904
+ * const store = createStore()
905
+ * .setTable('pets', {
906
+ * fido: {species: 'dog', ownerId: '1'},
907
+ * felix: {species: 'cat', ownerId: '2'},
908
+ * cujo: {species: 'dog', ownerId: '3'},
909
+ * })
910
+ * .setTable('owners', {
911
+ * '1': {name: 'Alice', state: 'CA'},
912
+ * '2': {name: 'Bob', state: 'CA'},
913
+ * '3': {name: 'Carol', state: 'WA'},
914
+ * });
915
+ *
916
+ * const queries = createQueries(store);
917
+ * queries.setQueryDefinition('query', 'pets', ({select, join, where}) => {
918
+ * select('species');
919
+ * // from pets
920
+ * join('owners', 'ownerId').as('petOwners');
921
+ * where('petOwners', 'state', 'CA');
922
+ * });
923
+ *
924
+ * queries.forEachResultRow('query', (rowId) => {
925
+ * console.log({[rowId]: queries.getResultRow('query', rowId)});
926
+ * });
927
+ * // -> {fido: {species: 'dog'}}
928
+ * // -> {felix: {species: 'cat'}}
929
+ * ```
930
+ * @example
931
+ * This example shows a query that filters the results of a query with a
932
+ * condition that is calculated from underlying Cell values from the main and
933
+ * joined Table. Note that the joined table has also been aliased, and so its
934
+ * alias is used in the Where clause.
935
+ *
936
+ * ```js
937
+ * const store = createStore()
938
+ * .setTable('pets', {
939
+ * fido: {species: 'dog', ownerId: '1'},
940
+ * felix: {species: 'cat', ownerId: '2'},
941
+ * cujo: {species: 'dog', ownerId: '3'},
942
+ * })
943
+ * .setTable('owners', {
944
+ * '1': {name: 'Alice', state: 'CA'},
945
+ * '2': {name: 'Bob', state: 'CA'},
946
+ * '3': {name: 'Carol', state: 'WA'},
947
+ * });
948
+ *
949
+ * const queries = createQueries(store);
950
+ * queries.setQueryDefinition('query', 'pets', ({select, join, where}) => {
951
+ * select('species');
952
+ * select('petOwners', 'state');
953
+ * // from pets
954
+ * join('owners', 'ownerId').as('petOwners');
955
+ * where(
956
+ * (getTableCell) =>
957
+ * getTableCell('pets', 'species') === 'cat' ||
958
+ * getTableCell('petOwners', 'state') === 'WA',
959
+ * );
960
+ * });
961
+ *
962
+ * queries.forEachResultRow('query', (rowId) => {
963
+ * console.log({[rowId]: queries.getResultRow('query', rowId)});
964
+ * });
965
+ * // -> {felix: {species: 'cat', state: 'CA'}}
966
+ * // -> {cujo: {species: 'dog', state: 'WA'}}
967
+ * ```
968
+ * @category Definition
969
+ * @since v2.0.0
970
+ */
971
+ export type Where = {
972
+ /**
973
+ * Calling this function with two parameters is used to include only those
974
+ * Rows for which a specified Cell in the query's main Table has a specified
975
+ * value.
976
+ *
977
+ * @param cellId The Id of the Cell in the query's main Table to test.
978
+ * @param equals The value that the Cell has to have for the Row to be
979
+ * included in the result.
980
+ */
981
+ (cellId: Id, equals: Cell): void;
982
+ /**
983
+ * Calling this function with three parameters is used to include only those
984
+ * Rows for which a specified Cell in a joined Table has a specified value.
985
+ *
986
+ * @param joinedTableId The Id of the joined Table to test a value in. If the
987
+ * underlying Table was joined 'as' a different Id, that should instead be
988
+ * used.
989
+ * @param joinedCellId The Id of the Cell in the joined Table to test.
990
+ * @param equals The value that the Cell has to have for the Row to be
991
+ * included in the result.
992
+ */
993
+ (joinedTableId: Id, joinedCellId: Id, equals: Cell): void;
994
+ /**
995
+ * Calling this function with one callback parameter is used to include only
996
+ * those Rows which meet a calculated boolean condition, based on values in
997
+ * the main and (optionally) joined Tables.
998
+ *
999
+ * @param condition A callback that takes a GetTableCell function and that
1000
+ * should return `true` for the Row to be included in the result.
1001
+ */
1002
+ (condition: (getTableCell: GetTableCell) => boolean): void;
1003
+ };
1004
+
1005
+ /**
1006
+ * The Group type describes a function that lets you specify that the values of
1007
+ * a Cell in multiple result Rows should be aggregated together.
1008
+ *
1009
+ * The Group function is provided to the third `query` parameter of the
1010
+ * setQueryDefinition method. When called, it should refer to a Cell Id (or
1011
+ * aliased Id) specified in one of the Select functions, and indicate how the
1012
+ * values should be aggregated.
1013
+ *
1014
+ * This is applied after any joins or where-based filtering.
1015
+ *
1016
+ * If you provide a Group for every Select, the result will be a single Row with
1017
+ * every Cell having been aggregated. If you provide a Group for only one, or
1018
+ * some, of the Select clauses, the _others_ will be automatically used as
1019
+ * dimensional values (analogous to the 'group by` semantics in SQL), within
1020
+ * which the aggregations of Group Cells will be performed.
1021
+ *
1022
+ * You can join the same underlying Cell multiple times, but in that case you
1023
+ * will need to use the 'as' function to distinguish them from each other.
1024
+ *
1025
+ * The second parameter can be one of five predefined aggregates - 'count',
1026
+ * 'sum', 'avg', 'min', and 'max' - or a custom function that produces your own
1027
+ * aggregation of an array of Cell values.
1028
+ *
1029
+ * The final three parameters, `aggregateAdd`, `aggregateRemove`,
1030
+ * `aggregateReplace` need only be provided when you are using your own custom
1031
+ * `aggregate` function. These give you the opportunity to reduce your custom
1032
+ * function's algorithmic complexity by providing shortcuts that can nudge an
1033
+ * aggregation result when a single value is added, removed, or replaced in the
1034
+ * input values.
1035
+ *
1036
+ * @param selectedCellId The Id of the Cell to aggregate. If the underlying Cell
1037
+ * was selected 'as' a different Id, that should instead be used.
1038
+ * @param aggregate Either a string representing one of a set of common
1039
+ * aggregation techniques ('count', 'sum', 'avg', 'min', or 'max'), or a
1040
+ * function that aggregates Cell values from each Row to create the aggregate's
1041
+ * overall.
1042
+ * @param aggregateAdd A function that can be used to optimize a custom
1043
+ * Aggregate by providing a shortcut for when a single value is added to the
1044
+ * input values - for example, when a Row is added to the Table.
1045
+ * @param aggregateRemove A function that can be used to optimize a custom
1046
+ * Aggregate by providing a shortcut for when a single value is removed from the
1047
+ * input values - for example ,when a Row is removed from the Table.
1048
+ * @param aggregateReplace A function that can be used to optimize a custom
1049
+ * Aggregate by providing a shortcut for when a single value in the input values
1050
+ * is replaced with another - for example, when a Row is updated.
1051
+ * @returns A GroupedAs object so that the grouped Cell Id can be optionally
1052
+ * aliased.
1053
+ * @example
1054
+ * This example shows a query that calculates the average of all the values in a
1055
+ * single selected Cell from a joined Table.
1056
+ *
1057
+ * ```js
1058
+ * const store = createStore()
1059
+ * .setTable('pets', {
1060
+ * fido: {species: 'dog'},
1061
+ * felix: {species: 'cat'},
1062
+ * cujo: {species: 'dog'},
1063
+ * lowly: {species: 'worm'},
1064
+ * })
1065
+ * .setTable('species', {
1066
+ * dog: {price: 5},
1067
+ * cat: {price: 4},
1068
+ * worm: {price: 1},
1069
+ * });
1070
+ *
1071
+ * const queries = createQueries(store);
1072
+ * queries.setQueryDefinition('query', 'pets', ({select, join, group}) => {
1073
+ * select('species', 'price');
1074
+ * // from pets
1075
+ * join('species', 'species');
1076
+ * group('price', 'avg').as('avgPrice');
1077
+ * });
1078
+ *
1079
+ * console.log(queries.getResultTable('query'));
1080
+ * // -> {0: {avgPrice: 3.75}}
1081
+ * // 2 dogs at 5, 1 cat at 4, 1 worm at 1: a total of 15 for 4 pets
1082
+ * ```
1083
+ * @example
1084
+ * This example shows a query that calculates the average of a two Cell values,
1085
+ * aggregated by the two other dimensional 'group by' Cells.
1086
+ *
1087
+ * ```js
1088
+ * const store = createStore()
1089
+ * .setTable('pets', {
1090
+ * fido: {species: 'dog', color: 'brown', owner: 'alice'},
1091
+ * felix: {species: 'cat', color: 'black', owner: 'bob'},
1092
+ * cujo: {species: 'dog', color: 'black', owner: 'bob'},
1093
+ * lowly: {species: 'worm', color: 'brown', owner: 'alice'},
1094
+ * carnaby: {species: 'parrot', color: 'black', owner: 'bob'},
1095
+ * polly: {species: 'parrot', color: 'red', owner: 'alice'},
1096
+ * })
1097
+ * .setTable('species', {
1098
+ * dog: {price: 5, legs: 4},
1099
+ * cat: {price: 4, legs: 4},
1100
+ * parrot: {price: 3, legs: 2},
1101
+ * worm: {price: 1, legs: 0},
1102
+ * });
1103
+ *
1104
+ * const queries = createQueries(store);
1105
+ * queries.setQueryDefinition('query', 'pets', ({select, join, group}) => {
1106
+ * select('pets', 'color'); // group by
1107
+ * select('pets', 'owner'); // group by
1108
+ * select('species', 'price'); // grouped
1109
+ * select('species', 'legs'); // grouped
1110
+ * // from pets
1111
+ * join('species', 'species');
1112
+ * group('price', 'avg').as('avgPrice');
1113
+ * group('legs', 'sum').as('sumLegs');
1114
+ * });
1115
+ *
1116
+ * queries.forEachResultRow('query', (rowId) => {
1117
+ * console.log({[rowId]: queries.getResultRow('query', rowId)});
1118
+ * });
1119
+ * // -> {0: {color: 'brown', owner: 'alice', avgPrice: 3, sumLegs: 4}}
1120
+ * // -> {1: {color: 'black', owner: 'bob', avgPrice: 4, sumLegs: 10}}
1121
+ * // -> {2: {color: 'red', owner: 'alice', avgPrice: 3, sumLegs: 2}}
1122
+ * ```
1123
+ * @example
1124
+ * This example shows a query that calculates the a custom aggregate of one
1125
+ * Cell's values, grouped by another. Note how `aggregateAdd`,
1126
+ * `aggregateRemove`, and `aggregateReplace` parameters are provided to make the
1127
+ * custom aggregation more efficient as individual values are added, removed, or
1128
+ * replaced during the lifecycle of the Table.
1129
+ *
1130
+ * ```js
1131
+ * const store = createStore()
1132
+ * .setTable('pets', {
1133
+ * fido: {species: 'dog', owner: 'alice'},
1134
+ * felix: {species: 'cat', owner: 'bob'},
1135
+ * cujo: {species: 'dog', owner: 'bob'},
1136
+ * lowly: {species: 'worm', owner: 'alice'},
1137
+ * carnaby: {species: 'parrot', owner: 'bob'},
1138
+ * polly: {species: 'parrot', owner: 'alice'},
1139
+ * })
1140
+ * .setTable('species', {
1141
+ * dog: {price: 5, legs: 4},
1142
+ * cat: {price: 4, legs: 4},
1143
+ * parrot: {price: 3, legs: 2},
1144
+ * worm: {price: 1, legs: 0},
1145
+ * });
1146
+ *
1147
+ * const queries = createQueries(store);
1148
+ * queries.setQueryDefinition('query', 'pets', ({select, join, group}) => {
1149
+ * select('pets', 'owner'); // group by
1150
+ * select('species', 'price'); // grouped
1151
+ * // from pets
1152
+ * join('species', 'species');
1153
+ * group(
1154
+ * 'price',
1155
+ * (cells) => Math.min(...cells.filter((cell) => cell > 2)),
1156
+ * (current, add) => (add > 2 ? Math.min(current, add) : current),
1157
+ * (current, remove) => (remove == current ? undefined : current),
1158
+ * (current, add, remove) =>
1159
+ * remove == current
1160
+ * ? undefined
1161
+ * : add > 2
1162
+ * ? Math.min(current, add)
1163
+ * : current,
1164
+ * ).as('lowestPriceOver2');
1165
+ * });
1166
+ *
1167
+ * queries.forEachResultRow('query', (rowId) => {
1168
+ * console.log({[rowId]: queries.getResultRow('query', rowId)});
1169
+ * });
1170
+ * // -> {0: {owner: 'alice', lowestPriceOver2: 3}}
1171
+ * // -> {1: {owner: 'bob', lowestPriceOver2: 3}}
1172
+ * // Both have a parrot at 3. Alice's worm at 1 is excluded from aggregation.
1173
+ * ```
1174
+ * @category Definition
1175
+ * @since v2.0.0
1176
+ */
1177
+ export type Group = (
1178
+ selectedCellId: Id,
1179
+ aggregate: 'count' | 'sum' | 'avg' | 'min' | 'max' | Aggregate,
1180
+ aggregateAdd?: AggregateAdd,
1181
+ aggregateRemove?: AggregateRemove,
1182
+ aggregateReplace?: AggregateReplace,
1183
+ ) => GroupedAs;
1184
+ /**
1185
+ * The GroupedAs type describes an object returned from calling a Group function
1186
+ * so that the grouped Cell Id can be optionally aliased.
1187
+ *
1188
+ * Note that if two Group clauses are both aliased to the same name (or if you
1189
+ * create two groups of the same underlying Cell, both _without_ aliases), only
1190
+ * the latter of two will be used in the query.
1191
+ *
1192
+ * @example
1193
+ * This example shows a query that groups the same underlying Cell twice, for
1194
+ * different purposes. Both groups are aliased with the 'as' function to
1195
+ * disambiguate them.
1196
+ *
1197
+ * ```js
1198
+ * const store = createStore().setTable('pets', {
1199
+ * fido: {species: 'dog', price: 5},
1200
+ * felix: {species: 'cat', price: 4},
1201
+ * cujo: {species: 'dog', price: 4},
1202
+ * tom: {species: 'cat', price: 3},
1203
+ * });
1204
+ *
1205
+ * const queries = createQueries(store);
1206
+ * queries.setQueryDefinition('query', 'pets', ({select, group}) => {
1207
+ * select('pets', 'species');
1208
+ * select('pets', 'price');
1209
+ * group('price', 'min').as('minPrice');
1210
+ * group('price', 'max').as('maxPrice');
1211
+ * });
1212
+ *
1213
+ * queries.forEachResultRow('query', (rowId) => {
1214
+ * console.log({[rowId]: queries.getResultRow('query', rowId)});
1215
+ * });
1216
+ * // -> {0: {species: 'dog', minPrice: 4, maxPrice: 5}}
1217
+ * // -> {1: {species: 'cat', minPrice: 3, maxPrice: 4}}
1218
+ * ```
1219
+ * @category Definition
1220
+ * @since v2.0.0
1221
+ */
1222
+ export type GroupedAs = {as: (groupedCellId: Id) => void};
1223
+
1224
+ /**
1225
+ * The Having type describes a function that lets you specify conditions to
1226
+ * filter results, based on the grouped Cells resulting from a Group clause.
1227
+ *
1228
+ * The Having function is provided to the third `query` parameter of the
1229
+ * setQueryDefinition method.
1230
+ *
1231
+ * A Having condition has to be true for a Row to be included in the results.
1232
+ * Each Having class is additive, as though combined with a logical 'and'. If
1233
+ * you wish to create an 'or' expression, use the single parameter version of
1234
+ * the type that allows arbitrary programmatic conditions.
1235
+ *
1236
+ * The Where keyword differs from the Having keyword in that the former
1237
+ * describes conditions that should be met by underlying Cell values (whether
1238
+ * selected or not), and the latter describes conditions based on calculated and
1239
+ * aggregated values - after Group clauses have been applied.
1240
+ *
1241
+ * Whilst it is technically possible to use a Having clause even if the results
1242
+ * have not been grouped with a Group clause, you should expect it to be less
1243
+ * performant than using a Where clause, due to that being applied earlier in
1244
+ * the query process.
1245
+ *
1246
+ * @example
1247
+ * This example shows a query that filters the results from a grouped Table by
1248
+ * comparing a Cell from it with a value.
1249
+ *
1250
+ * ```js
1251
+ * const store = createStore().setTable('pets', {
1252
+ * fido: {species: 'dog', price: 5},
1253
+ * felix: {species: 'cat', price: 4},
1254
+ * cujo: {species: 'dog', price: 4},
1255
+ * tom: {species: 'cat', price: 3},
1256
+ * carnaby: {species: 'parrot', price: 3},
1257
+ * polly: {species: 'parrot', price: 3},
1258
+ * });
1259
+ *
1260
+ * const queries = createQueries(store);
1261
+ * queries.setQueryDefinition('query', 'pets', ({select, group, having}) => {
1262
+ * select('pets', 'species');
1263
+ * select('pets', 'price');
1264
+ * group('price', 'min').as('minPrice');
1265
+ * group('price', 'max').as('maxPrice');
1266
+ * having('minPrice', 3);
1267
+ * });
1268
+ *
1269
+ * queries.forEachResultRow('query', (rowId) => {
1270
+ * console.log({[rowId]: queries.getResultRow('query', rowId)});
1271
+ * });
1272
+ * // -> {0: {species: 'cat', minPrice: 3, maxPrice: 4}}
1273
+ * // -> {1: {species: 'parrot', minPrice: 3, maxPrice: 3}}
1274
+ * ```
1275
+ * @example
1276
+ * This example shows a query that filters the results from a grouped Table with
1277
+ * a condition that is calculated from Cell values.
1278
+ *
1279
+ * ```js
1280
+ * const store = createStore().setTable('pets', {
1281
+ * fido: {species: 'dog', price: 5},
1282
+ * felix: {species: 'cat', price: 4},
1283
+ * cujo: {species: 'dog', price: 4},
1284
+ * tom: {species: 'cat', price: 3},
1285
+ * carnaby: {species: 'parrot', price: 3},
1286
+ * polly: {species: 'parrot', price: 3},
1287
+ * });
1288
+ *
1289
+ * const queries = createQueries(store);
1290
+ * queries.setQueryDefinition('query', 'pets', ({select, group, having}) => {
1291
+ * select('pets', 'species');
1292
+ * select('pets', 'price');
1293
+ * group('price', 'min').as('minPrice');
1294
+ * group('price', 'max').as('maxPrice');
1295
+ * having(
1296
+ * (getSelectedOrGroupedCell) =>
1297
+ * getSelectedOrGroupedCell('minPrice') !=
1298
+ * getSelectedOrGroupedCell('maxPrice'),
1299
+ * );
1300
+ * });
1301
+ *
1302
+ * queries.forEachResultRow('query', (rowId) => {
1303
+ * console.log({[rowId]: queries.getResultRow('query', rowId)});
1304
+ * });
1305
+ * // -> {0: {species: 'dog', minPrice: 4, maxPrice: 5}}
1306
+ * // -> {1: {species: 'cat', minPrice: 3, maxPrice: 4}}
1307
+ * // Parrots are filtered out because they have zero range in price.
1308
+ * ```
1309
+ * @category Definition
1310
+ * @since v2.0.0
1311
+ */
1312
+ export type Having = {
1313
+ /**
1314
+ * Calling this function with two parameters is used to include only those
1315
+ * Rows for which a specified Cell in the query's main Table has a specified
1316
+ * value.
1317
+ *
1318
+ * @param selectedOrGroupedCellId The Id of the Cell in the query to test.
1319
+ * @param equals The value that the Cell has to have for the Row to be
1320
+ * included in the result.
1321
+ */
1322
+ (selectedOrGroupedCellId: Id, equals: Cell): void;
1323
+ /**
1324
+ * Calling this function with one callback parameter is used to include only
1325
+ * those Rows which meet a calculated boolean condition.
1326
+ *
1327
+ * @param condition A callback that takes a GetCell function and that should
1328
+ * return `true` for the Row to be included in the result.
1329
+ */
1330
+ (condition: (getSelectedOrGroupedCell: GetCell) => boolean): void;
1331
+ };
1332
+
1333
+ /**
1334
+ * A Queries object lets you create and track queries of the data in Store
1335
+ * objects.
1336
+ *
1337
+ * This is useful for creating a reactive view of data that is stored in
1338
+ * physical tables: selecting columns, joining tables together, filtering rows,
1339
+ * aggregating data, sorting it, and so on.
1340
+ *
1341
+ * This provides a generalized query concept for Store data. If you just want to
1342
+ * create and track metrics, indexes, or relationships between rows, you may
1343
+ * prefer to use the dedicated Metrics, Indexes, and Relationships objects,
1344
+ * which have simpler APIs.
1345
+ *
1346
+ * Create a Queries object easily with the createQueries function. From there,
1347
+ * you can add new query definitions (with the setQueryDefinition method), query
1348
+ * the results (with the getResultTable method, the getResultRow method, the
1349
+ * getResultCell method, and so on), and add listeners for when they change
1350
+ * (with the addResultTableListener method, the addResultRowListener method, the
1351
+ * addResultCellListener method, and so on).
1352
+ *
1353
+ * @example
1354
+ * This example shows a very simple lifecycle of a Queries object: from
1355
+ * creation, to adding definitions, getting their contents, and then registering
1356
+ * and removing listeners for them.
1357
+ *
1358
+ * ```js
1359
+ * const store = createStore()
1360
+ * .setTable('pets', {
1361
+ * fido: {species: 'dog', color: 'brown', ownerId: '1'},
1362
+ * felix: {species: 'cat', color: 'black', ownerId: '2'},
1363
+ * cujo: {species: 'dog', color: 'black', ownerId: '3'},
1364
+ * })
1365
+ * .setTable('species', {
1366
+ * dog: {price: 5},
1367
+ * cat: {price: 4},
1368
+ * worm: {price: 1},
1369
+ * })
1370
+ * .setTable('owners', {
1371
+ * '1': {name: 'Alice'},
1372
+ * '2': {name: 'Bob'},
1373
+ * '3': {name: 'Carol'},
1374
+ * });
1375
+ *
1376
+ * const queries = createQueries(store);
1377
+ *
1378
+ * // A filtered table query:
1379
+ * queries.setQueryDefinition('blackPets', 'pets', ({select, where}) => {
1380
+ * select('species');
1381
+ * where('color', 'black');
1382
+ * });
1383
+ * console.log(queries.getResultTable('blackPets'));
1384
+ * // -> {felix: {species: 'cat'}, cujo: {species: 'dog'}}
1385
+ *
1386
+ * // A joined table query:
1387
+ * queries.setQueryDefinition('petOwners', 'pets', ({select, join}) => {
1388
+ * select('owners', 'name').as('owner');
1389
+ * join('owners', 'ownerId');
1390
+ * });
1391
+ * console.log(queries.getResultTable('petOwners'));
1392
+ * // -> {fido: {owner: 'Alice'}, felix: {owner: 'Bob'}, cujo: {owner: 'Carol'}}
1393
+ *
1394
+ * // A grouped query:
1395
+ * queries.setQueryDefinition(
1396
+ * 'colorPrice',
1397
+ * 'pets',
1398
+ * ({select, join, group}) => {
1399
+ * select('color');
1400
+ * select('species', 'price');
1401
+ * join('species', 'species');
1402
+ * group('price', 'avg');
1403
+ * },
1404
+ * );
1405
+ * console.log(queries.getResultTable('colorPrice'));
1406
+ * // -> {"1": {color: 'black', price: 4.5}, "0": {color: 'brown', price: 5}}
1407
+ * console.log(queries.getResultSortedRowIds('colorPrice', 'price', true));
1408
+ * // -> ["0", "1"]
1409
+ *
1410
+ * const listenerId = queries.addResultTableListener('colorPrice', () => {
1411
+ * console.log('Average prices per color changed');
1412
+ * console.log(queries.getResultTable('colorPrice'));
1413
+ * console.log(queries.getResultSortedRowIds('colorPrice', 'price', true));
1414
+ * });
1415
+ *
1416
+ * store.setRow('pets', 'lowly', {species: 'worm', color: 'brown'});
1417
+ * // -> 'Average prices per color changed'
1418
+ * // -> {"0": {color: 'brown', price: 3}, "1": {color: 'black', price: 4.5}}
1419
+ * // -> ["1", "0"]
1420
+ *
1421
+ * queries.delListener(listenerId);
1422
+ * queries.destroy();
1423
+ * ```
1424
+ * @see Making Queries guides
1425
+ * @see Car Analysis demo
1426
+ * @see Movie Database demo
1427
+ * @category Queries
1428
+ * @since v2.0.0
1429
+ */
1430
+ export interface Queries {
1431
+ /**
1432
+ * The setQueryDefinition method lets you set the definition of a query.
1433
+ *
1434
+ * Every query definition is identified by a unique Id, and if you re-use an
1435
+ * existing Id with this method, the previous definition is overwritten.
1436
+ *
1437
+ * A query provides a tabular result formed from each Row within a main Table.
1438
+ * The definition must specify this 'main' Table (by its Id) to be aggregated.
1439
+ * Other Tables can be joined to that using Join clauses.
1440
+ *
1441
+ * The third `query` parameter is a callback that you provide to define the
1442
+ * query. That callback is provided with a `keywords` object that contains the
1443
+ * functions you use to define the query, like `select`, `join`, and so on.
1444
+ * You can see how that is used in the simple example below. The following
1445
+ * five clause types are supported:
1446
+ *
1447
+ * - The Select type describes a function that lets you specify a Cell or
1448
+ * calculated value for including into the query's result.
1449
+ * - The Join type describes a function that lets you specify a Cell or
1450
+ * calculated value to join the main query Table to others, by Row Id.
1451
+ * - The Where type describes a function that lets you specify conditions to
1452
+ * filter results, based on the underlying Cells of the main or joined
1453
+ * Tables.
1454
+ * - The Group type describes a function that lets you specify that the values
1455
+ * of a Cell in multiple result Rows should be aggregated together.
1456
+ * - The Having type describes a function that lets you specify conditions to
1457
+ * filter results, based on the grouped Cells resulting from a Group clause.
1458
+ *
1459
+ * Full documentation and examples are provided in the sections for each of
1460
+ * those clause types.
1461
+ *
1462
+ * Additionally, you can use the getResultSortedRowIds method and
1463
+ * addResultSortedRowIdsListener method to sort and paginate the results.
1464
+ *
1465
+ * @param queryId The Id of the query to define.
1466
+ * @param tableId The Id of the main Table the query will be based on.
1467
+ * @param query A callback which can take a `keywords` object and which uses
1468
+ the functions it contains to define the query.
1469
+ * @returns A reference to the Queries object.
1470
+ * @example
1471
+ * This example creates a Store, creates a Queries object, and defines a
1472
+ * simple query to select just one column from the Table, for each Row where
1473
+ * the `species` Cell matches as certain value.
1474
+ *
1475
+ * ```js
1476
+ * const store = createStore().setTable('pets', {
1477
+ * fido: {species: 'dog', color: 'brown'},
1478
+ * felix: {species: 'cat', color: 'black'},
1479
+ * cujo: {species: 'dog', color: 'black'},
1480
+ * });
1481
+ *
1482
+ * const queries = createQueries(store);
1483
+ * queries.setQueryDefinition('dogColors', 'pets', ({select, where}) => {
1484
+ * select('color');
1485
+ * where('species', 'dog');
1486
+ * });
1487
+ *
1488
+ * console.log(queries.getResultTable('dogColors'));
1489
+ * // -> {fido: {color: 'brown'}, cujo: {color: 'black'}}
1490
+ * ```
1491
+ * @category Configuration
1492
+ * @since v2.0.0
1493
+ */
1494
+ setQueryDefinition(
1495
+ queryId: Id,
1496
+ tableId: Id,
1497
+ query: (keywords: {
1498
+ select: Select;
1499
+ join: Join;
1500
+ where: Where;
1501
+ group: Group;
1502
+ having: Having;
1503
+ }) => void,
1504
+ ): Queries;
1505
+
1506
+ /**
1507
+ * The delQueryDefinition method removes an existing query definition.
1508
+ *
1509
+ * @param queryId The Id of the query to remove.
1510
+ * @returns A reference to the Queries object.
1511
+ * @example
1512
+ * This example creates a Store, creates a Queries object, defines a simple
1513
+ * query, and then removes it.
1514
+ *
1515
+ * ```js
1516
+ * const store = createStore().setTable('pets', {
1517
+ * fido: {species: 'dog', color: 'brown'},
1518
+ * felix: {species: 'cat', color: 'black'},
1519
+ * cujo: {species: 'dog', color: 'black'},
1520
+ * });
1521
+ *
1522
+ * const queries = createQueries(store);
1523
+ * queries.setQueryDefinition('dogColors', 'pets', ({select, where}) => {
1524
+ * select('color');
1525
+ * where('species', 'dog');
1526
+ * });
1527
+ * console.log(queries.getQueryIds());
1528
+ * // -> ['dogColors']
1529
+ *
1530
+ * queries.delQueryDefinition('dogColors');
1531
+ * console.log(queries.getQueryIds());
1532
+ * // -> []
1533
+ * ```
1534
+ * @category Configuration
1535
+ * @since v2.0.0
1536
+ */
1537
+ delQueryDefinition(queryId: Id): Queries;
1538
+
1539
+ /**
1540
+ * The getStore method returns a reference to the underlying Store that is
1541
+ * backing this Queries object.
1542
+ *
1543
+ * @returns A reference to the Store.
1544
+ * @example
1545
+ * This example creates a Queries object against a newly-created Store and
1546
+ * then gets its reference in order to update its data.
1547
+ *
1548
+ * ```js
1549
+ * const queries = createQueries(createStore());
1550
+ * queries.setQueryDefinition('dogColors', 'pets', ({select, where}) => {
1551
+ * select('color');
1552
+ * where('species', 'dog');
1553
+ * });
1554
+ * queries
1555
+ * .getStore()
1556
+ * .setRow('pets', 'fido', {species: 'dog', color: 'brown'});
1557
+ * console.log(queries.getResultTable('dogColors'));
1558
+ * // -> {fido: {color: 'brown'}}
1559
+ * ```
1560
+ * @category Getter
1561
+ * @since v2.0.0
1562
+ */
1563
+ getStore(): Store;
1564
+
1565
+ /**
1566
+ * The getQueryIds method returns an array of the query Ids registered with
1567
+ * this Queries object.
1568
+ *
1569
+ * @returns An array of Ids.
1570
+ * @example
1571
+ * This example creates a Queries object with two definitions, and then gets
1572
+ * the Ids of the definitions.
1573
+ *
1574
+ * ```js
1575
+ * const queries = createQueries(createStore())
1576
+ * .setQueryDefinition('dogColors', 'pets', ({select, where}) => {
1577
+ * select('color');
1578
+ * where('species', 'dog');
1579
+ * })
1580
+ * .setQueryDefinition('catColors', 'pets', ({select, where}) => {
1581
+ * select('color');
1582
+ * where('species', 'cat');
1583
+ * });
1584
+ *
1585
+ * console.log(queries.getQueryIds());
1586
+ * // -> ['dogColors', 'catColors']
1587
+ * ```
1588
+ * @category Getter
1589
+ * @since v2.0.0
1590
+ */
1591
+ getQueryIds(): Ids;
1592
+
1593
+ /**
1594
+ * The forEachQuery method takes a function that it will then call for each
1595
+ * Query in the Queries object.
1596
+ *
1597
+ * This method is useful for iterating over all the queries in a functional
1598
+ * style. The `queryCallback` parameter is a QueryCallback function that will
1599
+ * be called with the Id of each query.
1600
+ *
1601
+ * @param queryCallback The function that should be called for every query.
1602
+ * @example
1603
+ * This example iterates over each query in a Queries object.
1604
+ *
1605
+ * ```js
1606
+ * const queries = createQueries(createStore())
1607
+ * .setQueryDefinition('dogColors', 'pets', ({select, where}) => {
1608
+ * select('color');
1609
+ * where('species', 'dog');
1610
+ * })
1611
+ * .setQueryDefinition('catColors', 'pets', ({select, where}) => {
1612
+ * select('color');
1613
+ * where('species', 'cat');
1614
+ * });
1615
+ *
1616
+ * queries.forEachQuery((queryId) => {
1617
+ * console.log(queryId);
1618
+ * });
1619
+ * // -> 'dogColors'
1620
+ * // -> 'catColors'
1621
+ * ```
1622
+ * @category Iterator
1623
+ * @since v2.0.0
1624
+ */
1625
+ forEachQuery(queryCallback: QueryCallback): void;
1626
+
1627
+ /**
1628
+ * The hasQuery method returns a boolean indicating whether a given query
1629
+ * exists in the Queries object.
1630
+ *
1631
+ * @param queryId The Id of a possible query in the Queries object.
1632
+ * @returns Whether a query with that Id exists.
1633
+ * @example
1634
+ * This example shows two simple query existence checks.
1635
+ *
1636
+ * ```js
1637
+ * const queries = createQueries(createStore()).setQueryDefinition(
1638
+ * 'dogColors',
1639
+ * 'pets',
1640
+ * ({select, where}) => {
1641
+ * select('color');
1642
+ * where('species', 'dog');
1643
+ * },
1644
+ * );
1645
+ *
1646
+ * console.log(queries.hasQuery('dogColors'));
1647
+ * // -> true
1648
+ * console.log(queries.hasQuery('catColors'));
1649
+ * // -> false
1650
+ * ```
1651
+ * @category Getter
1652
+ * @since v2.0.0
1653
+ */
1654
+ hasQuery(queryId: Id): boolean;
1655
+
1656
+ /**
1657
+ * The getTableId method returns the Id of the underlying Table that is
1658
+ * backing a query.
1659
+ *
1660
+ * If the query Id is invalid, the method returns `undefined`.
1661
+ *
1662
+ * @param queryId The Id of a query.
1663
+ * @returns The Id of the Table backing the query, or `undefined`.
1664
+ * @example
1665
+ * This example creates a Queries object, a single query definition, and then
1666
+ * calls this method on it (as well as a non-existent definition) to get the
1667
+ * underlying Table Id.
1668
+ *
1669
+ * ```js
1670
+ * const queries = createQueries(createStore()).setQueryDefinition(
1671
+ * 'dogColors',
1672
+ * 'pets',
1673
+ * ({select, where}) => {
1674
+ * select('color');
1675
+ * where('species', 'dog');
1676
+ * },
1677
+ * );
1678
+ *
1679
+ * console.log(queries.getTableId('dogColors'));
1680
+ * // -> 'pets'
1681
+ * console.log(queries.getTableId('catColors'));
1682
+ * // -> undefined
1683
+ * ```
1684
+ * @category Getter
1685
+ * @since v2.0.0
1686
+ */
1687
+ getTableId(queryId: Id): Id | undefined;
1688
+
1689
+ /**
1690
+ * The getResultTable method returns an object containing the entire data of
1691
+ * the result Table of the given query.
1692
+ *
1693
+ * This has the same behavior as a Store's getTable method. For example, if
1694
+ * the query Id is invalid, the method returns an empty object. Similarly, it
1695
+ * returns a copy of, rather than a reference to the underlying data, so
1696
+ * changes made to the returned object are not made to the query results
1697
+ * themselves.
1698
+ *
1699
+ * @param queryId The Id of a query.
1700
+ * @returns An object containing the entire data of the result Table of the
1701
+ * query.
1702
+ * @returns The result of the query, structured as a Table.
1703
+ * @example
1704
+ * This example creates a Queries object, a single query definition, and then
1705
+ * calls this method on it (as well as a non-existent definition) to get the
1706
+ * result Table.
1707
+ *
1708
+ * ```js
1709
+ * const store = createStore().setTable('pets', {
1710
+ * fido: {species: 'dog', color: 'brown'},
1711
+ * felix: {species: 'cat', color: 'black'},
1712
+ * cujo: {species: 'dog', color: 'black'},
1713
+ * });
1714
+ *
1715
+ * const queries = createQueries(store).setQueryDefinition(
1716
+ * 'dogColors',
1717
+ * 'pets',
1718
+ * ({select, where}) => {
1719
+ * select('color');
1720
+ * where('species', 'dog');
1721
+ * },
1722
+ * );
1723
+ *
1724
+ * console.log(queries.getResultTable('dogColors'));
1725
+ * // -> {fido: {color: 'brown'}, cujo: {color: 'black'}}
1726
+ *
1727
+ * console.log(queries.getResultTable('catColors'));
1728
+ * // -> {}
1729
+ * ```
1730
+ * @category Result
1731
+ * @since v2.0.0
1732
+ */
1733
+ getResultTable(queryId: Id): Table;
1734
+
1735
+ /**
1736
+ * The getResultRowIds method returns the Ids of every Row in the result Table
1737
+ * of the given query.
1738
+ *
1739
+ * This has the same behavior as a Store's getRowIds method. For example, if
1740
+ * the query Id is invalid, the method returns an empty array. Similarly, it
1741
+ * returns a copy of, rather than a reference to the list of Ids, so changes
1742
+ * made to the list object are not made to the query results themselves.
1743
+ *
1744
+ * @param queryId The Id of a query.
1745
+ * @returns An array of the Ids of every Row in the result of the query.
1746
+ * @example
1747
+ * This example creates a Queries object, a single query definition, and then
1748
+ * calls this method on it (as well as a non-existent definition) to get the
1749
+ * result Row Ids.
1750
+ *
1751
+ * ```js
1752
+ * const store = createStore().setTable('pets', {
1753
+ * fido: {species: 'dog', color: 'brown'},
1754
+ * felix: {species: 'cat', color: 'black'},
1755
+ * cujo: {species: 'dog', color: 'black'},
1756
+ * });
1757
+ *
1758
+ * const queries = createQueries(store).setQueryDefinition(
1759
+ * 'dogColors',
1760
+ * 'pets',
1761
+ * ({select, where}) => {
1762
+ * select('color');
1763
+ * where('species', 'dog');
1764
+ * },
1765
+ * );
1766
+ *
1767
+ * console.log(queries.getResultRowIds('dogColors'));
1768
+ * // -> ['fido', 'cujo']
1769
+ *
1770
+ * console.log(queries.getResultRowIds('catColors'));
1771
+ * // -> []
1772
+ * ```
1773
+ * @category Result
1774
+ * @since v2.0.0
1775
+ */
1776
+ getResultRowIds(queryId: Id): Ids;
1777
+
1778
+ /**
1779
+ * The getResultSortedRowIds method returns the Ids of every Row in the result
1780
+ * Table of the given query, sorted according to the values in a specified
1781
+ * Cell.
1782
+ *
1783
+ * This has the same behavior as a Store's getSortedRowIds method. For
1784
+ * example, if the query Id is invalid, the method returns an empty array.
1785
+ * Similarly, the sorting of the rows is alphanumeric, and you can indicate
1786
+ * whether it should be in descending order. The `offset` and `limit`
1787
+ * parameters are used to paginate results, but default to `0` and `undefined`
1788
+ * to return all available Row Ids if not specified.
1789
+ *
1790
+ * Note that every call to this method will perform the sorting afresh - there
1791
+ * is no caching of the results - and so you are advised to memoize the
1792
+ * results yourself, especially when the result Table is large. For a
1793
+ * performant approach to tracking the sorted Row Ids when they change, use
1794
+ * the addResultSortedRowIdsListener method.
1795
+ *
1796
+ * @param queryId The Id of a query.
1797
+ * @param cellId The Id of the Cell whose values are used for the sorting, or
1798
+ * `undefined` to by sort the Row Id itself.
1799
+ * @param descending Whether the sorting should be in descending order.
1800
+ * @param offset The number of Row Ids to skip for pagination purposes, if
1801
+ * any.
1802
+ * @param limit The maximum number of Row Ids to return, or `undefined` for
1803
+ * all.
1804
+ * @returns An array of the sorted Ids of every Row in the result of the
1805
+ * query.
1806
+ * @example
1807
+ * This example creates a Queries object, a single query definition, and then
1808
+ * calls this method on it (as well as a non-existent definition) to get the
1809
+ * result Row Ids.
1810
+ *
1811
+ * ```js
1812
+ * const store = createStore().setTable('pets', {
1813
+ * fido: {species: 'dog', color: 'brown'},
1814
+ * felix: {species: 'cat', color: 'black'},
1815
+ * cujo: {species: 'dog', color: 'black'},
1816
+ * });
1817
+ *
1818
+ * const queries = createQueries(store).setQueryDefinition(
1819
+ * 'dogColors',
1820
+ * 'pets',
1821
+ * ({select, where}) => {
1822
+ * select('color');
1823
+ * where('species', 'dog');
1824
+ * },
1825
+ * );
1826
+ *
1827
+ * console.log(queries.getResultSortedRowIds('dogColors', 'color'));
1828
+ * // -> ['cujo', 'fido']
1829
+ *
1830
+ * console.log(queries.getResultSortedRowIds('catColors', 'color'));
1831
+ * // -> []
1832
+ * ```
1833
+ * @category Result
1834
+ * @since v2.0.0
1835
+ */
1836
+ getResultSortedRowIds(
1837
+ queryId: Id,
1838
+ cellId?: Id,
1839
+ descending?: boolean,
1840
+ offset?: number,
1841
+ limit?: number,
1842
+ ): Ids;
1843
+
1844
+ /**
1845
+ * The getResultRow method returns an object containing the entire data of a
1846
+ * single Row in the result Table of the given query.
1847
+ *
1848
+ * This has the same behavior as a Store's getRow method. For example, if the
1849
+ * query or Row Id is invalid, the method returns an empty object. Similarly,
1850
+ * it returns a copy of, rather than a reference to the underlying data, so
1851
+ * changes made to the returned object are not made to the query results
1852
+ * themselves.
1853
+ *
1854
+ * @param queryId The Id of a query.
1855
+ * @param rowId The Id of the Row in the result Table.
1856
+ * @returns An object containing the entire data of the Row in the result
1857
+ * Table of the query.
1858
+ * @example
1859
+ * This example creates a Queries object, a single query definition, and then
1860
+ * calls this method on it (as well as a non-existent Row Id) to get the
1861
+ * result Row.
1862
+ *
1863
+ * ```js
1864
+ * const store = createStore().setTable('pets', {
1865
+ * fido: {species: 'dog', color: 'brown'},
1866
+ * felix: {species: 'cat', color: 'black'},
1867
+ * cujo: {species: 'dog', color: 'black'},
1868
+ * });
1869
+ *
1870
+ * const queries = createQueries(store).setQueryDefinition(
1871
+ * 'dogColors',
1872
+ * 'pets',
1873
+ * ({select, where}) => {
1874
+ * select('color');
1875
+ * where('species', 'dog');
1876
+ * },
1877
+ * );
1878
+ *
1879
+ * console.log(queries.getResultRow('dogColors', 'fido'));
1880
+ * // -> {color: 'brown'}
1881
+ *
1882
+ * console.log(queries.getResultRow('dogColors', 'felix'));
1883
+ * // -> {}
1884
+ * ```
1885
+ * @category Result
1886
+ * @since v2.0.0
1887
+ */
1888
+ getResultRow(queryId: Id, rowId: Id): Row;
1889
+
1890
+ /**
1891
+ * The getResultCellIds method returns the Ids of every Cell in a given Row,
1892
+ * in the result Table of the given query.
1893
+ *
1894
+ * This has the same behavior as a Store's getCellIds method. For example, if
1895
+ * the query Id or Row Id is invalid, the method returns an empty array.
1896
+ * Similarly, it returns a copy of, rather than a reference to the list of
1897
+ * Ids, so changes made to the list object are not made to the query results
1898
+ * themselves.
1899
+ *
1900
+ * @param queryId The Id of a query.
1901
+ * @param rowId The Id of the Row in the result Table.
1902
+ * @returns An array of the Ids of every Cell in the Row in the result of the
1903
+ * query.
1904
+ * @example
1905
+ * This example creates a Queries object, a single query definition, and then
1906
+ * calls this method on it (as well as a non-existent Row Id) to get the
1907
+ * result Cell Ids.
1908
+ *
1909
+ * ```js
1910
+ * const store = createStore().setTable('pets', {
1911
+ * fido: {species: 'dog', color: 'brown'},
1912
+ * felix: {species: 'cat', color: 'black'},
1913
+ * cujo: {species: 'dog', color: 'black'},
1914
+ * });
1915
+ *
1916
+ * const queries = createQueries(store).setQueryDefinition(
1917
+ * 'dogColors',
1918
+ * 'pets',
1919
+ * ({select, where}) => {
1920
+ * select('color');
1921
+ * where('species', 'dog');
1922
+ * },
1923
+ * );
1924
+ *
1925
+ * console.log(queries.getResultCellIds('dogColors', 'fido'));
1926
+ * // -> ['color']
1927
+ *
1928
+ * console.log(queries.getResultCellIds('dogColors', 'felix'));
1929
+ * // -> []
1930
+ * ```
1931
+ * @category Result
1932
+ * @since v2.0.0
1933
+ */
1934
+ getResultCellIds(queryId: Id, rowId: Id): Ids;
1935
+
1936
+ /**
1937
+ * The getResultCell method returns the value of a single Cell in a given Row,
1938
+ * in the result Table of the given query.
1939
+ *
1940
+ * This has the same behavior as a Store's getCell method. For example, if the
1941
+ * query, or Row, or Cell Id is invalid, the method returns `undefined`.
1942
+ *
1943
+ * @param queryId The Id of a query.
1944
+ * @param rowId The Id of the Row in the result Table.
1945
+ * @param cellId The Id of the Cell in the Row.
1946
+ * @returns The value of the Cell, or `undefined`.
1947
+ * @example
1948
+ * This example creates a Queries object, a single query definition, and then
1949
+ * calls this method on it (as well as a non-existent Cell Id) to get the
1950
+ * result Cell.
1951
+ *
1952
+ * ```js
1953
+ * const store = createStore().setTable('pets', {
1954
+ * fido: {species: 'dog', color: 'brown'},
1955
+ * felix: {species: 'cat', color: 'black'},
1956
+ * cujo: {species: 'dog', color: 'black'},
1957
+ * });
1958
+ *
1959
+ * const queries = createQueries(store).setQueryDefinition(
1960
+ * 'dogColors',
1961
+ * 'pets',
1962
+ * ({select, where}) => {
1963
+ * select('color');
1964
+ * where('species', 'dog');
1965
+ * },
1966
+ * );
1967
+ *
1968
+ * console.log(queries.getResultCell('dogColors', 'fido', 'color'));
1969
+ * // -> 'brown'
1970
+ *
1971
+ * console.log(queries.getResultCell('dogColors', 'fido', 'species'));
1972
+ * // -> undefined
1973
+ * ```
1974
+ * @category Result
1975
+ * @since v2.0.0
1976
+ */
1977
+ getResultCell(queryId: Id, rowId: Id, cellId: Id): CellOrUndefined;
1978
+
1979
+ /**
1980
+ * The hasResultTable method returns a boolean indicating whether a given
1981
+ * result Table exists.
1982
+ *
1983
+ * @param queryId The Id of a possible query.
1984
+ * @returns Whether a result Table for that query Id exists.
1985
+ * @example
1986
+ * This example shows two simple result Table existence checks.
1987
+ *
1988
+ * ```js
1989
+ * const store = createStore().setTable('pets', {
1990
+ * fido: {species: 'dog', color: 'brown'},
1991
+ * felix: {species: 'cat', color: 'black'},
1992
+ * cujo: {species: 'dog', color: 'black'},
1993
+ * });
1994
+ *
1995
+ * const queries = createQueries(store).setQueryDefinition(
1996
+ * 'dogColors',
1997
+ * 'pets',
1998
+ * ({select, where}) => {
1999
+ * select('color');
2000
+ * where('species', 'dog');
2001
+ * },
2002
+ * );
2003
+ *
2004
+ * console.log(queries.hasResultTable('dogColors'));
2005
+ * // -> true
2006
+ * console.log(queries.hasResultTable('catColors'));
2007
+ * // -> false
2008
+ * ```
2009
+ * @category Result
2010
+ * @since v2.0.0
2011
+ */
2012
+ hasResultTable(queryId: Id): boolean;
2013
+
2014
+ /**
2015
+ * The hasResultRow method returns a boolean indicating whether a given result
2016
+ * Row exists.
2017
+ *
2018
+ * @param queryId The Id of a possible query.
2019
+ * @param rowId The Id of a possible Row.
2020
+ * @returns Whether a result Row for that Id exists.
2021
+ * @example
2022
+ * This example shows two simple result Row existence checks.
2023
+ *
2024
+ * ```js
2025
+ * const store = createStore().setTable('pets', {
2026
+ * fido: {species: 'dog', color: 'brown'},
2027
+ * felix: {species: 'cat', color: 'black'},
2028
+ * cujo: {species: 'dog', color: 'black'},
2029
+ * });
2030
+ *
2031
+ * const queries = createQueries(store).setQueryDefinition(
2032
+ * 'dogColors',
2033
+ * 'pets',
2034
+ * ({select, where}) => {
2035
+ * select('color');
2036
+ * where('species', 'dog');
2037
+ * },
2038
+ * );
2039
+ *
2040
+ * console.log(queries.hasResultRow('dogColors', 'fido'));
2041
+ * // -> true
2042
+ * console.log(queries.hasResultRow('dogColors', 'felix'));
2043
+ * // -> false
2044
+ * ```
2045
+ * @category Result
2046
+ * @since v2.0.0
2047
+ */
2048
+ hasResultRow(queryId: Id, rowId: Id): boolean;
2049
+
2050
+ /**
2051
+ * The hasResultCell method returns a boolean indicating whether a given
2052
+ * result Cell exists.
2053
+ *
2054
+ * @param queryId The Id of a possible query.
2055
+ * @param rowId The Id of a possible Row.
2056
+ * @param cellId The Id of a possible Cell.
2057
+ * @returns Whether a result Cell for that Id exists.
2058
+ * @example
2059
+ * This example shows two simple result Row existence checks.
2060
+ *
2061
+ * ```js
2062
+ * const store = createStore().setTable('pets', {
2063
+ * fido: {species: 'dog', color: 'brown'},
2064
+ * felix: {species: 'cat', color: 'black'},
2065
+ * cujo: {species: 'dog', color: 'black'},
2066
+ * });
2067
+ *
2068
+ * const queries = createQueries(store).setQueryDefinition(
2069
+ * 'dogColors',
2070
+ * 'pets',
2071
+ * ({select, where}) => {
2072
+ * select('color');
2073
+ * where('species', 'dog');
2074
+ * },
2075
+ * );
2076
+ *
2077
+ * console.log(queries.hasResultCell('dogColors', 'fido', 'color'));
2078
+ * // -> true
2079
+ * console.log(queries.hasResultCell('dogColors', 'fido', 'species'));
2080
+ * // -> false
2081
+ * ```
2082
+ * @category Result
2083
+ * @since v2.0.0
2084
+ */
2085
+ hasResultCell(queryId: Id, rowId: Id, cellId: Id): boolean;
2086
+
2087
+ /**
2088
+ * The forEachResultTable method takes a function that it will then call for
2089
+ * each result Table in the Queries object.
2090
+ *
2091
+ * This method is useful for iterating over all the result Tables of the
2092
+ * queries in a functional style. The `tableCallback` parameter is a
2093
+ * TableCallback function that will be called with the Id of each result
2094
+ * Table, and with a function that can then be used to iterate over each Row
2095
+ * of the result Table, should you wish.
2096
+ *
2097
+ * @param tableCallback The function that should be called for every query's
2098
+ * result Table.
2099
+ * @example
2100
+ * This example iterates over each query's result Table in a Queries object.
2101
+ *
2102
+ * ```js
2103
+ * const store = createStore().setTable('pets', {
2104
+ * fido: {species: 'dog', color: 'brown'},
2105
+ * felix: {species: 'cat', color: 'black'},
2106
+ * cujo: {species: 'dog', color: 'black'},
2107
+ * });
2108
+ *
2109
+ * const queries = createQueries(store)
2110
+ * .setQueryDefinition('dogColors', 'pets', ({select, where}) => {
2111
+ * select('color');
2112
+ * where('species', 'dog');
2113
+ * })
2114
+ * .setQueryDefinition('catColors', 'pets', ({select, where}) => {
2115
+ * select('color');
2116
+ * where('species', 'cat');
2117
+ * });
2118
+ *
2119
+ * queries.forEachResultTable((queryId, forEachRow) => {
2120
+ * console.log(queryId);
2121
+ * forEachRow((rowId) => console.log(`- ${rowId}`));
2122
+ * });
2123
+ * // -> 'dogColors'
2124
+ * // -> '- fido'
2125
+ * // -> '- cujo'
2126
+ * // -> 'catColors'
2127
+ * // -> '- felix'
2128
+ * ```
2129
+ * @category Iterator
2130
+ * @since v2.0.0
2131
+ */
2132
+ forEachResultTable(tableCallback: TableCallback): void;
2133
+
2134
+ /**
2135
+ * The forEachResultRow method takes a function that it will then call for
2136
+ * each Row in the result Table of a query.
2137
+ *
2138
+ * This method is useful for iterating over each Row of the result Table of
2139
+ * the query in a functional style. The `rowCallback` parameter is a
2140
+ * RowCallback function that will be called with the Id of each result Row,
2141
+ * and with a function that can then be used to iterate over each Cell of the
2142
+ * result Row, should you wish.
2143
+ *
2144
+ * @param queryId The Id of a query.
2145
+ * @param rowCallback The function that should be called for every Row of the
2146
+ * query's result Table.
2147
+ * @example
2148
+ * This example iterates over each Row in a query's result Table.
2149
+ *
2150
+ * ```js
2151
+ * const store = createStore().setTable('pets', {
2152
+ * fido: {species: 'dog', color: 'brown'},
2153
+ * felix: {species: 'cat', color: 'black'},
2154
+ * cujo: {species: 'dog', color: 'black'},
2155
+ * });
2156
+ *
2157
+ * const queries = createQueries(store).setQueryDefinition(
2158
+ * 'dogColors',
2159
+ * 'pets',
2160
+ * ({select, where}) => {
2161
+ * select('color');
2162
+ * where('species', 'dog');
2163
+ * },
2164
+ * );
2165
+ *
2166
+ * queries.forEachResultRow('dogColors', (rowId, forEachCell) => {
2167
+ * console.log(rowId);
2168
+ * forEachCell((cellId) => console.log(`- ${cellId}`));
2169
+ * });
2170
+ * // -> 'fido'
2171
+ * // -> '- color'
2172
+ * // -> 'cujo'
2173
+ * // -> '- color'
2174
+ * ```
2175
+ * @category Iterator
2176
+ * @since v2.0.0
2177
+ */
2178
+ forEachResultRow(queryId: Id, rowCallback: RowCallback): void;
2179
+
2180
+ /**
2181
+ * The forEachResultCell method takes a function that it will then call for
2182
+ * each Cell in the result Row of a query.
2183
+ *
2184
+ * This method is useful for iterating over each Cell of the result Row of the
2185
+ * query in a functional style. The `cellCallback` parameter is a CellCallback
2186
+ * function that will be called with the Id and value of each result Cell.
2187
+ *
2188
+ * @param queryId The Id of a query.
2189
+ * @param rowId The Id of a Row in the query's result Table.
2190
+ * @param cellCallback The function that should be called for every Cell of
2191
+ * the query's result Row.
2192
+ * @example
2193
+ * This example iterates over each Cell in a query's result Row.
2194
+ *
2195
+ * ```js
2196
+ * const store = createStore().setTable('pets', {
2197
+ * fido: {species: 'dog', color: 'brown'},
2198
+ * felix: {species: 'cat', color: 'black'},
2199
+ * cujo: {species: 'dog', color: 'black'},
2200
+ * });
2201
+ *
2202
+ * const queries = createQueries(store).setQueryDefinition(
2203
+ * 'dogColors',
2204
+ * 'pets',
2205
+ * ({select, where}) => {
2206
+ * select('species');
2207
+ * select('color');
2208
+ * where('species', 'dog');
2209
+ * },
2210
+ * );
2211
+ *
2212
+ * queries.forEachResultCell('dogColors', 'fido', (cellId, cell) => {
2213
+ * console.log(`${cellId}: ${cell}`);
2214
+ * });
2215
+ * // -> 'species: dog'
2216
+ * // -> 'color: brown'
2217
+ * ```
2218
+ * @category Iterator
2219
+ * @since v2.0.0
2220
+ */
2221
+ forEachResultCell(queryId: Id, rowId: Id, cellCallback: CellCallback): void;
2222
+
2223
+ /**
2224
+ * The addResultTableListener method registers a listener function with the
2225
+ * Queries object that will be called whenever data in a result Table changes.
2226
+ *
2227
+ * The provided listener is a ResultTableListener function, and will be called
2228
+ * with a reference to the Queries object, the Id of the Table that changed
2229
+ * (which is also the query Id), and a GetCellChange function in case you need
2230
+ * to inspect any changes that occurred.
2231
+ *
2232
+ * You can either listen to a single result Table (by specifying a query Id as
2233
+ * the method's first parameter) or changes to any result Table (by providing
2234
+ * a `null` wildcard).
2235
+ *
2236
+ * @param queryId The Id of the query to listen to, or `null` as a wildcard.
2237
+ * @param listener The function that will be called whenever data in the
2238
+ * matching result Table changes.
2239
+ * @returns A unique Id for the listener that can later be used to remove it.
2240
+ * @example
2241
+ * This example registers a listener that responds to any changes to a
2242
+ * specific result Table.
2243
+ *
2244
+ * ```js
2245
+ * const store = createStore().setTable('pets', {
2246
+ * fido: {species: 'dog', color: 'brown'},
2247
+ * felix: {species: 'cat', color: 'black'},
2248
+ * cujo: {species: 'dog', color: 'black'},
2249
+ * });
2250
+ *
2251
+ * const queries = createQueries(store).setQueryDefinition(
2252
+ * 'dogColors',
2253
+ * 'pets',
2254
+ * ({select, where}) => {
2255
+ * select('color');
2256
+ * where('species', 'dog');
2257
+ * },
2258
+ * );
2259
+ *
2260
+ * const listenerId = queries.addResultTableListener(
2261
+ * 'dogColors',
2262
+ * (queries, tableId, getCellChange) => {
2263
+ * console.log('dogColors result table changed');
2264
+ * console.log(getCellChange('dogColors', 'fido', 'color'));
2265
+ * },
2266
+ * );
2267
+ *
2268
+ * store.setCell('pets', 'fido', 'color', 'walnut');
2269
+ * // -> 'dogColors result table changed'
2270
+ * // -> [true, 'brown', 'walnut']
2271
+ *
2272
+ * store.delListener(listenerId);
2273
+ * ```
2274
+ * @example
2275
+ * This example registers a listener that responds to any changes to any
2276
+ * result Table.
2277
+ *
2278
+ * ```js
2279
+ * const store = createStore().setTable('pets', {
2280
+ * fido: {species: 'dog', color: 'brown'},
2281
+ * felix: {species: 'cat', color: 'black'},
2282
+ * cujo: {species: 'dog', color: 'black'},
2283
+ * });
2284
+ *
2285
+ * const queries = createQueries(store)
2286
+ * .setQueryDefinition('dogColors', 'pets', ({select, where}) => {
2287
+ * select('color');
2288
+ * where('species', 'dog');
2289
+ * })
2290
+ * .setQueryDefinition('catColors', 'pets', ({select, where}) => {
2291
+ * select('color');
2292
+ * where('species', 'cat');
2293
+ * });
2294
+ *
2295
+ * const listenerId = queries.addResultTableListener(
2296
+ * null,
2297
+ * (queries, tableId) => {
2298
+ * console.log(`${tableId} result table changed`);
2299
+ * },
2300
+ * );
2301
+ *
2302
+ * store.setCell('pets', 'fido', 'color', 'walnut');
2303
+ * // -> 'dogColors result table changed'
2304
+ * store.setCell('pets', 'felix', 'color', 'tortoiseshell');
2305
+ * // -> 'catColors result table changed'
2306
+ *
2307
+ * store.delListener(listenerId);
2308
+ * ```
2309
+ * @category Listener
2310
+ * @since v2.0.0
2311
+ */
2312
+ addResultTableListener(queryId: IdOrNull, listener: ResultTableListener): Id;
2313
+
2314
+ /**
2315
+ * The addResultRowIdsListener method registers a listener function with the
2316
+ * Queries object that will be called whenever the Row Ids in a result Table
2317
+ * change.
2318
+ *
2319
+ * The provided listener is a ResultRowIdsListener function, and will be
2320
+ * called with a reference to the Queries object and the Id of the Table that
2321
+ * changed (which is also the query Id).
2322
+ *
2323
+ * By default, such a listener is only called when a Row is added to, or
2324
+ * removed from, the result Table. To listen to all changes in the result
2325
+ * Table, use the addResultTableListener method.
2326
+ *
2327
+ * You can either listen to a single result Table (by specifying a query Id as
2328
+ * the method's first parameter) or changes to any result Table (by providing
2329
+ * a `null` wildcard).
2330
+ *
2331
+ * @param queryId The Id of the query to listen to, or `null` as a wildcard.
2332
+ * @param listener The function that will be called whenever the Row Ids in
2333
+ * the result Table change.
2334
+ * @returns A unique Id for the listener that can later be used to remove it.
2335
+ * @example
2336
+ * This example registers a listener that responds to any change to the Row
2337
+ * Ids of a specific result Table.
2338
+ *
2339
+ * ```js
2340
+ * const store = createStore().setTable('pets', {
2341
+ * fido: {species: 'dog', color: 'brown'},
2342
+ * felix: {species: 'cat', color: 'black'},
2343
+ * cujo: {species: 'dog', color: 'black'},
2344
+ * });
2345
+ *
2346
+ * const queries = createQueries(store).setQueryDefinition(
2347
+ * 'dogColors',
2348
+ * 'pets',
2349
+ * ({select, where}) => {
2350
+ * select('color');
2351
+ * where('species', 'dog');
2352
+ * },
2353
+ * );
2354
+ *
2355
+ * const listenerId = queries.addResultRowIdsListener(
2356
+ * 'dogColors',
2357
+ * (queries, tableId) => {
2358
+ * console.log(`Row Ids for dogColors result table changed`);
2359
+ * console.log(queries.getResultRowIds('dogColors'));
2360
+ * },
2361
+ * );
2362
+ *
2363
+ * store.setRow('pets', 'rex', {species: 'dog', color: 'tan'});
2364
+ * // -> 'Row Ids for dogColors result table changed'
2365
+ * // -> ['fido', 'cujo', 'rex']
2366
+ *
2367
+ * store.delListener(listenerId);
2368
+ * ```
2369
+ * @example
2370
+ * This example registers a listener that responds to any change to the Row
2371
+ * Ids of any result Table.
2372
+ *
2373
+ * ```js
2374
+ * const store = createStore().setTable('pets', {
2375
+ * fido: {species: 'dog', color: 'brown'},
2376
+ * felix: {species: 'cat', color: 'black'},
2377
+ * cujo: {species: 'dog', color: 'black'},
2378
+ * });
2379
+ *
2380
+ * const queries = createQueries(store)
2381
+ * .setQueryDefinition('dogColors', 'pets', ({select, where}) => {
2382
+ * select('color');
2383
+ * where('species', 'dog');
2384
+ * })
2385
+ * .setQueryDefinition('catColors', 'pets', ({select, where}) => {
2386
+ * select('color');
2387
+ * where('species', 'cat');
2388
+ * });
2389
+ *
2390
+ * const listenerId = queries.addResultRowIdsListener(
2391
+ * null,
2392
+ * (queries, tableId) => {
2393
+ * console.log(`Row Ids for ${tableId} result table changed`);
2394
+ * },
2395
+ * );
2396
+ *
2397
+ * store.setRow('pets', 'rex', {species: 'dog', color: 'tan'});
2398
+ * // -> 'Row Ids for dogColors result table changed'
2399
+ * store.setRow('pets', 'tom', {species: 'cat', color: 'gray'});
2400
+ * // -> 'Row Ids for catColors result table changed'
2401
+ *
2402
+ * store.delListener(listenerId);
2403
+ * ```
2404
+ * @category Listener
2405
+ * @since v2.0.0
2406
+ */
2407
+ addResultRowIdsListener(
2408
+ queryId: IdOrNull,
2409
+ listener: ResultRowIdsListener,
2410
+ ): Id;
2411
+
2412
+ /**
2413
+ * The addResultSortedRowIdsListener method registers a listener function with
2414
+ * the Queries object that will be called whenever sorted (and optionally,
2415
+ * paginated) Row Ids in a result Table change.
2416
+ *
2417
+ * The provided listener is a ResultSortedRowIdsListener function, and will be
2418
+ * called with a reference to the Queries object, the Id of the result Table
2419
+ * whose Row Ids sorting changed (which is also the query Id), the Cell Id
2420
+ * being used to sort them, whether descending or not, and the offset and
2421
+ * limit of the number of Ids returned, for pagination purposes. It also
2422
+ * receives the sorted array of Ids itself, so that you can use them in the
2423
+ * listener without the additional cost of an explicit call to
2424
+ * getResultSortedRowIds
2425
+ *
2426
+ * Such a listener is called when a Row is added or removed, but also when a
2427
+ * value in the specified Cell (somewhere in the result Table) has changed
2428
+ * enough to change the sorting of the Row Ids.
2429
+ *
2430
+ * Unlike most other listeners, you cannot provide wildcards (due to the cost
2431
+ * of detecting changes to the sorting). You can only listen to a single
2432
+ * specified result Table, sorted by a single specified Cell.
2433
+ *
2434
+ * The sorting of the rows is alphanumeric, and you can indicate whether it
2435
+ * should be in descending order. The `offset` and `limit` parameters are used
2436
+ * to paginate results, but default to `0` and `undefined` to return all
2437
+ * available Row Ids if not specified.
2438
+ *
2439
+ * @param queryId The Id of the query to listen to.
2440
+ * @param cellId The Id of the Cell whose values are used for the sorting, or
2441
+ * `undefined` to by sort the result Row Id itself.
2442
+ * @param descending Whether the sorting should be in descending order.
2443
+ * @param offset The number of Row Ids to skip for pagination purposes, if
2444
+ * any.
2445
+ * @param limit The maximum number of Row Ids to return, or `undefined` for
2446
+ * all.
2447
+ * @param listener The function that will be called whenever the sorted Row
2448
+ * Ids in the result Table change.
2449
+ * @returns A unique Id for the listener that can later be used to remove it.
2450
+ * @example
2451
+ * This example registers a listener that responds to any change to the sorted
2452
+ * Row Ids of a specific result Table.
2453
+ *
2454
+ * ```js
2455
+ * const store = createStore().setTable('pets', {
2456
+ * fido: {species: 'dog', color: 'brown'},
2457
+ * felix: {species: 'cat', color: 'black'},
2458
+ * cujo: {species: 'dog', color: 'black'},
2459
+ * });
2460
+ *
2461
+ * const queries = createQueries(store).setQueryDefinition(
2462
+ * 'dogColors',
2463
+ * 'pets',
2464
+ * ({select, where}) => {
2465
+ * select('color');
2466
+ * where('species', 'dog');
2467
+ * },
2468
+ * );
2469
+ *
2470
+ * const listenerId = queries.addResultSortedRowIdsListener(
2471
+ * 'dogColors',
2472
+ * 'color',
2473
+ * false,
2474
+ * 0,
2475
+ * undefined,
2476
+ * (queries, tableId, cellId, descending, offset, limit, sortedRowIds) => {
2477
+ * console.log(`Sorted Row Ids for dogColors result table changed`);
2478
+ * console.log(sortedRowIds);
2479
+ * // ^ cheaper than calling getResultSortedRowIds again
2480
+ * },
2481
+ * );
2482
+ *
2483
+ * store.setRow('pets', 'rex', {species: 'dog', color: 'tan'});
2484
+ * // -> 'Sorted Row Ids for dogColors result table changed'
2485
+ * // -> ['cujo', 'fido', 'rex']
2486
+ *
2487
+ * store.delListener(listenerId);
2488
+ * ```
2489
+ * @example
2490
+ * This example registers a listener that responds to any change to the sorted
2491
+ * Row Ids of a specific Table. The Row Ids are sorted by their own value,
2492
+ * since the `cellId` parameter is explicitly undefined.
2493
+ *
2494
+ * ```js
2495
+ * const store = createStore().setTable('pets', {
2496
+ * fido: {species: 'dog', color: 'brown'},
2497
+ * felix: {species: 'cat', color: 'black'},
2498
+ * cujo: {species: 'dog', color: 'black'},
2499
+ * });
2500
+ *
2501
+ * const queries = createQueries(store).setQueryDefinition(
2502
+ * 'dogColors',
2503
+ * 'pets',
2504
+ * ({select, where}) => {
2505
+ * select('color');
2506
+ * where('species', 'dog');
2507
+ * },
2508
+ * );
2509
+ * console.log(queries.getResultSortedRowIds('dogColors', undefined));
2510
+ * // -> ['cujo', 'fido']
2511
+ *
2512
+ * const listenerId = queries.addResultSortedRowIdsListener(
2513
+ * 'dogColors',
2514
+ * undefined,
2515
+ * false,
2516
+ * 0,
2517
+ * undefined,
2518
+ * (queries, tableId, cellId, descending, offset, limit, sortedRowIds) => {
2519
+ * console.log(`Sorted Row Ids for dogColors result table changed`);
2520
+ * console.log(sortedRowIds);
2521
+ * // ^ cheaper than calling getSortedRowIds again
2522
+ * },
2523
+ * );
2524
+ *
2525
+ * store.setRow('pets', 'rex', {species: 'dog', color: 'tan'});
2526
+ * // -> 'Sorted Row Ids for dogColors result table changed'
2527
+ * // -> ['cujo', 'fido', 'rex']
2528
+ *
2529
+ * store.delListener(listenerId);
2530
+ * ```
2531
+ * @category Listener
2532
+ * @since v2.0.0
2533
+ */
2534
+ addResultSortedRowIdsListener(
2535
+ queryId: Id,
2536
+ cellId: Id | undefined,
2537
+ descending: boolean,
2538
+ offset: number,
2539
+ limit: number | undefined,
2540
+ listener: ResultSortedRowIdsListener,
2541
+ ): Id;
2542
+
2543
+ /**
2544
+ * The addResultRowListener method registers a listener function with the
2545
+ * Queries object that will be called whenever data in a result Row changes.
2546
+ *
2547
+ * The provided listener is a ResultRowListener function, and will be called
2548
+ * with a reference to the Queries object, the Id of the Table that changed
2549
+ * (which is also the query Id), and a GetCellChange function in case you need
2550
+ * to inspect any changes that occurred.
2551
+ *
2552
+ * You can either listen to a single result Row (by specifying a query Id and
2553
+ * Row Id as the method's first two parameters) or changes to any result Row
2554
+ * (by providing `null` wildcards).
2555
+ *
2556
+ * Both, either, or neither of the `queryId` and `rowId` parameters can be
2557
+ * wildcarded with `null`. You can listen to a specific result Row in a
2558
+ * specific query, any result Row in a specific query, a specific result Row
2559
+ * in any query, or any result Row in any query.
2560
+ *
2561
+ * @param queryId The Id of the query to listen to, or `null` as a wildcard.
2562
+ * @param rowId The Id of the result Row to listen to, or `null` as a
2563
+ * wildcard.
2564
+ * @param listener The function that will be called whenever data in the
2565
+ * matching result Row changes.
2566
+ * @returns A unique Id for the listener that can later be used to remove it.
2567
+ * @example
2568
+ * This example registers a listener that responds to any changes to a
2569
+ * specific result Row.
2570
+ *
2571
+ * ```js
2572
+ * const store = createStore().setTable('pets', {
2573
+ * fido: {species: 'dog', color: 'brown'},
2574
+ * felix: {species: 'cat', color: 'black'},
2575
+ * cujo: {species: 'dog', color: 'black'},
2576
+ * });
2577
+ *
2578
+ * const queries = createQueries(store).setQueryDefinition(
2579
+ * 'dogColors',
2580
+ * 'pets',
2581
+ * ({select, where}) => {
2582
+ * select('color');
2583
+ * where('species', 'dog');
2584
+ * },
2585
+ * );
2586
+ *
2587
+ * const listenerId = queries.addResultRowListener(
2588
+ * 'dogColors',
2589
+ * 'fido',
2590
+ * (queries, tableId, rowId, getCellChange) => {
2591
+ * console.log('fido row in dogColors result table changed');
2592
+ * console.log(getCellChange('dogColors', 'fido', 'color'));
2593
+ * },
2594
+ * );
2595
+ *
2596
+ * store.setCell('pets', 'fido', 'color', 'walnut');
2597
+ * // -> 'fido row in dogColors result table changed'
2598
+ * // -> [true, 'brown', 'walnut']
2599
+ *
2600
+ * store.delListener(listenerId);
2601
+ * ```
2602
+ * @example
2603
+ * This example registers a listener that responds to any changes to any
2604
+ * result Row.
2605
+ *
2606
+ * ```js
2607
+ * const store = createStore().setTable('pets', {
2608
+ * fido: {species: 'dog', color: 'brown'},
2609
+ * felix: {species: 'cat', color: 'black'},
2610
+ * cujo: {species: 'dog', color: 'black'},
2611
+ * });
2612
+ *
2613
+ * const queries = createQueries(store)
2614
+ * .setQueryDefinition('dogColors', 'pets', ({select, where}) => {
2615
+ * select('color');
2616
+ * where('species', 'dog');
2617
+ * })
2618
+ * .setQueryDefinition('catColors', 'pets', ({select, where}) => {
2619
+ * select('color');
2620
+ * where('species', 'cat');
2621
+ * });
2622
+ *
2623
+ * const listenerId = queries.addResultRowListener(
2624
+ * null,
2625
+ * null,
2626
+ * (queries, tableId, rowId) => {
2627
+ * console.log(`${rowId} row in ${tableId} result table changed`);
2628
+ * },
2629
+ * );
2630
+ *
2631
+ * store.setCell('pets', 'fido', 'color', 'walnut');
2632
+ * // -> 'fido row in dogColors result table changed'
2633
+ * store.setCell('pets', 'felix', 'color', 'tortoiseshell');
2634
+ * // -> 'felix row in catColors result table changed'
2635
+ *
2636
+ * store.delListener(listenerId);
2637
+ * ```
2638
+ * @category Listener
2639
+ * @since v2.0.0
2640
+ */
2641
+ addResultRowListener(
2642
+ queryId: IdOrNull,
2643
+ rowId: IdOrNull,
2644
+ listener: ResultRowListener,
2645
+ ): Id;
2646
+
2647
+ /**
2648
+ * The addResultCellIdsListener method registers a listener function with the
2649
+ * Queries object that will be called whenever the Cell Ids in a result Row
2650
+ * change.
2651
+ *
2652
+ * The provided listener is a ResultCellIdsListener function, and will be
2653
+ * called with a reference to the Queries object, the Id of the Table (which
2654
+ * is also the query Id), and the Id of the result Row that changed.
2655
+ *
2656
+ * Such a listener is only called when a Cell is added to, or removed from,
2657
+ * the result Row. To listen to all changes in the result Row, use the
2658
+ * addResultRowListener method.
2659
+ *
2660
+ * You can either listen to a single result Row (by specifying the query Id
2661
+ * and Row Id as the method's first two parameters) or changes to any Row (by
2662
+ * providing `null` wildcards).
2663
+ *
2664
+ * Both, either, or neither of the `queryId` and `rowId` parameters can be
2665
+ * wildcarded with `null`. You can listen to a specific result Row in a
2666
+ * specific query, any result Row in a specific query, a specific result Row
2667
+ * in any query, or any result Row in any query.
2668
+ *
2669
+ * @param queryId The Id of the query to listen to, or `null` as a wildcard.
2670
+ * @param rowId The Id of the result Row to listen to, or `null` as a
2671
+ * wildcard.
2672
+ * @param listener The function that will be called whenever the Cell Ids in
2673
+ * the result Row change.
2674
+ * @returns A unique Id for the listener that can later be used to remove it.
2675
+ * @example
2676
+ * This example registers a listener that responds to any change to the Cell
2677
+ * Ids of a specific result Row.
2678
+ *
2679
+ * ```js
2680
+ * const store = createStore().setTable('pets', {
2681
+ * fido: {species: 'dog', color: 'brown'},
2682
+ * felix: {species: 'cat', color: 'black'},
2683
+ * cujo: {species: 'dog', color: 'black'},
2684
+ * });
2685
+ *
2686
+ * const queries = createQueries(store).setQueryDefinition(
2687
+ * 'dogColors',
2688
+ * 'pets',
2689
+ * ({select, where}) => {
2690
+ * select('color');
2691
+ * select('price');
2692
+ * where('species', 'dog');
2693
+ * },
2694
+ * );
2695
+ *
2696
+ * const listenerId = queries.addResultCellIdsListener(
2697
+ * 'dogColors',
2698
+ * 'fido',
2699
+ * (store, tableId, rowId) => {
2700
+ * console.log(`Cell Ids for fido row in dogColors result table changed`);
2701
+ * console.log(queries.getResultCellIds('dogColors', 'fido'));
2702
+ * },
2703
+ * );
2704
+ *
2705
+ * store.setCell('pets', 'fido', 'price', 5);
2706
+ * // -> 'Cell Ids for fido row in dogColors result table changed'
2707
+ * // -> ['color', 'price']
2708
+ *
2709
+ * store.delListener(listenerId);
2710
+ * ```
2711
+ * @example
2712
+ * This example registers a listener that responds to any change to the Cell
2713
+ * Ids of any result Row.
2714
+ *
2715
+ * ```js
2716
+ * const store = createStore().setTable('pets', {
2717
+ * fido: {species: 'dog', color: 'brown'},
2718
+ * felix: {species: 'cat', color: 'black'},
2719
+ * cujo: {species: 'dog', color: 'black'},
2720
+ * });
2721
+ *
2722
+ * const queries = createQueries(store)
2723
+ * .setQueryDefinition('dogColors', 'pets', ({select, where}) => {
2724
+ * select('color');
2725
+ * select('price');
2726
+ * where('species', 'dog');
2727
+ * })
2728
+ * .setQueryDefinition('catColors', 'pets', ({select, where}) => {
2729
+ * select('color');
2730
+ * select('purrs');
2731
+ * where('species', 'cat');
2732
+ * });
2733
+ *
2734
+ * const listenerId = queries.addResultCellIdsListener(
2735
+ * null,
2736
+ * null,
2737
+ * (queries, tableId, rowId) => {
2738
+ * console.log(
2739
+ * `Cell Ids for ${rowId} row in ${tableId} result table changed`,
2740
+ * );
2741
+ * },
2742
+ * );
2743
+ *
2744
+ * store.setCell('pets', 'fido', 'price', 5);
2745
+ * // -> 'Cell Ids for fido row in dogColors result table changed'
2746
+ * store.setCell('pets', 'felix', 'purrs', true);
2747
+ * // -> 'Cell Ids for felix row in catColors result table changed'
2748
+ *
2749
+ * store.delListener(listenerId);
2750
+ * ```
2751
+ * @category Listener
2752
+ * @since v2.0.0
2753
+ */
2754
+ addResultCellIdsListener(
2755
+ queryId: IdOrNull,
2756
+ rowId: IdOrNull,
2757
+ listener: ResultCellIdsListener,
2758
+ ): Id;
2759
+
2760
+ /**
2761
+ * The addResultCellListener method registers a listener function with the
2762
+ * Queries object that will be called whenever data in a result Cell changes.
2763
+ *
2764
+ * The provided listener is a ResultCellListener function, and will be called
2765
+ * with a reference to the Queries object, the Id of the Table that changed
2766
+ * (which is also the query Id), the Id of the Row that changed, the Id of the
2767
+ * Cell that changed, the new Cell value, the old Cell value, and a
2768
+ * GetCellChange function in case you need to inspect any changes that
2769
+ * occurred.
2770
+ *
2771
+ * You can either listen to a single result Row (by specifying a query Id, Row
2772
+ * Id, and Cell Id as the method's first three parameters) or changes to any
2773
+ * result Cell (by providing `null` wildcards).
2774
+ *
2775
+ * All, some, or none of the `queryId`, `rowId`, and `cellId` parameters can
2776
+ * be wildcarded with `null`. You can listen to a specific Cell in a specific
2777
+ * result Row in a specific query, any Cell in any result Row in any query,
2778
+ * for example - or every other combination of wildcards.
2779
+ *
2780
+ * @param queryId The Id of the query to listen to, or `null` as a wildcard.
2781
+ * @param rowId The Id of the result Row to listen to, or `null` as a
2782
+ * wildcard.
2783
+ * @param cellId The Id of the result Cell to listen to, or `null` as a
2784
+ * wildcard.
2785
+ * @param listener The function that will be called whenever data in the
2786
+ * matching result Cell changes.
2787
+ * @returns A unique Id for the listener that can later be used to remove it.
2788
+ * @example
2789
+ * This example registers a listener that responds to any changes to a
2790
+ * specific result Cell.
2791
+ *
2792
+ * ```js
2793
+ * const store = createStore().setTable('pets', {
2794
+ * fido: {species: 'dog', color: 'brown'},
2795
+ * felix: {species: 'cat', color: 'black'},
2796
+ * cujo: {species: 'dog', color: 'black'},
2797
+ * });
2798
+ *
2799
+ * const queries = createQueries(store).setQueryDefinition(
2800
+ * 'dogColors',
2801
+ * 'pets',
2802
+ * ({select, where}) => {
2803
+ * select('color');
2804
+ * where('species', 'dog');
2805
+ * },
2806
+ * );
2807
+ *
2808
+ * const listenerId = queries.addResultCellListener(
2809
+ * 'dogColors',
2810
+ * 'fido',
2811
+ * 'color',
2812
+ * (queries, tableId, rowId, cellId, newCell, oldCell, getCellChange) => {
2813
+ * console.log(
2814
+ * 'color cell in fido row in dogColors result table changed',
2815
+ * );
2816
+ * console.log([oldCell, newCell]);
2817
+ * console.log(getCellChange('dogColors', 'fido', 'color'));
2818
+ * },
2819
+ * );
2820
+ *
2821
+ * store.setCell('pets', 'fido', 'color', 'walnut');
2822
+ * // -> 'color cell in fido row in dogColors result table changed'
2823
+ * // -> ['brown', 'walnut']
2824
+ * // -> [true, 'brown', 'walnut']
2825
+ *
2826
+ * store.delListener(listenerId);
2827
+ * ```
2828
+ * @example
2829
+ * This example registers a listener that responds to any changes to any
2830
+ * result Cell.
2831
+ *
2832
+ * ```js
2833
+ * const store = createStore().setTable('pets', {
2834
+ * fido: {species: 'dog', color: 'brown', price: 5},
2835
+ * felix: {species: 'cat', color: 'black', price: 4},
2836
+ * cujo: {species: 'dog', color: 'black', price: 5},
2837
+ * });
2838
+ *
2839
+ * const queries = createQueries(store)
2840
+ * .setQueryDefinition('dogColors', 'pets', ({select, where}) => {
2841
+ * select('color');
2842
+ * where('species', 'dog');
2843
+ * })
2844
+ * .setQueryDefinition('catColors', 'pets', ({select, where}) => {
2845
+ * select('color');
2846
+ * select('price');
2847
+ * where('species', 'cat');
2848
+ * });
2849
+ *
2850
+ * const listenerId = queries.addResultCellListener(
2851
+ * null,
2852
+ * null,
2853
+ * null,
2854
+ * (queries, tableId, rowId, cellId) => {
2855
+ * console.log(
2856
+ * `${cellId} cell in ${rowId} row in ${tableId} result table changed`,
2857
+ * );
2858
+ * },
2859
+ * );
2860
+ *
2861
+ * store.setCell('pets', 'fido', 'color', 'walnut');
2862
+ * // -> 'color cell in fido row in dogColors result table changed'
2863
+ * store.setCell('pets', 'felix', 'price', 3);
2864
+ * // -> 'price cell in felix row in catColors result table changed'
2865
+ *
2866
+ * store.delListener(listenerId);
2867
+ * ```
2868
+ * @category Listener
2869
+ * @since v2.0.0
2870
+ */
2871
+ addResultCellListener(
2872
+ queryId: IdOrNull,
2873
+ rowId: IdOrNull,
2874
+ cellId: IdOrNull,
2875
+ listener: ResultCellListener,
2876
+ ): Id;
2877
+
2878
+ /**
2879
+ * The delListener method removes a listener that was previously added to the
2880
+ * Queries object.
2881
+ *
2882
+ * Use the Id returned by the addMetricListener method. Note that the Queries
2883
+ * object may re-use this Id for future listeners added to it.
2884
+ *
2885
+ * @param listenerId The Id of the listener to remove.
2886
+ * @returns A reference to the Queries object.
2887
+ * @example
2888
+ * This example creates a Store, a Queries object, registers a listener, and
2889
+ * then removes it.
2890
+ *
2891
+ * ```js
2892
+ * const store = createStore().setTable('pets', {
2893
+ * fido: {species: 'dog'},
2894
+ * felix: {species: 'cat'},
2895
+ * cujo: {species: 'dog'},
2896
+ * });
2897
+ *
2898
+ * const queries = createQueries(store).setQueryDefinition(
2899
+ * 'species',
2900
+ * 'pets',
2901
+ * ({select}) => {
2902
+ * select('species');
2903
+ * },
2904
+ * );
2905
+ *
2906
+ * const listenerId = queries.addResultTableListener('species', (queries) =>
2907
+ * console.log('species result changed'),
2908
+ * );
2909
+ *
2910
+ * store.setCell('pets', 'ed', 'species', 'horse');
2911
+ * // -> 'species result changed'
2912
+ *
2913
+ * queries.delListener(listenerId);
2914
+ *
2915
+ * store.setCell('pets', 'molly', 'species', 'cow');
2916
+ * // -> undefined
2917
+ * // The listener is not called.
2918
+ * ```
2919
+ * @category Listener
2920
+ * @since v2.0.0
2921
+ */
2922
+ delListener(listenerId: Id): Queries;
2923
+
2924
+ /**
2925
+ * The destroy method should be called when this Queries object is no longer
2926
+ * used.
2927
+ *
2928
+ * This guarantees that all of the listeners that the object registered with
2929
+ * the underlying Store are removed and it can be correctly garbage collected.
2930
+ *
2931
+ * @example
2932
+ * This example creates a Store, adds a Queries object with a definition (that
2933
+ * registers a RowListener with the underlying Store), and then destroys it
2934
+ * again, removing the listener.
2935
+ *
2936
+ * ```js
2937
+ * const store = createStore().setTable('pets', {
2938
+ * fido: {species: 'dog'},
2939
+ * felix: {species: 'cat'},
2940
+ * cujo: {species: 'dog'},
2941
+ * });
2942
+ *
2943
+ * const queries = createQueries(store);
2944
+ * queries.setQueryDefinition('species', 'species', ({select}) => {
2945
+ * select('species');
2946
+ * });
2947
+ * console.log(store.getListenerStats().row);
2948
+ * // -> 1
2949
+ *
2950
+ * queries.destroy();
2951
+ *
2952
+ * console.log(store.getListenerStats().row);
2953
+ * // -> 0
2954
+ * ```
2955
+ * @category Lifecycle
2956
+ * @since v2.0.0
2957
+ */
2958
+ destroy(): void;
2959
+
2960
+ /**
2961
+ * The getListenerStats method provides a set of statistics about the
2962
+ * listeners registered with the Queries object, and is used for debugging
2963
+ * purposes.
2964
+ *
2965
+ * The statistics are only populated in a debug build: production builds
2966
+ * return an empty object. The method is intended to be used during
2967
+ * development to ensure your application is not leaking listener
2968
+ * registrations, for example.
2969
+ *
2970
+ * @returns A QueriesListenerStats object containing Queries listener
2971
+ * statistics.
2972
+ * @example
2973
+ * This example gets the listener statistics of a Queries object.
2974
+ *
2975
+ * ```js
2976
+ * const store = createStore();
2977
+ * const queries = createQueries(store);
2978
+ * queries.addResultTableListener(null, () => console.log('Result changed'));
2979
+ *
2980
+ * console.log(queries.getListenerStats().table);
2981
+ * // -> 1
2982
+ * console.log(queries.getListenerStats().row);
2983
+ * // -> 0
2984
+ * ```
2985
+ * @category Development
2986
+ * @since v2.0.0
2987
+ */
2988
+ getListenerStats(): QueriesListenerStats;
2989
+ }
2990
+
2991
+ /**
2992
+ * The createQueries function creates a Queries object, and is the main entry
2993
+ * point into the queries module.
2994
+ *
2995
+ * A given Store can only have one Queries object associated with it. If you
2996
+ * call this function twice on the same Store, your second call will return a
2997
+ * reference to the Queries object created by the first.
2998
+ *
2999
+ * @param store The Store for which to register query definitions.
3000
+ * @returns A reference to the new Queries object.
3001
+ * @example
3002
+ * This example creates a Queries object.
3003
+ *
3004
+ * ```js
3005
+ * const store = createStore();
3006
+ * const queries = createQueries(store);
3007
+ * console.log(queries.getQueryIds());
3008
+ * // -> []
3009
+ * ```
3010
+ * @example
3011
+ * This example creates a Queries object, and calls the method a second time
3012
+ * for the same Store to return the same object.
3013
+ *
3014
+ * ```js
3015
+ * const store = createStore();
3016
+ * const queries1 = createQueries(store);
3017
+ * const queries2 = createQueries(store);
3018
+ * console.log(queries1 === queries2);
3019
+ * // -> true
3020
+ * ```
3021
+ * @category Creation
3022
+ * @since v2.0.0
3023
+ */
3024
+ export function createQueries(store: Store): Queries;