tinybase 2.2.6 → 3.0.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.
Files changed (110) hide show
  1. package/bin/cli.js +1 -1
  2. package/lib/checkpoints.d.ts +18 -3
  3. package/lib/checkpoints.js +1 -1
  4. package/lib/checkpoints.js.gz +0 -0
  5. package/lib/debug/checkpoints.d.ts +18 -3
  6. package/lib/debug/checkpoints.js +97 -49
  7. package/lib/debug/indexes.js +31 -16
  8. package/lib/debug/metrics.js +31 -16
  9. package/lib/debug/persisters.d.ts +33 -23
  10. package/lib/debug/persisters.js +10 -5
  11. package/lib/debug/queries.js +2 -2
  12. package/lib/debug/relationships.js +31 -16
  13. package/lib/debug/store.d.ts +2022 -383
  14. package/lib/debug/store.js +440 -108
  15. package/lib/debug/tinybase.js +508 -140
  16. package/lib/debug/tools.d.ts +89 -38
  17. package/lib/debug/tools.js +757 -453
  18. package/lib/debug/ui-react.d.ts +1525 -381
  19. package/lib/debug/ui-react.js +163 -12
  20. package/lib/es6/checkpoints.d.ts +18 -3
  21. package/lib/es6/checkpoints.js +1 -1
  22. package/lib/es6/checkpoints.js.gz +0 -0
  23. package/lib/es6/indexes.js +1 -1
  24. package/lib/es6/indexes.js.gz +0 -0
  25. package/lib/es6/metrics.js +1 -1
  26. package/lib/es6/metrics.js.gz +0 -0
  27. package/lib/es6/persisters.d.ts +33 -23
  28. package/lib/es6/persisters.js +1 -1
  29. package/lib/es6/persisters.js.gz +0 -0
  30. package/lib/es6/relationships.js +1 -1
  31. package/lib/es6/relationships.js.gz +0 -0
  32. package/lib/es6/store.d.ts +2022 -383
  33. package/lib/es6/store.js +1 -1
  34. package/lib/es6/store.js.gz +0 -0
  35. package/lib/es6/tinybase.js +1 -1
  36. package/lib/es6/tinybase.js.gz +0 -0
  37. package/lib/es6/tools.d.ts +89 -38
  38. package/lib/es6/tools.js +1 -1
  39. package/lib/es6/tools.js.gz +0 -0
  40. package/lib/es6/ui-react.d.ts +1525 -381
  41. package/lib/es6/ui-react.js +1 -1
  42. package/lib/es6/ui-react.js.gz +0 -0
  43. package/lib/indexes.js +1 -1
  44. package/lib/indexes.js.gz +0 -0
  45. package/lib/metrics.js +1 -1
  46. package/lib/metrics.js.gz +0 -0
  47. package/lib/persisters.d.ts +33 -23
  48. package/lib/persisters.js +1 -1
  49. package/lib/persisters.js.gz +0 -0
  50. package/lib/relationships.js +1 -1
  51. package/lib/relationships.js.gz +0 -0
  52. package/lib/store.d.ts +2022 -383
  53. package/lib/store.js +1 -1
  54. package/lib/store.js.gz +0 -0
  55. package/lib/tinybase.js +1 -1
  56. package/lib/tinybase.js.gz +0 -0
  57. package/lib/tools.d.ts +89 -38
  58. package/lib/tools.js +1 -1
  59. package/lib/tools.js.gz +0 -0
  60. package/lib/ui-react.d.ts +1525 -381
  61. package/lib/ui-react.js +1 -1
  62. package/lib/ui-react.js.gz +0 -0
  63. package/lib/umd/checkpoints.d.ts +18 -3
  64. package/lib/umd/checkpoints.js +1 -1
  65. package/lib/umd/checkpoints.js.gz +0 -0
  66. package/lib/umd/indexes.js +1 -1
  67. package/lib/umd/indexes.js.gz +0 -0
  68. package/lib/umd/metrics.js +1 -1
  69. package/lib/umd/metrics.js.gz +0 -0
  70. package/lib/umd/persisters.d.ts +33 -23
  71. package/lib/umd/persisters.js +1 -1
  72. package/lib/umd/persisters.js.gz +0 -0
  73. package/lib/umd/relationships.js +1 -1
  74. package/lib/umd/relationships.js.gz +0 -0
  75. package/lib/umd/store.d.ts +2022 -383
  76. package/lib/umd/store.js +1 -1
  77. package/lib/umd/store.js.gz +0 -0
  78. package/lib/umd/tinybase.js +1 -1
  79. package/lib/umd/tinybase.js.gz +0 -0
  80. package/lib/umd/tools.d.ts +89 -38
  81. package/lib/umd/tools.js +1 -1
  82. package/lib/umd/tools.js.gz +0 -0
  83. package/lib/umd/ui-react.d.ts +1525 -381
  84. package/lib/umd/ui-react.js +1 -1
  85. package/lib/umd/ui-react.js.gz +0 -0
  86. package/lib/umd-es6/checkpoints.d.ts +18 -3
  87. package/lib/umd-es6/checkpoints.js +1 -1
  88. package/lib/umd-es6/checkpoints.js.gz +0 -0
  89. package/lib/umd-es6/indexes.js +1 -1
  90. package/lib/umd-es6/indexes.js.gz +0 -0
  91. package/lib/umd-es6/metrics.js +1 -1
  92. package/lib/umd-es6/metrics.js.gz +0 -0
  93. package/lib/umd-es6/persisters.d.ts +33 -23
  94. package/lib/umd-es6/persisters.js +1 -1
  95. package/lib/umd-es6/persisters.js.gz +0 -0
  96. package/lib/umd-es6/relationships.js +1 -1
  97. package/lib/umd-es6/relationships.js.gz +0 -0
  98. package/lib/umd-es6/store.d.ts +2022 -383
  99. package/lib/umd-es6/store.js +1 -1
  100. package/lib/umd-es6/store.js.gz +0 -0
  101. package/lib/umd-es6/tinybase.js +1 -1
  102. package/lib/umd-es6/tinybase.js.gz +0 -0
  103. package/lib/umd-es6/tools.d.ts +89 -38
  104. package/lib/umd-es6/tools.js +1 -1
  105. package/lib/umd-es6/tools.js.gz +0 -0
  106. package/lib/umd-es6/ui-react.d.ts +1525 -381
  107. package/lib/umd-es6/ui-react.js +1 -1
  108. package/lib/umd-es6/ui-react.js.gz +0 -0
  109. package/package.json +26 -26
  110. package/readme.md +20 -19
@@ -19,11 +19,19 @@ const arrayShift = (array) => array.shift();
19
19
 
20
20
  const jsonParse = JSON.parse;
21
21
  const isFiniteNumber = isFinite;
22
+ const isInstanceOf = (thing, cls) => thing instanceof cls;
22
23
  const isUndefined = (thing) => thing == void 0;
23
24
  const isTypeStringOrBoolean = (type) => type == STRING || type == BOOLEAN;
24
25
  const isString = (thing) => getTypeOf(thing) == STRING;
25
26
  const isArray = (thing) => Array.isArray(thing);
26
27
 
28
+ const getCellOrValueType = (cell) => {
29
+ const type = getTypeOf(cell);
30
+ return isTypeStringOrBoolean(type) || (type == NUMBER && isFiniteNumber(cell))
31
+ ? type
32
+ : void 0;
33
+ };
34
+
27
35
  const collHas = (coll, keyOrValue) => coll?.has(keyOrValue) ?? false;
28
36
  const collValues = (coll) => [...(coll?.values() ?? [])];
29
37
  const collForEach = (coll, cb) => coll?.forEach(cb);
@@ -55,6 +63,7 @@ const setAdd = (set, value) => set?.add(value);
55
63
  const pairNew = (value) => [value, value];
56
64
  const pairNewMap = () => [mapNew(), mapNew()];
57
65
 
66
+ const NON_ALPHA = /[^A-Za-z]+/;
58
67
  const NON_ALPHANUMERIC = /[^A-Za-z0-9]+/;
59
68
  const JSDOC = /^( *)\/\*\* *(.*?) *\*\/$/gm;
60
69
  const substr = (str, start, end) => str.substring(start, end);
@@ -92,7 +101,14 @@ const camel = (str, firstCap = 0) =>
92
101
  ),
93
102
  );
94
103
  const snake = (str) =>
95
- upper(join((str ? str : ' ').split(NON_ALPHANUMERIC), '_'));
104
+ upper(
105
+ join(
106
+ (str && !NON_ALPHA.test(str[0]) ? str : ' ' + str).split(
107
+ NON_ALPHANUMERIC,
108
+ ),
109
+ '_',
110
+ ),
111
+ );
96
112
  const comment = (doc) => `/** ${doc}. */`;
