tinybase 6.1.0-beta.1 → 6.1.0-beta.3

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