getPrettyStoreApi
The getPrettyStoreApi
method attempts to returns a prettified code-generated .d.ts file and a .ts file that describe the schema of a Store
in an ORM style.
getPrettyStoreApi(storeName: string): Promise<[string, string]>
Type | Description | |
---|---|---|
storeName | string | The name you want to provide to the generated |
returns | Promise<[string, string]> | A pair of strings representing the contents of the `.d.ts` and `.ts` files. |
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 files as [storeName].d.ts
and [storeName].ts
, and alongside each other so that the latter can import types from the former.
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 Schema
.
const store = createStore().setSchema({
pets: {
price: {type: 'number'},
},
});
const tools = createTools(store);
const [dTs, ts] = await tools.getPrettyStoreApi('shop');
const dTsLines = dTs.split('\n');
console.log(dTsLines[14]);
// -> 'export type PetsTable = {[rowId: Id]: PetsRow};'
console.log(dTsLines[19]);
// -> 'export type PetsRow = {price?: number};'
const tsLines = ts.split('\n');
console.log(tsLines[73]);
// -> ' hasPetsTable: (): boolean => store.hasTable(PETS),'
This example creates a Tools
object and generates code for a Store
that doesn't already have a Schema
.
const store = createStore().setTable('pets', {
fido: {price: 5},
felix: {price: 4},
});
const tools = createTools(store);
const [dTs, ts] = await tools.getPrettyStoreApi('shop');
const dTsLines = dTs.split('\n');
console.log(dTsLines[14]);
// -> 'export type PetsTable = {[rowId: Id]: PetsRow};'
console.log(dTsLines[19]);
// -> 'export type PetsRow = {price: number};'
const tsLines = ts.split('\n');
console.log(tsLines[75]);
// -> ' hasPetsTable: (): boolean => store.hasTable(PETS),'
Since
v2.2.0