TinyBase logoTinyBase

getStoreApi

The getStoreApi method returns code-generated .d.ts and .ts(x) files that describe the schema of a Store and React bindings (since v3.1) in an ORM style.

getStoreApi(storeName: string): [string, string, string, string]
TypeDescription
storeNamestring

The name you want to provide to the generated Store, which should also be used to save the .d.ts, .ts, and .tsx files.

returns[string, string, string, string]

A set of four strings representing the contents of the .d.ts, .ts, and .tsx files for the generated Store and React modules.

If the Store does not already have an explicit TablesSchema or ValuesSchema associated with it, the data in the Store will be scanned to attempt to infer new schemas. The method returns four strings (which should be saved as files) though if no schema can be inferred, the strings will be empty.

The method takes a single argument which represents the name you want the generated store object to have in code. You are expected to save the four files yourself, as, respectively:

  • [storeName].d.ts
  • [storeName].ts
  • [storeName]-ui-react.d.ts
  • [storeName]-ui-react.tsx

Also you should save these alongside each other so that the .ts(x) files can import types from the .d.ts files.

The .d.ts and .ts(x) files that are generated are designed to resemble the main TinyBase Store and React binding files, but provide named types and methods that describe the domain of the schema in the Store.

For example, from a Store that has a pets Table, you will get methods like getPetsTable, types like PetsRow, and hooks and components that are more specific versions of the underlying getTable method or the Row type, and so on. For example:

Store typeEquivalent generated type
Table[Table]Table
Row[Table]Row
(Cell) Id[Table]CellId
CellCallback[Table]CellCallback
......
Store methodEquivalent generated method
setTableset[Table]Table
hasRowhas[Table]Row
getCellget[Table][Cell]Cell
......

Equivalent to the TinyBase createStore function, a create[StoreName] function will also be created. This acts as the main entry point to the generated implementation.

Each method is refined correctly to take, or return, the types specified by the schema. For example, if the pets Table has a numeric price Cell in the schema, the getPetsPriceCell method will be typed to return a number.

The tables above include just a sample of the generated output. For the full set of methods and types generated by this method, inspect the output directly.

Examples

This example creates a Tools object and generates code for a Store that already has a TablesSchema.

const store = createStore().setTablesSchema({
  pets: {
    price: {type: 'number'},
  },
});
const [dTs, ts, uiReactDTs, uiReactTsx] =
  createTools(store).getStoreApi('shop');

const dTsLines = dTs.split('\n');
console.log(dTsLines[3]);
// -> `export type Tables = {'pets'?: {[rowId: Id]: {'price'?: number}}};`

const tsLines = ts.split('\n');
console.log(tsLines[39]);
// -> 'getPetsTable: (): PetsTable => store.getTable(PETS) as PetsTable,'

This example creates a Tools object and generates code for a Store that doesn't already have a TablesSchema.

const store = createStore().setTable('pets', {
  fido: {price: 5},
  felix: {price: 4},
});
const [dTs, ts, uiReactDTs, uiReactTsx] =
  createTools(store).getStoreApi('shop');

const dTsLines = dTs.split('\n');
console.log(dTsLines[3]);
// -> `export type Tables = {'pets'?: {[rowId: Id]: {'price': number}}};`

const tsLines = ts.split('\n');
console.log(tsLines[41]);
// -> 'getPetsTable: (): PetsTable => store.getTable(PETS) as PetsTable,'

Since

v2.2.0