tinybase 6.0.0-beta.1 → 6.0.0-beta.2

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.
@@ -1,588 +0,0 @@
1
- /**
2
- * The tools module of the TinyBase project provides utilities for working with
3
- * TinyBase during development.
4
- *
5
- * This module is not intended to be directly used at runtime in a production
6
- * environment.
7
- * @packageDocumentation
8
- * @module tools
9
- * @since v2.2.0
10
- */
11
-
12
- import type {Store, TablesSchema, ValuesSchema} from '../store/index.d.cts';
13
- import type {Id} from '../common/index.d.cts';
14
-
15
- /**
16
- * The StoreStats type describes a set of statistics about the Store, and
17
- * is used for debugging purposes.
18
- *
19
- * A StoreStats object is returned from the getStoreStats method.
20
- * @category Statistics
21
- * @since v2.2.0
22
- */
23
- export type StoreStats = {
24
- /**
25
- * The number of Table objects in the Store.
26
- * @category Stat
27
- * @since v2.2.0
28
- */
29
- totalTables: number;
30
- /**
31
- * The number of Row objects in the Store, across all Table objects.
32
- * @category Stat
33
- * @since v2.2.0
34
- */
35
- totalRows: number;
36
- /**
37
- * The number of Cell objects in the Store, across all Row objects, across all
38
- * Table objects.
39
- * @category Stat
40
- * @since v2.2.0
41
- */
42
- totalCells: number;
43
- /**
44
- * The number of Value objects in the Store, since v3.0.
45
- * @category Stat
46
- * @since v2.2.0
47
- */
48
- totalValues: number;
49
- /**
50
- * The string length of the Store when serialized to JSON.
51
- * @category Stat
52
- * @since v2.2.0
53
- */
54
- jsonLength: number;
55
- /**
56
- * Additional detailed statistics about the Store if the `detail` flag is
57
- * specified in the getStoreStats method.
58
- * @category Stat
59
- * @since v2.2.0
60
- */
61
- detail?: StoreStatsDetail;
62
- };
63
-
64
- /**
65
- * The StoreStatsDetail type describes a more detailed set of statistics about
66
- * the Store, and is used for debugging purposes.
67
- *
68
- * A StoreStatsDetail object is added to the StoreStats object (returned from
69
- * the getStoreStats method) when the `detail` flag is specified.
70
- * @category Statistics
71
- * @since v2.2.0
72
- */
73
- export type StoreStatsDetail = {
74
- /**
75
- * Information about each Table in the Store.
76
- * @category Stat
77
- * @since v2.2.0
78
- */
79
- tables: {[tableId: Id]: StoreStatsTableDetail};
80
- };
81
-
82
- /**
83
- * The StoreStatsTableDetail type describes a detailed set of statistics about a
84
- * single Table in the Store, and is used for debugging purposes.
85
- * @category Statistics
86
- * @since v2.2.0
87
- */
88
- export type StoreStatsTableDetail = {
89
- /**
90
- * The number of Row objects in the Table.
91
- * @category Stat
92
- * @since v2.2.0
93
- */
94
- tableRows: number;
95
- /**
96
- * The number of Cell objects in the Table, across all Row objects.
97
- * @category Stat
98
- * @since v2.2.0
99
- */
100
- tableCells: number;
101
- /**
102
- * Detail about the Table object.
103
- * @category Stat
104
- * @since v2.2.0
105
- */
106
- rows: {[rowId: Id]: StoreStatsRowDetail};
107
- };
108
-
109
- /**
110
- * The StoreStatsRowDetail type describes statistics about a single Row in the
111
- * Store, and is used for debugging purposes.
112
- * @category Statistics
113
- * @since v2.2.0
114
- */
115
- export type StoreStatsRowDetail = {
116
- /**
117
- * The number of Cell objects in the Row.
118
- * @category Stat
119
- * @since v2.2.0
120
- */
121
- rowCells: number;
122
- };
123
-
124
- /**
125
- * A Tools object lets you run various utilities on, and get certain information
126
- * about, Store objects in development.
127
- * @see Developer Tools guides
128
- * @category Tools
129
- * @since v2.2.0
130
- */
131
- export interface Tools {
132
- //
133
- /* eslint-disable max-len */
134
- /**
135
- * The getStoreStats method provides a set of statistics about the Store, and
136
- * is used for debugging purposes.
137
- * @param detail An optional boolean that indicates more detailed stats about
138
- * the inner structure of the Store should be returned.
139
- * @returns A StoreStats object containing statistics about the Store.
140
- * @example
141
- * This example creates a Tools object and gets basic statistics about it.
142
- *
143
- * ```js
144
- * import {createStore} from 'tinybase';
145
- * import {createTools} from 'tinybase/tools';
146
- *
147
- * const store = createStore()
148
- * .setTable('pets', {
149
- * fido: {species: 'dog', color: 'brown'},
150
- * felix: {species: 'cat', color: 'black'},
151
- * cujo: {species: 'dog', color: 'black'},
152
- * })
153
- * .setTable('species', {
154
- * dog: {price: 5},
155
- * cat: {price: 4},
156
- * })
157
- * .setValues({open: true, employees: 3});
158
- * console.log(createTools(store).getStoreStats());
159
- * // -> {totalTables: 2, totalRows: 5, totalCells: 8, totalValues: 2, jsonLength: 212}
160
- * ```
161
- * @example
162
- * This example creates a Tools object and gets detailed statistics about it.
163
- *
164
- * ```js
165
- * import {createStore} from 'tinybase';
166
- * import {createTools} from 'tinybase/tools';
167
- *
168
- * const store = createStore()
169
- * .setTable('pets', {
170
- * fido: {species: 'dog', color: 'brown'},
171
- * felix: {species: 'cat', color: 'black'},
172
- * cujo: {species: 'dog', color: 'black'},
173
- * })
174
- * .setTable('species', {
175
- * dog: {price: 5},
176
- * cat: {price: 4},
177
- * });
178
- * const stats = createTools(store).getStoreStats(true);
179
- *
180
- * console.log(stats.totalTables);
181
- * // -> 2
182
- * console.log(stats.totalRows);
183
- * // -> 5
184
- * console.log(stats.totalCells);
185
- * // -> 8
186
- * console.log(stats.detail.tables.pets.tableRows);
187
- * // -> 3
188
- * console.log(stats.detail.tables.pets.tableCells);
189
- * // -> 6
190
- * console.log(stats.detail.tables.pets.rows);
191
- * // -> {fido: {rowCells: 2}, felix: {rowCells: 2}, cujo: {rowCells: 2}}
192
- * ```
193
- * @category Statistics
194
- * @since v2.2.0
195
- */
196
- getStoreStats(detail?: boolean): StoreStats;
197
-
198
- /* eslint-enable max-len */
199
-
200
- /**
201
- * The getStoreTablesSchema method returns the TablesSchema of the Store as an
202
- * object.
203
- *
204
- * If the Store does not already have an explicit TablesSchema associated with
205
- * it, the data in the Store will be scanned to attempt to infer a new
206
- * TablesSchema.
207
- *
208
- * To be successful, this requires all the values of a given Cell across a
209
- * Table object's Row objects to have a consistent type. If a given Cell Id
210
- * appears in every Row, then a `default` for that Cell is specified in the
211
- * TablesSchema, based on the most common value found.
212
- * @returns A TablesSchema object for the Store.
213
- * @example
214
- * This example creates a Tools object and gets the schema of a Store that
215
- * already has a TablesSchema.
216
- *
217
- * ```js
218
- * import {createStore} from 'tinybase';
219
- * import {createTools} from 'tinybase/tools';
220
- *
221
- * const store = createStore().setTablesSchema({
222
- * pets: {
223
- * species: {type: 'string'},
224
- * color: {type: 'string'},
225
- * },
226
- * species: {
227
- * price: {type: 'number'},
228
- * },
229
- * });
230
- * const schema = createTools(store).getStoreTablesSchema();
231
- * console.log(schema.pets);
232
- * // -> {species: {type: 'string'}, color: {type: 'string'}}
233
- * ```
234
- * @example
235
- * This example creates a Tools object and infers the schema of a Store that
236
- * doesn't already have a TablesSchema.
237
- *
238
- * ```js
239
- * import {createStore} from 'tinybase';
240
- * import {createTools} from 'tinybase/tools';
241
- *
242
- * const store = createStore()
243
- * .setTable('pets', {
244
- * fido: {species: 'dog', color: 'brown'},
245
- * felix: {species: 'cat', color: 'black'},
246
- * cujo: {species: 'dog', color: 'black'},
247
- * })
248
- * .setTable('species', {
249
- * dog: {price: 5, barks: true},
250
- * cat: {price: 4, purrs: true},
251
- * });
252
- * const schema = createTools(store).getStoreTablesSchema();
253
- * console.log(schema.pets.species);
254
- * // -> {type: 'string', default: 'dog'}
255
- * console.log(schema.pets.color);
256
- * // -> {type: 'string', default: 'black'}
257
- * console.log(schema.species.price);
258
- * // -> {type: 'number', default: 5}
259
- * console.log(schema.species.barks);
260
- * // -> {type: 'boolean'}
261
- * console.log(schema.species.purrs);
262
- * // -> {type: 'boolean'}
263
- * ```
264
- * @category Modelling
265
- * @since v3.0.0
266
- */
267
- getStoreTablesSchema(): TablesSchema;
268
-
269
- /**
270
- * The getStoreValuesSchema method returns the ValuesSchema of the Store as an
271
- * object.
272
- *
273
- * If the Store does not already have an explicit ValuesSchema associated with
274
- * it, the data in the Store will be scanned to infer a new ValuesSchema,
275
- * based on the types of the Values present. Note that, unlike the inference
276
- * of Cell values in the TablesSchema, it is not able to determine whether a
277
- * Value should have a default or not.
278
- * @returns A ValuesSchema object for the Store.
279
- * @example
280
- * This example creates a Tools object and gets the schema of a Store that
281
- * already has a ValuesSchema.
282
- *
283
- * ```js
284
- * import {createStore} from 'tinybase';
285
- * import {createTools} from 'tinybase/tools';
286
- *
287
- * const store = createStore().setValuesSchema({
288
- * open: {type: 'boolean', default: true},
289
- * employees: {type: 'number'},
290
- * });
291
- *
292
- * const schema = createTools(store).getStoreValuesSchema();
293
- * console.log(schema);
294
- * // -> {open: {type: 'boolean', default: true}, employees: {type: 'number'}}
295
- * ```
296
- * @example
297
- * This example creates a Tools object and infers the schema of a Store that
298
- * doesn't already have a ValuesSchema.
299
- *
300
- * ```js
301
- * import {createStore} from 'tinybase';
302
- * import {createTools} from 'tinybase/tools';
303
- *
304
- * const store = createStore().setValues({open: true, employees: 3});
305
- * const schema = createTools(store).getStoreValuesSchema();
306
- *
307
- * console.log(schema);
308
- * // -> {open: {type: 'boolean'}, employees: {type: 'number'}}
309
- * ```
310
- * @category Modelling
311
- * @since v3.0.0
312
- */
313
- getStoreValuesSchema(): ValuesSchema;
314
-
315
- /**
316
- * The getStoreApi method returns code-generated `.d.cts` and `.ts(x)` files
317
- * that describe the schema of a Store and React bindings (since v3.1) in an
318
- * ORM style.
319
- *
320
- * If the Store does not already have an explicit TablesSchema or ValuesSchema
321
- * associated with it, the data in the Store will be scanned to attempt to
322
- * infer new schemas. The method returns four strings (which should be saved
323
- * as files) though if no schema can be inferred, the strings will be empty.
324
- *
325
- * The method takes a single argument which represents the name you want the
326
- * generated store object to have in code. You are expected to save the four
327
- * files yourself, as the following:
328
- *
329
- * - `[storeName].d.cts` as the main definition,
330
- * - `[storeName].ts` as the main library,
331
- * - `[storeName]-ui-react.d.cts` as the ui-react module definition,
332
- * - `[storeName]-ui-react.tsx` as the ui-react module library.
333
- *
334
- * Also you should save these alongside each other so that the `.ts(x)` files
335
- * can import types from the `.d.cts` files.
336
- *
337
- * The `.d.cts` and `.ts(x)` files that are generated are designed to resemble
338
- * the main TinyBase Store and React binding files, but provide named types
339
- * and methods that describe the domain of the schema in the Store.
340
- *
341
- * For example, from a Store that has a `pets` Table, you will get methods
342
- * like `getPetsTable`, types like `PetsRow`, and hooks and components that
343
- * are more specific versions of the underlying getTable method or the Row
344
- * type, and so on. For example:
345
- *
346
- * |Store type|Equivalent generated type|
347
- * |-|-|
348
- * |Table|[Table]Table|
349
- * |Row|[Table]Row|
350
- * |(Cell) Id|[Table]CellId|
351
- * |CellCallback|[Table]CellCallback|
352
- * |...|...|
353
- *
354
- * |Store method|Equivalent generated method|
355
- * |-|-|
356
- * |setTable|set[Table]Table|
357
- * |hasRow|has[Table]Row|
358
- * |getCell|get[Table][Cell]Cell|
359
- * |...|...|
360
- *
361
- * Equivalent to the TinyBase createStore function, a `create[StoreName]`
362
- * function will also be created. This acts as the main entry point to the
363
- * generated implementation.
364
- *
365
- * Each method is refined correctly to take, or return, the types specified by
366
- * the schema. For example, if the `pets` Table has a numeric `price` Cell in
367
- * the schema, the `getPetsPriceCell` method will be typed to return a number.
368
- *
369
- * The tables above include just a sample of the generated output. For the
370
- * full set of methods and types generated by this method, inspect the output
371
- * directly.
372
- * @param storeName The name you want to provide to the generated Store, which
373
- * should also be used to save the `.d.cts`, `.ts`, and `.tsx` files.
374
- * @returns A set of four strings representing the contents of the `.d.cts`,
375
- * `.ts`, and `.tsx` files for the generated Store and React modules.
376
- * @example
377
- * This example creates a Tools object and generates code for a Store that
378
- * already has a TablesSchema.
379
- *
380
- * ```js
381
- * import {createStore} from 'tinybase';
382
- * import {createTools} from 'tinybase/tools';
383
- *
384
- * const store = createStore().setTablesSchema({
385
- * pets: {
386
- * price: {type: 'number'},
387
- * },
388
- * });
389
- * const [dTs, ts, _uiReactDTs, _uiReactTsx] =
390
- * createTools(store).getStoreApi('shop');
391
- *
392
- * const dTsLines = dTs.split('\n');
393
- * console.log(dTsLines[3]);
394
- * // -> `export type Tables = {'pets'?: {[rowId: Id]: {'price'?: number}}};`
395
- *
396
- * const tsLines = ts.split('\n');
397
- * console.log(tsLines[39]);
398
- * // -> 'getPetsTable: (): PetsTable => store.getTable(PETS) as PetsTable,'
399
- * ```
400
- * @example
401
- * This example creates a Tools object and generates code for a Store that
402
- * doesn't already have a TablesSchema.
403
- *
404
- * ```js
405
- * import {createStore} from 'tinybase';
406
- * import {createTools} from 'tinybase/tools';
407
- *
408
- * const store = createStore().setTable('pets', {
409
- * fido: {price: 5},
410
- * felix: {price: 4},
411
- * });
412
- * const [dTs, ts, _uiReactDTs, _uiReactTsx] =
413
- * createTools(store).getStoreApi('shop');
414
- *
415
- * const dTsLines = dTs.split('\n');
416
- * console.log(dTsLines[3]);
417
- * // -> `export type Tables = {'pets'?: {[rowId: Id]: {'price': number}}};`
418
- *
419
- * const tsLines = ts.split('\n');
420
- * console.log(tsLines[41]);
421
- * // -> 'getPetsTable: (): PetsTable => store.getTable(PETS) as PetsTable,'
422
- * ```
423
- * @category Modelling
424
- * @since v2.2.0
425
- */
426
- getStoreApi(storeName: string): [string, string, string, string];
427
-
428
- /**
429
- * The getPrettyStoreApi method attempts to return prettified code-generated
430
- * `.d.cts` and `.ts(x)` files that describe the schema of a Store and React
431
- * bindings (since v3.1) in an ORM style.
432
- *
433
- * This is simply a wrapper around the getStoreApi method that attempts to
434
- * invoke the `prettier` module (which it hopes you have installed) to format
435
- * the generated code. If `prettier` is not present, the output will resemble
436
- * that of the underlying getStoreApi method.
437
- *
438
- * The method is asynchronous, so you should use the `await` keyword or handle
439
- * the results as a promise.
440
- *
441
- * The method takes a single argument which represents the name you want the
442
- * generated store object to have in code. You are expected to save the four
443
- * files yourself, as the following:
444
- *
445
- * - `[storeName].d.cts` as the main definition,
446
- * - `[storeName].ts` as the main library,
447
- * - `[storeName]-ui-react.d.cts` as the ui-react module definition,
448
- * - `[storeName]-ui-react.tsx` as the ui-react module library.
449
- *
450
- * Also you should save these alongside each other so that the `.ts(x)` files
451
- * can import types from the `.d.cts` files.
452
- *
453
- * See the documentation for the getStoreApi method for details of the content
454
- * of the generated files.
455
- * @param storeName The name you want to provide to the generated Store, which
456
- * should also be used to save the `.d.cts`, `.ts`, and `.tsx` files.
457
- * @returns A set of four strings representing the contents of the `.d.cts`,
458
- * `.ts`, and `.tsx` files for the generated Store and React modules.
459
- * @example
460
- * This example creates a Tools object and generates code for a Store that
461
- * already has a TablesSchema.
462
- *
463
- * ```js
464
- * import {createStore} from 'tinybase';
465
- * import {createTools} from 'tinybase/tools';
466
- *
467
- * const store = createStore().setTablesSchema({
468
- * pets: {
469
- * price: {type: 'number'},
470
- * },
471
- * });
472
- * const [dTs, ts, _uiReactDTs, _uiReactTsx] =
473
- * await createTools(store).getPrettyStoreApi('shop');
474
- *
475
- * const dTsLines = dTs.split('\n');
476
- * console.log(dTsLines[15]);
477
- * // -> `export type Tables = {pets?: {[rowId: Id]: {price?: number}}};`
478
- *
479
- * const tsLines = ts.split('\n');
480
- * console.log(tsLines[89]);
481
- * // -> ' hasPetsTable: (): boolean => store.hasTable(PETS),'
482
- * ```
483
- * @example
484
- * This example creates a Tools object and generates code for a Store that
485
- * doesn't already have a TablesSchema.
486
- *
487
- * ```js
488
- * import {createStore} from 'tinybase';
489
- * import {createTools} from 'tinybase/tools';
490
- *
491
- * const store = createStore().setTable('pets', {
492
- * fido: {price: 5},
493
- * felix: {price: 4},
494
- * });
495
- * const [dTs, ts, _uiReactDTs, _uiReactTsx] =
496
- * await createTools(store).getPrettyStoreApi('shop');
497
- *
498
- * const dTsLines = dTs.split('\n');
499
- * console.log(dTsLines[15]);
500
- * // -> 'export type Tables = {pets?: {[rowId: Id]: {price: number}}};'
501
- *
502
- * const tsLines = ts.split('\n');
503
- * console.log(tsLines[91]);
504
- * // -> ' hasPetsTable: (): boolean => store.hasTable(PETS),'
505
- * ```
506
- * @category Modelling
507
- * @since v2.2.0
508
- */
509
- getPrettyStoreApi(
510
- storeName: string,
511
- ): Promise<[string, string, string, string]>;
512
-
513
- /**
514
- * The getStore method returns a reference to the underlying Store that is
515
- * backing this Tools object.
516
- * @returns A reference to the Store.
517
- * @example
518
- * This example creates a Tools object against a newly-created Store and
519
- * then gets its reference in order to update its data.
520
- *
521
- * ```js
522
- * import {createStore} from 'tinybase';
523
- * import {createTools} from 'tinybase/tools';
524
- *
525
- * const tools = createTools(createStore());
526
- * tools.getStore().setCell('species', 'dog', 'price', 5);
527
- * console.log(tools.getStoreStats().totalCells);
528
- * // -> 1
529
- * ```
530
- * @category Getter
531
- * @since v3.0.0
532
- */
533
- getStore(): Store;
534
- //
535
- }
536
-
537
- /* eslint-disable max-len */
538
- /**
539
- * The createTools function creates a Tools object, and is the main entry point
540
- * into the tools module.
541
- *
542
- * A given Store can only have one Tools object associated with it. If you call
543
- * this function twice on the same Store, your second call will return a
544
- * reference to the Tools object created by the first.
545
- * @param store The Store for which to register tools.
546
- * @returns A reference to the new Tools object.
547
- * @example
548
- * This example creates a Tools object.
549
- *
550
- * ```js
551
- * import {createStore} from 'tinybase';
552
- * import {createTools} from 'tinybase/tools';
553
- *
554
- * const store = createStore()
555
- * .setTable('pets', {
556
- * fido: {species: 'dog', color: 'brown'},
557
- * felix: {species: 'cat', color: 'black'},
558
- * cujo: {species: 'dog', color: 'black'},
559
- * })
560
- * .setTable('species', {
561
- * dog: {price: 5},
562
- * cat: {price: 4},
563
- * })
564
- * .setValues({open: true, employees: 3});
565
- * console.log(createTools(store).getStoreStats());
566
- * // -> {totalTables: 2, totalRows: 5, totalCells: 8, totalValues: 2, jsonLength: 212}
567
- * ```
568
- * @example
569
- * This example creates a Tools object, and calls the method a second time
570
- * for the same Store to return the same object.
571
- *
572
- * ```js
573
- * import {createStore} from 'tinybase';
574
- * import {createTools} from 'tinybase/tools';
575
- *
576
- * const store = createStore();
577
- * const tools1 = createTools(store);
578
- * const tools2 = createTools(store);
579
- * console.log(tools1 === tools2);
580
- * // -> true
581
- * ```
582
- * @category Creation
583
- * @since v2.2.0
584
- */
585
- export function createTools(store: Store): Tools;
586
- //
587
-
588
- /* eslint-enable max-len */