TinyBase logoTinyBase

getStoreApi

The getStoreApi method returns a code-generated .d.ts file and a .ts file that describe the schema of a Store in an ORM style.

getStoreApi(storeName: 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 and .ts files.

returns[string, string]

A pair of strings representing the contents of the `.d.ts` and `.ts` files.

If the Store does not already have an explicit TablesSchema associated with it, the data in the Store will be scanned to attempt to infer a new TablesSchema. The method returns two 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 files as [storeName].d.ts and [storeName].ts, and alongside each other so that the latter can import types from the former.

The .d.ts and .ts files that are generated are designed to resemble the main TinyBase store.d.ts and store.ts 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, and types like PetsRow, that are more specific versions of the underlying getTable method or the Row type. 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] = createTools(store).getStoreApi('shop');

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

const tsLines = ts.split('\n');
console.log(tsLines[41]);
// -> '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] = createTools(store).getStoreApi('shop');

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

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

Since

v2.2.0