tinybase 1.3.4 → 2.0.0-beta.1

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