tinybase 6.1.0-beta.1 → 6.1.0-beta.2

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