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]
Type | Description | |
---|---|---|
storeName | string | The name you want to provide to the generated |
returns | [string, string, string, string] | A set of four strings representing the contents of the |
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 the following:
[storeName].d.ts
as the main definition,[storeName].ts
as the main library,[storeName]-ui-react.d.ts
as theui-react
module definition,[storeName]-ui-react.tsx
as theui-react
module library.
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 type | Equivalent generated type |
---|---|
Table | [Table]Table |
Row | [Table]Row |
(Cell ) Id | [Table]CellId |
CellCallback | [Table]CellCallback |
... | ... |
Store method | Equivalent generated method |
---|---|
setTable | set[Table]Table |
hasRow | has[Table]Row |
getCell | get[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
.
import {createStore} from 'tinybase';
import {createTools} from 'tinybase/tools';
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
.
import {createStore} from 'tinybase';
import {createTools} from 'tinybase/tools';
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