getPrettyStoreApi
The getPrettyStoreApi
method attempts to return prettified 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.
getPrettyStoreApi(storeName: string): Promise<[string, string, string, string]>
Type | Description | |
---|---|---|
storeName | string | The name you want to provide to the generated |
returns | Promise<[string, string, string, string]> | A set of four strings representing the contents of the |
This is simply a wrapper around the getStoreApi
method that attempts to invoke the prettier
module (which it hopes you have installed) to format the generated code. If prettier
is not present, the output will resemble that of the underlying getStoreApi
method.
The method is asynchronous, so you should use the await
keyword or handle the results as a promise.
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.
See the documentation for the getStoreApi
method for details of the content of the generated files.
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] =
await createTools(store).getPrettyStoreApi('shop');
const dTsLines = dTs.split('\n');
console.log(dTsLines[15]);
// -> `export type Tables = {pets?: {[rowId: Id]: {price?: number}}};`
const tsLines = ts.split('\n');
console.log(tsLines[89]);
// -> ' hasPetsTable: (): boolean => store.hasTable(PETS),'
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] =
await createTools(store).getPrettyStoreApi('shop');
const dTsLines = dTs.split('\n');
console.log(dTsLines[15]);
// -> 'export type Tables = {pets?: {[rowId: Id]: {price: number}}};'
const tsLines = ts.split('\n');
console.log(tsLines[91]);
// -> ' hasPetsTable: (): boolean => store.hasTable(PETS),'
Since
v2.2.0