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