TinyBase logoTinyBase

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]>
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.

returnsPromise<[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.

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, 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.

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.

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

const dTsLines = dTs.split('\n');
console.log(dTsLines[17]);
// -> `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.

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

const dTsLines = dTs.split('\n');
console.log(dTsLines[17]);
// -> '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