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,1004 +0,0 @@
1
- /**
2
- * The metrics module of the TinyBase project provides the ability to create and
3
- * track metrics and aggregates of the data in Store objects.
4
- *
5
- * The main entry point to this module is the createMetrics function, which
6
- * returns a new Metrics object. From there, you can create new Metric
7
- * definitions, access the values of those Metrics directly, and register
8
- * listeners for when they change.
9
- * @packageDocumentation
10
- * @module metrics
11
- * @since v1.0.0
12
- */
13
- import type {
14
- CellIdFromSchema,
15
- TableIdFromSchema,
16
- } from '../../_internal/store/with-schemas/index.d.cts';
17
- import type {Id, IdOrNull, Ids} from '../../common/with-schemas/index.d.cts';
18
- import type {
19
- GetCell,
20
- OptionalSchemas,
21
- Store,
22
- } from '../../store/with-schemas/index.d.cts';
23
-
24
- /**
25
- * The Metric type is simply an alias, but represents a number formed by
26
- * aggregating multiple other numbers together.
27
- * @category Metric
28
- * @since v1.0.0
29
- */
30
- export type Metric = number;
31
-
32
- /**
33
- * The MetricCallback type describes a function that takes a Metric's Id and a
34
- * callback to loop over each Row within it.
35
- *
36
- * A MetricCallback is provided when using the forEachMetric method, so that you
37
- * can do something based on every Metric in the Metrics object. See that method
38
- * for specific examples.
39
- * @param metricId The Id of the Metric that the callback can operate on.
40
- * @param metric The value of the Metric.
41
- * @category Callback
42
- * @since v1.0.0
43
- */
44
- export type MetricCallback = (metricId: Id, metric?: Metric) => void;
45
-
46
- /**
47
- * The MetricAggregate type describes a custom function that takes an array of
48
- * numbers and returns an aggregate that is used as a Metric.
49
- *
50
- * There are a number of common predefined aggregators, such as for counting,
51
- * summing, and averaging values. This type is instead used for when you wish to
52
- * use a more complex aggregation of your own devising. See the
53
- * setMetricDefinition method for more examples.
54
- * @param numbers The array of numbers in the Metric's aggregation.
55
- * @param length The length of the array of numbers in the Metric's aggregation.
56
- * @returns The value of the Metric.
57
- * @category Aggregators
58
- * @since v1.0.0
59
- */
60
- export type MetricAggregate = (numbers: number[], length: number) => Metric;
61
-
62
- /**
63
- * The MetricAggregateAdd type describes a function that can be used to optimize
64
- * a custom MetricAggregate by providing a shortcut for when a single value is
65
- * added to the input values.
66
- *
67
- * Some aggregation functions do not need to recalculate the aggregation of the
68
- * whole set when one value changes. For example, when adding a new number to a
69
- * series, the new sum of the series is the new value added to the previous sum.
70
- *
71
- * If it is not possible to shortcut the aggregation based on just one value
72
- * being added, return `undefined` and the Metric will be completely
73
- * recalculated.
74
- *
75
- * When possible, if you are providing a custom MetricAggregate, seek an
76
- * implementation of an MetricAggregateAdd function that can reduce the
77
- * complexity cost of growing the input data set. See the setMetricDefinition
78
- * method for more examples.
79
- * @param metric The current value of the Metric.
80
- * @param add The number being added to the Metric's aggregation.
81
- * @param length The length of the array of numbers in the Metric's aggregation.
82
- * @returns The new value of the Metric.
83
- * @category Aggregators
84
- * @since v1.0.0
85
- */
86
- export type MetricAggregateAdd = (
87
- metric: Metric,
88
- add: number,
89
- length: number,
90
- ) => Metric | undefined;
91
-
92
- /**
93
- * The MetricAggregateRemove type describes a function that can be used to
94
- * optimize a custom MetricAggregate by providing a shortcut for when a single
95
- * value is removed from the input values.
96
- *
97
- * Some aggregation functions do not need to recalculate the aggregation of the
98
- * whole set when one value changes. For example, when removing a number from a
99
- * series, the new sum of the series is the new value subtracted from the
100
- * previous sum.
101
- *
102
- * If it is not possible to shortcut the aggregation based on just one value
103
- * being removed, return `undefined` and the Metric will be completely
104
- * recalculated. One example might be if you were taking the minimum of the
105
- * values, and the previous minimum is being removed. The whole of the rest of
106
- * the list will need to be re-scanned to find a new minimum.
107
- *
108
- * When possible, if you are providing a custom MetricAggregate, seek an
109
- * implementation of an MetricAggregateRemove function that can reduce the
110
- * complexity cost of shrinking the input data set. See the setMetricDefinition
111
- * method for more examples.
112
- * @param metric The current value of the Metric.
113
- * @param remove The number being removed from the Metric's aggregation.
114
- * @param length The length of the array of numbers in the Metric's aggregation.
115
- * @returns The new value of the Metric.
116
- * @category Aggregators
117
- * @since v1.0.0
118
- */
119
- export type MetricAggregateRemove = (
120
- metric: Metric,
121
- remove: number,
122
- length: number,
123
- ) => Metric | undefined;
124
-
125
- /**
126
- * The MetricAggregateReplace type describes a function that can be used to
127
- * optimize a custom MetricAggregate by providing a shortcut for when a single
128
- * value in the input values is replaced with another.
129
- *
130
- * Some aggregation functions do not need to recalculate the aggregation of the
131
- * whole set when one value changes. For example, when replacing a number in a
132
- * series, the new sum of the series is the previous sum, plus the new value,
133
- * minus the old value.
134
- *
135
- * If it is not possible to shortcut the aggregation based on just one value
136
- * changing, return `undefined` and the Metric will be completely recalculated.
137
- *
138
- * When possible, if you are providing a custom MetricAggregate, seek an
139
- * implementation of an MetricAggregateReplace function that can reduce the
140
- * complexity cost of changing the input data set in place. See the
141
- * setMetricDefinition method for more examples.
142
- * @param metric The current value of the Metric.
143
- * @param add The number being added to the Metric's aggregation.
144
- * @param remove The number being removed from the Metric's aggregation.
145
- * @param length The length of the array of numbers in the Metric's aggregation.
146
- * @returns The new value of the Metric.
147
- * @category Aggregators
148
- * @since v1.0.0
149
- */
150
- export type MetricAggregateReplace = (
151
- metric: Metric,
152
- add: number,
153
- remove: number,
154
- length: number,
155
- ) => Metric | undefined;
156
-
157
- /**
158
- * The MetricIdsListener type describes a function that is used to listen to
159
- * Metric definitions being added or removed.
160
- *
161
- * This has schema-based typing. The following is a simplified representation:
162
- *
163
- * ```ts override
164
- * (metrics: Metrics) => void;
165
- * ```
166
- *
167
- * A MetricIdsListener is provided when using the addMetricIdsListener method.
168
- * See that method for specific examples.
169
- *
170
- * When called, a MetricIdsListener is given a reference to the Metrics object.
171
- * @param metrics A reference to the Metrics object that changed.
172
- * @category Listener
173
- * @since v1.0.0
174
- */
175
- export type MetricIdsListener<Schemas extends OptionalSchemas> = (
176
- metrics: Metrics<Schemas>,
177
- ) => void;
178
-
179
- /**
180
- * The MetricListener type describes a function that is used to listen to
181
- * changes to a Metric.
182
- *
183
- * This has schema-based typing. The following is a simplified representation:
184
- *
185
- * ```ts override
186
- * (
187
- * metrics: Metrics,
188
- * metricId: Id,
189
- * newMetric: Metric | undefined,
190
- * oldMetric: Metric | undefined,
191
- * ) => void;
192
- * ```
193
- *
194
- * A MetricListener is provided when using the addMetricListener method. See
195
- * that method for specific examples.
196
- *
197
- * When called, a MetricListener is given a reference to the Metrics object, the
198
- * Id of the Metric that changed, and the new and old values of the Metric.
199
- *
200
- * If this is the first time that a Metric has had a value (such as when a table
201
- * has gained its first row), the old value will be `undefined`. If a Metric now
202
- * no longer has a value, the new value will be `undefined`.
203
- * @param metrics A reference to the Metrics object that changed.
204
- * @param metricId The Id of the Metric that changed.
205
- * @param newMetric The new value of the Metric that changed.
206
- * @param oldMetric The old value of the Metric that changed.
207
- * @category Listener
208
- * @since v1.0.0
209
- */
210
- export type MetricListener<Schemas extends OptionalSchemas> = (
211
- metrics: Metrics<Schemas>,
212
- metricId: Id,
213
- newMetric: Metric | undefined,
214
- oldMetric: Metric | undefined,
215
- ) => void;
216
-
217
- /**
218
- * The MetricsListenerStats type describes the number of listeners registered
219
- * with the Metrics object, and can be used for debugging purposes.
220
- *
221
- * A MetricsListenerStats object is returned from the getListenerStats method.
222
- * @category Development
223
- * @since v1.0.0
224
- */
225
- export type MetricsListenerStats = {
226
- /**
227
- * The number of MetricListener functions registered with the Metrics object.
228
- * @category Stat
229
- * @since v1.0.0
230
- */
231
- metric: number;
232
- };
233
-
234
- /**
235
- * A Metrics object lets you define, query, and listen to, aggregations of Cell
236
- * values within a Table in a Store.
237
- *
238
- * This is useful for counting the number of Row objects in a Table, averaging
239
- * Cell values, or efficiently performing any arbitrary aggregations.
240
- *
241
- * Create a Metrics object easily with the createMetrics function. From there,
242
- * you can add new Metric definitions (with the setMetricDefinition method),
243
- * query their values (with the getMetric method), and add listeners for when
244
- * they change (with the addMetricListener method).
245
- *
246
- * This module provides a number of predefined and self-explanatory aggregations
247
- * ('sum', 'avg', 'min', and 'max'), and defaults to counting Row objects when
248
- * using the setMetricDefinition method. However, far more complex aggregations
249
- * can be configured with custom functions.
250
- * @example
251
- * This example shows a very simple lifecycle of a Metrics object: from
252
- * creation, to adding a definition, getting a Metric, and then registering and
253
- * removing a listener for it.
254
- *
255
- * ```js
256
- * import {createMetrics, createStore} from 'tinybase';
257
- *
258
- * const store = createStore().setTable('species', {
259
- * dog: {price: 5},
260
- * cat: {price: 4},
261
- * worm: {price: 1},
262
- * });
263
- *
264
- * const metrics = createMetrics(store);
265
- * metrics.setMetricDefinition(
266
- * 'highestPrice', // metricId
267
- * 'species', // tableId to aggregate
268
- * 'max', // aggregation
269
- * 'price', // cellId to aggregate
270
- * );
271
- *
272
- * console.log(metrics.getMetric('highestPrice'));
273
- * // -> 5
274
- *
275
- * const listenerId = metrics.addMetricListener('highestPrice', () => {
276
- * console.log(metrics.getMetric('highestPrice'));
277
- * });
278
- * store.setCell('species', 'horse', 'price', 20);
279
- * // -> 20
280
- *
281
- * metrics.delListener(listenerId);
282
- * metrics.destroy();
283
- * ```
284
- * @see Using Metrics guides
285
- * @see Rolling Dice demos
286
- * @see Country demo
287
- * @see Todo App demos
288
- * @category Metrics
289
- * @since v1.0.0
290
- */
291
- export interface Metrics<in out Schemas extends OptionalSchemas> {
292
- /**
293
- * The setMetricDefinition method lets you set the definition of a Metric.
294
- *
295
- * This has schema-based typing. The following is a simplified representation:
296
- *
297
- * ```ts override
298
- * setMetricDefinition(
299
- * metricId: Id,
300
- * tableId: Id,
301
- * aggregate?: 'sum' | 'avg' | 'min' | 'max' | MetricAggregate,
302
- * getNumber?: Id | ((getCell: GetCell, rowId: Id) => number),
303
- * aggregateAdd?: MetricAggregateAdd,
304
- * aggregateRemove?: MetricAggregateRemove,
305
- * aggregateReplace?: MetricAggregateReplace,
306
- * ): Metrics;
307
- * ```
308
- *
309
- * Every Metric definition is identified by a unique Id, and if you re-use an
310
- * existing Id with this method, the previous definition is overwritten.
311
- *
312
- * A Metric is an aggregation of numeric values produced from each Row within
313
- * a single Table. Therefore the definition must specify the Table (by its Id)
314
- * to be aggregated.
315
- *
316
- * Without the third `aggregate` parameter, the Metric will simply be a count
317
- * of the number of Row objects in the Table. But often you will specify a
318
- * more interesting aggregate - such as the four predefined aggregates, 'sum',
319
- * 'avg', 'min', and 'max' - or a custom function that produces your own
320
- * aggregation of an array of numbers.
321
- *
322
- * The fourth `getNumber` parameter specifies which Cell in each Row contains
323
- * the numerical values to be used in the aggregation. Alternatively, a custom
324
- * function can be provided that produces your own numeric value from the
325
- * local Row as a whole.
326
- *
327
- * The final three parameters, `aggregateAdd`, `aggregateRemove`,
328
- * `aggregateReplace` need only be provided when you are using your own custom
329
- * `aggregate` function. These give you the opportunity to reduce your custom
330
- * function's algorithmic complexity by providing shortcuts that can nudge an
331
- * aggregation result when a single value is added, removed, or replaced in
332
- * the input values.
333
- * @param metricId The Id of the Metric to define.
334
- * @param tableId The Id of the Table the Metric will be calculated from.
335
- * @param aggregate Either a string representing one of a set of common
336
- * aggregation techniques ('sum', 'avg', 'min', or 'max'), or a function that
337
- * aggregates numeric values from each Row to create the Metric's overall
338
- * value. Defaults to 'sum'.
339
- * @param getNumber Either the Id of a Cell containing, or a function that
340
- * produces, the numeric value that will be aggregated in the way specified by
341
- * the `aggregate` parameter. Defaults to a function that returns `1` (meaning
342
- * that if the `aggregate` and `getNumber` parameters are both omitted, the
343
- * Metric will simply be a count of the Row objects in the Table).
344
- * @param aggregateAdd A function that can be used to optimize a custom
345
- * MetricAggregate by providing a shortcut for when a single value is added to
346
- * the input values - for example, when a Row is added to the Table.
347
- * @param aggregateRemove A function that can be used to optimize a custom
348
- * MetricAggregate by providing a shortcut for when a single value is removed
349
- * from the input values - for example ,when a Row is removed from the Table.
350
- * @param aggregateReplace A function that can be used to optimize a custom
351
- * MetricAggregate by providing a shortcut for when a single value in the
352
- * input values is replaced with another - for example, when a Row is updated.
353
- * @returns A reference to the Metrics object.
354
- * @example
355
- * This example creates a Store, creates a Metrics object, and defines a
356
- * simple Metric to count the Row objects in the Table.
357
- *
358
- * ```js
359
- * import {createMetrics, createStore} from 'tinybase';
360
- *
361
- * const store = createStore().setTable('species', {
362
- * dog: {price: 5},
363
- * cat: {price: 4},
364
- * worm: {price: 1},
365
- * });
366
- *
367
- * const metrics = createMetrics(store);
368
- * metrics.setMetricDefinition('speciesCount', 'species');
369
- *
370
- * console.log(metrics.getMetric('speciesCount'));
371
- * // -> 3
372
- * ```
373
- * @example
374
- * This example creates a Store, creates a Metrics object, and defines a
375
- * standard Metric to get the highest value of each `price` Cell in the Row
376
- * objects in the Table.
377
- *
378
- * ```js
379
- * import {createMetrics, createStore} from 'tinybase';
380
- *
381
- * const store = createStore().setTable('species', {
382
- * dog: {price: 5},
383
- * cat: {price: 4},
384
- * worm: {price: 1},
385
- * });
386
- *
387
- * const metrics = createMetrics(store);
388
- * metrics.setMetricDefinition('highestPrice', 'species', 'max', 'price');
389
- *
390
- * console.log(metrics.getMetric('highestPrice'));
391
- * // -> 5
392
- * ```
393
- * @example
394
- * This example creates a Store, creates a Metrics object, and defines a
395
- * custom Metric to get the lowest value of each `price` Cell, greater than 2.
396
- *
397
- * ```js
398
- * import {createMetrics, createStore} from 'tinybase';
399
- *
400
- * const store = createStore().setTable('species', {
401
- * dog: {price: 5},
402
- * cat: {price: 4},
403
- * worm: {price: 1},
404
- * });
405
- *
406
- * const metrics = createMetrics(store);
407
- * metrics.setMetricDefinition(
408
- * 'lowestPriceOver2',
409
- * 'species',
410
- * (numbers) => Math.min(...numbers.filter((number) => number > 2)),
411
- * 'price',
412
- * );
413
- *
414
- * console.log(metrics.getMetric('lowestPriceOver2'));
415
- * // -> 4
416
- * ```
417
- * @example
418
- * This example also creates a Store, creates a Metrics object, and defines a
419
- * custom Metric to get the lowest value of each `price` Cell, greater than 2.
420
- * However, it also reduces algorithmic complexity with two shortcut
421
- * functions.
422
- *
423
- * ```js
424
- * import {createMetrics, createStore} from 'tinybase';
425
- *
426
- * const store = createStore().setTable('species', {
427
- * dog: {price: 5},
428
- * cat: {price: 4},
429
- * worm: {price: 1},
430
- * });
431
- *
432
- * const metrics = createMetrics(store);
433
- * metrics.setMetricDefinition(
434
- * 'lowestPriceOver2',
435
- * 'species',
436
- * (numbers) => Math.min(...numbers.filter((number) => number > 2)),
437
- * 'price',
438
- * (metric, add) => (add > 2 ? Math.min(metric, add) : metric),
439
- * (metric, remove) => (remove == metric ? undefined : metric),
440
- * (metric, add, remove) =>
441
- * remove == metric
442
- * ? undefined
443
- * : add > 2
444
- * ? Math.min(metric, add)
445
- * : metric,
446
- * );
447
- *
448
- * console.log(metrics.getMetric('lowestPriceOver2'));
449
- * // -> 4
450
- * store.setRow('species', 'fish', {price: 3});
451
- * console.log(metrics.getMetric('lowestPriceOver2'));
452
- * // -> 3
453
- * ```
454
- * @example
455
- * This example creates a Store, creates a Metrics object, and defines a
456
- * custom Metric to get the average value of a discounted price.
457
- *
458
- * ```js
459
- * import {createMetrics, createStore} from 'tinybase';
460
- *
461
- * const store = createStore().setTable('species', {
462
- * dog: {price: 5, discount: 0.3},
463
- * cat: {price: 4, discount: 0.2},
464
- * worm: {price: 1, discount: 0.2},
465
- * });
466
- *
467
- * const metrics = createMetrics(store);
468
- * metrics.setMetricDefinition(
469
- * 'averageDiscountedPrice',
470
- * 'species',
471
- * 'avg',
472
- * (getCell) => getCell('price') * (1 - getCell('discount')),
473
- * );
474
- *
475
- * console.log(metrics.getMetric('averageDiscountedPrice'));
476
- * // -> 2.5
477
- * ```
478
- * @category Configuration
479
- * @since v1.0.0
480
- */
481
- setMetricDefinition<TableId extends TableIdFromSchema<Schemas[0]>>(
482
- metricId: Id,
483
- tableId: TableId,
484
- aggregate?: 'sum' | 'avg' | 'min' | 'max' | MetricAggregate,
485
- getNumber?:
486
- | CellIdFromSchema<Schemas[0], TableId>
487
- | ((getCell: GetCell<Schemas[0], TableId>, rowId: Id) => number),
488
- aggregateAdd?: MetricAggregateAdd,
489
- aggregateRemove?: MetricAggregateRemove,
490
- aggregateReplace?: MetricAggregateReplace,
491
- ): Metrics<Schemas>;
492
-
493
- /**
494
- * The delMetricDefinition method removes an existing Metric definition.
495
- * @param metricId The Id of the Metric to remove.
496
- * @returns A reference to the Metrics object.
497
- * @example
498
- * This example creates a Store, creates a Metrics object, defines a simple
499
- * Metric, and then removes it.
500
- *
501
- * This has schema-based typing. The following is a simplified representation:
502
- *
503
- * ```ts override
504
- * delMetricDefinition(metricId: Id): Metrics;
505
- * ```
506
- *
507
- * ```js
508
- * import {createMetrics, createStore} from 'tinybase';
509
- *
510
- * const store = createStore().setTable('species', {
511
- * dog: {price: 5},
512
- * cat: {price: 4},
513
- * worm: {price: 1},
514
- * });
515
- *
516
- * const metrics = createMetrics(store);
517
- * metrics.setMetricDefinition('speciesCount', 'species');
518
- * console.log(metrics.getMetricIds());
519
- * // -> ['speciesCount']
520
- *
521
- * metrics.delMetricDefinition('speciesCount');
522
- * console.log(metrics.getMetricIds());
523
- * // -> []
524
- * ```
525
- * @category Configuration
526
- * @since v1.0.0
527
- */
528
- delMetricDefinition(metricId: Id): Metrics<Schemas>;
529
-
530
- /**
531
- * The getStore method returns a reference to the underlying Store that is
532
- * backing this Metrics object.
533
- * @returns A reference to the Store.
534
- * @example
535
- * This example creates a Metrics object against a newly-created Store and
536
- * then gets its reference in order to update its data.
537
- *
538
- * This has schema-based typing. The following is a simplified representation:
539
- *
540
- * ```ts override
541
- * getStore(): Store;
542
- * ```
543
- *
544
- * ```js
545
- * import {createMetrics, createStore} from 'tinybase';
546
- *
547
- * const metrics = createMetrics(createStore());
548
- * metrics.setMetricDefinition('speciesCount', 'species');
549
- * metrics.getStore().setCell('species', 'dog', 'price', 5);
550
- * console.log(metrics.getMetric('speciesCount'));
551
- * // -> 1
552
- * ```
553
- * @category Getter
554
- * @since v1.0.0
555
- */
556
- getStore(): Store<Schemas>;
557
-
558
- /**
559
- * The getMetricIds method returns an array of the Metric Ids registered with
560
- * this Metrics object.
561
- * @returns An array of Ids.
562
- * @example
563
- * This example creates a Metrics object with two definitions, and then gets
564
- * the Ids of the definitions.
565
- *
566
- * ```js
567
- * import {createMetrics, createStore} from 'tinybase';
568
- *
569
- * const metrics = createMetrics(createStore())
570
- * .setMetricDefinition('speciesCount', 'species')
571
- * .setMetricDefinition('petsCount', 'pets');
572
- *
573
- * console.log(metrics.getMetricIds());
574
- * // -> ['speciesCount', 'petsCount']
575
- * ```
576
- * @category Getter
577
- * @since v1.0.0
578
- */
579
- getMetricIds(): Ids;
580
-
581
- /**
582
- * The forEachMetric method takes a function that it will then call for each
583
- * Metric in the Metrics object.
584
- *
585
- * This method is useful for iterating over all the Metrics in a functional
586
- * style. The `metricCallback` parameter is a MetricCallback function that
587
- * will be called with the Id of each Metric and its value.
588
- * @param metricCallback The function that should be called for every Metric.
589
- * @example
590
- * This example iterates over each Metric in a Metrics object.
591
- *
592
- * ```js
593
- * import {createMetrics, createStore} from 'tinybase';
594
- *
595
- * const store = createStore().setTable('species', {
596
- * dog: {price: 5},
597
- * cat: {price: 4},
598
- * worm: {price: 1},
599
- * });
600
- * const metrics = createMetrics(store)
601
- * .setMetricDefinition('highestPrice', 'species', 'max', 'price')
602
- * .setMetricDefinition('lowestPrice', 'species', 'min', 'price');
603
- *
604
- * metrics.forEachMetric((metricId, metric) => {
605
- * console.log([metricId, metric]);
606
- * });
607
- * // -> ['highestPrice', 5]
608
- * // -> ['lowestPrice', 1]
609
- * ```
610
- * @category Iterator
611
- * @since v1.0.0
612
- */
613
- forEachMetric(metricCallback: MetricCallback): void;
614
-
615
- /**
616
- * The hasMetric method returns a boolean indicating whether a given Metric
617
- * exists in the Metrics object, and has a value.
618
- * @param metricId The Id of a possible Metric in the Metrics object.
619
- * @returns Whether a Metric with that Id exists.
620
- * @example
621
- * This example shows two simple Metric existence checks.
622
- *
623
- * ```js
624
- * import {createMetrics, createStore} from 'tinybase';
625
- *
626
- * const store = createStore();
627
- * const metrics = createMetrics(store);
628
- * metrics.setMetricDefinition('highestPrice', 'species', 'max', 'price');
629
- *
630
- * console.log(metrics.hasMetric('lowestPrice'));
631
- * // -> false
632
- * console.log(metrics.hasMetric('highestPrice'));
633
- * // -> false
634
- * store.setTable('species', {dog: {price: 5}, cat: {price: 4}});
635
- * console.log(metrics.hasMetric('highestPrice'));
636
- * // -> true
637
- * ```
638
- * @category Getter
639
- * @since v1.0.0
640
- */
641
- hasMetric(metricId: Id): boolean;
642
-
643
- /**
644
- * The getTableId method returns the Id of the underlying Table that is
645
- * backing a Metric.
646
- *
647
- * This has schema-based typing. The following is a simplified representation:
648
- *
649
- * ```ts override
650
- * getTableId(metricId: Id): Id | undefined;
651
- * ```
652
- *
653
- * If the Metric Id is invalid, the method returns `undefined`.
654
- * @param metricId The Id of a Metric.
655
- * @returns The Id of the Table backing the Metric, or `undefined`.
656
- * @example
657
- * This example creates a Metrics object, a single Metric definition, and then
658
- * queries it (and a non-existent definition) to get the underlying Table Id.
659
- *
660
- * ```js
661
- * import {createMetrics, createStore} from 'tinybase';
662
- *
663
- * const metrics = createMetrics(createStore());
664
- * metrics.setMetricDefinition('speciesCount', 'species');
665
- *
666
- * console.log(metrics.getTableId('speciesCount'));
667
- * // -> 'species'
668
- * console.log(metrics.getTableId('petsCount'));
669
- * // -> undefined
670
- * ```
671
- * @category Getter
672
- * @since v1.0.0
673
- */
674
- getTableId<TableId extends TableIdFromSchema<Schemas[0]>>(
675
- metricId: Id,
676
- ): TableId | undefined;
677
-
678
- /**
679
- * The getMetric method gets the current value of a Metric.
680
- *
681
- * If the identified Metric does not exist (or if the definition references a
682
- * Table or Cell value that does not exist) then `undefined` is returned.
683
- * @param metricId The Id of the Metric.
684
- * @returns The numeric value of the Metric, or `undefined`.
685
- * @example
686
- * This example creates a Store, creates a Metrics object, and defines a
687
- * simple Metric to average the price values in the Table. It then uses
688
- * getMetric to access its value (and also the value of a Metric that has not
689
- * been defined).
690
- *
691
- * ```js
692
- * import {createMetrics, createStore} from 'tinybase';
693
- *
694
- * const store = createStore().setTable('species', {
695
- * dog: {price: 5},
696
- * cat: {price: 4},
697
- * worm: {price: 1},
698
- * });
699
- *
700
- * const metrics = createMetrics(store);
701
- * metrics.setMetricDefinition('highestPrice', 'species', 'max', 'price');
702
- *
703
- * console.log(metrics.getMetric('highestPrice'));
704
- * // -> 5
705
- * console.log(metrics.getMetric('lowestPrice'));
706
- * // -> undefined
707
- * ```
708
- * @category Getter
709
- * @since v1.0.0
710
- */
711
- getMetric(metricId: Id): Metric | undefined;
712
-
713
- /**
714
- * The addMetricIdsListener method registers a listener function with the
715
- * Metrics object that will be called whenever a Metric definition is added or
716
- * removed.
717
- *
718
- * This has schema-based typing. The following is a simplified representation:
719
- *
720
- * ```ts override
721
- * addMetricIdsListener(listener: MetricIdsListener): Id;
722
- * ```
723
- *
724
- * The provided listener is a MetricIdsListener function, and will be called
725
- * with a reference to the Metrics object.
726
- * @param listener The function that will be called whenever a Metric
727
- * definition is added or removed.
728
- * @example
729
- * This example creates a Store, a Metrics object, and then registers a
730
- * listener that responds to the addition and the removal of a Metric
731
- * definition.
732
- *
733
- * ```js
734
- * import {createMetrics, createStore} from 'tinybase';
735
- *
736
- * const store = createStore().setTable('species', {
737
- * dog: {price: 5},
738
- * cat: {price: 4},
739
- * worm: {price: 1},
740
- * });
741
- *
742
- * const metrics = createMetrics(store);
743
- * const listenerId = metrics.addMetricIdsListener((metrics) => {
744
- * console.log(metrics.getMetricIds());
745
- * });
746
- *
747
- * metrics.setMetricDefinition('highestPrice', 'species', 'max', 'price');
748
- * // -> ['highestPrice']
749
- * metrics.delMetricDefinition('highestPrice');
750
- * // -> []
751
- *
752
- * metrics.delListener(listenerId);
753
- * ```
754
- * @category Listener
755
- * @since v4.1.0
756
- */
757
- addMetricIdsListener(listener: MetricIdsListener<Schemas>): Id;
758
-
759
- /**
760
- * The addMetricListener method registers a listener function with the Metrics
761
- * object that will be called whenever the value of a specified Metric
762
- * changes.
763
- *
764
- * This has schema-based typing. The following is a simplified representation:
765
- *
766
- * ```ts override
767
- * addMetricListener(metricId: IdOrNull, listener: MetricListener): Id;
768
- * ```
769
- *
770
- * You can either listen to a single Metric (by specifying the Metric Id as
771
- * the method's first parameter), or changes to any Metric (by providing a
772
- * `null` wildcard).
773
- *
774
- * The provided listener is a MetricListener function, and will be called with
775
- * a reference to the Metrics object, the Id of the Metric that changed, the
776
- * new Metric value, and the old Metric value.
777
- * @param metricId The Id of the Metric to listen to, or `null` as a wildcard.
778
- * @param listener The function that will be called whenever the Metric
779
- * changes.
780
- * @returns A unique Id for the listener that can later be used to remove it.
781
- * @example
782
- * This example creates a Store, a Metrics object, and then registers a
783
- * listener that responds to any changes to a specific Metric.
784
- *
785
- * ```js
786
- * import {createMetrics, createStore} from 'tinybase';
787
- *
788
- * const store = createStore().setTable('species', {
789
- * dog: {price: 5},
790
- * cat: {price: 4},
791
- * worm: {price: 1},
792
- * });
793
- *
794
- * const metrics = createMetrics(store);
795
- * metrics.setMetricDefinition('highestPrice', 'species', 'max', 'price');
796
- *
797
- * const listenerId = metrics.addMetricListener(
798
- * 'highestPrice',
799
- * (metrics, _metricId, newMetric, oldMetric) => {
800
- * console.log('highestPrice metric changed');
801
- * console.log([oldMetric, newMetric]);
802
- * },
803
- * );
804
- *
805
- * store.setCell('species', 'horse', 'price', 20);
806
- * // -> 'highestPrice metric changed'
807
- * // -> [5, 20]
808
- *
809
- * metrics.delListener(listenerId);
810
- * ```
811
- * @example
812
- * This example creates a Store, a Metrics object, and then registers a
813
- * listener that responds to any changes to any Metric.
814
- *
815
- * ```js
816
- * import {createMetrics, createStore} from 'tinybase';
817
- *
818
- * const store = createStore().setTable('species', {
819
- * dog: {price: 5},
820
- * cat: {price: 4},
821
- * worm: {price: 1},
822
- * });
823
- *
824
- * const metrics = createMetrics(store)
825
- * .setMetricDefinition('highestPrice', 'species', 'max', 'price')
826
- * .setMetricDefinition('speciesCount', 'species');
827
- *
828
- * const listenerId = metrics.addMetricListener(
829
- * null,
830
- * (metrics, metricId, newMetric, oldMetric) => {
831
- * console.log(`${metricId} metric changed`);
832
- * console.log([oldMetric, newMetric]);
833
- * },
834
- * );
835
- *
836
- * store.setCell('species', 'horse', 'price', 20);
837
- * // -> 'highestPrice metric changed'
838
- * // -> [5, 20]
839
- * // -> 'speciesCount metric changed'
840
- * // -> [3, 4]
841
- *
842
- * metrics.delListener(listenerId);
843
- * ```
844
- * @category Listener
845
- * @since v1.0.0
846
- */
847
- addMetricListener(metricId: IdOrNull, listener: MetricListener<Schemas>): Id;
848
-
849
- /**
850
- * The delListener method removes a listener that was previously added to the
851
- * Metrics object.
852
- *
853
- * This has schema-based typing. The following is a simplified representation:
854
- *
855
- * ```ts override
856
- * delListener(listenerId: Id): Metrics;
857
- * ```
858
- *
859
- * Use the Id returned by the addMetricListener method. Note that the Metrics
860
- * object may re-use this Id for future listeners added to it.
861
- * @param listenerId The Id of the listener to remove.
862
- * @returns A reference to the Metrics object.
863
- * @example
864
- * This example creates a Store, a Metrics object, registers a listener, and
865
- * then removes it.
866
- *
867
- * ```js
868
- * import {createMetrics, createStore} from 'tinybase';
869
- *
870
- * const store = createStore().setTable('species', {
871
- * dog: {price: 5},
872
- * cat: {price: 4},
873
- * worm: {price: 1},
874
- * });
875
- *
876
- * const metrics = createMetrics(store);
877
- * metrics.setMetricDefinition('highestPrice', 'species', 'max', 'price');
878
- *
879
- * const listenerId = metrics.addMetricListener('highestPrice', () => {
880
- * console.log('highestPrice metric changed');
881
- * });
882
- *
883
- * store.setCell('species', 'horse', 'price', 20);
884
- * // -> 'highestPrice metric changed'
885
- *
886
- * metrics.delListener(listenerId);
887
- *
888
- * store.setCell('species', 'giraffe', 'price', 50);
889
- * // -> undefined
890
- * // The listener is not called.
891
- * ```
892
- * @category Listener
893
- * @since v1.0.0
894
- */
895
- delListener(listenerId: Id): Metrics<Schemas>;
896
-
897
- /**
898
- * The destroy method should be called when this Metrics object is no longer
899
- * used.
900
- *
901
- * This guarantees that all of the listeners that the object registered with
902
- * the underlying Store are removed and it can be correctly garbage collected.
903
- * @example
904
- * This example creates a Store, adds a Metrics object with a definition (that
905
- * registers a RowListener with the underlying Store), and then destroys it
906
- * again, removing the listener.
907
- *
908
- * ```js
909
- * import {createMetrics, createStore} from 'tinybase';
910
- *
911
- * const store = createStore().setTable('species', {
912
- * dog: {price: 5},
913
- * cat: {price: 4},
914
- * worm: {price: 1},
915
- * });
916
- *
917
- * const metrics = createMetrics(store);
918
- * metrics.setMetricDefinition('speciesCount', 'species');
919
- * console.log(store.getListenerStats().row);
920
- * // -> 1
921
- *
922
- * metrics.destroy();
923
- *
924
- * console.log(store.getListenerStats().row);
925
- * // -> 0
926
- * ```
927
- * @category Lifecycle
928
- * @since v1.0.0
929
- */
930
- destroy(): void;
931
-
932
- /**
933
- * The getListenerStats method provides a set of statistics about the
934
- * listeners registered with the Metrics object, and is used for debugging
935
- * purposes.
936
- *
937
- * The method is intended to be used during development to ensure your
938
- * application is not leaking listener registrations, for example.
939
- * @returns A MetricsListenerStats object containing Metrics listener
940
- * statistics.
941
- * @example
942
- * This example gets the listener statistics of a Metrics object.
943
- *
944
- * ```js
945
- * import {createMetrics, createStore} from 'tinybase';
946
- *
947
- * const store = createStore();
948
- * const metrics = createMetrics(store);
949
- * metrics.addMetricListener(null, () => console.log('Metric changed'));
950
- *
951
- * console.log(metrics.getListenerStats());
952
- * // -> {metric: 1}
953
- * ```
954
- * @category Development
955
- * @since v1.0.0
956
- */
957
- getListenerStats(): MetricsListenerStats;
958
- }
959
-
960
- /**
961
- * The createMetrics function creates a Metrics object, and is the main entry
962
- * point into the metrics module.
963
- *
964
- * This has schema-based typing. The following is a simplified representation:
965
- *
966
- * ```ts override
967
- * createMetrics(store: Store): Metrics;
968
- * ```
969
- *
970
- * A given Store can only have one Metrics object associated with it. If you
971
- * call this function twice on the same Store, your second call will return a
972
- * reference to the Metrics object created by the first.
973
- * @param store The Store for which to register Metric definitions.
974
- * @returns A reference to the new Metrics object.
975
- * @example
976
- * This example creates a Metrics object.
977
- *
978
- * ```js
979
- * import {createMetrics, createStore} from 'tinybase';
980
- *
981
- * const store = createStore();
982
- * const metrics = createMetrics(store);
983
- * console.log(metrics.getMetricIds());
984
- * // -> []
985
- * ```
986
- * @example
987
- * This example creates a Metrics object, and calls the method a second time
988
- * for the same Store to return the same object.
989
- *
990
- * ```js
991
- * import {createMetrics, createStore} from 'tinybase';
992
- *
993
- * const store = createStore();
994
- * const metrics1 = createMetrics(store);
995
- * const metrics2 = createMetrics(store);
996
- * console.log(metrics1 === metrics2);
997
- * // -> true
998
- * ```
999
- * @category Creation
1000
- * @since v1.0.0
1001
- */
1002
- export function createMetrics<Schemas extends OptionalSchemas>(
1003
- store: Store<Schemas>,
1004
- ): Metrics<Schemas>;