tinybase 3.1.0-beta.4 → 3.1.0

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