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