Skip to content

Commit 5255bd8

Browse files
committed
[params] Array params
1 parent b1a6520 commit 5255bd8

File tree

4 files changed

+165
-8
lines changed

4 files changed

+165
-8
lines changed

src/@types/queries/docs.js

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
* The ParamValue type describes a single param value that can be used in a
1616
* parameterized query.
1717
*
18-
* A ParamValue is similar to a Cell, but params cannot be `undefined`. They
19-
* must be a JavaScript string, number, boolean, or null.
18+
* A ParamValue is a JavaScript string, number, boolean, null - or an array of
19+
* strings, numbers, or booleans. Arrays are useful for filtering by multiple
20+
* values, such as checking if a cell value is included in a list of options.
2021
* @example
2122
* ```js
2223
* import type {ParamValue} from 'tinybase';
@@ -25,6 +26,9 @@
2526
* export const paramValue2: ParamValue = 5;
2627
* export const paramValue3: ParamValue = true;
2728
* export const paramValue4: ParamValue = null;
29+
* export const paramValue5: ParamValue = ['Ford', 'Toyota', 'Honda'];
30+
* export const paramValue6: ParamValue = [1970, 1975, 1980];
31+
* export const paramValue7: ParamValue = [true, false];
2832
* ```
2933
* @category Params
3034
* @since v7.2.0
@@ -597,7 +601,8 @@
597601
*
598602
* A Param function is provided when setting parameterized query definitions,
599603
* and allows you to reference dynamic param values that can be updated without
600-
* redefining the entire query.
604+
* redefining the entire query. Param values can be primitives (string, number,
605+
* boolean, null) or arrays of those types.
601606
* @param paramId The Id of the param to fetch the value for.
602607
* @returns A Cell value or `undefined` if the param is not set.
603608
* @example
@@ -630,6 +635,40 @@
630635
* console.log(queries.getResultTable('query'));
631636
* // -> {felix: {color: 'black'}}
632637
* ```
638+
* @example
639+
* This example shows a query that uses an array param to filter by multiple
640+
* values.
641+
*
642+
* ```js
643+
* import {createQueries, createStore} from 'tinybase';
644+
*
645+
* const store = createStore().setTable('pets', {
646+
* fido: {species: 'dog', color: 'brown'},
647+
* felix: {species: 'cat', color: 'black'},
648+
* cujo: {species: 'dog', color: 'black'},
649+
* rex: {species: 'dog', color: 'gold'},
650+
* });
651+
*
652+
* const queries = createQueries(store);
653+
* queries.setQueryDefinition(
654+
* 'query',
655+
* 'pets',
656+
* ({select, where, param}) => {
657+
* select('species');
658+
* where((getTableCell) =>
659+
* (param('colors') as string[])?.includes(getTableCell('color')),
660+
* );
661+
* },
662+
* {colors: ['brown', 'gold']},
663+
* );
664+
*
665+
* console.log(queries.getResultTable('query'));
666+
* // -> {fido: {species: 'dog'}, rex: {species: 'dog'}}
667+
*
668+
* queries.setParamValue('query', 'colors', ['black']);
669+
* console.log(queries.getResultTable('query'));
670+
* // -> {felix: {species: 'cat'}, cujo: {species: 'dog'}}
671+
* ```
633672
* @category Definition
634673
* @since v7.2.0
635674
*/

src/@types/queries/index.d.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,14 @@ import type {
99
} from '../store/index.d.ts';
1010

1111
/// ParamValue
12-
export type ParamValue = string | number | boolean | null;
12+
export type ParamValue =
13+
| string
14+
| number
15+
| boolean
16+
| null
17+
| string[]
18+
| number[]
19+
| boolean[];
1320

1421
/// ParamValues
1522
export type ParamValues = {[paramId: Id]: ParamValue};

src/@types/queries/with-schemas/index.d.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@ import type {
2020
} from '../../store/with-schemas/index.d.ts';
2121

2222
/// ParamValue
23-
export type ParamValue = string | number | boolean | null;
23+
export type ParamValue =
24+
| string
25+
| number
26+
| boolean
27+
| null
28+
| string[]
29+
| number[]
30+
| boolean[];
2431

2532
/// ParamValues
2633
export type ParamValues = {[paramId: Id]: ParamValue};

test/unit/core/other/queries.test.ts

Lines changed: 107 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {beforeEach, describe, expect, test, vi} from 'vitest';
22

3-
import type {Id, Queries, Store} from 'tinybase';
3+
import type {Cell, Id, Queries, Store} from 'tinybase';
44
import {createQueries, createStore} from 'tinybase';
55
import {expectChanges, expectNoChanges} from '../../common/expect.ts';
66
import {createQueriesListener} from '../../common/listeners.ts';
@@ -4551,7 +4551,7 @@ describe('Parameterized', () => {
45514551
't1',
45524552
({select, where, param}) => {
45534553
select('c2');
4554-
where('c2', param('p2') ?? '');
4554+
where('c2', param('p2') as Cell);
45554555
},
45564556
{p2: 'odd'},
45574557
);
@@ -4663,6 +4663,110 @@ describe('Parameterized', () => {
46634663

46644664
expect(queries.getResultTable('q1')).toEqual({r1: {c4: true}});
46654665
});
4666+
4667+
test('param with string array value', () => {
4668+
queries.setQueryDefinition(
4669+
'q1',
4670+
't1',
4671+
({select, where, param}) => {
4672+
select('c1');
4673+
select('c2');
4674+
where((getTableCell) =>
4675+
(param('colors') as string[])?.includes(
4676+
getTableCell('c2') as string,
4677+
),
4678+
);
4679+
},
4680+
{colors: ['odd', 'even']},
4681+
);
4682+
4683+
expect(queries.getResultTable('q1')).toEqual({
4684+
r1: {c1: 'a', c2: 'odd'},
4685+
r2: {c1: 'b', c2: 'even'},
4686+
r3: {c1: 'c', c2: 'odd'},
4687+
r4: {c1: 'd', c2: 'even'},
4688+
r5: {c1: 'e', c2: 'odd'},
4689+
});
4690+
4691+
queries.setParamValue('q1', 'colors', ['odd']);
4692+
expect(queries.getResultTable('q1')).toEqual({
4693+
r1: {c1: 'a', c2: 'odd'},
4694+
r3: {c1: 'c', c2: 'odd'},
4695+
r5: {c1: 'e', c2: 'odd'},
4696+
});
4697+
4698+
queries.setParamValue('q1', 'colors', ['even']);
4699+
expect(queries.getResultTable('q1')).toEqual({
4700+
r2: {c1: 'b', c2: 'even'},
4701+
r4: {c1: 'd', c2: 'even'},
4702+
});
4703+
});
4704+
4705+
test('param with number array value', () => {
4706+
queries.setQueryDefinition(
4707+
'q1',
4708+
't1',
4709+
({select, where, param}) => {
4710+
select('c1');
4711+
select('c3');
4712+
where((getTableCell) =>
4713+
(param('values') as number[])?.includes(
4714+
getTableCell('c3') as number,
4715+
),
4716+
);
4717+
},
4718+
{values: [1, 3, 5]},
4719+
);
4720+
4721+
expect(queries.getResultTable('q1')).toEqual({
4722+
r1: {c1: 'a', c3: 1},
4723+
r3: {c1: 'c', c3: 3},
4724+
r5: {c1: 'e', c3: 5},
4725+
});
4726+
4727+
queries.setParamValue('q1', 'values', [2, 4]);
4728+
expect(queries.getResultTable('q1')).toEqual({
4729+
r2: {c1: 'b', c3: 2},
4730+
r4: {c1: 'd', c3: 4},
4731+
});
4732+
});
4733+
4734+
test('param with boolean array value', () => {
4735+
store.setCell('t1', 'r1', 'c4', true);
4736+
store.setCell('t1', 'r2', 'c4', false);
4737+
store.setCell('t1', 'r3', 'c4', true);
4738+
queries.setQueryDefinition(
4739+
'q1',
4740+
't1',
4741+
({select, where, param}) => {
4742+
select('c1');
4743+
select('c4');
4744+
where((getTableCell) =>
4745+
(param('flags') as boolean[])?.includes(
4746+
getTableCell('c4') as boolean,
4747+
),
4748+
);
4749+
},
4750+
{flags: [true]},
4751+
);
4752+
4753+
expect(queries.getResultTable('q1')).toEqual({
4754+
r1: {c1: 'a', c4: true},
4755+
r3: {c1: 'c', c4: true},
4756+
});
4757+
4758+
queries.setParamValue('q1', 'flags', [false]);
4759+
expect(queries.getResultTable('q1')).toEqual({
4760+
r2: {c1: 'b', c4: false},
4761+
});
4762+
4763+
queries.setParamValue('q1', 'flags', [true, false]);
4764+
expect(queries.getResultTable('q1')).toEqual({
4765+
r1: {c1: 'a', c4: true},
4766+
r2: {c1: 'b', c4: false},
4767+
r3: {c1: 'c', c4: true},
4768+
});
4769+
});
46664770
});
46674771

46684772
describe('Select', () => {
@@ -4737,7 +4841,7 @@ describe('Parameterized', () => {
47374841
({select, where, group, param}) => {
47384842
select('c2');
47394843
select('c3');
4740-
where('c2', param('p2') ?? '');
4844+
where('c2', param('p2') as Cell);
47414845
group('c3', 'avg').as('a');
47424846
},
47434847
{p2: 'odd'},

0 commit comments

Comments
 (0)