tinybase 2.2.0-beta.0 → 2.2.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/cli.js +1 -1
- package/lib/checkpoints.js +1 -1
- package/lib/checkpoints.js.gz +0 -0
- package/lib/debug/queries.js +4 -3
- package/lib/debug/store.js +5 -5
- package/lib/debug/tinybase.js +6 -6
- package/lib/debug/tools.d.ts +155 -5
- package/lib/debug/tools.js +488 -431
- package/lib/es6/checkpoints.js +1 -1
- package/lib/es6/checkpoints.js.gz +0 -0
- package/lib/es6/indexes.js +1 -1
- package/lib/es6/indexes.js.gz +0 -0
- package/lib/es6/metrics.js +1 -1
- package/lib/es6/metrics.js.gz +0 -0
- package/lib/es6/queries.js +1 -1
- package/lib/es6/queries.js.gz +0 -0
- package/lib/es6/relationships.js +1 -1
- package/lib/es6/relationships.js.gz +0 -0
- package/lib/es6/store.js +1 -1
- package/lib/es6/store.js.gz +0 -0
- package/lib/es6/tinybase.js +1 -1
- package/lib/es6/tinybase.js.gz +0 -0
- package/lib/es6/tools.d.ts +155 -5
- package/lib/es6/tools.js +1 -1
- package/lib/es6/tools.js.gz +0 -0
- package/lib/es6/ui-react.js +1 -1
- package/lib/es6/ui-react.js.gz +0 -0
- package/lib/indexes.js +1 -1
- package/lib/indexes.js.gz +0 -0
- package/lib/metrics.js +1 -1
- package/lib/metrics.js.gz +0 -0
- package/lib/queries.js +1 -1
- package/lib/queries.js.gz +0 -0
- package/lib/relationships.js +1 -1
- package/lib/relationships.js.gz +0 -0
- package/lib/store.js +1 -1
- package/lib/store.js.gz +0 -0
- package/lib/tinybase.js +1 -1
- package/lib/tinybase.js.gz +0 -0
- package/lib/tools.d.ts +155 -5
- package/lib/tools.js +1 -1
- package/lib/tools.js.gz +0 -0
- package/lib/umd/checkpoints.js +1 -1
- package/lib/umd/checkpoints.js.gz +0 -0
- package/lib/umd/indexes.js +1 -1
- package/lib/umd/indexes.js.gz +0 -0
- package/lib/umd/metrics.js +1 -1
- package/lib/umd/metrics.js.gz +0 -0
- package/lib/umd/queries.js +1 -1
- package/lib/umd/queries.js.gz +0 -0
- package/lib/umd/relationships.js +1 -1
- package/lib/umd/relationships.js.gz +0 -0
- package/lib/umd/store.js +1 -1
- package/lib/umd/store.js.gz +0 -0
- package/lib/umd/tinybase.js +1 -1
- package/lib/umd/tinybase.js.gz +0 -0
- package/lib/umd/tools.d.ts +155 -5
- package/lib/umd/tools.js +1 -1
- package/lib/umd/tools.js.gz +0 -0
- package/lib/umd-es6/checkpoints.js +1 -1
- package/lib/umd-es6/checkpoints.js.gz +0 -0
- package/lib/umd-es6/indexes.js +1 -1
- package/lib/umd-es6/indexes.js.gz +0 -0
- package/lib/umd-es6/metrics.js +1 -1
- package/lib/umd-es6/metrics.js.gz +0 -0
- package/lib/umd-es6/queries.js +1 -1
- package/lib/umd-es6/queries.js.gz +0 -0
- package/lib/umd-es6/relationships.js +1 -1
- package/lib/umd-es6/relationships.js.gz +0 -0
- package/lib/umd-es6/store.js +1 -1
- package/lib/umd-es6/store.js.gz +0 -0
- package/lib/umd-es6/tinybase.js +1 -1
- package/lib/umd-es6/tinybase.js.gz +0 -0
- package/lib/umd-es6/tools.d.ts +155 -5
- package/lib/umd-es6/tools.js +1 -1
- package/lib/umd-es6/tools.js.gz +0 -0
- package/lib/umd-es6/ui-react.js +1 -1
- package/lib/umd-es6/ui-react.js.gz +0 -0
- package/package.json +6 -5
- package/readme.md +21 -2
package/bin/cli.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
#! /usr/bin/env node
|
|
2
|
-
(
|
|
2
|
+
import{writeFileSync as t,readFileSync as e}from"fs";import{createStore as r}from"tinybase";import{createTools as s}from"tinybase/tools";import{resolve as a}from"path";const o=JSON.parse,i=Object,n=(...t)=>{return e=t=>process.stdout.write(t+"\n"),t.forEach(e);var e},m=t=>o(e(t,"utf8")),p=()=>{var t,e;n("","tinybase <command>","","Usage:",""),t=c,e=([,t,e],r)=>n(` tinybase ${r} ${t}`," - "+e,""),((t,e)=>{t.map(e)})(i.entries(t),(([t,r])=>e(r,t)))},c={help:[p,"","print this message"],version:[()=>n(m("./package.json").version),"","get the current TinyBase version"],getStoreApi:[async(e,o,i)=>{try{const p=s(r().setSchema(m(e))),[c,f]=await p.getPrettyStoreApi(o),h=a(i,o+".d.ts"),u=a(i,o+".ts");t(h,c,"utf8"),t(u,f,"utf8"),n(" Definition: "+h,"Implementation: "+u)}catch{process.stderr.write("ERROR: provide a valid schemaFile, storeName, and outputDir\n")}},"<schemaFile> <storeName> <outputDir>","generate .d.ts and .ts files from a schema file"]};(()=>{const[,,t,...e]=process.argv;(c[t]?.[0]??p)(...e)})();
|
package/lib/checkpoints.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const e=(e,t)=>e.includes(t),t=(e,t)=>e.forEach(t),n=e=>e.length,r=e=>0==n(e),s=(e,...t)=>e.push(...t),o=e=>e.pop(),l=e=>e.shift(),c=e=>null==e,i=(e,t,n)=>c(e)?n?.():t(e),a=(e,t)=>e?.has(t)??!1,d=e=>c(e)||0==(e=>e.size)(e),u=(e,t)=>e?.forEach(t),h=(e,t)=>e?.delete(t),p=e=>new Map(e),C=(e,t)=>e?.get(t),g=(e,t,n)=>c(n)?(h(e,t),e):e?.set(t,n),k=(e,t,n)=>(a(e,t)||g(e,t,n()),C(e,t)),f=(e,t,r,s,o=0)=>i((r?k:C)(e,t[o],o>n(t)-2?r:p),(l=>{if(o>n(t)-2)return s?.(l)&&g(e,t[o]),l;const c=f(l,t,r,s,o+1);return d(l)&&g(e,t[o]),c})),v=e=>new Set(Array.isArray(e)||c(e)?e:[e]),L=/^\d+$/,w=e=>{let r;const[o,a]=(()=>{const e=[];let t=0;return[()=>l(e)??""+t++,t=>{L.test(t)&&n(e)<1e3&&s(e,t)}]})(),k=p();return[(t,n
|
|
1
|
+
const e=(e,t)=>e.includes(t),t=(e,t)=>e.forEach(t),n=e=>e.length,r=e=>0==n(e),s=(e,...t)=>e.push(...t),o=e=>e.pop(),l=e=>e.shift(),c=e=>null==e,i=(e,t,n)=>c(e)?n?.():t(e),a=(e,t)=>e?.has(t)??!1,d=e=>c(e)||0==(e=>e.size)(e),u=(e,t)=>e?.forEach(t),h=(e,t)=>e?.delete(t),p=e=>new Map(e),C=(e,t)=>e?.get(t),g=(e,t,n)=>c(n)?(h(e,t),e):e?.set(t,n),k=(e,t,n)=>(a(e,t)||g(e,t,n()),C(e,t)),f=(e,t,r,s,o=0)=>i((r?k:C)(e,t[o],o>n(t)-2?r:p),(l=>{if(o>n(t)-2)return s?.(l)&&g(e,t[o]),l;const c=f(l,t,r,s,o+1);return d(l)&&g(e,t[o]),c})),v=e=>new Set(Array.isArray(e)||c(e)?e:[e]),L=/^\d+$/,w=Object.freeze,S=(S=>{const y=new WeakMap;return S=>(y.has(S)||y.set(S,(S=>{let y,z,E,A=100,I=p(),M=1;const b=p(),j=p(),[x,B,F]=(e=>{let r;const[o,a]=(()=>{const e=[];let t=0;return[()=>l(e)??""+t++,t=>{L.test(t)&&n(e)<1e3&&s(e,t)}]})(),k=p();return[(e,t,n)=>{r??=V;const s=o();var l;return g(k,s,[e,t,n]),l=s,f(t,n??[""],v)?.add(l),s},(e,o,...l)=>t(((e,r=[""])=>{const o=[],l=(e,c)=>c==n(r)?s(o,e):null===r[c]?u(e,(e=>l(e,c+1))):t([r[c],null],(t=>l(C(e,t),c+1)));return l(e,0),o})(e,o),(e=>u(e,(e=>C(k,e)[0](r,...o??[],...l))))),e=>i(C(k,e),(([,t,n])=>(f(t,n??[""],void 0,(t=>(h(t,e),d(t)?1:0))),g(k,e),a(e),n))),(e,s,o)=>i(C(k,e),(([e,,l=[]])=>{const i=(...a)=>{const d=n(a);d==n(l)?e(r,...a,...o(a)):c(l[d])?t(s[d](...a),(e=>i(...a,e))):i(...a,l[d])};i()}))]})(),O=p(),T=p(),W=[],$=[],m=(e,t)=>{M=0,S.transaction((()=>u(C(O,t),((t,n)=>u(t,((t,r)=>u(t,((t,s)=>((e,t,n,r,s)=>c(s)?e.delCell(t,n,r,!0):e.setCell(t,n,r,s))(S,n,r,s,t[e]))))))))),M=1},q=e=>{g(O,e),g(T,e),B(j,[e])},D=(e,r)=>t(((e,t)=>e.splice(0,t))(e,r??n(e)),q),G=()=>D(W,n(W)-A),H=S.addCellListener(null,null,null,((e,t,n,r,l,c)=>{if(M){i(y,(()=>{s(W,y),G(),D($),y=void 0,E=1}));const e=k(I,t,p),a=k(e,n,p),u=k(a,r,(()=>[c,void 0]));u[1]=l,u[0]===l&&d(g(a,r))&&d(g(e,n))&&d(g(I,t))&&(y=o(W),E=1),P()}})),J=(e="")=>(c(y)&&(y=""+z++,g(O,y,I),R(y,e),I=p(),E=1),y),K=()=>{r(W)||(((e,...t)=>{e.unshift(...t)})($,J()),m(0,y),y=o(W),E=1)},N=()=>{r($)||(s(W,y),y=l($),m(1,y),E=1)},P=()=>{E&&(B(b),E=0)},Q=e=>{const t=J(e);return P(),t},R=(e,t)=>(U(e)&&C(T,e)!==t&&(g(T,e,t),B(j,[e])),V),U=e=>a(O,e),V={setSize:e=>(A=e,G(),V),addCheckpoint:Q,setCheckpoint:R,getStore:()=>S,getCheckpointIds:()=>[[...W],y,[...$]],forEachCheckpoint:e=>{return t=e,u(T,((e,n)=>t(n,e)));var t},hasCheckpoint:U,getCheckpoint:e=>C(T,e),goBackward:()=>(K(),P(),V),goForward:()=>(N(),P(),V),goTo:t=>{const n=e(W,t)?K:e($,t)?N:null;for(;!c(n)&&t!=y;)n();return P(),V},addCheckpointIdsListener:e=>x(e,b),addCheckpointListener:(e,t)=>x(t,j,[e]),delListener:e=>(F(e),V),clear:()=>(D(W),D($),c(y)||q(y),y=void 0,z=0,Q(),V),destroy:()=>{S.delListener(H)},getListenerStats:()=>({})};return w(V.clear())})(S)),y.get(S))})();export{S as createCheckpoints};
|
package/lib/checkpoints.js.gz
CHANGED
|
Binary file
|
package/lib/debug/queries.js
CHANGED
|
@@ -24,6 +24,7 @@ const arrayIsEqual = (array1, array2) =>
|
|
|
24
24
|
arrayLength(array1) === arrayLength(array2) &&
|
|
25
25
|
arrayEvery(array1, (value1, index) => array2[index] === value1);
|
|
26
26
|
const arrayForEach = (array, cb) => array.forEach(cb);
|
|
27
|
+
const arrayMap = (array, cb) => array.map(cb);
|
|
27
28
|
const arraySum = (array) => arrayReduce(array, (i, j) => i + j, 0);
|
|
28
29
|
const arrayLength = (array) => array.length;
|
|
29
30
|
const arrayIsEmpty = (array) => arrayLength(array) == 0;
|
|
@@ -329,8 +330,8 @@ const getCreateFunction = (getFunction) => {
|
|
|
329
330
|
|
|
330
331
|
const object = Object;
|
|
331
332
|
const objFreeze = object.freeze;
|
|
332
|
-
const
|
|
333
|
-
|
|
333
|
+
const objMap = (obj, cb) =>
|
|
334
|
+
arrayMap(object.entries(obj), ([id, value]) => cb(value, id));
|
|
334
335
|
|
|
335
336
|
const createQueries = getCreateFunction((store) => {
|
|
336
337
|
const createStore = store.createStore;
|
|
@@ -744,7 +745,7 @@ const createQueries = getCreateFunction((store) => {
|
|
|
744
745
|
destroy,
|
|
745
746
|
getListenerStats,
|
|
746
747
|
};
|
|
747
|
-
|
|
748
|
+
objMap(
|
|
748
749
|
{
|
|
749
750
|
[TABLE]: [1, 1],
|
|
750
751
|
[ROW_IDS]: [0, 1],
|
package/lib/debug/store.js
CHANGED
|
@@ -134,8 +134,8 @@ const isObject = (obj) =>
|
|
|
134
134
|
const objGet = (obj, id) => ifNotUndefined(obj, (obj2) => obj2[id]);
|
|
135
135
|
const objHas = (obj, id) => !isUndefined(objGet(obj, id));
|
|
136
136
|
const objDel = (obj, id) => delete obj[id];
|
|
137
|
-
const
|
|
138
|
-
|
|
137
|
+
const objMap = (obj, cb) =>
|
|
138
|
+
arrayMap(object.entries(obj), ([id, value]) => cb(value, id));
|
|
139
139
|
const objIsEmpty = (obj) => arrayIsEmpty(objIds(obj));
|
|
140
140
|
|
|
141
141
|
const setNew = (entryOrEntries) =>
|
|
@@ -248,7 +248,7 @@ const validate = (obj, validateChild, onInvalidObj) => {
|
|
|
248
248
|
onInvalidObj?.();
|
|
249
249
|
return false;
|
|
250
250
|
}
|
|
251
|
-
|
|
251
|
+
objMap(obj, (child, id2) => {
|
|
252
252
|
if (!validateChild(child, id2)) {
|
|
253
253
|
objDel(obj, id2);
|
|
254
254
|
}
|
|
@@ -711,7 +711,7 @@ const createStore = () => {
|
|
|
711
711
|
(tableId2, rowId2) => {
|
|
712
712
|
if (validateRow(tableId2, rowId2, partialRow, 1)) {
|
|
713
713
|
const table = getOrCreateTable(tableId2);
|
|
714
|
-
|
|
714
|
+
objMap(partialRow, (cell, cellId) =>
|
|
715
715
|
setCellIntoDefaultRow(tableId2, table, rowId2, cellId, cell),
|
|
716
716
|
);
|
|
717
717
|
}
|
|
@@ -1005,7 +1005,7 @@ const createStore = () => {
|
|
|
1005
1005
|
getListenerStats,
|
|
1006
1006
|
createStore,
|
|
1007
1007
|
};
|
|
1008
|
-
|
|
1008
|
+
objMap(
|
|
1009
1009
|
{
|
|
1010
1010
|
[TABLES]: [0, tablesListeners],
|
|
1011
1011
|
[TABLE_IDS]: [0, tableIdsListeners],
|
package/lib/debug/tinybase.js
CHANGED
|
@@ -392,8 +392,8 @@ const isObject = (obj) =>
|
|
|
392
392
|
const objGet = (obj, id) => ifNotUndefined(obj, (obj2) => obj2[id]);
|
|
393
393
|
const objHas = (obj, id) => !isUndefined(objGet(obj, id));
|
|
394
394
|
const objDel = (obj, id) => delete obj[id];
|
|
395
|
-
const
|
|
396
|
-
|
|
395
|
+
const objMap = (obj, cb) =>
|
|
396
|
+
arrayMap(object.entries(obj), ([id, value]) => cb(value, id));
|
|
397
397
|
const objIsEmpty = (obj) => arrayIsEmpty(objIds(obj));
|
|
398
398
|
|
|
399
399
|
const getCellType = (cell) => {
|
|
@@ -1546,7 +1546,7 @@ const createQueries = getCreateFunction((store) => {
|
|
|
1546
1546
|
destroy,
|
|
1547
1547
|
getListenerStats,
|
|
1548
1548
|
};
|
|
1549
|
-
|
|
1549
|
+
objMap(
|
|
1550
1550
|
{
|
|
1551
1551
|
[TABLE]: [1, 1],
|
|
1552
1552
|
[ROW_IDS]: [0, 1],
|
|
@@ -1780,7 +1780,7 @@ const validate = (obj, validateChild, onInvalidObj) => {
|
|
|
1780
1780
|
onInvalidObj?.();
|
|
1781
1781
|
return false;
|
|
1782
1782
|
}
|
|
1783
|
-
|
|
1783
|
+
objMap(obj, (child, id2) => {
|
|
1784
1784
|
if (!validateChild(child, id2)) {
|
|
1785
1785
|
objDel(obj, id2);
|
|
1786
1786
|
}
|
|
@@ -2243,7 +2243,7 @@ const createStore = () => {
|
|
|
2243
2243
|
(tableId2, rowId2) => {
|
|
2244
2244
|
if (validateRow(tableId2, rowId2, partialRow, 1)) {
|
|
2245
2245
|
const table = getOrCreateTable(tableId2);
|
|
2246
|
-
|
|
2246
|
+
objMap(partialRow, (cell, cellId) =>
|
|
2247
2247
|
setCellIntoDefaultRow(tableId2, table, rowId2, cellId, cell),
|
|
2248
2248
|
);
|
|
2249
2249
|
}
|
|
@@ -2537,7 +2537,7 @@ const createStore = () => {
|
|
|
2537
2537
|
getListenerStats,
|
|
2538
2538
|
createStore,
|
|
2539
2539
|
};
|
|
2540
|
-
|
|
2540
|
+
objMap(
|
|
2541
2541
|
{
|
|
2542
2542
|
[TABLES]: [0, tablesListeners],
|
|
2543
2543
|
[TABLE_IDS]: [0, tableIdsListeners],
|
package/lib/debug/tools.d.ts
CHANGED
|
@@ -224,21 +224,171 @@ export interface Tools {
|
|
|
224
224
|
|
|
225
225
|
/**
|
|
226
226
|
* The getStoreApi method returns a code-generated .d.ts file and a .ts file
|
|
227
|
-
* that
|
|
227
|
+
* that describe the schema of a Store in an ORM style.
|
|
228
228
|
*
|
|
229
|
-
*
|
|
229
|
+
* If the Store does not already have an explicit Schema associated with it,
|
|
230
|
+
* the data in the Store will be scanned to attempt to infer a new Schema. The
|
|
231
|
+
* method returns two strings (which should be saved as files) though if no
|
|
232
|
+
* schema can be inferred, the strings will be empty.
|
|
233
|
+
*
|
|
234
|
+
* The method takes a single argument which represents the name you want the
|
|
235
|
+
* generated store object to have in code. You are expected to save the files
|
|
236
|
+
* as `[storeName].d.ts` and `[storeName].ts`, and alongside each other so
|
|
237
|
+
* that the latter can import types from the former.
|
|
238
|
+
*
|
|
239
|
+
* The .d.ts and .ts files that are generated are designed to resemble the
|
|
240
|
+
* main TinyBase `store.d.ts` and `store.ts` files, but provide named types
|
|
241
|
+
* and methods that describe the domain of the schema in the store.
|
|
242
|
+
*
|
|
243
|
+
* For example, from a Store that has a `pets` Table, you will get methods
|
|
244
|
+
* like `getPetsTable`, and types like `PetsRow`, that are more specific
|
|
245
|
+
* versions of the underlying getTable method or the Row type. For example:
|
|
246
|
+
*
|
|
247
|
+
* |Store type|Equivalent generated type|
|
|
248
|
+
* |-|-|
|
|
249
|
+
* |Table|[Table]Table|
|
|
250
|
+
* |Row|[Table]Row|
|
|
251
|
+
* |(Cell) Id|[Table]CellId|
|
|
252
|
+
* |CellCallback|[Table]CellCallback|
|
|
253
|
+
* |...|...|
|
|
254
|
+
*
|
|
255
|
+
* |Store method|Equivalent generated method|
|
|
256
|
+
* |-|-|
|
|
257
|
+
* |setTable|set[Table]Table|
|
|
258
|
+
* |hasRow|has[Table]Row|
|
|
259
|
+
* |getCell|get[Table][Cell]Cell|
|
|
260
|
+
* |...|...|
|
|
261
|
+
*
|
|
262
|
+
* Equivalent to the TinyBase createStore function, a `create[StoreName]`
|
|
263
|
+
* function will also be created. This acts as the main entry point to the
|
|
264
|
+
* generated implementation.
|
|
265
|
+
*
|
|
266
|
+
* Each method is refined correctly to take, or return, the types specified by
|
|
267
|
+
* the schema. For example, if the `pets` Table has a numeric `price` Cell in
|
|
268
|
+
* the schema, the `getPetsPriceCell` method will be typed to return a number.
|
|
269
|
+
*
|
|
270
|
+
* The tables above include just a sample of the generated output. For the
|
|
271
|
+
* full set of methods and types generated by this method, inspect the output
|
|
272
|
+
* directly.
|
|
273
|
+
*
|
|
274
|
+
* @param storeName The name you want to provide to the generated Store, which
|
|
275
|
+
* should also be used to save the `.d.ts` and `.ts` files.
|
|
276
|
+
* @returns A pair of strings representing the contents of the `.d.ts` and
|
|
277
|
+
* `.ts` files.
|
|
278
|
+
* @example
|
|
279
|
+
* This example creates a Tools object and generates code for a Store that
|
|
280
|
+
* already has a Schema.
|
|
281
|
+
* ```js
|
|
282
|
+
* const store = createStore().setSchema({
|
|
283
|
+
* pets: {
|
|
284
|
+
* price: {type: 'number'},
|
|
285
|
+
* },
|
|
286
|
+
* });
|
|
287
|
+
* const [dTs, ts] = createTools(store).getStoreApi('shop');
|
|
288
|
+
*
|
|
289
|
+
* const dTsLines = dTs.split('\n');
|
|
290
|
+
* console.log(dTsLines[3]);
|
|
291
|
+
* // -> 'export type PetsTable = {[rowId: Id]: PetsRow};'
|
|
292
|
+
* console.log(dTsLines[6]);
|
|
293
|
+
* // -> 'export type PetsRow = {\'price\'?: number;};'
|
|
294
|
+
*
|
|
295
|
+
* const tsLines = ts.split('\n');
|
|
296
|
+
* console.log(tsLines[40]);
|
|
297
|
+
* // -> 'getPetsTable: (): PetsTable => store.getTable(PETS) as PetsTable,'
|
|
298
|
+
* ```
|
|
299
|
+
* @example
|
|
300
|
+
* This example creates a Tools object and generates code for a Store that
|
|
301
|
+
* doesn't already have a Schema.
|
|
302
|
+
* ```js
|
|
303
|
+
* const store = createStore().setTable('pets', {
|
|
304
|
+
* fido: {price: 5},
|
|
305
|
+
* felix: {price: 4},
|
|
306
|
+
* });
|
|
307
|
+
* const [dTs, ts] = createTools(store).getStoreApi('shop');
|
|
230
308
|
*
|
|
309
|
+
* const dTsLines = dTs.split('\n');
|
|
310
|
+
* console.log(dTsLines[3]);
|
|
311
|
+
* // -> 'export type PetsTable = {[rowId: Id]: PetsRow};'
|
|
312
|
+
* console.log(dTsLines[6]);
|
|
313
|
+
* // -> 'export type PetsRow = {\'price\': number;};'
|
|
314
|
+
*
|
|
315
|
+
* const tsLines = ts.split('\n');
|
|
316
|
+
* console.log(tsLines[42]);
|
|
317
|
+
* // -> 'getPetsTable: (): PetsTable => store.getTable(PETS) as PetsTable,'
|
|
318
|
+
* ```
|
|
231
319
|
* @category Modelling
|
|
232
320
|
* @since v2.2.0
|
|
233
321
|
*/
|
|
234
322
|
getStoreApi(storeName: string): [string, string];
|
|
235
323
|
|
|
236
324
|
/**
|
|
237
|
-
* The
|
|
238
|
-
* file and a .ts file that
|
|
325
|
+
* The getPrettyStoreApi method attempts to returns a prettified
|
|
326
|
+
* code-generated .d.ts file and a .ts file that describe the schema of a
|
|
327
|
+
* Store in an ORM style.
|
|
328
|
+
*
|
|
329
|
+
* This is simply a wrapper around the getStoreApi method that attempts to
|
|
330
|
+
* invoke the `prettier` module (which it hopes you have installed) to format
|
|
331
|
+
* the generated code. If `prettier` is not present, the output will resemble
|
|
332
|
+
* that of the underlying getStoreApi method.
|
|
239
333
|
*
|
|
240
|
-
*
|
|
334
|
+
* The method is asynchronous, so you should use the `await` keyword or handle
|
|
335
|
+
* the results as a promise.
|
|
241
336
|
*
|
|
337
|
+
* The method takes a single argument which represents the name you want the
|
|
338
|
+
* generated store object to have in code. You are expected to save the files
|
|
339
|
+
* as `[storeName].d.ts` and `[storeName].ts`, and alongside each other so
|
|
340
|
+
* that the latter can import types from the former.
|
|
341
|
+
*
|
|
342
|
+
* See the documentation for the getStoreApi method for details of the content
|
|
343
|
+
* of the generated files.
|
|
344
|
+
*
|
|
345
|
+
* @param storeName The name you want to provide to the generated Store, which
|
|
346
|
+
* should also be used to save the `.d.ts` and `.ts` files.
|
|
347
|
+
* @returns A pair of strings representing the contents of the `.d.ts` and
|
|
348
|
+
* `.ts` files.
|
|
349
|
+
* @example
|
|
350
|
+
* This example creates a Tools object and generates code for a Store that
|
|
351
|
+
* already has a Schema.
|
|
352
|
+
* ```js
|
|
353
|
+
* const store = createStore().setSchema({
|
|
354
|
+
* pets: {
|
|
355
|
+
* price: {type: 'number'},
|
|
356
|
+
* },
|
|
357
|
+
* });
|
|
358
|
+
* const tools = createTools(store);
|
|
359
|
+
* const [dTs, ts] = await tools.getPrettyStoreApi('shop');
|
|
360
|
+
*
|
|
361
|
+
* const dTsLines = dTs.split('\n');
|
|
362
|
+
* console.log(dTsLines[14]);
|
|
363
|
+
* // -> 'export type PetsTable = {[rowId: Id]: PetsRow};'
|
|
364
|
+
* console.log(dTsLines[19]);
|
|
365
|
+
* // -> 'export type PetsRow = {price?: number};'
|
|
366
|
+
*
|
|
367
|
+
* const tsLines = ts.split('\n');
|
|
368
|
+
* console.log(tsLines[73]);
|
|
369
|
+
* // -> ' hasPetsTable: (): boolean => store.hasTable(PETS),'
|
|
370
|
+
* ```
|
|
371
|
+
* @example
|
|
372
|
+
* This example creates a Tools object and generates code for a Store that
|
|
373
|
+
* doesn't already have a Schema.
|
|
374
|
+
* ```js
|
|
375
|
+
* const store = createStore().setTable('pets', {
|
|
376
|
+
* fido: {price: 5},
|
|
377
|
+
* felix: {price: 4},
|
|
378
|
+
* });
|
|
379
|
+
* const tools = createTools(store);
|
|
380
|
+
* const [dTs, ts] = await tools.getPrettyStoreApi('shop');
|
|
381
|
+
*
|
|
382
|
+
* const dTsLines = dTs.split('\n');
|
|
383
|
+
* console.log(dTsLines[14]);
|
|
384
|
+
* // -> 'export type PetsTable = {[rowId: Id]: PetsRow};'
|
|
385
|
+
* console.log(dTsLines[19]);
|
|
386
|
+
* // -> 'export type PetsRow = {price: number};'
|
|
387
|
+
*
|
|
388
|
+
* const tsLines = ts.split('\n');
|
|
389
|
+
* console.log(tsLines[75]);
|
|
390
|
+
* // -> ' hasPetsTable: (): boolean => store.hasTable(PETS),'
|
|
391
|
+
* ```
|
|
242
392
|
* @category Modelling
|
|
243
393
|
* @since v2.2.0
|
|
244
394
|
*/
|