97
113
  const getCodeFunctions = () => {
98
114
  const allImports = pairNewMap();
@@ -104,8 +120,7 @@ const getCodeFunctions = () => {
104
120
  arrayForEach(items, (item) =>
105
121
  setAdd(mapEnsure(allImports[location], source, setNew), item),
106
122
  );
107
- const addType = (name, body = '', doc = '') =>
108
- mapUnique(types, name, [body, doc]);
123
+ const addType = (name, body, doc) => mapUnique(types, name, [body, doc]);
109
124
  const addMethod = (name, parameters, returnType, body, doc, generic = '') =>
110
125
  mapUnique(methods, name, [parameters, returnType, body, doc, generic]);
111
126
  const addFunction = (name, parameters, body) =>
@@ -171,16 +186,11 @@ const getCodeFunctions = () => {
171
186
  const object = Object;
172
187
  const objIds = object.keys;
173
188
  const objFreeze = object.freeze;
189
+ const isObject = (obj) =>
190
+ isInstanceOf(obj, object) && obj.constructor == object;
174
191
  const objMap = (obj, cb) =>
175
192
  arrayMap(object.entries(obj), ([id, value]) => cb(value, id));
176
- const objIsEmpty = (obj) => arrayIsEmpty(objIds(obj));
177
-
178
- const getCellType = (cell) => {
179
- const type = getTypeOf(cell);
180
- return isTypeStringOrBoolean(type) || (type == NUMBER && isFiniteNumber(cell))
181
- ? type
182
- : void 0;
183
- };
193
+ const objIsEmpty = (obj) => isObject(obj) && arrayIsEmpty(objIds(obj));
184
194
 
185
195
  const getCreateFunction = (getFunction) => {
186
196
  const getFunctionsByStore = /* @__PURE__ */ new WeakMap();
@@ -192,7 +202,6 @@ const getCreateFunction = (getFunction) => {
192
202
  };
193
203
  };
194
204
 
195
- const THE_CONTENT_OF = 'the content of';
196
205
  const THE_STORE = 'the Store';
197
206
  const A_FUNCTION_FOR = 'A function for';
198
207
  const EXPORT = 'export';
@@ -201,13 +210,17 @@ const OR_UNDEFINED = ' | undefined';
201
210
  const REGISTERS_A_LISTENER = `Registers a ${LISTENER} that will be called`;
202
211
  const REPRESENTS = 'Represents';
203
212
  const RETURNS_VOID = ' => void';
204
- const THE_CONTENT_OF_THE_STORE = `${THE_CONTENT_OF} ${THE_STORE}`;
205
213
  const THE_END_OF_THE_TRANSACTION = 'the end of the transaction';
206
214
  const THE_SPECIFIED_ROW = 'the specified Row';
215
+ const A_STRING_SERIALIZATION_OF = 'a string serialization of';
216
+ const getTheContentOfDoc = (content = 0, theStore = 0) =>
217
+ `the ${CONTENT[content]}content of${theStore ? ` ${THE_STORE}` : ''}`;
218
+ const getTheContentOfTheStoreDoc = (verb, content = 0, set = 0) =>
219
+ `${VERBS[verb]} ${getTheContentOfDoc(content, 1)}${set ? ' when set' : ''}`;
207
220
  const getRowTypeDoc = (tableId, set = 0) =>
208
221
  `${REPRESENTS} a Row when ${
209
222
  set ? 's' : 'g'
210
- }etting ${THE_CONTENT_OF} the '${tableId}' Table`;
223
+ }etting ${getTheContentOfDoc()} the '${tableId}' Table`;
211
224
  const getIdsDoc = (idsNoun, parentNoun, sorted = 0) =>
212
225
  `Gets ${
213
226
  sorted ? 'sorted, paginated' : 'the'
@@ -217,27 +230,37 @@ const getForEachDoc = (childNoun, parentNoun) =>
217
230
  const getHasDoc = (childNoun, parentNoun = THE_STORE) =>
218
231
  `Gets whether ${childNoun} exists in ${parentNoun}`;
219
232
  const getCallbackDoc = (takes) => `A function that takes ${takes}`;
220
- const getListenerTypeDoc = (childNoun = 0, parentNoun = 0) =>
221
- `${A_FUNCTION_FOR} listening to changes to ${nouns[childNoun]} in ${nouns[parentNoun]}`;
233
+ const getListenerTypeDoc = (childNoun, parentNoun = 0) =>
234
+ `${A_FUNCTION_FOR} listening to changes to ${NOUNS[childNoun]} in ${NOUNS[parentNoun]}`;
222
235
  const getListenerDoc = (childNoun, parentNoun, pluralChild = 0) =>
223
236
  `${REGISTERS_A_LISTENER} whenever ${childNoun} in ${parentNoun} change` +
224
237
  (pluralChild ? EMPTY_STRING : 's');
225
- const getStoreContentDoc = (verb = 0) =>
226
- `${verbs[verb]} ${THE_CONTENT_OF_THE_STORE}`;
227
238
  const getTableDoc = (tableId) => `the '${tableId}' Table`;
228
239
  const getRowDoc = (tableId) =>
229
240
  `${THE_SPECIFIED_ROW} in ${getTableDoc(tableId)}`;
230
241
  const getCellDoc = (cellId) => `the '${cellId}' Cell`;
242
+ const getValueDoc = (valueId) => `the '${valueId}' Value`;
231
243
  const getTableContentDoc = (tableId, verb = 0) =>
232
- `${verbs[verb]} ${THE_CONTENT_OF} ${getTableDoc(tableId)}`;
244
+ `${VERBS[verb]} ${getTheContentOfDoc()} ${getTableDoc(tableId)}`;
233
245
  const getRowContentDoc = (tableId, verb = 0) =>
234
- `${verbs[verb]} ${THE_CONTENT_OF} ${getRowDoc(tableId)}`;
246
+ `${VERBS[verb]} ${getTheContentOfDoc()} ${getRowDoc(tableId)}`;
235
247
  const getCellContentDoc = (tableId, cellId, verb = 0) =>
236
- `${verbs[verb]} ${getCellDoc(cellId)} for ${getRowDoc(tableId)}`;
237
- const verbs = ['Gets', 'Sets', 'Sets part of', 'Deletes'];
238
- const nouns = [
248
+ `${VERBS[verb]} ${getCellDoc(cellId)} for ${getRowDoc(tableId)}`;
249
+ const getValueContentDoc = (valueId, verb = 0) =>
250
+ `${VERBS[verb]} ${getValueDoc(valueId)}`;
251
+ const VERBS = [
252
+ 'Gets',
253
+ 'Sets',
254
+ 'Sets part of',
255
+ 'Deletes',
256
+ REPRESENTS,
257
+ `Gets ${A_STRING_SERIALIZATION_OF}`,
258
+ `Sets ${A_STRING_SERIALIZATION_OF}`,
259
+ `${REGISTERS_A_LISTENER} whenever`,
260
+ ];
261
+ const NOUNS = [
239
262
  THE_STORE,
240
- 'anything',
263
+ 'Tables',
241
264
  'Table Ids',
242
265
  'a Table',
243
266
  'Row Ids',
@@ -245,17 +268,14 @@ const nouns = [
245
268
  'Cell Ids',
246
269
  'a Cell',
247
270
  'invalid Cell changes',
271
+ 'Values',
272
+ 'Value Ids',
273
+ 'a Value',
274
+ 'invalid Value changes',
248
275
  ];
276
+ const CONTENT = ['', 'tabular ', 'keyed value '];
249
277
 
250
- const COMMON_IMPORTS = [
251
- 'ChangedCells',
252
- 'Id',
253
- 'IdOrNull',
254
- 'Ids',
255
- 'InvalidCells',
256
- 'Json',
257
- 'Store',
258
- ];
278
+ const COMMON_IMPORTS = ['DoRollback', 'Id', 'IdOrNull', 'Ids', 'Json', 'Store'];
259
279
  const storeMethod = (method, parameters = EMPTY_STRING, cast = EMPTY_STRING) =>
260
280
  `store.${method}(${parameters})${cast ? ` as ${cast}` : EMPTY_STRING}`;
261
281
  const fluentStoreMethod = (method, parameters = EMPTY_STRING) =>
@@ -270,8 +290,8 @@ const storeListener = (
270
290
  }proxy(${LISTENER})${
271
291
  afterParameters ? `, ${afterParameters}` : EMPTY_STRING
272
292
  })`;
273
- const getStoreApi = (schema, module) => {
274
- if (objIsEmpty(schema)) {
293
+ const getStoreApi = (tablesSchema, valuesSchema, module) => {
294
+ if (objIsEmpty(tablesSchema) && objIsEmpty(valuesSchema)) {
275
295
  return pairNew(EMPTY_STRING);
276
296
  }
277
297
  const [
@@ -289,9 +309,10 @@ const getStoreApi = (schema, module) => {
289
309
  const moduleDefinition = `./${camel(module)}.d`;
290
310
  const storeType = camel(module, 1);
291
311
  const storeInstance = camel(storeType);
312
+ const createSteps = [];
292
313
  const tableTypes = mapNew();
293
- const mapTableSchema = (callback) =>
294
- objMap(schema, (_, tableId) => {
314
+ const mapTablesSchema = (callback) =>
315
+ objMap(tablesSchema, (_, tableId) => {
295
316
  return callback(
296
317
  tableId,
297
318
  mapEnsure(tableTypes, tableId, () => {
@@ -362,7 +383,7 @@ const getStoreApi = (schema, module) => {
362
383
  );
363
384
  });
364
385
  const mapCellSchema = (tableId, callback) =>
365
- objMap(schema[tableId], (cellSchema, cellId) =>
386
+ objMap(tablesSchema[tableId], (cellSchema, cellId) =>
366
387
  callback(
367
388
  cellId,
368
389
  cellSchema[TYPE],
@@ -371,342 +392,715 @@ const getStoreApi = (schema, module) => {
371
392
  camel(cellId, 1),
372
393
  ),
373
394
  );
374
- const tablesType = addType(
375
- 'Tables',
376
- `{${join(
377
- mapTableSchema(
378
- (tableId, tableTypes2) => `'${tableId}'?: ${tableTypes2[0]};`,
395
+ const mapValuesSchema = (callback) =>
396
+ objMap(valuesSchema, (valueSchema, valueId) =>
397
+ callback(
398
+ valueId,
399
+ valueSchema[TYPE],
400
+ valueSchema[DEFAULT],
401
+ addConstant(snake(valueId), `'${valueId}'`),
402
+ camel(valueId, 1),
379
403
  ),
380
- ' ',
381
- )}}`,
382
- `${REPRESENTS} ${THE_CONTENT_OF_THE_STORE}`,
383
- );
384
- const tableIdType = addType(
385
- 'TableId',
386
- join(
387
- mapTableSchema((tableId) => `'${tableId}'`),
388
- ' | ',
389
- ),
390
- `A Table Id in ${THE_STORE}`,
404
+ );
405
+ addImport(
406
+ 1,
407
+ moduleDefinition,
408
+ storeType,
409
+ `create${storeType} as create${storeType}Decl`,
391
410
  );
392
- const tableCallbackType = addType(
393
- 'TableCallback',
394
- `(...[tableId, rowCallback]: ${join(
395
- mapTableSchema(
396
- (tableId, tableTypes2) =>
397
- `[tableId: '${tableId}', forEachRow: (rowCallback: ${tableTypes2[5]})${RETURNS_VOID}]`,
411
+ if (!objIsEmpty(tablesSchema)) {
412
+ const tablesType = addType(
413
+ 'Tables',
414
+ `{${join(
415
+ mapTablesSchema(
416
+ (tableId, tableTypes2) => `'${tableId}'?: ${tableTypes2[0]};`,
417
+ ),
418
+ ' ',
419
+ )}}`,
420
+ getTheContentOfTheStoreDoc(4, 1),
421
+ );
422
+ const tableIdType = addType(
423
+ 'TableId',
424
+ join(
425
+ mapTablesSchema((tableId) => `'${tableId}'`),
426
+ ' | ',
398
427
  ),
399
- ' | ',
400
- )})${RETURNS_VOID}`,
401
- getCallbackDoc('a Table Id, and a Row iterator'),
402
- );
403
- const getCellChangeType = addType(
404
- 'GetCellChange',
405
- `(...[tableId, rowId, cellId]: ${join(
406
- mapTableSchema(
407
- (tableId, tableTypes2) =>
408
- `[tableId: '${tableId}', rowId: Id, cellId: ${tableTypes2[3]}]`,
428
+ `A Table Id in ${THE_STORE}`,
429
+ );
430
+ const tableCallbackType = addType(
431
+ 'TableCallback',
432
+ `(...[tableId, rowCallback]: ${join(
433
+ mapTablesSchema(
434
+ (tableId, tableTypes2) =>
435
+ `[tableId: '${tableId}', forEachRow: (rowCallback: ${tableTypes2[5]})${RETURNS_VOID}]`,
436
+ ),
437
+ ' | ',
438
+ )})${RETURNS_VOID}`,
439
+ getCallbackDoc('a Table Id, and a Row iterator'),
440
+ );
441
+ const getCellChangeType = addType(
442
+ 'GetCellChange',
443
+ `(...[tableId, rowId, cellId]: ${join(
444
+ mapTablesSchema(
445
+ (tableId, tableTypes2) =>
446
+ `[tableId: '${tableId}', rowId: Id, cellId: ${tableTypes2[3]}]`,
447
+ ),
448
+ ' | ',
449
+ )}) => CellChange`,
450
+ `${A_FUNCTION_FOR} returning information about any Cell's changes during a transaction`,
451
+ );
452
+ const tablesListenerType = addType(
453
+ 'TablesListener',
454
+ `(${storeInstance}: ${storeType}, getCellChange: ${getCellChangeType}${OR_UNDEFINED})${RETURNS_VOID}`,
455
+ getListenerTypeDoc(1),
456
+ );
457
+ const tableIdsListenerType = addType(
458
+ 'TableIdsListener',
459
+ `(${storeInstance}: ${storeType})${RETURNS_VOID}`,
460
+ getListenerTypeDoc(2),
461
+ );
462
+ const tableListenerType = addType(
463
+ 'TableListener',
464
+ `(${storeInstance}: ${storeType}, tableId: ${tableIdType}, getCellChange: ${getCellChangeType}${OR_UNDEFINED})${RETURNS_VOID}`,
465
+ getListenerTypeDoc(3),
466
+ );
467
+ const rowIdsListenerType = addType(
468
+ 'RowIdsListener',
469
+ `(${storeInstance}: ${storeType}, tableId: ${tableIdType})` +
470
+ RETURNS_VOID,
471
+ getListenerTypeDoc(4, 3),
472
+ );
473
+ const rowListenerType = addType(
474
+ 'RowListener',
475
+ `(${storeInstance}: ${storeType}, tableId: ${tableIdType}, rowId: Id, getCellChange: ${getCellChangeType}${OR_UNDEFINED})${RETURNS_VOID}`,
476
+ getListenerTypeDoc(5, 3),
477
+ );
478
+ const cellIdsListenerType = addType(
479
+ 'CellIdsListener',
480
+ `(${storeInstance}: ${storeType}, tableId: ${tableIdType}, rowId: Id)` +
481
+ RETURNS_VOID,
482
+ getListenerTypeDoc(6, 5),
483
+ );
484
+ const cellListenerType = addType(
485
+ 'CellListener',
486
+ `(...[${storeInstance}, tableId, rowId, cellId, newCell, oldCell, getCellChange]: ${join(
487
+ flat(
488
+ mapTablesSchema((tableId) =>
489
+ mapCellSchema(
490
+ tableId,
491
+ (cellId, type) =>
492
+ `[${storeInstance}: ${storeType}, tableId: '${tableId}', rowId: Id, cellId: '${cellId}', newCell: ${type}${OR_UNDEFINED}, oldCell: ${type}${OR_UNDEFINED}, getCellChange: ${getCellChangeType} | undefined]`,
493
+ ),
494
+ ),
495
+ ),
496
+ ' | ',
497
+ )})${RETURNS_VOID}`,
498
+ getListenerTypeDoc(7, 5),
499
+ );
500
+ const invalidCellListenerType = addType(
501
+ 'InvalidCellListener',
502
+ `(${storeInstance}: ${storeType}, tableId: Id, rowId: Id, cellId: Id, invalidCells: any[])${RETURNS_VOID}`,
503
+ getListenerTypeDoc(8),
504
+ );
505
+ addMethod(
506
+ `hasTables`,
507
+ EMPTY_STRING,
508
+ BOOLEAN,
509
+ storeMethod('hasTables'),
510
+ getHasDoc('any Table'),
511
+ );
512
+ addMethod(
513
+ `getTables`,
514
+ EMPTY_STRING,
515
+ tablesType,
516
+ storeMethod('getTables'),
517
+ getTheContentOfTheStoreDoc(0, 1),
518
+ );
519
+ addMethod(
520
+ `setTables`,
521
+ `tables: ${tablesType}`,
522
+ storeType,
523
+ fluentStoreMethod('setTables', 'tables'),
524
+ getTheContentOfTheStoreDoc(1, 1),
525
+ );
526
+ addMethod(
527
+ `delTables`,
528
+ EMPTY_STRING,
529
+ storeType,
530
+ fluentStoreMethod('delTables'),
531
+ getTheContentOfTheStoreDoc(3, 1),
532
+ );
533
+ addMethod(
534
+ `getTableIds`,
535
+ EMPTY_STRING,
536
+ `${tableIdType}[]`,
537
+ storeMethod('getTableIds', EMPTY_STRING, `${tableIdType}[]`),
538
+ getIdsDoc('Table', THE_STORE),
539
+ );
540
+ addMethod(
541
+ 'forEachTable',
542
+ `tableCallback: ${tableCallbackType}`,
543
+ 'void',
544
+ storeMethod('forEachTable', 'tableCallback as any'),
545
+ getForEachDoc('Table', THE_STORE),
546
+ );
547
+ const mapCellTypes = mapNew();
548
+ mapTablesSchema(
549
+ (
550
+ tableId,
551
+ [
552
+ tableType,
553
+ rowType,
554
+ rowWhenSetType,
555
+ cellIdType,
556
+ cellCallbackType,
557
+ rowCallbackType,
558
+ ],
559
+ tableName,
560
+ TABLE_ID,
561
+ ) => {
562
+ addImport(
563
+ 1,
564
+ moduleDefinition,
565
+ tableType,
566
+ rowType,
567
+ rowWhenSetType,
568
+ cellIdType,
569
+ cellCallbackType,
570
+ rowCallbackType,
571
+ );
572
+ addMethod(
573
+ `has${tableName}Table`,
574
+ EMPTY_STRING,
575
+ BOOLEAN,
576
+ storeMethod('hasTable', TABLE_ID),
577
+ getHasDoc(getTableDoc(tableId)),
578
+ );
579
+ addMethod(
580
+ `get${tableName}Table`,
581
+ EMPTY_STRING,
582
+ tableType,
583
+ storeMethod('getTable', TABLE_ID, tableType),
584
+ getTableContentDoc(tableId),
585
+ );
586
+ addMethod(
587
+ `set${tableName}Table`,
588
+ `table: ${tableType}`,
589
+ storeType,
590
+ fluentStoreMethod('setTable', `${TABLE_ID}, table`),
591
+ getTableContentDoc(tableId, 1),
592
+ );
593
+ addMethod(
594
+ `del${tableName}Table`,
595
+ EMPTY_STRING,
596
+ storeType,
597
+ fluentStoreMethod('delTable', TABLE_ID),
598
+ getTableContentDoc(tableId, 3),
599
+ );
600
+ addMethod(
601
+ `get${tableName}RowIds`,
602
+ EMPTY_STRING,
603
+ 'Ids',
604
+ storeMethod('getRowIds', TABLE_ID),
605
+ getIdsDoc('Row', getTableDoc(tableId)),
606
+ );
607
+ addMethod(
608
+ `get${tableName}SortedRowIds`,
609
+ `cellId?: ${cellIdType}, descending?: boolean, offset?: number, limit?: number`,
610
+ 'Ids',
611
+ storeMethod(
612
+ 'getSortedRowIds',
613
+ `${TABLE_ID}, cellId, descending, offset, limit`,
614
+ ),
615
+ getIdsDoc('Row', getTableDoc(tableId), 1),
616
+ );
617
+ addMethod(
618
+ `forEach${tableName}Row`,
619
+ `rowCallback: ${rowCallbackType}`,
620
+ 'void',
621
+ storeMethod('forEachRow', `${TABLE_ID}, rowCallback as any`),
622
+ getForEachDoc('Row', getTableDoc(tableId)),
623
+ );
624
+ addMethod(
625
+ `has${tableName}Row`,
626
+ 'rowId: Id',
627
+ BOOLEAN,
628
+ storeMethod('hasRow', `${TABLE_ID}, rowId`),
629
+ getHasDoc(THE_SPECIFIED_ROW, getTableDoc(tableId)),
630
+ );
631
+ addMethod(
632
+ `get${tableName}Row`,
633
+ 'rowId: Id',
634
+ rowType,
635
+ storeMethod('getRow', `${TABLE_ID}, rowId`, rowType),
636
+ getRowContentDoc(tableId),
637
+ );
638
+ addMethod(
639
+ `set${tableName}Row`,
640
+ `rowId: Id, row: ${rowWhenSetType}`,
641
+ storeType,
642
+ fluentStoreMethod('setRow', `${TABLE_ID}, rowId, row`),
643
+ getRowContentDoc(tableId, 1),
644
+ );
645
+ addMethod(
646
+ `add${tableName}Row`,
647
+ `row: ${rowWhenSetType}`,
648
+ `Id${OR_UNDEFINED}`,
649
+ storeMethod('addRow', `${TABLE_ID}, row`),
650
+ `Adds a new Row to ${getTableDoc(tableId)}`,
651
+ );
652
+ addMethod(
653
+ `set${tableName}PartialRow`,
654
+ `rowId: Id, partialRow: ${rowWhenSetType}`,
655
+ storeType,
656
+ fluentStoreMethod('setPartialRow', `${TABLE_ID}, rowId, partialRow`),
657
+ getRowContentDoc(tableId, 2),
658
+ );
659
+ addMethod(
660
+ `del${tableName}Row`,
661
+ `rowId: Id`,
662
+ storeType,
663
+ fluentStoreMethod('delRow', `${TABLE_ID}, rowId`),
664
+ getRowContentDoc(tableId, 3),
665
+ );
666
+ addMethod(
667
+ `get${tableName}CellIds`,
668
+ 'rowId: Id',
669
+ `${cellIdType}[]`,
670
+ storeMethod('getCellIds', `${TABLE_ID}, rowId`, `${cellIdType}[]`),
671
+ getIdsDoc('Cell', getRowDoc(tableId)),
672
+ );
673
+ addMethod(
674
+ `forEach${tableName}Cell`,
675
+ `rowId: Id, cellCallback: ${cellCallbackType}`,
676
+ 'void',
677
+ storeMethod('forEachCell', `${TABLE_ID}, rowId, cellCallback as any`),
678
+ getForEachDoc('Cell', getRowDoc(tableId)),
679
+ );
680
+ mapCellSchema(
681
+ tableId,
682
+ (cellId, type, defaultValue, CELL_ID, cellName) => {
683
+ const mapCellType = `Map${camel(type, 1)}`;
684
+ mapSet(mapCellTypes, type, mapCellType);
685
+ addMethod(
686
+ `has${tableName}${cellName}Cell`,
687
+ 'rowId: Id',
688
+ BOOLEAN,
689
+ storeMethod('hasCell', `${TABLE_ID}, rowId, ${CELL_ID}`),
690
+ getHasDoc(getCellDoc(cellId), getRowDoc(tableId)),
691
+ );
692
+ const returnCellType = `${type}${
693
+ isUndefined(defaultValue) ? OR_UNDEFINED : EMPTY_STRING
694
+ }`;
695
+ addMethod(
696
+ `get${tableName}${cellName}Cell`,
697
+ 'rowId: Id',
698
+ returnCellType,
699
+ storeMethod(
700
+ 'getCell',
701
+ `${TABLE_ID}, rowId, ${CELL_ID}`,
702
+ returnCellType,
703
+ ),
704
+ getCellContentDoc(tableId, cellId),
705
+ );
706
+ addMethod(
707
+ `set${tableName}${cellName}Cell`,
708
+ `rowId: Id, cell: ${type} | ${mapCellType}`,
709
+ storeType,
710
+ fluentStoreMethod(
711
+ 'setCell',
712
+ `${TABLE_ID}, rowId, ${CELL_ID}, cell as any`,
713
+ ),
714
+ getCellContentDoc(tableId, cellId, 1),
715
+ );
716
+ addMethod(
717
+ `del${tableName}${cellName}Cell`,
718
+ `rowId: Id`,
719
+ storeType,
720
+ fluentStoreMethod('delCell', `${TABLE_ID}, rowId, ${CELL_ID}`),
721
+ getCellContentDoc(tableId, cellId, 3),
722
+ );
723
+ },
724
+ );
725
+ },
726
+ );
727
+ addMethod(
728
+ 'getTablesJson',
729
+ EMPTY_STRING,
730
+ 'Json',
731
+ storeMethod('getTablesJson'),
732
+ getTheContentOfTheStoreDoc(5, 1),
733
+ );
734
+ addMethod(
735
+ 'setTablesJson',
736
+ 'tablesJson: Json',
737
+ storeType,
738
+ fluentStoreMethod('setTablesJson', 'tablesJson'),
739
+ getTheContentOfTheStoreDoc(6, 1),
740
+ );
741
+ addMethod(
742
+ 'addTablesListener',
743
+ `${LISTENER}: ${tablesListenerType}, mutator?: boolean`,
744
+ 'Id',
745
+ storeListener('addTablesListener', EMPTY_STRING, 'mutator'),
746
+ getTheContentOfTheStoreDoc(7, 1) + ' changes',
747
+ );
748
+ addMethod(
749
+ 'addTableIdsListener',
750
+ `${LISTENER}: ${tableIdsListenerType}, mutator?: boolean`,
751
+ 'Id',
752
+ storeListener('addTableIdsListener', EMPTY_STRING, 'mutator'),
753
+ getListenerDoc('the Table Ids', THE_STORE, 1),
754
+ );
755
+ addMethod(
756
+ 'addTableListener',
757
+ `tableId: ${tableIdType} | null, ${LISTENER}: ${tableListenerType}, mutator?: boolean`,
758
+ 'Id',
759
+ storeListener('addTableListener', 'tableId', 'mutator'),
760
+ getListenerDoc('a Table', THE_STORE),
761
+ );
762
+ addMethod(
763
+ 'addRowIdsListener',
764
+ `tableId: ${tableIdType} | null, ${LISTENER}: ${rowIdsListenerType}, mutator?: boolean`,
765
+ 'Id',
766
+ storeListener('addRowIdsListener', 'tableId', 'mutator'),
767
+ getListenerDoc('the Row Ids', 'a Table', 1),
768
+ );
769
+ addMethod(
770
+ 'addRowListener',
771
+ `tableId: ${tableIdType} | null, rowId: IdOrNull, ${LISTENER}: ${rowListenerType}, mutator?: boolean`,
772
+ 'Id',
773
+ storeListener('addRowListener', 'tableId, rowId', 'mutator'),
774
+ getListenerDoc('a Row', 'a Table'),
775
+ );
776
+ addMethod(
777
+ 'addCellIdsListener',
778
+ `tableId: ${tableIdType} | null, rowId: IdOrNull, ${LISTENER}: ${cellIdsListenerType}, mutator?: boolean`,
779
+ 'Id',
780
+ storeListener('addCellIdsListener', 'tableId, rowId', 'mutator'),
781
+ getListenerDoc('the Cell Ids', 'a Row', 1),
782
+ );
783
+ addMethod(
784
+ 'addCellListener',
785
+ `tableId: ${tableIdType} | null, rowId: IdOrNull, cellId: ${join(
786
+ mapTablesSchema((_, tableTypes2) => tableTypes2[3]),
787
+ ' | ',
788
+ )} | null, ${LISTENER}: ${cellListenerType}, mutator?: boolean`,
789
+ 'Id',
790
+ storeListener('addCellListener', 'tableId, rowId, cellId', 'mutator'),
791
+ getListenerDoc('a Cell', 'a Row'),
792
+ );
793
+ addMethod(
794
+ 'addInvalidCellListener',
795
+ `tableId: IdOrNull, rowId: IdOrNull, cellId: IdOrNull, ${LISTENER}: ${invalidCellListenerType}, mutator?: boolean`,
796
+ 'Id',
797
+ storeListener(
798
+ 'addInvalidCellListener',
799
+ 'tableId, rowId, cellId',
800
+ 'mutator',
409
801
  ),
410
- ' | ',
411
- )}) => CellChange`,
412
- `${A_FUNCTION_FOR} returning information about any Cell's changes during a transaction`,
413
- );
414
- const tablesListenerType = addType(
415
- 'TablesListener',
416
- `(${storeInstance}: ${storeType}, getCellChange: ${getCellChangeType}${OR_UNDEFINED})${RETURNS_VOID}`,
417
- getListenerTypeDoc(1),
418
- );
419
- const tableIdsListenerType = addType(
420
- 'TableIdsListener',
421
- `(${storeInstance}: ${storeType})${RETURNS_VOID}`,
422
- getListenerTypeDoc(2),
423
- );
424
- const tableListenerType = addType(
425
- 'TableListener',
426
- `(${storeInstance}: ${storeType}, tableId: ${tableIdType}, getCellChange: ${getCellChangeType}${OR_UNDEFINED})${RETURNS_VOID}`,
427
- getListenerTypeDoc(3),
428
- );
429
- const rowIdsListenerType = addType(
430
- 'RowIdsListener',
431
- `(${storeInstance}: ${storeType}, tableId: ${tableIdType})${RETURNS_VOID}`,
432
- getListenerTypeDoc(4, 3),
433
- );
434
- const rowListenerType = addType(
435
- 'RowListener',
436
- `(${storeInstance}: ${storeType}, tableId: ${tableIdType}, rowId: Id, getCellChange: ${getCellChangeType}${OR_UNDEFINED})${RETURNS_VOID}`,
437
- getListenerTypeDoc(5, 3),
438
- );
439
- const cellIdsListenerType = addType(
440
- 'CellIdsListener',
441
- `(${storeInstance}: ${storeType}, tableId: ${tableIdType}, rowId: Id)` +
442
- RETURNS_VOID,
443
- getListenerTypeDoc(6, 5),
444
- );
445
- const cellListenerType = addType(
446
- 'CellListener',
447
- `(...[${storeInstance}, tableId, rowId, cellId, newCell, oldCell, getCellChange]: ${join(
802
+ `${REGISTERS_A_LISTENER} whenever an invalid Cell change was attempted`,
803
+ );
804
+ mapForEach(mapCellTypes, (type, mapCellType) =>
805
+ addType(
806
+ mapCellType,
807
+ `(cell: ${type}${OR_UNDEFINED}) => ${type}`,
808
+ `Takes a ${type} Cell value and returns another`,
809
+ ),
810
+ );
811
+ addImport(
812
+ 1,
813
+ moduleDefinition,
814
+ tablesType,
815
+ tableIdType,
816
+ tableCallbackType,
817
+ tablesListenerType,
818
+ tableIdsListenerType,
819
+ tableListenerType,
820
+ rowIdsListenerType,
821
+ rowListenerType,
822
+ cellIdsListenerType,
823
+ cellListenerType,
824
+ invalidCellListenerType,
825
+ ...collValues(mapCellTypes),
826
+ );
827
+ addImport(0, 'tinybase', 'CellChange');
828
+ arrayPush(
829
+ createSteps,
830
+ '.setTablesSchema({',
448
831
  flat(
449
- mapTableSchema((tableId) =>
450
- mapCellSchema(
832
+ mapTablesSchema((tableId, _, __, TABLE_ID) => [
833
+ `[${TABLE_ID}]: {`,
834
+ ...mapCellSchema(
451
835
  tableId,
452
- (cellId, type) =>
453
- `[${storeInstance}: ${storeType}, tableId: '${tableId}', rowId: Id, cellId: '${cellId}', newCell: ${type}${OR_UNDEFINED}, oldCell: ${type}${OR_UNDEFINED}, getCellChange: ${getCellChangeType} | undefined]`,
836
+ (_2, type, defaultValue, CELL_ID) =>
837
+ `[${CELL_ID}]: {[${addConstant(
838
+ snake(TYPE),
839
+ `'${TYPE}'`,
840
+ )}]: ${addConstant(snake(type), `'${type}'`)}${
841
+ isUndefined(defaultValue)
842
+ ? EMPTY_STRING
843
+ : `, [${addConstant(snake(DEFAULT), `'${DEFAULT}'`)}]: ${
844
+ isString(defaultValue)
845
+ ? addConstant(snake(defaultValue), `'${defaultValue}'`)
846
+ : defaultValue
847
+ }`
848
+ }},`,
454
849
  ),
850
+ `},`,
851
+ ]),
852
+ ),
853
+ '})',
854
+ );
855
+ }
856
+ if (!objIsEmpty(valuesSchema)) {
857
+ const valuesType = addType(
858
+ 'Values',
859
+ `{${join(
860
+ mapValuesSchema(
861
+ (valueId, type, defaultValue) =>
862
+ `'${valueId}'${
863
+ isUndefined(defaultValue) ? '?' : EMPTY_STRING
864
+ }: ${type};`,
455
865
  ),
866
+ ' ',
867
+ )}}`,
868
+ getTheContentOfTheStoreDoc(4, 2),
869
+ );
870
+ const valuesWhenSetType = addType(
871
+ 'ValuesWhenSet',
872
+ `{${join(
873
+ mapValuesSchema((valueId, type) => `'${valueId}'?: ${type};`),
874
+ ' ',
875
+ )}}`,
876
+ getTheContentOfTheStoreDoc(4, 2, 1),
877
+ );
878
+ const valueIdType = addType(
879
+ 'ValueId',
880
+ join(
881
+ mapValuesSchema((valueId) => `'${valueId}'`),
882
+ ' | ',
456
883
  ),
457
- ' | ',
458
- )})${RETURNS_VOID}`,
459
- getListenerTypeDoc(7, 5),
460
- );
461
- const invalidCellListenerType = addType(
462
- 'InvalidCellListener',
463
- `(${storeInstance}: ${storeType}, tableId: Id, rowId: Id, cellId: Id, invalidCells: any[])${RETURNS_VOID}`,
464
- getListenerTypeDoc(8),
465
- );
466
- const transactionListenerType = addType(
467
- 'TransactionListener',
468
- `(${storeInstance}: ${storeType}, cellsTouched: boolean)${RETURNS_VOID}`,
469
- `${A_FUNCTION_FOR} listening to the completion of a transaction`,
470
- );
471
- addMethod(
472
- `hasTables`,
473
- EMPTY_STRING,
474
- BOOLEAN,
475
- storeMethod('hasTables'),
476
- getHasDoc('any Table'),
477
- );
478
- addMethod(
479
- `getTables`,
480
- EMPTY_STRING,
481
- tablesType,
482
- storeMethod('getTables'),
483
- getStoreContentDoc(),
484
- );
485
- addMethod(
486
- `setTables`,
487
- `tables: ${tablesType}`,
488
- storeType,
489
- fluentStoreMethod('setTables', 'tables'),
490
- getStoreContentDoc(1),
491
- );
492
- addMethod(
493
- `delTables`,
494
- EMPTY_STRING,
495
- storeType,
496
- fluentStoreMethod('delTables'),
497
- getStoreContentDoc(3),
498
- );
499
- addMethod(
500
- `getTableIds`,
501
- EMPTY_STRING,
502
- `${tableIdType}[]`,
503
- storeMethod('getTableIds', EMPTY_STRING, `${tableIdType}[]`),
504
- getIdsDoc('Table', THE_STORE),
505
- );
506
- addMethod(
507
- 'forEachTable',
508
- `tableCallback: ${tableCallbackType}`,
509
- 'void',
510
- storeMethod('forEachTable', 'tableCallback as any'),
511
- getForEachDoc('Table', THE_STORE),
512
- );
513
- const mapCellTypes = mapNew();
514
- mapTableSchema(
515
- (
516
- tableId,
517
- [
518
- tableType,
519
- rowType,
520
- rowWhenSetType,
521
- cellIdType,
522
- cellCallbackType,
523
- rowCallbackType,
524
- ],
525
- tableName,
526
- TABLE_ID,
527
- ) => {
528
- addImport(
529
- 1,
530
- moduleDefinition,
531
- tableType,
532
- rowType,
533
- rowWhenSetType,
534
- cellIdType,
535
- cellCallbackType,
536
- rowCallbackType,
537
- );
884
+ `A Value Id in ${THE_STORE}`,
885
+ );
886
+ const valueCallbackType = addType(
887
+ 'ValueCallback',
888
+ `(...[valueId, rowCallback]: ${join(
889
+ mapValuesSchema(
890
+ (valueId, type) => `[valueId: '${valueId}', value: ${type}]`,
891
+ ),
892
+ ' | ',
893
+ )})${RETURNS_VOID}`,
894
+ getCallbackDoc('a Value Id, and value'),
895
+ );
896
+ const getValueChangeType = addType(
897
+ 'GetValueChange',
898
+ `(valueId: ${valueIdType}) => ValueChange`,
899
+ `${A_FUNCTION_FOR} returning information about any Value's changes during a transaction`,
900
+ );
901
+ const valuesListenerType = addType(
902
+ 'ValuesListener',
903
+ `(${storeInstance}: ${storeType}, getValueChange: ${getValueChangeType}${OR_UNDEFINED})` +
904
+ RETURNS_VOID,
905
+ getListenerTypeDoc(9),
906
+ );
907
+ const valueIdsListenerType = addType(
908
+ 'ValueIdsListener',
909
+ `(${storeInstance}: ${storeType})${RETURNS_VOID}`,
910
+ getListenerTypeDoc(10),
911
+ );
912
+ const valueListenerType = addType(
913
+ 'ValueListener',
914
+ `(...[${storeInstance}, valueId, newValue, oldValue, getValueChange]: ${join(
915
+ mapValuesSchema(
916
+ (valueId, type) =>
917
+ `[${storeInstance}: ${storeType}, valueId: '${valueId}', newValue: ${type}${OR_UNDEFINED}, oldValue: ${type}${OR_UNDEFINED}, getValueChange: ${getValueChangeType} | undefined]`,
918
+ ),
919
+ ' | ',
920
+ )})${RETURNS_VOID}`,
921
+ getListenerTypeDoc(11),
922
+ );
923
+ const invalidValueListenerType = addType(
924
+ 'InvalidValueListener',
925
+ `(${storeInstance}: ${storeType}, valueId: Id, invalidValues: any[])${RETURNS_VOID}`,
926
+ getListenerTypeDoc(12),
927
+ );
928
+ addMethod(
929
+ `hasValues`,
930
+ EMPTY_STRING,
931
+ BOOLEAN,
932
+ storeMethod('hasValues'),
933
+ getHasDoc('any Value'),
934
+ );
935
+ addMethod(
936
+ `getValues`,
937
+ EMPTY_STRING,
938
+ valuesType,
939
+ storeMethod('getValues'),
940
+ getTheContentOfTheStoreDoc(0, 2),
941
+ );
942
+ addMethod(
943
+ `setValues`,
944
+ `values: ${valuesWhenSetType}`,
945
+ storeType,
946
+ fluentStoreMethod('setValues', 'values'),
947
+ getTheContentOfTheStoreDoc(1, 2),
948
+ );
949
+ addMethod(
950
+ `setPartialValues`,
951
+ `partialValues: ${valuesWhenSetType}`,
952
+ storeType,
953
+ fluentStoreMethod('setPartialValues', 'partialValues'),
954
+ getTheContentOfTheStoreDoc(2, 2),
955
+ );
956
+ addMethod(
957
+ `delValues`,
958
+ EMPTY_STRING,
959
+ storeType,
960
+ fluentStoreMethod('delValues'),
961
+ getTheContentOfTheStoreDoc(3, 2),
962
+ );
963
+ addMethod(
964
+ `getValueIds`,
965
+ EMPTY_STRING,
966
+ `${valueIdType}[]`,
967
+ storeMethod('getValueIds', EMPTY_STRING, `${valueIdType}[]`),
968
+ getIdsDoc('Value', THE_STORE),
969
+ );
970
+ addMethod(
971
+ 'forEachValue',
972
+ `valueCallback: ${valueCallbackType}`,
973
+ 'void',
974
+ storeMethod('forEachValue', 'valueCallback as any'),
975
+ getForEachDoc('Value', THE_STORE),
976
+ );
977
+ mapValuesSchema((valueId, type, _, VALUE_ID, valueName) => {
538
978
  addMethod(
539
- `has${tableName}Table`,
979
+ `has${valueName}Value`,
540
980
  EMPTY_STRING,
541
981
  BOOLEAN,
542
- storeMethod('hasTable', TABLE_ID),
543
- getHasDoc(getTableDoc(tableId)),
982
+ storeMethod('hasValue', VALUE_ID),
983
+ getHasDoc(getValueDoc(valueId)),
544
984
  );
545
985
  addMethod(
546
- `get${tableName}Table`,
986
+ `get${valueName}Value`,
547
987
  EMPTY_STRING,
548
- tableType,
549
- storeMethod('getTable', TABLE_ID, tableType),
550
- getTableContentDoc(tableId),
551
- );
552
- addMethod(
553
- `set${tableName}Table`,
554
- `table: ${tableType}`,
555
- storeType,
556
- fluentStoreMethod('setTable', `${TABLE_ID}, table`),
557
- getTableContentDoc(tableId, 1),
988
+ type,
989
+ storeMethod('getValue', VALUE_ID, type),
990
+ getValueContentDoc(valueId),
558
991
  );
559
992
  addMethod(
560
- `del${tableName}Table`,
561
- EMPTY_STRING,
993
+ `set${valueName}Value`,
994
+ `value: ${type}`,
562
995
  storeType,
563
- fluentStoreMethod('delTable', TABLE_ID),
564
- getTableContentDoc(tableId, 3),
996
+ fluentStoreMethod('setValue', `${VALUE_ID}, value`),
997
+ getValueContentDoc(valueId, 1),
565
998
  );
566
999
  addMethod(
567
- `get${tableName}RowIds`,
1000
+ `del${valueName}Value`,
568
1001
  EMPTY_STRING,
569
- 'Ids',
570
- storeMethod('getRowIds', TABLE_ID),
571
- getIdsDoc('Row', getTableDoc(tableId)),
572
- );
573
- addMethod(
574
- `get${tableName}SortedRowIds`,
575
- `cellId?: ${cellIdType}, descending?: boolean, offset?: number, limit?: number`,
576
- 'Ids',
577
- storeMethod(
578
- 'getSortedRowIds',
579
- `${TABLE_ID}, cellId, descending, offset, limit`,
580
- ),
581
- getIdsDoc('Row', getTableDoc(tableId), 1),
582
- );
583
- addMethod(
584
- `forEach${tableName}Row`,
585
- `rowCallback: ${rowCallbackType}`,
586
- 'void',
587
- storeMethod('forEachRow', `${TABLE_ID}, rowCallback as any`),
588
- getForEachDoc('Row', getTableDoc(tableId)),
589
- );
590
- addMethod(
591
- `has${tableName}Row`,
592
- 'rowId: Id',
593
- BOOLEAN,
594
- storeMethod('hasRow', `${TABLE_ID}, rowId`),
595
- getHasDoc(THE_SPECIFIED_ROW, getTableDoc(tableId)),
596
- );
597
- addMethod(
598
- `get${tableName}Row`,
599
- 'rowId: Id',
600
- rowType,
601
- storeMethod('getRow', `${TABLE_ID}, rowId`, rowType),
602
- getRowContentDoc(tableId),
603
- );
604
- addMethod(
605
- `set${tableName}Row`,
606
- `rowId: Id, row: ${rowWhenSetType}`,
607
1002
  storeType,
608
- fluentStoreMethod('setRow', `${TABLE_ID}, rowId, row`),
609
- getRowContentDoc(tableId, 1),
610
- );
611
- addMethod(
612
- `add${tableName}Row`,
613
- `row: ${rowWhenSetType}`,
614
- `Id${OR_UNDEFINED}`,
615
- storeMethod('addRow', `${TABLE_ID}, row`),
616
- `Adds a new Row to ${getTableDoc(tableId)}`,
617
- );
618
- addMethod(
619
- `set${tableName}PartialRow`,
620
- `rowId: Id, partialRow: ${rowWhenSetType}`,
621
- storeType,
622
- fluentStoreMethod('setPartialRow', `${TABLE_ID}, rowId, partialRow`),
623
- getRowContentDoc(tableId, 2),
624
- );
625
- addMethod(
626
- `del${tableName}Row`,
627
- `rowId: Id`,
628
- storeType,
629
- fluentStoreMethod('delRow', `${TABLE_ID}, rowId`),
630
- getRowContentDoc(tableId, 3),
631
- );
632
- addMethod(
633
- `get${tableName}CellIds`,
634
- 'rowId: Id',
635
- `${cellIdType}[]`,
636
- storeMethod('getCellIds', `${TABLE_ID}, rowId`, `${cellIdType}[]`),
637
- getIdsDoc('Cell', getRowDoc(tableId)),
638
- );
639
- addMethod(
640
- `forEach${tableName}Cell`,
641
- `rowId: Id, cellCallback: ${cellCallbackType}`,
642
- 'void',
643
- storeMethod('forEachCell', `${TABLE_ID}, rowId, cellCallback as any`),
644
- getForEachDoc('Cell', getRowDoc(tableId)),
1003
+ fluentStoreMethod('delValue', VALUE_ID),
1004
+ getValueContentDoc(valueId, 3),
645
1005
  );
646
- mapCellSchema(
647
- tableId,
648
- (cellId, type, defaultValue, CELL_ID, cellName) => {
649
- const mapCellType = `Map${camel(type, 1)}`;
650
- mapSet(mapCellTypes, type, mapCellType);
651
- addMethod(
652
- `has${tableName}${cellName}Cell`,
653
- 'rowId: Id',
654
- BOOLEAN,
655
- storeMethod('hasCell', `${TABLE_ID}, rowId, ${CELL_ID}`),
656
- getHasDoc(getCellDoc(cellId), getRowDoc(tableId)),
657
- );
658
- const returnCellType = `${type}${
659
- isUndefined(defaultValue) ? OR_UNDEFINED : EMPTY_STRING
660
- }`;
661
- addMethod(
662
- `get${tableName}${cellName}Cell`,
663
- 'rowId: Id',
664
- returnCellType,
665
- storeMethod(
666
- 'getCell',
667
- `${TABLE_ID}, rowId, ${CELL_ID}`,
668
- returnCellType,
669
- ),
670
- getCellContentDoc(tableId, cellId),
671
- );
672
- addMethod(
673
- `set${tableName}${cellName}Cell`,
674
- `rowId: Id, cell: ${type} | ${mapCellType}`,
675
- storeType,
676
- fluentStoreMethod(
677
- 'setCell',
678
- `${TABLE_ID}, rowId, ${CELL_ID}, cell as any`,
679
- ),
680
- getCellContentDoc(tableId, cellId, 1),
681
- );
682
- addMethod(
683
- `del${tableName}${cellName}Cell`,
684
- `rowId: Id`,
685
- storeType,
686
- fluentStoreMethod('delCell', `${TABLE_ID}, rowId, ${CELL_ID}`),
687
- getCellContentDoc(tableId, cellId, 3),
688
- );
689
- },
690
- );
691
- },
1006
+ });
1007
+ addMethod(
1008
+ 'getValuesJson',
1009
+ EMPTY_STRING,
1010
+ 'Json',
1011
+ storeMethod('getValuesJson'),
1012
+ getTheContentOfTheStoreDoc(5, 2),
1013
+ );
1014
+ addMethod(
1015
+ 'setValuesJson',
1016
+ 'valuesJson: Json',
1017
+ storeType,
1018
+ fluentStoreMethod('setValuesJson', 'valuesJson'),
1019
+ getTheContentOfTheStoreDoc(6, 2),
1020
+ );
1021
+ addMethod(
1022
+ 'addValuesListener',
1023
+ `${LISTENER}: ${valuesListenerType}, mutator?: boolean`,
1024
+ 'Id',
1025
+ storeListener('addValuesListener', EMPTY_STRING, 'mutator'),
1026
+ getTheContentOfTheStoreDoc(7, 2) + ' changes',
1027
+ );
1028
+ addMethod(
1029
+ 'addValueIdsListener',
1030
+ `${LISTENER}: ${valueIdsListenerType}, mutator?: boolean`,
1031
+ 'Id',
1032
+ storeListener('addValueIdsListener', EMPTY_STRING, 'mutator'),
1033
+ getListenerDoc('the Value Ids', THE_STORE, 1),
1034
+ );
1035
+ addMethod(
1036
+ 'addValueListener',
1037
+ `valueId: ${valueIdType} | null, ${LISTENER}: ${valueListenerType}, mutator?: boolean`,
1038
+ 'Id',
1039
+ storeListener('addValueListener', 'valueId', 'mutator'),
1040
+ getListenerDoc('a Value', THE_STORE),
1041
+ );
1042
+ addMethod(
1043
+ 'addInvalidValueListener',
1044
+ `valueId: IdOrNull, ${LISTENER}: ${invalidValueListenerType}, mutator?: boolean`,
1045
+ 'Id',
1046
+ storeListener('addInvalidValueListener', 'valueId', 'mutator'),
1047
+ `${REGISTERS_A_LISTENER} whenever an invalid Cell change was attempted`,
1048
+ );
1049
+ addImport(
1050
+ 1,
1051
+ moduleDefinition,
1052
+ valuesType,
1053
+ valueIdType,
1054
+ valueCallbackType,
1055
+ valuesListenerType,
1056
+ valueIdsListenerType,
1057
+ valueListenerType,
1058
+ invalidValueListenerType,
1059
+ );
1060
+ addImport(0, 'tinybase', 'ValueChange');
1061
+ arrayPush(
1062
+ createSteps,
1063
+ '.setValuesSchema({',
1064
+ mapValuesSchema((valueId, type, defaultValue, VALUE_ID) => [
1065
+ `[${VALUE_ID}]: {[${addConstant(
1066
+ snake(TYPE),
1067
+ `'${TYPE}'`,
1068
+ )}]: ${addConstant(snake(type), `'${type}'`)}${
1069
+ isUndefined(defaultValue)
1070
+ ? EMPTY_STRING
1071
+ : `, [${addConstant(snake(DEFAULT), `'${DEFAULT}'`)}]: ${
1072
+ isString(defaultValue)
1073
+ ? addConstant(snake(defaultValue), `'${defaultValue}'`)
1074
+ : defaultValue
1075
+ }`
1076
+ }},`,
1077
+ ]),
1078
+ '})',
1079
+ );
1080
+ }
1081
+ addImport(0, 'tinybase', ...COMMON_IMPORTS);
1082
+ const transactionListenerType = addType(
1083
+ 'TransactionListener',
1084
+ `(${storeInstance}: ${storeType}, cellsTouched: boolean, valuesTouched: boolean)${RETURNS_VOID}`,
1085
+ `${A_FUNCTION_FOR} listening to the completion of a transaction`,
692
1086
  );
693
1087
  addMethod(
694
1088
  'getJson',
695
1089
  EMPTY_STRING,
696
1090
  'Json',
697
1091
  storeMethod('getJson'),
698
- `${verbs[0]} a string serialization ${THE_CONTENT_OF_THE_STORE}`,
1092
+ getTheContentOfTheStoreDoc(5),
699
1093
  );
700
1094
  addMethod(
701
1095
  'setJson',
702
1096
  'json: Json',
703
1097
  storeType,
704
1098
  fluentStoreMethod('setJson', 'json'),
705
- `${verbs[1]} ${THE_CONTENT_OF_THE_STORE} from a serialized string`,
1099
+ getTheContentOfTheStoreDoc(6),
706
1100
  );
707
1101
  addMethod(
708
1102
  'transaction',
709
- 'actions: () => Return, doRollback?: (changedCells: ChangedCells, invalidCells: InvalidCells) => boolean',
1103
+ 'actions: () => Return, doRollback?: DoRollback',
710
1104
  'Return',
711
1105
  storeMethod('transaction', 'actions, doRollback'),
712
1106
  'Execute a transaction to make multiple mutations',
@@ -721,70 +1115,11 @@ const getStoreApi = (schema, module) => {
721
1115
  );
722
1116
  addMethod(
723
1117
  'finishTransaction',
724
- 'doRollback?: (changedCells: ChangedCells, invalidCells: InvalidCells) => boolean,',
1118
+ 'doRollback?: DoRollback,',
725
1119
  storeType,
726
1120
  fluentStoreMethod('finishTransaction', 'doRollback'),
727
1121
  'Explicitly finishes a transaction',
728
1122
  );
729
- addMethod(
730
- 'addTablesListener',
731
- `${LISTENER}: ${tablesListenerType}, mutator?: boolean`,
732
- 'Id',
733
- storeListener('addTablesListener', EMPTY_STRING, 'mutator'),
734
- `${REGISTERS_A_LISTENER} whenever ${THE_CONTENT_OF_THE_STORE} changes`,
735
- );
736
- addMethod(
737
- 'addTableIdsListener',
738
- `${LISTENER}: ${tableIdsListenerType}, mutator?: boolean`,
739
- 'Id',
740
- storeListener('addTableIdsListener', EMPTY_STRING, 'mutator'),
741
- getListenerDoc('the Table Ids', THE_STORE, 1),
742
- );
743
- addMethod(
744
- 'addTableListener',
745
- `tableId: ${tableIdType} | null, ${LISTENER}: ${tableListenerType}, mutator?: boolean`,
746
- 'Id',
747
- storeListener('addTableListener', 'tableId', 'mutator'),
748
- getListenerDoc('a Table', THE_STORE),
749
- );
750
- addMethod(
751
- 'addRowIdsListener',
752
- `tableId: ${tableIdType} | null, ${LISTENER}: ${rowIdsListenerType}, mutator?: boolean`,
753
- 'Id',
754
- storeListener('addRowIdsListener', 'tableId', 'mutator'),
755
- getListenerDoc('the Row Ids', 'a Table', 1),
756
- );
757
- addMethod(
758
- 'addRowListener',
759
- `tableId: ${tableIdType} | null, rowId: IdOrNull, ${LISTENER}: ${rowListenerType}, mutator?: boolean`,
760
- 'Id',
761
- storeListener('addRowListener', 'tableId, rowId', 'mutator'),
762
- getListenerDoc('a Row', 'a Table'),
763
- );
764
- addMethod(
765
- 'addCellIdsListener',
766
- `tableId: ${tableIdType} | null, rowId: IdOrNull, ${LISTENER}: ${cellIdsListenerType}, mutator?: boolean`,
767
- 'Id',
768
- storeListener('addCellIdsListener', 'tableId, rowId', 'mutator'),
769
- getListenerDoc('the Cell Ids', 'a Row', 1),
770
- );
771
- addMethod(
772
- 'addCellListener',
773
- `tableId: ${tableIdType} | null, rowId: IdOrNull, cellId: ${join(
774
- mapTableSchema((_, tableTypes2) => tableTypes2[3]),
775
- ' | ',
776
- )} | null, ${LISTENER}: ${cellListenerType}, mutator?: boolean`,
777
- 'Id',
778
- storeListener('addCellListener', 'tableId, rowId, cellId', 'mutator'),
779
- getListenerDoc('a Cell', 'a Row'),
780
- );
781
- addMethod(
782
- 'addInvalidCellListener',
783
- `tableId: IdOrNull, rowId: IdOrNull, cellId: IdOrNull, ${LISTENER}: ${invalidCellListenerType}, mutator?: boolean`,
784
- 'Id',
785
- storeListener('addCellListener', 'tableId, rowId, cellId', 'mutator'),
786
- `${REGISTERS_A_LISTENER} whenever an invalid Cell change was attempted`,
787
- );
788
1123
  addMethod(
789
1124
  'addWillFinishTransactionListener',
790
1125
  `${LISTENER}: ${transactionListenerType}`,
@@ -818,62 +1153,17 @@ const getStoreApi = (schema, module) => {
818
1153
  EMPTY_STRING,
819
1154
  'Store',
820
1155
  'store',
821
- `${verbs[0]} the underlying Store object`,
1156
+ `${VERBS[0]} the underlying Store object`,
822
1157
  );
823
- mapForEach(mapCellTypes, (type, mapCellType) =>
824
- addType(
825
- mapCellType,
826
- `(cell: ${type}${OR_UNDEFINED}) => ${type}`,
827
- `Takes a ${type} Cell value and returns another`,
828
- ),
829
- );
830
- addImport(0, 'tinybase', 'CellChange', ...COMMON_IMPORTS);
831
1158
  addImport(1, 'tinybase', 'createStore', ...COMMON_IMPORTS);
832
1159
  addImport(
833
1160
  1,
834
1161
  moduleDefinition,
835
1162
  storeType,
836
1163
  `create${storeType} as create${storeType}Decl`,
837
- tablesType,
838
- tableIdType,
839
- tableCallbackType,
840
- tablesListenerType,
841
- tableIdsListenerType,
842
- tableListenerType,
843
- rowIdsListenerType,
844
- rowListenerType,
845
- cellIdsListenerType,
846
- cellListenerType,
847
- invalidCellListenerType,
848
1164
  transactionListenerType,
849
- ...collValues(mapCellTypes),
850
1165
  );
851
- addConstant('store', [
852
- 'createStore().setSchema({',
853
- flat(
854
- mapTableSchema((tableId, _, __, TABLE_ID) => [
855
- `[${TABLE_ID}]: {`,
856
- ...mapCellSchema(
857
- tableId,
858
- (_2, type, defaultValue, CELL_ID) =>
859
- `[${CELL_ID}]: {[${addConstant(
860
- snake(TYPE),
861
- `'${TYPE}'`,
862
- )}]: ${addConstant(snake(type), `'${type}'`)}${
863
- isUndefined(defaultValue)
864
- ? EMPTY_STRING
865
- : `, [${addConstant(snake(DEFAULT), `'${DEFAULT}'`)}]: ${
866
- isString(defaultValue)
867
- ? addConstant(snake(defaultValue), `'${defaultValue}'`)
868
- : defaultValue
869
- }`
870
- }},`,
871
- ),
872
- `},`,
873
- ]),
874
- ),
875
- '})',
876
- ]);
1166
+ addConstant('store', ['createStore()', ...createSteps]);
877
1167
  addFunction('fluent', 'actions: () => Store', [
878
1168
  'actions();',
879
1169
  `return ${storeInstance};`,
@@ -942,14 +1232,15 @@ const createTools = getCreateFunction((store) => {
942
1232
  totalTables,
943
1233
  totalRows,
944
1234
  totalCells,
1235
+ totalValues: arrayLength(store.getValueIds()),
945
1236
  jsonLength: length(store.getJson()),
946
1237
  ...(detail ? {detail: {tables}} : {}),
947
1238
  };
948
1239
  };
949
- const getStoreSchema = () => {
950
- const schema = jsonParse(store.getSchemaJson());
1240
+ const getStoreTablesSchema = () => {
1241
+ const tablesSchema = jsonParse(store.getTablesSchemaJson());
951
1242
  if (
952
- !objIsEmpty(schema) ||
1243
+ !objIsEmpty(tablesSchema) ||
953
1244
  arrayEvery(store.getTableIds(), (tableId) => {
954
1245
  const rowIds = store.getRowIds(tableId);
955
1246
  const cellsMeta = mapNew();
@@ -958,7 +1249,7 @@ const createTools = getCreateFunction((store) => {
958
1249
  arrayEvery(store.getCellIds(tableId, rowId), (cellId) => {
959
1250
  const value = store.getCell(tableId, rowId, cellId);
960
1251
  const cellMeta = mapEnsure(cellsMeta, cellId, () => [
961
- getCellType(value),
1252
+ getCellOrValueType(value),
962
1253
  mapNew(),
963
1254
  [0],
964
1255
  0,
@@ -970,13 +1261,13 @@ const createTools = getCreateFunction((store) => {
970
1261
  }
971
1262
  mapSet(values, value, count);
972
1263
  cellMeta[3]++;
973
- return type == getCellType(value);
1264
+ return type == getCellOrValueType(value);
974
1265
  }),
975
1266
  )
976
1267
  ) {
977
- schema[tableId] = {};
1268
+ tablesSchema[tableId] = {};
978
1269
  collForEach(cellsMeta, ([type, , [, maxValue], count], cellId) => {
979
- schema[tableId][cellId] = {
1270
+ tablesSchema[tableId][cellId] = {
980
1271
  [TYPE]: type,
981
1272
  ...(count == arrayLength(rowIds) ? {[DEFAULT]: maxValue} : {}),
982
1273
  };
@@ -985,11 +1276,23 @@ const createTools = getCreateFunction((store) => {
985
1276
  }
986
1277
  })
987
1278
  ) {
988
- return schema;
1279
+ return tablesSchema;
989
1280
  }
990
1281
  return {};
991
1282
  };
992
- const getStoreApi$1 = (module) => getStoreApi(getStoreSchema(), module);
1283
+ const getStoreValuesSchema = () => {
1284
+ const valuesSchema = jsonParse(store.getValuesSchemaJson());
1285
+ if (objIsEmpty(valuesSchema)) {
1286
+ store.forEachValue((valueId, value) => {
1287
+ valuesSchema[valueId] = {
1288
+ [TYPE]: getCellOrValueType(value),
1289
+ };
1290
+ });
1291
+ }
1292
+ return valuesSchema;
1293
+ };
1294
+ const getStoreApi$1 = (module) =>
1295
+ getStoreApi(getStoreTablesSchema(), getStoreValuesSchema(), module);
993
1296
  const getPrettyStoreApi = async (module) => {
994
1297
  let format;
995
1298
  try {
@@ -1003,7 +1306,8 @@ const createTools = getCreateFunction((store) => {
1003
1306
  };
1004
1307
  const tools = {
1005
1308
  getStoreStats,
1006
- getStoreSchema,
1309
+ getStoreTablesSchema,
1310
+ getStoreValuesSchema,
1007
1311
  getStoreApi: getStoreApi$1,
1008
1312
  getPrettyStoreApi,
1009
1313
  };