tinybase 7.2.0-beta.2 → 7.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/@types/queries/index.d.ts +333 -4
  2. package/@types/queries/with-schemas/index.d.ts +359 -4
  3. package/@types/ui-react/index.d.ts +477 -8
  4. package/@types/ui-react/with-schemas/index.d.ts +519 -8
  5. package/agents.md +34 -0
  6. package/index.js +100 -18
  7. package/min/index.js +1 -1
  8. package/min/index.js.gz +0 -0
  9. package/min/omni/index.js +1 -1
  10. package/min/omni/index.js.gz +0 -0
  11. package/min/omni/with-schemas/index.js +1 -1
  12. package/min/omni/with-schemas/index.js.gz +0 -0
  13. package/min/queries/index.js +1 -1
  14. package/min/queries/index.js.gz +0 -0
  15. package/min/queries/with-schemas/index.js +1 -1
  16. package/min/queries/with-schemas/index.js.gz +0 -0
  17. package/min/ui-react/index.js +1 -1
  18. package/min/ui-react/index.js.gz +0 -0
  19. package/min/ui-react/with-schemas/index.js +1 -1
  20. package/min/ui-react/with-schemas/index.js.gz +0 -0
  21. package/min/ui-react-dom/index.js +1 -1
  22. package/min/ui-react-dom/index.js.gz +0 -0
  23. package/min/ui-react-dom/with-schemas/index.js +1 -1
  24. package/min/ui-react-dom/with-schemas/index.js.gz +0 -0
  25. package/min/ui-react-inspector/index.js +1 -1
  26. package/min/ui-react-inspector/index.js.gz +0 -0
  27. package/min/ui-react-inspector/with-schemas/index.js +1 -1
  28. package/min/ui-react-inspector/with-schemas/index.js.gz +0 -0
  29. package/min/with-schemas/index.js +1 -1
  30. package/min/with-schemas/index.js.gz +0 -0
  31. package/omni/index.js +173 -48
  32. package/omni/with-schemas/index.js +173 -48
  33. package/package.json +3 -3
  34. package/queries/index.js +120 -18
  35. package/queries/with-schemas/index.js +120 -18
  36. package/readme.md +13 -13
  37. package/releases.md +40 -40
  38. package/ui-react/index.js +98 -30
  39. package/ui-react/with-schemas/index.js +98 -30
  40. package/ui-react-dom/index.js +38 -15
  41. package/ui-react-dom/with-schemas/index.js +38 -15
  42. package/ui-react-inspector/index.js +40 -17
  43. package/ui-react-inspector/with-schemas/index.js +40 -17
  44. package/with-schemas/index.js +100 -18
