TinyBase logoTinyBase β

Select

The Select type describes a function that lets you specify a Cell or calculated value for including into the query's result.

Calling this function with one Id parameter will indicate that the query should select the value of the specified Cell from the query's root Table.

(cellId: string): SelectedAs
TypeDescription
cellIdstring

The Id of the Cell to fetch the value for.

returnsSelectedAs

A SelectedAs object so that the selected Cell Id can be optionally aliased.

Since

v2.0.0


Calling this function with two parameters will indicate that the query should select the value of the specified Cell from a Table that has been joined in the query.

(
  joinedTableId: string,
  joinedCellId: string,
): SelectedAs
TypeDescription
joinedTableIdstring

The Id of the Table to fetch the value from. If the underlying Table was joined 'as' a different Id, that should instead be used.

joinedCellIdstring

The Id of the Cell to fetch the value for.

returnsSelectedAs

A SelectedAs object so that the selected Cell Id can be optionally aliased.

Since

v2.0.0


Calling this function with one callback parameter will indicate that the query should select a calculated value, based on one or more Cell values in the root Table or a joined Table, or on the root Table's Row Id.

(getCell: (getTableCell: GetTableCell, rowId: string) => ResultCellOrUndefined): SelectedAs
TypeDescription
getCell(getTableCell: GetTableCell, rowId: string) => ResultCellOrUndefined

A callback that takes a GetTableCell function and the main Table's Row Id. These can be used to programmatically create a calculated value from multiple Cell values and the Row Id.

returnsSelectedAs

A SelectedAs object so that the selected Cell Id can be optionally aliased.

Since

v2.0.0

The Select function is provided to the third query parameter of the setQueryDefinition method. A query definition must call the Select function at least once, otherwise it will be meaningless and return no data.

Examples

This example shows a query that selects two Cells from the main query Table.

import {createQueries, createStore} from 'tinybase';

const store = createStore().setTable('pets', {
  fido: {species: 'dog', color: 'brown', legs: 4},
  felix: {species: 'cat', color: 'black', legs: 4},
  cujo: {species: 'dog', color: 'black', legs: 4},
});

const queries = createQueries(store);
queries.setQueryDefinition('query', 'pets', ({select}) => {
  select('species');
  select('color');
});

queries.forEachResultRow('query', (rowId) => {
  console.log({[rowId]: queries.getResultRow('query', rowId)});
});
// -> {fido: {species: 'dog', color: 'brown'}}
// -> {felix: {species: 'cat', color: 'black'}}
// -> {cujo: {species: 'dog', color: 'black'}}

This example shows a query that selects two Cells, one from a joined Table.

import {createQueries, createStore} from 'tinybase';

const store = createStore()
  .setTable('pets', {
    fido: {species: 'dog', ownerId: '1'},
    felix: {species: 'cat', ownerId: '2'},
    cujo: {species: 'dog', ownerId: '3'},
  })
  .setTable('owners', {
    '1': {name: 'Alice'},
    '2': {name: 'Bob'},
    '3': {name: 'Carol'},
  });

const queries = createQueries(store);
queries.setQueryDefinition('query', 'pets', ({select, join}) => {
  select('species');
  select('owners', 'name');
  // from pets
  join('owners', 'ownerId');
});

queries.forEachResultRow('query', (rowId) => {
  console.log({[rowId]: queries.getResultRow('query', rowId)});
});
// -> {fido: {species: 'dog', name: 'Alice'}}
// -> {felix: {species: 'cat', name: 'Bob'}}
// -> {cujo: {species: 'dog', name: 'Carol'}}

This example shows a query that calculates a value from two underlying Cells.

import {createQueries, createStore} from 'tinybase';

const store = createStore()
  .setTable('pets', {
    fido: {species: 'dog', ownerId: '1'},
    felix: {species: 'cat', ownerId: '2'},
    cujo: {species: 'dog', ownerId: '3'},
  })
  .setTable('owners', {
    '1': {name: 'Alice'},
    '2': {name: 'Bob'},
    '3': {name: 'Carol'},
  });

const queries = createQueries(store);
queries.setQueryDefinition('query', 'pets', ({select, join}) => {
  select(
    (getTableCell) =>
      `${getTableCell('species')} for ${getTableCell('owners', 'name')}`,
  ).as('description');
  join('owners', 'ownerId');
});

queries.forEachResultRow('query', (rowId) => {
  console.log({[rowId]: queries.getResultRow('query', rowId)});
});
// -> {fido: {description: 'dog for Alice'}}
// -> {felix: {description: 'cat for Bob'}}
// -> {cujo: {description: 'dog for Carol'}}

Since

v2.0.0