Skip to content

Commit 69e9c69

Browse files
committed
[params] Getters
1 parent 72afe29 commit 69e9c69

File tree

5 files changed

+142
-0
lines changed

5 files changed

+142
-0
lines changed

src/@types/queries/docs.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,6 +1816,89 @@
18161816
* @since v2.0.0
18171817
*/
18181818
/// Queries.delQueryDefinition
1819+
/**
1820+
* The getParamValues method returns all the param values currently set for a
1821+
* parameterized query.
1822+
* @param queryId The Id of the query to get the params for.
1823+
* @returns An object containing all param Ids and their values, or
1824+
* `undefined` if the query doesn't exist.
1825+
* @example
1826+
* This example creates a parameterized query and retrieves its param values.
1827+
*
1828+
* ```js
1829+
* import {createQueries, createStore} from 'tinybase';
1830+
*
1831+
* const store = createStore().setTable('pets', {
1832+
* fido: {species: 'dog', color: 'brown'},
1833+
* felix: {species: 'cat', color: 'black'},
1834+
* cujo: {species: 'dog', color: 'black'},
1835+
* });
1836+
*
1837+
* const queries = createQueries(store);
1838+
* queries.setQueryDefinition(
1839+
* 'query',
1840+
* 'pets',
1841+
* ({select, where, param}) => {
1842+
* select('color');
1843+
* where('species', param('species'));
1844+
* where((getTableCell) => getTableCell('age') >= param('minAge'));
1845+
* },
1846+
* {species: 'dog', minAge: 5},
1847+
* );
1848+
*
1849+
* console.log(queries.getParamValues('query'));
1850+
* // -> {species: 'dog', minAge: 5}
1851+
*
1852+
* queries.setParamValue('query', 'species', 'cat');
1853+
* console.log(queries.getParamValues('query'));
1854+
* // -> {species: 'cat', minAge: 5}
1855+
* ```
1856+
* @category Getter
1857+
* @since v7.2.0
1858+
*/
1859+
/// Queries.getParamValues
1860+
/**
1861+
* The getParamValue method returns a single param value currently set for a
1862+
* parameterized query.
1863+
* @param queryId The Id of the query to get the param for.
1864+
* @param paramId The Id of the param to get.
1865+
* @returns The value of the param, or `undefined` if the query or param
1866+
* doesn't exist.
1867+
* @example
1868+
* This example creates a parameterized query and retrieves one of its param
1869+
* values.
1870+
*
1871+
* ```js
1872+
* import {createQueries, createStore} from 'tinybase';
1873+
*
1874+
* const store = createStore().setTable('pets', {
1875+
* fido: {species: 'dog', color: 'brown'},
1876+
* felix: {species: 'cat', color: 'black'},
1877+
* cujo: {species: 'dog', color: 'black'},
1878+
* });
1879+
*
1880+
* const queries = createQueries(store);
1881+
* queries.setQueryDefinition(
1882+
* 'query',
1883+
* 'pets',
1884+
* ({select, where, param}) => {
1885+
* select('color');
1886+
* where('species', param('species'));
1887+
* },
1888+
* {species: 'dog'},
1889+
* );
1890+
*
1891+
* console.log(queries.getParamValue('query', 'species'));
1892+
* // -> 'dog'
1893+
*
1894+
* queries.setParamValue('query', 'species', 'cat');
1895+
* console.log(queries.getParamValue('query', 'species'));
1896+
* // -> 'cat'
1897+
* ```
1898+
* @category Getter
1899+
* @since v7.2.0
1900+
*/
1901+
/// Queries.getParamValue
18191902
/**
18201903
* The setParamValues method sets multiple param values for a parameterized
18211904
* query at once, causing the query to re-evaluate with the new param values.

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,12 @@ export interface Queries {
290290
/// Queries.delQueryDefinition
291291
delQueryDefinition(queryId: Id): Queries;
292292

293+
/// Queries.getParamValues
294+
getParamValues(queryId: Id): ParamValues | undefined;
295+
296+
/// Queries.getParamValue
297+
getParamValue(queryId: Id, paramId: Id): ParamValue | undefined;
298+
293299
/// Queries.setParamValues
294300
setParamValues(queryId: Id, paramValues: ParamValues): Queries;
295301

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,12 @@ export interface Queries<in out Schemas extends OptionalSchemas> {
376376
/// Queries.delQueryDefinition
377377
delQueryDefinition(queryId: Id): Queries<Schemas>;
378378

379+
/// Queries.getParamValues
380+
getParamValues(queryId: Id): ParamValues | undefined;
381+
382+
/// Queries.getParamValue
383+
getParamValue(queryId: Id, paramId: Id): ParamValue | undefined;
384+
379385
/// Queries.setParamValues
380386
setParamValues(queryId: Id, paramValues: ParamValues): Queries<Schemas>;
381387

src/queries/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,12 @@ export const createQueries = getCreateFunction((store: Store): Queries => {
615615
[paramId]: value,
616616
});
617617

618+
const getParamValues = (queryId: Id): ParamValues | undefined =>
619+
ifNotUndefined(getQueryArgs(queryId)?.[1], mapToObj);
620+
621+
const getParamValue = (queryId: Id, paramId: Id): ParamValue | undefined =>
622+
mapGet(getQueryArgs(queryId)?.[1], paramId);
623+
618624
const addQueryIdsListener = (listener: QueryIdsListener) =>
619625
addQueryIdsListenerImpl(() => listener(queries));
620626

@@ -636,6 +642,8 @@ export const createQueries = getCreateFunction((store: Store): Queries => {
636642
const queries: any = {
637643
setQueryDefinition,
638644
delQueryDefinition,
645+
getParamValues,
646+
getParamValue,
639647
setParamValues,
640648
setParamValue,
641649

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4767,6 +4767,45 @@ describe('Parameterized', () => {
47674767
r3: {c1: 'c', c4: true},
47684768
});
47694769
});
4770+
4771+
test('getParamValues', () => {
4772+
queries.setQueryDefinition(
4773+
'q1',
4774+
't1',
4775+
({select, where, param}) => {
4776+
select('c1');
4777+
where('c2', param('p2') as Cell);
4778+
},
4779+
{p2: 'odd', p3: 5},
4780+
);
4781+
4782+
expect(queries.getParamValues('q1')).toEqual({p2: 'odd', p3: 5});
4783+
4784+
queries.setParamValue('q1', 'p2', 'even');
4785+
expect(queries.getParamValues('q1')).toEqual({p2: 'even', p3: 5});
4786+
4787+
expect(queries.getParamValues('nonexistent')).toBeUndefined();
4788+
});
4789+
4790+
test('getParamValue', () => {
4791+
queries.setQueryDefinition(
4792+
'q1',
4793+
't1',
4794+
({select, where, param}) => {
4795+
select('c1');
4796+
where('c2', param('p2') as Cell);
4797+
},
4798+
{p2: 'odd', p3: 5},
4799+
);
4800+
4801+
expect(queries.getParamValue('q1', 'p2')).toBe('odd');
4802+
expect(queries.getParamValue('q1', 'p3')).toBe(5);
4803+
expect(queries.getParamValue('q1', 'nonexistent')).toBeUndefined();
4804+
expect(queries.getParamValue('nonexistent', 'p2')).toBeUndefined();
4805+
4806+
queries.setParamValue('q1', 'p2', 'even');
4807+
expect(queries.getParamValue('q1', 'p2')).toBe('even');
4808+
});
47704809
});
47714810

47724811
describe('Select', () => {

0 commit comments

Comments
 (0)