@@ -34,8 +34,9 @@ import type {
34
34
  * The ParamValue type describes a single param value that can be used in a
35
35
  * parameterized query.
36
36
  *
37
- * A ParamValue is similar to a Cell, but params cannot be `undefined`. They
38
- * must be a JavaScript string, number, boolean, or null.
37
+ * A ParamValue is a JavaScript string, number, boolean, null - or an array of
38
+ * strings, numbers, or booleans. Arrays are useful for filtering by multiple
39
+ * values, such as checking if a cell value is included in a list of options.
39
40
  * @example
40
41
  * ```js
41
42
  * import type {ParamValue} from 'tinybase';
@@ -44,11 +45,21 @@ import type {
44
45
  * export const paramValue2: ParamValue = 5;
45
46
  * export const paramValue3: ParamValue = true;
46
47
  * export const paramValue4: ParamValue = null;
48
+ * export const paramValue5: ParamValue = ['Ford', 'Toyota', 'Honda'];
49
+ * export const paramValue6: ParamValue = [1970, 1975, 1980];
50
+ * export const paramValue7: ParamValue = [true, false];
47
51
  * ```
48
52
  * @category Params
49
53
  * @since v7.2.0
50
54
  */
51
- export type ParamValue = string | number | boolean | null;
55
+ export type ParamValue =
56
+ | string
57
+ | number
58
+ | boolean
59
+ | null
60
+ | string[]
61
+ | number[]
62
+ | boolean[];
52
63
 
53
64
  /**
54
65
  * The ParamValues type describes an object of param values, keyed by param Id,
@@ -331,6 +342,70 @@ export type QueryIdsListener<Schemas extends OptionalSchemas> = (
331
342
  queries: Queries<Schemas>,
332
343
  ) => void;
333
344
 
345
+ /**
346
+ * The ParamValuesListener type describes a function that is used to listen to
347
+ * changes to the param values of a parameterized query.
348
+ *
349
+ * This has schema-based typing. The following is a simplified representation:
350
+ *
351
+ * ```ts override
352
+ * (
353
+ * queries: Queries,
354
+ * queryId: Id,
355
+ * paramValues: ParamValues,
356
+ * ) => void;
357
+ * ```
358
+ *
359
+ * A ParamValuesListener is provided when using the addParamValuesListener
360
+ * method. See that method for specific examples.
361
+ *
362
+ * When called, a ParamValuesListener is given a reference to the Queries
363
+ * object, and the Id of the query whose param values changed.
364
+ * @param queries A reference to the Queries object that changed.
365
+ * @param queryId The Id of the query whose param values changed.
366
+ * @category Listener
367
+ * @since v7.2.0
368
+ */
369
+ export type ParamValuesListener<Schemas extends OptionalSchemas> = (
370
+ queries: Queries<Schemas>,
371
+ queryId: Id,
372
+ paramValues: ParamValues,
373
+ ) => void;
374
+
375
+ /**
376
+ * The ParamValueListener type describes a function that is used to listen to
377
+ * changes to a single param value of a parameterized query.
378
+ *
379
+ * This has schema-based typing. The following is a simplified representation:
380
+ *
381
+ * ```ts override
382
+ * (
383
+ * queries: Queries,
384
+ * queryId: Id,
385
+ * paramId: Id,
386
+ * paramValue: ParamValue,
387
+ * ) => void;
388
+ * ```
389
+ *
390
+ * A ParamValueListener is provided when using the addParamValueListener method.
391
+ * See that method for specific examples.
392
+ *
393
+ * When called, a ParamValueListener is given a reference to the Queries object,
394
+ * the Id of the query whose param value changed, and the Id of the param whose
395
+ * value changed.
396
+ * @param queries A reference to the Queries object that changed.
397
+ * @param queryId The Id of the query whose param value changed.
398
+ * @param paramId The Id of the param whose value changed.
399
+ * @category Listener
400
+ * @since v7.2.0
401
+ */
402
+ export type ParamValueListener<Schemas extends OptionalSchemas> = (
403
+ queries: Queries<Schemas>,
404
+ queryId: Id,
405
+ paramId: Id,
406
+ paramValue: ParamValue,
407
+ ) => void;
408
+
334
409
  /**
335
410
  * The ResultTableListener type describes a function that is used to listen to
336
411
  * changes to a query's ResultTable.
@@ -824,7 +899,8 @@ export type GetTableCell<
824
899
  *
825
900
  * A Param function is provided when setting parameterized query definitions,
826
901
  * and allows you to reference dynamic param values that can be updated without
827
- * redefining the entire query.
902
+ * redefining the entire query. Param values can be primitives (string, number,
903
+ * boolean, null) or arrays of those types.
828
904
  * @param paramId The Id of the param to fetch the value for.
829
905
  * @returns A Cell value or `undefined` if the param is not set.
830
906
  * @example
@@ -857,6 +933,40 @@ export type GetTableCell<
857
933
  * console.log(queries.getResultTable('query'));
858
934
  * // -> {felix: {color: 'black'}}
859
935
  * ```
936
+ * @example
937
+ * This example shows a query that uses an array param to filter by multiple
938
+ * values.
939
+ *
940
+ * ```js
941
+ * import {createQueries, createStore} from 'tinybase';
942
+ *
943
+ * const store = createStore().setTable('pets', {
944
+ * fido: {species: 'dog', color: 'brown'},
945
+ * felix: {species: 'cat', color: 'black'},
946
+ * cujo: {species: 'dog', color: 'black'},
947
+ * rex: {species: 'dog', color: 'gold'},
948
+ * });
949
+ *
950
+ * const queries = createQueries(store);
951
+ * queries.setQueryDefinition(
952
+ * 'query',
953
+ * 'pets',
954
+ * ({select, where, param}) => {
955
+ * select('species');
956
+ * where((getTableCell) =>
957
+ * (param('colors') as string[])?.includes(getTableCell('color')),
958
+ * );
959
+ * },
960
+ * {colors: ['brown', 'gold']},
961
+ * );
962
+ *
963
+ * console.log(queries.getResultTable('query'));
964
+ * // -> {fido: {species: 'dog'}, rex: {species: 'dog'}}
965
+ *
966
+ * queries.setParamValue('query', 'colors', ['black']);
967
+ * console.log(queries.getResultTable('query'));
968
+ * // -> {felix: {species: 'cat'}, cujo: {species: 'dog'}}
969
+ * ```
860
970
  * @category Definition
861
971
  * @since v7.2.0
862
972
  */
@@ -2123,6 +2233,91 @@ export interface Queries<in out Schemas extends OptionalSchemas> {
2123
2233
  */
2124
2234
  delQueryDefinition(queryId: Id): Queries<Schemas>;
2125
2235
 
2236
+ /**
2237
+ * The getParamValues method returns all the param values currently set for a
2238
+ * parameterized query.
2239
+ * @param queryId The Id of the query to get the params for.
2240
+ * @returns An object containing all param Ids and their values, or an empty
2241
+ * object if the query doesn't exist.
2242
+ * @example
2243
+ * This example creates a parameterized query and retrieves its param values.
2244
+ *
2245
+ * ```js
2246
+ * import {createQueries, createStore} from 'tinybase';
2247
+ *
2248
+ * const store = createStore().setTable('pets', {
2249
+ * fido: {species: 'dog', color: 'brown'},
2250
+ * felix: {species: 'cat', color: 'black'},
2251
+ * cujo: {species: 'dog', color: 'black'},
2252
+ * });
2253
+ *
2254
+ * const queries = createQueries(store);
2255
+ * queries.setQueryDefinition(
2256
+ * 'query',
2257
+ * 'pets',
2258
+ * ({select, where, param}) => {
2259
+ * select('color');
2260
+ * where('species', param('species'));
2261
+ * where((getTableCell) => getTableCell('age') >= param('minAge'));
2262
+ * },
2263
+ * {species: 'dog', minAge: 5},
2264
+ * );
2265
+ *
2266
+ * console.log(queries.getParamValues('query'));
2267
+ * // -> {species: 'dog', minAge: 5}
2268
+ *
2269
+ * queries.setParamValue('query', 'species', 'cat');
2270
+ * console.log(queries.getParamValues('query'));
2271
+ * // -> {species: 'cat', minAge: 5}
2272
+ * ```
2273
+ * @category Getter
2274
+ * @since v7.2.0
2275
+ */
2276
+ getParamValues(queryId: Id): ParamValues;
2277
+
2278
+ /**
2279
+ * The getParamValue method returns a single param value currently set for a
2280
+ * parameterized query.
2281
+ * @param queryId The Id of the query to get the param for.
2282
+ * @param paramId The Id of the param to get.
2283
+ * @returns The value of the param, or `undefined` if the query or param
2284
+ * doesn't exist.
2285
+ * @example
2286
+ * This example creates a parameterized query and retrieves one of its param
2287
+ * values.
2288
+ *
2289
+ * ```js
2290
+ * import {createQueries, createStore} from 'tinybase';
2291
+ *
2292
+ * const store = createStore().setTable('pets', {
2293
+ * fido: {species: 'dog', color: 'brown'},
2294
+ * felix: {species: 'cat', color: 'black'},
2295
+ * cujo: {species: 'dog', color: 'black'},
2296
+ * });
2297
+ *
2298
+ * const queries = createQueries(store);
2299
+ * queries.setQueryDefinition(
2300
+ * 'query',
2301
+ * 'pets',
2302
+ * ({select, where, param}) => {
2303
+ * select('color');
2304
+ * where('species', param('species'));
2305
+ * },
2306
+ * {species: 'dog'},
2307
+ * );
2308
+ *
2309
+ * console.log(queries.getParamValue('query', 'species'));
2310
+ * // -> 'dog'
2311
+ *
2312
+ * queries.setParamValue('query', 'species', 'cat');
2313
+ * console.log(queries.getParamValue('query', 'species'));
2314
+ * // -> 'cat'
2315
+ * ```
2316
+ * @category Getter
2317
+ * @since v7.2.0
2318
+ */
2319
+ getParamValue(queryId: Id, paramId: Id): ParamValue | undefined;
2320
+
2126
2321
  /**
2127
2322
  * The setParamValues method sets multiple param values for a parameterized
2128
2323
  * query at once, causing the query to re-evaluate with the new param values.
@@ -3079,6 +3274,166 @@ export interface Queries<in out Schemas extends OptionalSchemas> {
3079
3274
  */
3080
3275
  addQueryIdsListener(listener: QueryIdsListener<Schemas>): Id;
3081
3276
 
3277
+ /**
3278
+ * The addParamValuesListener method registers a listener function with the
3279
+ * Queries object that will be called whenever the param values of a query
3280
+ * change.
3281
+ *
3282
+ * This has schema-based typing. The following is a simplified representation:
3283
+ *
3284
+ * ```ts override
3285
+ * addParamValuesListener(queryId: IdOrNull, listener: ParamValuesListener): Id;
3286
+ * ```
3287
+ *
3288
+ * You can either listen to a single query (by specifying a query Id as the
3289
+ * method's first parameter) or changes to any query (by providing a `null`
3290
+ * wildcard).
3291
+ *
3292
+ * The provided listener is a ParamValuesListener function, and will be called
3293
+ * with a reference to the Queries object, the Id of the query whose param
3294
+ * values changed, and an object containing the new param values.
3295
+ * @param queryId The Id of the query to listen to, or `null` as a wildcard.
3296
+ * @param listener The function that will be called whenever the param values
3297
+ * of the query change.
3298
+ * @example
3299
+ * This example registers two listeners that respond to changes to the param
3300
+ * values of a specific query, or any query respectively.
3301
+ *
3302
+ * ```js
3303
+ * import {createQueries, createStore} from 'tinybase';
3304
+ *
3305
+ * const store = createStore().setTable('pets', {
3306
+ * // store data
3307
+ * });
3308
+ *
3309
+ * const queries = createQueries(store).setQueryDefinition(
3310
+ * 'petsByColor',
3311
+ * 'pets',
3312
+ * () => {
3313
+ * // query definition
3314
+ * },
3315
+ * {color: 'white'},
3316
+ * );
3317
+ *
3318
+ * const listenerId1 = queries.addParamValuesListener(
3319
+ * 'petsByColor',
3320
+ * (queries, queryId, paramValues) => {
3321
+ * console.log(
3322
+ * `Params for specific query changed: ${JSON.stringify(paramValues)}`,
3323
+ * );
3324
+ * },
3325
+ * );
3326
+ * const listenerId2 = queries.addParamValuesListener(
3327
+ * null,
3328
+ * (queries, queryId, paramValues) => {
3329
+ * console.log(
3330
+ * `Params for "${queryId}" changed: ${JSON.stringify(paramValues)}`,
3331
+ * );
3332
+ * },
3333
+ * );
3334
+ *
3335
+ * queries.setParamValues('petsByColor', {color: 'black'});
3336
+ * // -> 'Params for specific query changed: {"color":"black"}'
3337
+ * // -> 'Params for "petsByColor" changed: {"color":"black"}'
3338
+ * queries.setParamValues('petsByColor', {color: 'brown'});
3339
+ * // -> 'Params for specific query changed: {"color":"brown"}'
3340
+ * // -> 'Params for "petsByColor" changed: {"color":"brown"}'
3341
+ *
3342
+ * queries.delListener(listenerId1);
3343
+ * queries.delListener(listenerId2);
3344
+ * ```
3345
+ * @category Listener
3346
+ * @since v7.2.0
3347
+ */
3348
+ addParamValuesListener(
3349
+ queryId: IdOrNull,
3350
+ listener: ParamValuesListener<Schemas>,
3351
+ ): Id;
3352
+
3353
+ /**
3354
+ * The addParamValueListener method registers a listener function with the
3355
+ * Queries object that will be called whenever a specific param value of a
3356
+ * query changes.
3357
+ *
3358
+ * This has schema-based typing. The following is a simplified representation:
3359
+ *
3360
+ * ```ts override
3361
+ * addParamValueListener(
3362
+ * queryId: IdOrNull,
3363
+ * paramId: IdOrNull,
3364
+ * listener: ParamValueListener,
3365
+ * ): Id;
3366
+ * ```
3367
+ *
3368
+ * You can either listen to a single query (by specifying a query Id as the
3369
+ * method's first parameter) or changes to any query (by providing a `null`
3370
+ * wildcard). Additionally, you can either listen to a specific param (by
3371
+ * specifying a param Id as the second parameter) or changes to any param (by
3372
+ * providing a `null` wildcard).
3373
+ *
3374
+ * The provided listener is a ParamValueListener function, and will be called
3375
+ * with a reference to the Queries object, the Id of the query whose param
3376
+ * value changed, the Id of the param whose value changed, and the new value
3377
+ * of the param.
3378
+ * @param queryId The Id of the query to listen to, or `null` as a wildcard.
3379
+ * @param paramId The Id of the param to listen to, or `null` as a wildcard.
3380
+ * @param listener The function that will be called whenever the specific
3381
+ * param value of the query changes.
3382
+ * @example
3383
+ * This example registers two listeners that respond to changes to a specific
3384
+ * param value of a specific query, or any param value of any query
3385
+ * respectively.
3386
+ *
3387
+ * ```js
3388
+ * import {createQueries, createStore} from 'tinybase';
3389
+ *
3390
+ * const store = createStore().setTable('pets', {
3391
+ * // store data
3392
+ * });
3393
+ *
3394
+ * const queries = createQueries(store).setQueryDefinition(
3395
+ * 'petsByColor',
3396
+ * 'pets',
3397
+ * () => {
3398
+ * // query definition
3399
+ * },
3400
+ * {color: 'white'},
3401
+ * );
3402
+ *
3403
+ * const listenerId1 = queries.addParamValueListener(
3404
+ * 'petsByColor',
3405
+ * 'color',
3406
+ * (queries, queryId, paramId, value) => {
3407
+ * console.log(`Specific param for specific query changed: "${value}"`);
3408
+ * },
3409
+ * );
3410
+ * const listenerId2 = queries.addParamValueListener(
3411
+ * null,
3412
+ * null,
3413
+ * (queries, queryId, paramId, value) => {
3414
+ * console.log(`Param "${paramId}" for "${queryId}" changed: "${value}"`);
3415
+ * },
3416
+ * );
3417
+ *
3418
+ * queries.setParamValue('petsByColor', 'color', 'black');
3419
+ * // -> 'Specific param for specific query changed: "black"'
3420
+ * // -> 'Param "color" for "petsByColor" changed: "black"'
3421
+ * queries.setParamValue('petsByColor', 'color', 'brown');
3422
+ * // -> 'Specific param for specific query changed: "brown"'
3423
+ * // -> 'Param "color" for "petsByColor" changed: "brown"'
3424
+ *
3425
+ * queries.delListener(listenerId1);
3426
+ * queries.delListener(listenerId2);
3427
+ * ```
3428
+ * @category Listener
3429
+ * @since v7.2.0
3430
+ */
3431
+ addParamValueListener(
3432
+ queryId: IdOrNull,
3433
+ paramId: IdOrNull,
3434
+ listener: ParamValueListener<Schemas>,
3435
+ ): Id;
3436
+
3082
3437
  /**
3083
3438
  * The addResultTableListener method registers a listener function with the
3084
3439
  * Queries object that will be called whenever data in a ResultTable changes.