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.
Files changed (80) hide show
  1. package/bin/cli.js +1 -1
  2. package/lib/checkpoints.js +1 -1
  3. package/lib/checkpoints.js.gz +0 -0
  4. package/lib/debug/queries.js +4 -3
  5. package/lib/debug/store.js +5 -5
  6. package/lib/debug/tinybase.js +6 -6
  7. package/lib/debug/tools.d.ts +155 -5
  8. package/lib/debug/tools.js +488 -431
  9. package/lib/es6/checkpoints.js +1 -1
  10. package/lib/es6/checkpoints.js.gz +0 -0
  11. package/lib/es6/indexes.js +1 -1
  12. package/lib/es6/indexes.js.gz +0 -0
  13. package/lib/es6/metrics.js +1 -1
  14. package/lib/es6/metrics.js.gz +0 -0
  15. package/lib/es6/queries.js +1 -1
  16. package/lib/es6/queries.js.gz +0 -0
  17. package/lib/es6/relationships.js +1 -1
  18. package/lib/es6/relationships.js.gz +0 -0
  19. package/lib/es6/store.js +1 -1
  20. package/lib/es6/store.js.gz +0 -0
  21. package/lib/es6/tinybase.js +1 -1
  22. package/lib/es6/tinybase.js.gz +0 -0
  23. package/lib/es6/tools.d.ts +155 -5
  24. package/lib/es6/tools.js +1 -1
  25. package/lib/es6/tools.js.gz +0 -0
  26. package/lib/es6/ui-react.js +1 -1
  27. package/lib/es6/ui-react.js.gz +0 -0
  28. package/lib/indexes.js +1 -1
  29. package/lib/indexes.js.gz +0 -0
  30. package/lib/metrics.js +1 -1
  31. package/lib/metrics.js.gz +0 -0
  32. package/lib/queries.js +1 -1
  33. package/lib/queries.js.gz +0 -0
  34. package/lib/relationships.js +1 -1
  35. package/lib/relationships.js.gz +0 -0
  36. package/lib/store.js +1 -1
  37. package/lib/store.js.gz +0 -0
  38. package/lib/tinybase.js +1 -1
  39. package/lib/tinybase.js.gz +0 -0
  40. package/lib/tools.d.ts +155 -5
  41. package/lib/tools.js +1 -1
  42. package/lib/tools.js.gz +0 -0
  43. package/lib/umd/checkpoints.js +1 -1
  44. package/lib/umd/checkpoints.js.gz +0 -0
  45. package/lib/umd/indexes.js +1 -1
  46. package/lib/umd/indexes.js.gz +0 -0
  47. package/lib/umd/metrics.js +1 -1
  48. package/lib/umd/metrics.js.gz +0 -0
  49. package/lib/umd/queries.js +1 -1
  50. package/lib/umd/queries.js.gz +0 -0
  51. package/lib/umd/relationships.js +1 -1
  52. package/lib/umd/relationships.js.gz +0 -0
  53. package/lib/umd/store.js +1 -1
  54. package/lib/umd/store.js.gz +0 -0
  55. package/lib/umd/tinybase.js +1 -1
  56. package/lib/umd/tinybase.js.gz +0 -0
  57. package/lib/umd/tools.d.ts +155 -5
  58. package/lib/umd/tools.js +1 -1
  59. package/lib/umd/tools.js.gz +0 -0
  60. package/lib/umd-es6/checkpoints.js +1 -1
  61. package/lib/umd-es6/checkpoints.js.gz +0 -0
  62. package/lib/umd-es6/indexes.js +1 -1
  63. package/lib/umd-es6/indexes.js.gz +0 -0
  64. package/lib/umd-es6/metrics.js +1 -1
  65. package/lib/umd-es6/metrics.js.gz +0 -0
  66. package/lib/umd-es6/queries.js +1 -1
  67. package/lib/umd-es6/queries.js.gz +0 -0
  68. package/lib/umd-es6/relationships.js +1 -1
  69. package/lib/umd-es6/relationships.js.gz +0 -0
  70. package/lib/umd-es6/store.js +1 -1
  71. package/lib/umd-es6/store.js.gz +0 -0
  72. package/lib/umd-es6/tinybase.js +1 -1
  73. package/lib/umd-es6/tinybase.js.gz +0 -0
  74. package/lib/umd-es6/tools.d.ts +155 -5
  75. package/lib/umd-es6/tools.js +1 -1
  76. package/lib/umd-es6/tools.js.gz +0 -0
  77. package/lib/umd-es6/ui-react.js +1 -1
  78. package/lib/umd-es6/ui-react.js.gz +0 -0
  79. package/package.json +6 -5
  80. package/readme.md +21 -2
@@ -20,8 +20,6 @@ const arrayShift = (array) => array.shift();
20
20
  const jsonParse = JSON.parse;
21
21
  const isFiniteNumber = isFinite;
22
22
  const isUndefined = (thing) => thing == void 0;
23
- const ifNotUndefined = (value, then, otherwise) =>
24
- isUndefined(value) ? otherwise?.() : then(value);
25
23
  const isTypeStringOrBoolean = (type) => type == STRING || type == BOOLEAN;
26
24
  const isString = (thing) => getTypeOf(thing) == STRING;
27
25
  const isArray = (thing) => Array.isArray(thing);
@@ -82,7 +80,8 @@ ${text.replace(
82
80
  )}${indent} */`;
83
81
  });
84
82
  const length = (str) => str.length;
85
- const join = (str, sep = EMPTY_STRING) => str.join(sep);
83
+ const join = (array, sep = EMPTY_STRING) => array.join(sep);
84
+ const flat = (array) => array.flat(1e3);
86
85
  const camel = (str, firstCap = 0) =>
87
86
  join(
88
87
  arrayMap(
@@ -99,22 +98,13 @@ const getCodeFunctions = () => {
99
98
  const types = mapNew();
100
99
  const methods = mapNew();
101
100
  const constants = mapNew();
102
- const build = (...lines) => {
103
- const allLines = [];
104
- const handleLine = (line) =>
105
- isArray(line)
106
- ? arrayForEach(line, handleLine)
107
- : arrayPush(allLines, line);
108
- arrayForEach(lines, handleLine);
109
- return allLines.join('\n');
110
- };
101
+ const build = (...lines) => join(flat(lines), '\n');
111
102
  const addImport = (location, source, ...items) =>
112
103
  arrayForEach(items, (item) =>
113
104
  setAdd(mapEnsure(allImports[location], source, setNew), item),
114
105
  );
115
106
  const addType = (name, body = '', doc = '') =>
116
107
  mapUnique(types, name, [body, doc]);
117
- const updateType = (name, body, doc) => mapSet(types, name, [body, doc]);
118
108
  const addMethod = (name, parameters, returnType, body, doc, generic = '') =>
119
109
  mapUnique(methods, name, [parameters, returnType, body, doc, generic]);
120
110
  const addFunction = (name, parameters, body) =>
@@ -167,7 +157,6 @@ const getCodeFunctions = () => {
167
157
  build,
168
158
  addImport,
169
159
  addType,
170
- updateType,
171
160
  addMethod,
172
161
  addFunction,
173
162
  addConstant,
@@ -181,10 +170,8 @@ const getCodeFunctions = () => {
181
170
  const object = Object;
182
171
  const objIds = object.keys;
183
172
  const objFreeze = object.freeze;
184
- const objGet = (obj, id) => ifNotUndefined(obj, (obj2) => obj2[id]);
185
- const objHas = (obj, id) => !isUndefined(objGet(obj, id));
186
- const objForEach = (obj, cb) =>
187
- arrayForEach(object.entries(obj), ([id, value]) => cb(value, id));
173
+ const objMap = (obj, cb) =>
174
+ arrayMap(object.entries(obj), ([id, value]) => cb(value, id));
188
175
  const objIsEmpty = (obj) => arrayIsEmpty(objIds(obj));
189
176
 
190
177
  const getCellType = (cell) => {
@@ -204,16 +191,22 @@ const getCreateFunction = (getFunction) => {
204
191
  };
205
192
  };
206
193
 
194
+ const THE_CONTENT_OF = 'the content of';
207
195
  const THE_STORE = 'the Store';
196
+ const A_FUNCTION_FOR = 'A function for';
197
+ const EXPORT = 'export';
198
+ const LISTENER = 'listener';
199
+ const OR_UNDEFINED = ' | undefined';
200
+ const REGISTERS_A_LISTENER = `Registers a ${LISTENER} that will be called`;
208
201
  const REPRESENTS = 'Represents';
209
- const THE_CONTENT_OF = 'the content of';
202
+ const RETURNS_VOID = ' => void';
210
203
  const THE_CONTENT_OF_THE_STORE = `${THE_CONTENT_OF} ${THE_STORE}`;
204
+ const THE_END_OF_THE_TRANSACTION = 'the end of the transaction';
211
205
  const THE_SPECIFIED_ROW = 'the specified Row';
212
- const REGISTERS_A_LISTENER = 'Registers a listener that will be called';
213
- const storeMethod = (method, parameters = '', cast = '') =>
214
- `store.${method}(${parameters})${cast ? ` as ${cast}` : ''}`;
215
- const fluentStoreMethod = (method, parameters = '') =>
216
- `fluent(() => ${storeMethod(method, parameters)})`;
206
+ const getRowTypeDoc = (tableId, set = 0) =>
207
+ `${REPRESENTS} a Row when ${
208
+ set ? 's' : 'g'
209
+ }etting ${THE_CONTENT_OF} the '${tableId}' Table`;
217
210
  const getIdsDoc = (idsNoun, parentNoun, sorted = 0) =>
218
211
  `Gets ${
219
212
  sorted ? 'sorted, paginated' : 'the'
@@ -223,41 +216,67 @@ const getForEachDoc = (childNoun, parentNoun) =>
223
216
  const getHasDoc = (childNoun, parentNoun = THE_STORE) =>
224
217
  `Gets whether ${childNoun} exists in ${parentNoun}`;
225
218
  const getCallbackDoc = (takes) => `A function that takes ${takes}`;
219
+ const getListenerTypeDoc = (childNoun = 0, parentNoun = 0) =>
220
+ `${A_FUNCTION_FOR} listening to changes to ${nouns[childNoun]} in ${nouns[parentNoun]}`;
226
221
  const getListenerDoc = (childNoun, parentNoun, pluralChild = 0) =>
227
222
  `${REGISTERS_A_LISTENER} whenever ${childNoun} in ${parentNoun} change` +
228
223
  (pluralChild ? '' : 's');
229
- const getVerb = (verb = 0) =>
230
- verb == 1
231
- ? 'Sets'
232
- : verb == 2
233
- ? 'Sets part of'
234
- : verb == 3
235
- ? 'Deletes'
236
- : 'Gets';
224
+ const getStoreContentDoc = (verb = 0) =>
225
+ `${verbs[verb]} ${THE_CONTENT_OF_THE_STORE}`;
226
+ const getTableDoc = (tableId) => `the '${tableId}' Table`;
227
+ const getRowDoc = (tableId) =>
228
+ `${THE_SPECIFIED_ROW} in ${getTableDoc(tableId)}`;
229
+ const getCellDoc = (cellId) => `the '${cellId}' Cell`;
230
+ const getTableContentDoc = (tableId, verb = 0) =>
231
+ `${verbs[verb]} ${THE_CONTENT_OF} ${getTableDoc(tableId)}`;
232
+ const getRowContentDoc = (tableId, verb = 0) =>
233
+ `${verbs[verb]} ${THE_CONTENT_OF} ${getRowDoc(tableId)}`;
234
+ const getCellContentDoc = (tableId, cellId, verb = 0) =>
235
+ `${verbs[verb]} ${getCellDoc(cellId)} for ${getRowDoc(tableId)}`;
236
+ const verbs = ['Gets', 'Sets', 'Sets part of', 'Deletes'];
237
+ const nouns = [
238
+ THE_STORE,
239
+ 'anything',
240
+ 'Table Ids',
241
+ 'a Table',
242
+ 'Row Ids',
243
+ 'a Row',
244
+ 'Cell Ids',
245
+ 'a Cell',
246
+ 'invalid Cell changes',
247
+ ];
248
+
249
+ const COMMON_IMPORTS = [
250
+ 'ChangedCells',
251
+ 'Id',
252
+ 'IdOrNull',
253
+ 'Ids',
254
+ 'InvalidCells',
255
+ 'Json',
256
+ 'Store',
257
+ ];
258
+ const storeMethod = (method, parameters = EMPTY_STRING, cast = EMPTY_STRING) =>
259
+ `store.${method}(${parameters})${cast ? ` as ${cast}` : EMPTY_STRING}`;
260
+ const fluentStoreMethod = (method, parameters = EMPTY_STRING) =>
261
+ `fluent(() => ${storeMethod(method, parameters)})`;
262
+ const storeListener = (
263
+ method,
264
+ beforeParameters = EMPTY_STRING,
265
+ afterParameters = EMPTY_STRING,
266
+ ) =>
267
+ `store.${method}(${
268
+ beforeParameters ? `${beforeParameters}, ` : EMPTY_STRING
269
+ }proxy(${LISTENER})${
270
+ afterParameters ? `, ${afterParameters}` : EMPTY_STRING
271
+ })`;
237
272
  const getStoreApi = (schema, module) => {
238
273
  if (objIsEmpty(schema)) {
239
- return pairNew('// store has no inferable schema');
274
+ return pairNew(EMPTY_STRING);
240
275
  }
241
- const moduleName = camel(module);
242
- const storeType = camel(module, 1);
243
- const storeInstance = camel(storeType);
244
- const returnStore = `return ${storeInstance};`;
245
- const tablesTypes = [];
246
- const tableIdTypes = [];
247
- const tableCallbackArgTypes = [];
248
- const getCellChangeArgTypes = [];
249
- const cellListenerArgTypes = [];
250
- const allCellIdType = [];
251
- const schemaLines = [];
252
- const storeListener = (method, beforeParameters = '', afterParameters = '') =>
253
- `store.${method}(${
254
- beforeParameters ? `${beforeParameters}, ` : ''
255
- }proxy(listener)${afterParameters ? `, ${afterParameters}` : ''})`;
256
276
  const [
257
277
  build,
258
278
  addImport,
259
279
  addType,
260
- updateType,
261
280
  addMethod,
262
281
  addFunction,
263
282
  addConstant,
@@ -266,113 +285,198 @@ const getStoreApi = (schema, module) => {
266
285
  getMethods,
267
286
  getConstants,
268
287
  ] = getCodeFunctions();
269
- addImport(
270
- 0,
271
- 'tinybase',
272
- 'CellChange',
273
- 'ChangedCells',
274
- 'Id',
275
- 'IdOrNull',
276
- 'Ids',
277
- 'InvalidCells',
278
- 'Json',
279
- 'Store',
288
+ const moduleDefinition = `./${camel(module)}.d`;
289
+ const storeType = camel(module, 1);
290
+ const storeInstance = camel(storeType);
291
+ const tableTypes = mapNew();
292
+ const mapTableSchema = (callback) =>
293
+ objMap(schema, (_, tableId) => {
294
+ return callback(
295
+ tableId,
296
+ mapEnsure(tableTypes, tableId, () => {
297
+ const table = camel(tableId, 1);
298
+ return [
299
+ addType(
300
+ `${table}Table`,
301
+ `{[rowId: Id]: ${table}Row}`,
302
+ `${REPRESENTS} the '${tableId}' Table`,
303
+ ),
304
+ addType(
305
+ `${table}Row`,
306
+ `{${join(
307
+ mapCellSchema(
308
+ tableId,
309
+ (cellId, type, defaultValue) =>
310
+ `'${cellId}'${
311
+ isUndefined(defaultValue) ? '?' : EMPTY_STRING
312
+ }: ${type};`,
313
+ ),
314
+ ' ',
315
+ )}}`,
316
+ getRowTypeDoc(tableId),
317
+ ),
318
+ addType(
319
+ `${table}RowWhenSet`,
320
+ `{${join(
321
+ mapCellSchema(
322
+ tableId,
323
+ (cellId, type) => `'${cellId}'?: ${type};`,
324
+ ),
325
+ ' ',
326
+ )}}`,
327
+ getRowTypeDoc(tableId, 1),
328
+ ),
329
+ addType(
330
+ `${table}CellId`,
331
+ join(
332
+ mapCellSchema(tableId, (cellId) => `'${cellId}'`),
333
+ ' | ',
334
+ ),
335
+ `A Cell Id for the '${tableId}' Table`,
336
+ ),
337
+ addType(
338
+ `${table}CellCallback`,
339
+ `(...[cellId, cell]: ${join(
340
+ mapCellSchema(
341
+ tableId,
342
+ (cellId, type) => `[cellId: '${cellId}', cell: ${type}]`,
343
+ ),
344
+ ' | ',
345
+ )})${RETURNS_VOID}`,
346
+ getCallbackDoc(
347
+ `a Cell Id and value from a Row in the '${tableId}' Table`,
348
+ ),
349
+ ),
350
+ addType(
351
+ `${table}RowCallback`,
352
+ `(rowId: Id, forEachCell: (cellCallback: ${table}CellCallback)${RETURNS_VOID})${RETURNS_VOID}`,
353
+ getCallbackDoc(
354
+ `a Row Id from the '${tableId}' Table, and a Cell iterator`,
355
+ ),
356
+ ),
357
+ ];
358
+ }),
359
+ camel(tableId, 1),
360
+ addConstant(snake(tableId), `'${tableId}'`),
361
+ );
362
+ });
363
+ const mapCellSchema = (tableId, callback) =>
364
+ objMap(schema[tableId], (cellSchema, cellId) =>
365
+ callback(
366
+ cellId,
367
+ cellSchema[TYPE],
368
+ cellSchema[DEFAULT],
369
+ addConstant(snake(cellId), `'${cellId}'`),
370
+ camel(cellId, 1),
371
+ ),
372
+ );
373
+ const tablesType = addType(
374
+ 'Tables',
375
+ `{${join(
376
+ mapTableSchema(
377
+ (tableId, tableTypes2) => `'${tableId}'?: ${tableTypes2[0]};`,
378
+ ),
379
+ ' ',
380
+ )}}`,
381
+ `${REPRESENTS} ${THE_CONTENT_OF_THE_STORE}`,
280
382
  );
281
- addImport(
282
- 1,
283
- 'tinybase',
284
- 'ChangedCells',
285
- 'Id',
286
- 'IdOrNull',
287
- 'Ids',
288
- 'InvalidCells',
289
- 'Json',
290
- 'Store',
291
- 'createStore',
383
+ const tableIdType = addType(
384
+ 'TableId',
385
+ join(
386
+ mapTableSchema((tableId) => `'${tableId}'`),
387
+ ' | ',
388
+ ),
389
+ `A Table Id in ${THE_STORE}`,
390
+ );
391
+ const tableCallbackType = addType(
392
+ 'TableCallback',
393
+ `(...[tableId, rowCallback]: ${join(
394
+ mapTableSchema(
395
+ (tableId, tableTypes2) =>
396
+ `[tableId: '${tableId}', forEachRow: (rowCallback: ${tableTypes2[5]})${RETURNS_VOID}]`,
397
+ ),
398
+ ' | ',
399
+ )})${RETURNS_VOID}`,
400
+ getCallbackDoc('a Table Id, and a Row iterator'),
401
+ );
402
+ const getCellChangeType = addType(
403
+ 'GetCellChange',
404
+ `(...[tableId, rowId, cellId]: ${join(
405
+ mapTableSchema(
406
+ (tableId, tableTypes2) =>
407
+ `[tableId: '${tableId}', rowId: Id, cellId: ${tableTypes2[3]}]`,
408
+ ),
409
+ ' | ',
410
+ )}) => CellChange`,
411
+ `${A_FUNCTION_FOR} returning information about any Cell's changes during a transaction`,
292
412
  );
293
- addFunction('fluent', 'actions: () => Store', ['actions();', returnStore]);
294
- addFunction(
295
- 'proxy',
296
- `listener: any`,
297
- `(_: Store, ...args: any[]) => listener(${storeInstance}, ...args)`,
298
- );
299
- const TYPE2 = addConstant(snake(TYPE), `'${TYPE}'`);
300
- const DEFAULT2 = addConstant(snake(DEFAULT), `'${DEFAULT}'`);
301
- const tablesType = addType('Tables');
302
- const tableIdType = addType('TableId');
303
- const tableCallbackType = addType('TableCallback');
304
- const getCellChangeType = addType('GetCellChange');
305
413
  const tablesListenerType = addType(
306
414
  'TablesListener',
307
- `(${storeInstance}: ${storeType}, getCellChange: ${getCellChangeType} | undefined) => void`,
308
- `A function for listening to changes to ${THE_CONTENT_OF_THE_STORE}`,
415
+ `(${storeInstance}: ${storeType}, getCellChange: ${getCellChangeType}${OR_UNDEFINED})${RETURNS_VOID}`,
416
+ getListenerTypeDoc(1),
309
417
  );
310
418
  const tableIdsListenerType = addType(
311
419
  'TableIdsListener',
312
- `(${storeInstance}: ${storeType}) => void`,
313
- `A function for listening to changes to Table Ids in ${THE_STORE}`,
420
+ `(${storeInstance}: ${storeType})${RETURNS_VOID}`,
421
+ getListenerTypeDoc(2),
314
422
  );
315
423
  const tableListenerType = addType(
316
424
  'TableListener',
317
- `(${storeInstance}: ${storeType}, tableId: ${tableIdType}, getCellChange: ${getCellChangeType} | undefined) => void`,
318
- `A function for listening to changes to a Table in ${THE_STORE}`,
425
+ `(${storeInstance}: ${storeType}, tableId: ${tableIdType}, getCellChange: ${getCellChangeType}${OR_UNDEFINED})${RETURNS_VOID}`,
426
+ getListenerTypeDoc(3),
319
427
  );
320
428
  const rowIdsListenerType = addType(
321
429
  'RowIdsListener',
322
- `(${storeInstance}: ${storeType}, tableId: ${tableIdType}) => void`,
323
- `A function for listening to changes to Row Ids in ${THE_STORE}`,
430
+ `(${storeInstance}: ${storeType}, tableId: ${tableIdType})${RETURNS_VOID}`,
431
+ getListenerTypeDoc(4, 3),
324
432
  );
325
433
  const rowListenerType = addType(
326
434
  'RowListener',
327
- `(${storeInstance}: ${storeType}, tableId: ${tableIdType}, rowId: Id, getCellChange: ${getCellChangeType} | undefined) => void`,
328
- `A function for listening to changes to a Row in ${THE_STORE}`,
435
+ `(${storeInstance}: ${storeType}, tableId: ${tableIdType}, rowId: Id, getCellChange: ${getCellChangeType}${OR_UNDEFINED})${RETURNS_VOID}`,
436
+ getListenerTypeDoc(5, 3),
329
437
  );
330
438
  const cellIdsListenerType = addType(
331
439
  'CellIdsListener',
332
- `(${storeInstance}: ${storeType}, tableId: ${tableIdType}, rowId: Id) => void`,
333
- `A function for listening to changes to Cell Ids in ${THE_STORE}`,
440
+ `(${storeInstance}: ${storeType}, tableId: ${tableIdType}, rowId: Id)` +
441
+ RETURNS_VOID,
442
+ getListenerTypeDoc(6, 5),
443
+ );
444
+ const cellListenerType = addType(
445
+ 'CellListener',
446
+ `(...[${storeInstance}, tableId, rowId, cellId, newCell, oldCell, getCellChange]: ${join(
447
+ flat(
448
+ mapTableSchema((tableId) =>
449
+ mapCellSchema(
450
+ tableId,
451
+ (cellId, type) =>
452
+ `[${storeInstance}: ${storeType}, tableId: '${tableId}', rowId: Id, cellId: '${cellId}', newCell: ${type}${OR_UNDEFINED}, oldCell: ${type}${OR_UNDEFINED}, getCellChange: ${getCellChangeType} | undefined]`,
453
+ ),
454
+ ),
455
+ ),
456
+ ' | ',
457
+ )})${RETURNS_VOID}`,
458
+ getListenerTypeDoc(7, 5),
334
459
  );
335
- const cellListenerType = addType('CellListener');
336
460
  const invalidCellListenerType = addType(
337
461
  'InvalidCellListener',
338
- `(${storeInstance}: ${storeType}, tableId: Id, rowId: Id, cellId: Id, invalidCells: any[]) => void;`,
339
- `A function for listening to invalid Cell changes`,
462
+ `(${storeInstance}: ${storeType}, tableId: Id, rowId: Id, cellId: Id, invalidCells: any[])${RETURNS_VOID};`,
463
+ getListenerTypeDoc(8),
340
464
  );
341
465
  const transactionListenerType = addType(
342
466
  'TransactionListener',
343
- `(${storeInstance}: ${storeType}, cellsTouched: boolean) => void;`,
344
- `A function for listening to the completion of a transaction`,
345
- );
346
- addImport(
347
- 1,
348
- `./${moduleName}.d`,
349
- storeType,
350
- `create${storeType} as create${storeType}Decl`,
351
- tablesType,
352
- tableIdType,
353
- tableCallbackType,
354
- tablesListenerType,
355
- tableIdsListenerType,
356
- tableListenerType,
357
- rowIdsListenerType,
358
- rowListenerType,
359
- cellIdsListenerType,
360
- cellListenerType,
361
- invalidCellListenerType,
362
- transactionListenerType,
467
+ `(${storeInstance}: ${storeType}, cellsTouched: boolean)${RETURNS_VOID};`,
468
+ `${A_FUNCTION_FOR} listening to the completion of a transaction`,
363
469
  );
364
- const getStoreContentDoc = (verb = 0) =>
365
- `${getVerb(verb)} ${THE_CONTENT_OF_THE_STORE}`;
366
470
  addMethod(
367
471
  `hasTables`,
368
- '',
472
+ EMPTY_STRING,
369
473
  BOOLEAN,
370
474
  storeMethod('hasTables'),
371
475
  getHasDoc('any Table'),
372
476
  );
373
477
  addMethod(
374
478
  `getTables`,
375
- '',
479
+ EMPTY_STRING,
376
480
  tablesType,
377
481
  storeMethod('getTables'),
378
482
  getStoreContentDoc(),
@@ -386,16 +490,16 @@ const getStoreApi = (schema, module) => {
386
490
  );
387
491
  addMethod(
388
492
  `delTables`,
389
- '',
493
+ EMPTY_STRING,
390
494
  storeType,
391
495
  fluentStoreMethod('delTables'),
392
496
  getStoreContentDoc(3),
393
497
  );
394
498
  addMethod(
395
499
  `getTableIds`,
396
- '',
500
+ EMPTY_STRING,
397
501
  `${tableIdType}[]`,
398
- storeMethod('getTableIds', '', `${tableIdType}[]`),
502
+ storeMethod('getTableIds', EMPTY_STRING, `${tableIdType}[]`),
399
503
  getIdsDoc('Table', THE_STORE),
400
504
  );
401
505
  addMethod(
@@ -406,266 +510,198 @@ const getStoreApi = (schema, module) => {
406
510
  getForEachDoc('Table', THE_STORE),
407
511
  );
408
512
  const mapCellTypes = mapNew();
409
- objForEach(schema, (cellSchemas, tableId) => {
410
- const table = camel(tableId, 1);
411
- const TABLE_ID = addConstant(snake(tableId), `'${tableId}'`);
412
- const getCellsTypes = [];
413
- const setCellsTypes = [];
414
- const cellIdTypes = [];
415
- const cellCallbackArgTypes = [];
416
- const tableDoc = `the '${tableId}' Table`;
417
- const rowDoc = `${THE_SPECIFIED_ROW} in ${tableDoc}`;
418
- const tableContentDoc = `${THE_CONTENT_OF} ${tableDoc}`;
419
- const getTableContentDoc = (verb = 0) =>
420
- `${getVerb(verb)} ${THE_CONTENT_OF} ${tableDoc}`;
421
- const getRowContentDoc = (verb = 0) =>
422
- `${getVerb(verb)} ${THE_CONTENT_OF} ${rowDoc}`;
423
- const getRowTypeDoc = (set = 0) =>
424
- `${REPRESENTS} a Row when ${set ? 's' : 'g'}etting ${tableContentDoc}`;
425
- const tableType = addType(
426
- `${table}Table`,
427
- `{[rowId: Id]: ${table}Row}`,
428
- `${REPRESENTS} ${tableDoc}`,
429
- );
430
- const rowType = addType(`${table}Row`);
431
- const rowWhenSetType = addType(`${table}RowWhenSet`);
432
- const cellIdType = addType(`${table}CellId`);
433
- const cellCallbackType = addType(`${table}CellCallback`);
434
- const rowCallbackType = addType(`${table}RowCallback`);
435
- addImport(
436
- 1,
437
- `./${moduleName}.d`,
438
- tableType,
439
- rowType,
440
- rowWhenSetType,
441
- cellIdType,
442
- cellCallbackType,
443
- rowCallbackType,
444
- );
445
- addMethod(
446
- `has${table}Table`,
447
- '',
448
- BOOLEAN,
449
- storeMethod('hasTable', TABLE_ID),
450
- getHasDoc(tableDoc),
451
- );
452
- addMethod(
453
- `get${table}Table`,
454
- '',
455
- tableType,
456
- storeMethod('getTable', TABLE_ID, tableType),
457
- getTableContentDoc(),
458
- );
459
- addMethod(
460
- `set${table}Table`,
461
- `table: ${tableType}`,
462
- storeType,
463
- fluentStoreMethod('setTable', `${TABLE_ID}, table`),
464
- getTableContentDoc(1),
465
- );
466
- addMethod(
467
- `del${table}Table`,
468
- '',
469
- storeType,
470
- fluentStoreMethod('delTable', TABLE_ID),
471
- getTableContentDoc(3),
472
- );
473
- addMethod(
474
- `get${table}RowIds`,
475
- '',
476
- 'Ids',
477
- storeMethod('getRowIds', TABLE_ID),
478
- getIdsDoc('Row', tableDoc),
479
- );
480
- addMethod(
481
- `get${table}SortedRowIds`,
482
- `cellId?: ${cellIdType}, descending?: boolean, offset?: number, limit?: number`,
483
- 'Ids',
484
- storeMethod(
485
- 'getSortedRowIds',
486
- `${TABLE_ID}, cellId, descending, offset, limit`,
487
- ),
488
- getIdsDoc('Row', tableDoc, 1),
489
- );
490
- addMethod(
491
- `forEach${table}Row`,
492
- `rowCallback: ${rowCallbackType}`,
493
- 'void',
494
- storeMethod('forEachRow', `${TABLE_ID}, rowCallback as any`),
495
- getForEachDoc('Row', tableDoc),
496
- );
497
- addMethod(
498
- `has${table}Row`,
499
- 'rowId: Id',
500
- BOOLEAN,
501
- storeMethod('hasRow', `${TABLE_ID}, rowId`),
502
- getHasDoc(THE_SPECIFIED_ROW, tableDoc),
503
- );
504
- addMethod(
505
- `get${table}Row`,
506
- 'rowId: Id',
507
- rowType,
508
- storeMethod('getRow', `${TABLE_ID}, rowId`, rowType),
509
- getRowContentDoc(),
510
- );
511
- addMethod(
512
- `set${table}Row`,
513
- `rowId: Id, row: ${rowWhenSetType}`,
514
- storeType,
515
- fluentStoreMethod('setRow', `${TABLE_ID}, rowId, row`),
516
- getRowContentDoc(1),
517
- );
518
- addMethod(
519
- `add${table}Row`,
520
- `row: ${rowWhenSetType}`,
521
- 'Id | undefined',
522
- storeMethod('addRow', `${TABLE_ID}, row`),
523
- `Adds a new Row to ${tableDoc}`,
524
- );
525
- addMethod(
526
- `set${table}PartialRow`,
527
- `rowId: Id, partialRow: ${rowWhenSetType}`,
528
- storeType,
529
- fluentStoreMethod('setPartialRow', `${TABLE_ID}, rowId, partialRow`),
530
- getRowContentDoc(2),
531
- );
532
- addMethod(
533
- `del${table}Row`,
534
- `rowId: Id`,
535
- storeType,
536
- fluentStoreMethod('delRow', `${TABLE_ID}, rowId`),
537
- getRowContentDoc(3),
538
- );
539
- addMethod(
540
- `get${table}CellIds`,
541
- 'rowId: Id',
542
- `${cellIdType}[]`,
543
- storeMethod('getCellIds', `${TABLE_ID}, rowId`, `${cellIdType}[]`),
544
- getIdsDoc('Cell', rowDoc),
545
- );
546
- addMethod(
547
- `forEach${table}Cell`,
548
- `rowId: Id, cellCallback: ${cellCallbackType}`,
549
- 'void',
550
- storeMethod('forEachCell', `${TABLE_ID}, rowId, cellCallback as any`),
551
- getForEachDoc('Cell', rowDoc),
552
- );
553
- arrayPush(schemaLines, `[${TABLE_ID}]: {`);
554
- objForEach(cellSchemas, (cellSchema, cellId) => {
555
- const cell = camel(cellId, 1);
556
- const CELL_ID = addConstant(snake(cellId), `'${cellId}'`);
557
- const type = cellSchema[TYPE];
558
- const defaulted = objHas(cellSchema, DEFAULT);
559
- const defaultValue = cellSchema[DEFAULT];
560
- const cellDoc = `the '${cellId}' Cell`;
561
- const getCellContentDoc = (verb = 0) =>
562
- `${getVerb(verb)} ${cellDoc} for ${rowDoc}`;
563
- arrayPush(
564
- schemaLines,
565
- `[${CELL_ID}]: {[${TYPE2}]: ${addConstant(snake(type), `'${type}'`)}${
566
- defaulted
567
- ? `, [${DEFAULT2}]: ${
568
- isString(defaultValue)
569
- ? addConstant(snake(defaultValue), `'${defaultValue}'`)
570
- : defaultValue
571
- }`
572
- : ''
573
- }},`,
513
+ mapTableSchema(
514
+ (
515
+ tableId,
516
+ [
517
+ tableType,
518
+ rowType,
519
+ rowWhenSetType,
520
+ cellIdType,
521
+ cellCallbackType,
522
+ rowCallbackType,
523
+ ],
524
+ tableName,
525
+ TABLE_ID,
526
+ ) => {
527
+ addImport(
528
+ 1,
529
+ moduleDefinition,
530
+ tableType,
531
+ rowType,
532
+ rowWhenSetType,
533
+ cellIdType,
534
+ cellCallbackType,
535
+ rowCallbackType,
536
+ );
537
+ addMethod(
538
+ `has${tableName}Table`,
539
+ EMPTY_STRING,
540
+ BOOLEAN,
541
+ storeMethod('hasTable', TABLE_ID),
542
+ getHasDoc(getTableDoc(tableId)),
543
+ );
544
+ addMethod(
545
+ `get${tableName}Table`,
546
+ EMPTY_STRING,
547
+ tableType,
548
+ storeMethod('getTable', TABLE_ID, tableType),
549
+ getTableContentDoc(tableId),
550
+ );
551
+ addMethod(
552
+ `set${tableName}Table`,
553
+ `table: ${tableType}`,
554
+ storeType,
555
+ fluentStoreMethod('setTable', `${TABLE_ID}, table`),
556
+ getTableContentDoc(tableId, 1),
557
+ );
558
+ addMethod(
559
+ `del${tableName}Table`,
560
+ EMPTY_STRING,
561
+ storeType,
562
+ fluentStoreMethod('delTable', TABLE_ID),
563
+ getTableContentDoc(tableId, 3),
564
+ );
565
+ addMethod(
566
+ `get${tableName}RowIds`,
567
+ EMPTY_STRING,
568
+ 'Ids',
569
+ storeMethod('getRowIds', TABLE_ID),
570
+ getIdsDoc('Row', getTableDoc(tableId)),
574
571
  );
575
- arrayPush(getCellsTypes, `'${cellId}'${defaulted ? '' : '?'}: ${type};`);
576
- arrayPush(setCellsTypes, `'${cellId}'?: ${type};`);
577
- arrayPush(cellIdTypes, `'${cellId}'`);
578
- arrayPush(cellCallbackArgTypes, `[cellId: '${cellId}', cell: ${type}]`);
579
- arrayPush(
580
- cellListenerArgTypes,
581
- `[${storeInstance}: ${storeType}, tableId: '${tableId}', rowId: Id, cellId: '${cellId}', newCell: ${type} | undefined, oldCell: ${type} | undefined, getCellChange: ${getCellChangeType} | undefined]`,
572
+ addMethod(
573
+ `get${tableName}SortedRowIds`,
574
+ `cellId?: ${cellIdType}, descending?: boolean, offset?: number, limit?: number`,
575
+ 'Ids',
576
+ storeMethod(
577
+ 'getSortedRowIds',
578
+ `${TABLE_ID}, cellId, descending, offset, limit`,
579
+ ),
580
+ getIdsDoc('Row', getTableDoc(tableId), 1),
582
581
  );
583
- const mapCellType = `Map${camel(type, 1)}`;
584
- mapSet(mapCellTypes, type, mapCellType);
585
582
  addMethod(
586
- `has${table}${cell}Cell`,
583
+ `forEach${tableName}Row`,
584
+ `rowCallback: ${rowCallbackType}`,
585
+ 'void',
586
+ storeMethod('forEachRow', `${TABLE_ID}, rowCallback as any`),
587
+ getForEachDoc('Row', getTableDoc(tableId)),
588
+ );
589
+ addMethod(
590
+ `has${tableName}Row`,
587
591
  'rowId: Id',
588
592
  BOOLEAN,
589
- storeMethod('hasCell', `${TABLE_ID}, rowId, ${CELL_ID}`),
590
- getHasDoc(cellDoc, rowDoc),
593
+ storeMethod('hasRow', `${TABLE_ID}, rowId`),
594
+ getHasDoc(THE_SPECIFIED_ROW, getTableDoc(tableId)),
591
595
  );
592
- const returnCellType = `${type}${defaulted ? '' : ' | undefined'}`;
593
596
  addMethod(
594
- `get${table}${cell}Cell`,
597
+ `get${tableName}Row`,
595
598
  'rowId: Id',
596
- returnCellType,
597
- storeMethod(
598
- 'getCell',
599
- `${TABLE_ID}, rowId, ${CELL_ID}`,
600
- returnCellType,
601
- ),
602
- getCellContentDoc(),
599
+ rowType,
600
+ storeMethod('getRow', `${TABLE_ID}, rowId`, rowType),
601
+ getRowContentDoc(tableId),
603
602
  );
604
603
  addMethod(
605
- `set${table}${cell}Cell`,
606
- `rowId: Id, cell: ${type} | ${mapCellType}`,
604
+ `set${tableName}Row`,
605
+ `rowId: Id, row: ${rowWhenSetType}`,
607
606
  storeType,
608
- fluentStoreMethod(
609
- 'setCell',
610
- `${TABLE_ID}, rowId, ${CELL_ID}, cell as any`,
611
- ),
612
- getCellContentDoc(1),
607
+ fluentStoreMethod('setRow', `${TABLE_ID}, rowId, row`),
608
+ getRowContentDoc(tableId, 1),
613
609
  );
614
610
  addMethod(
615
- `del${table}${cell}Cell`,
611
+ `add${tableName}Row`,
612
+ `row: ${rowWhenSetType}`,
613
+ `Id${OR_UNDEFINED}`,
614
+ storeMethod('addRow', `${TABLE_ID}, row`),
615
+ `Adds a new Row to ${getTableDoc(tableId)}`,
616
+ );
617
+ addMethod(
618
+ `set${tableName}PartialRow`,
619
+ `rowId: Id, partialRow: ${rowWhenSetType}`,
620
+ storeType,
621
+ fluentStoreMethod('setPartialRow', `${TABLE_ID}, rowId, partialRow`),
622
+ getRowContentDoc(tableId, 2),
623
+ );
624
+ addMethod(
625
+ `del${tableName}Row`,
616
626
  `rowId: Id`,
617
627
  storeType,
618
- fluentStoreMethod('delCell', `${TABLE_ID}, rowId, ${CELL_ID}`),
619
- getCellContentDoc(3),
628
+ fluentStoreMethod('delRow', `${TABLE_ID}, rowId`),
629
+ getRowContentDoc(tableId, 3),
620
630
  );
621
- });
622
- arrayPush(schemaLines, `},`);
623
- arrayPush(tablesTypes, `'${tableId}'?: ${tableType};`);
624
- arrayPush(tableIdTypes, `'${tableId}'`);
625
- arrayPush(
626
- tableCallbackArgTypes,
627
- `[tableId: '${tableId}', forEachRow: (rowCallback: ${rowCallbackType}) => void]`,
628
- );
629
- arrayPush(
630
- getCellChangeArgTypes,
631
- `[tableId: '${tableId}', rowId: Id, cellId: ${cellIdType}]`,
632
- );
633
- arrayPush(allCellIdType, cellIdType);
634
- updateType(rowType, `{${join(getCellsTypes, ' ')}}`, getRowTypeDoc());
635
- updateType(
636
- rowWhenSetType,
637
- `{${join(setCellsTypes, ' ')}}`,
638
- getRowTypeDoc(1),
639
- );
640
- updateType(
641
- cellIdType,
642
- join(cellIdTypes, ' | '),
643
- `A Cell Id for ${tableDoc}`,
644
- );
645
- updateType(
646
- cellCallbackType,
647
- `(...[cellId, cell]: ${join(cellCallbackArgTypes, ' | ')}) => void`,
648
- getCallbackDoc(`a Cell Id and value from a Row in ${tableDoc}`),
649
- );
650
- updateType(
651
- rowCallbackType,
652
- `(rowId: Id, forEachCell: (cellCallback: ${cellCallbackType}) => void) => void`,
653
- getCallbackDoc(`a Row Id from ${tableDoc}, and a Cell iterator`),
654
- );
655
- });
631
+ addMethod(
632
+ `get${tableName}CellIds`,
633
+ 'rowId: Id',
634
+ `${cellIdType}[]`,
635
+ storeMethod('getCellIds', `${TABLE_ID}, rowId`, `${cellIdType}[]`),
636
+ getIdsDoc('Cell', getRowDoc(tableId)),
637
+ );
638
+ addMethod(
639
+ `forEach${tableName}Cell`,
640
+ `rowId: Id, cellCallback: ${cellCallbackType}`,
641
+ 'void',
642
+ storeMethod('forEachCell', `${TABLE_ID}, rowId, cellCallback as any`),
643
+ getForEachDoc('Cell', getRowDoc(tableId)),
644
+ );
645
+ mapCellSchema(
646
+ tableId,
647
+ (cellId, type, defaultValue, CELL_ID, cellName) => {
648
+ const mapCellType = `Map${camel(type, 1)}`;
649
+ mapSet(mapCellTypes, type, mapCellType);
650
+ addMethod(
651
+ `has${tableName}${cellName}Cell`,
652
+ 'rowId: Id',
653
+ BOOLEAN,
654
+ storeMethod('hasCell', `${TABLE_ID}, rowId, ${CELL_ID}`),
655
+ getHasDoc(getCellDoc(cellId), getRowDoc(tableId)),
656
+ );
657
+ const returnCellType = `${type}${
658
+ isUndefined(defaultValue) ? OR_UNDEFINED : EMPTY_STRING
659
+ }`;
660
+ addMethod(
661
+ `get${tableName}${cellName}Cell`,
662
+ 'rowId: Id',
663
+ returnCellType,
664
+ storeMethod(
665
+ 'getCell',
666
+ `${TABLE_ID}, rowId, ${CELL_ID}`,
667
+ returnCellType,
668
+ ),
669
+ getCellContentDoc(tableId, cellId),
670
+ );
671
+ addMethod(
672
+ `set${tableName}${cellName}Cell`,
673
+ `rowId: Id, cell: ${type} | ${mapCellType}`,
674
+ storeType,
675
+ fluentStoreMethod(
676
+ 'setCell',
677
+ `${TABLE_ID}, rowId, ${CELL_ID}, cell as any`,
678
+ ),
679
+ getCellContentDoc(tableId, cellId, 1),
680
+ );
681
+ addMethod(
682
+ `del${tableName}${cellName}Cell`,
683
+ `rowId: Id`,
684
+ storeType,
685
+ fluentStoreMethod('delCell', `${TABLE_ID}, rowId, ${CELL_ID}`),
686
+ getCellContentDoc(tableId, cellId, 3),
687
+ );
688
+ },
689
+ );
690
+ },
691
+ );
656
692
  addMethod(
657
693
  'getJson',
658
- '',
694
+ EMPTY_STRING,
659
695
  'Json',
660
696
  storeMethod('getJson'),
661
- `Gets a a string serialization ${THE_CONTENT_OF_THE_STORE}`,
697
+ `${verbs[0]} a string serialization ${THE_CONTENT_OF_THE_STORE}`,
662
698
  );
663
699
  addMethod(
664
700
  'setJson',
665
701
  'json: Json',
666
702
  storeType,
667
703
  fluentStoreMethod('setJson', 'json'),
668
- `Sets ${THE_CONTENT_OF_THE_STORE} from a serialized string`,
704
+ `${verbs[1]} ${THE_CONTENT_OF_THE_STORE} from a serialized string`,
669
705
  );
670
706
  addMethod(
671
707
  'transaction',
@@ -677,7 +713,7 @@ const getStoreApi = (schema, module) => {
677
713
  );
678
714
  addMethod(
679
715
  'startTransaction',
680
- '',
716
+ EMPTY_STRING,
681
717
  storeType,
682
718
  fluentStoreMethod('startTransaction'),
683
719
  'Explicitly starts a transaction',
@@ -691,42 +727,42 @@ const getStoreApi = (schema, module) => {
691
727
  );
692
728
  addMethod(
693
729
  'addTablesListener',
694
- `listener: ${tablesListenerType}, mutator?: boolean`,
730
+ `${LISTENER}: ${tablesListenerType}, mutator?: boolean`,
695
731
  'Id',
696
- storeListener('addTablesListener', '', 'mutator'),
732
+ storeListener('addTablesListener', EMPTY_STRING, 'mutator'),
697
733
  `${REGISTERS_A_LISTENER} whenever ${THE_CONTENT_OF_THE_STORE} changes`,
698
734
  );
699
735
  addMethod(
700
736
  'addTableIdsListener',
701
- `listener: ${tableIdsListenerType}, mutator?: boolean`,
737
+ `${LISTENER}: ${tableIdsListenerType}, mutator?: boolean`,
702
738
  'Id',
703
- storeListener('addTableIdsListener', '', 'mutator'),
739
+ storeListener('addTableIdsListener', EMPTY_STRING, 'mutator'),
704
740
  getListenerDoc('the Table Ids', THE_STORE, 1),
705
741
  );
706
742
  addMethod(
707
743
  'addTableListener',
708
- `tableId: ${tableIdType} | null, listener: ${tableListenerType}, mutator?: boolean`,
744
+ `tableId: ${tableIdType} | null, ${LISTENER}: ${tableListenerType}, mutator?: boolean`,
709
745
  'Id',
710
746
  storeListener('addTableListener', 'tableId', 'mutator'),
711
747
  getListenerDoc('a Table', THE_STORE),
712
748
  );
713
749
  addMethod(
714
750
  'addRowIdsListener',
715
- `tableId: ${tableIdType} | null, listener: ${rowIdsListenerType}, mutator?: boolean`,
751
+ `tableId: ${tableIdType} | null, ${LISTENER}: ${rowIdsListenerType}, mutator?: boolean`,
716
752
  'Id',
717
753
  storeListener('addRowIdsListener', 'tableId', 'mutator'),
718
754
  getListenerDoc('the Row Ids', 'a Table', 1),
719
755
  );
720
756
  addMethod(
721
757
  'addRowListener',
722
- `tableId: ${tableIdType} | null, rowId: IdOrNull, listener: ${rowListenerType}, mutator?: boolean`,
758
+ `tableId: ${tableIdType} | null, rowId: IdOrNull, ${LISTENER}: ${rowListenerType}, mutator?: boolean`,
723
759
  'Id',
724
760
  storeListener('addRowListener', 'tableId, rowId', 'mutator'),
725
761
  getListenerDoc('a Row', 'a Table'),
726
762
  );
727
763
  addMethod(
728
764
  'addCellIdsListener',
729
- `tableId: ${tableIdType} | null, rowId: IdOrNull, listener: ${cellIdsListenerType}, mutator?: boolean`,
765
+ `tableId: ${tableIdType} | null, rowId: IdOrNull, ${LISTENER}: ${cellIdsListenerType}, mutator?: boolean`,
730
766
  'Id',
731
767
  storeListener('addCellIdsListener', 'tableId, rowId', 'mutator'),
732
768
  getListenerDoc('the Cell Ids', 'a Row', 1),
@@ -734,113 +770,133 @@ const getStoreApi = (schema, module) => {
734
770
  addMethod(
735
771
  'addCellListener',
736
772
  `tableId: ${tableIdType} | null, rowId: IdOrNull, cellId: ${join(
737
- allCellIdType,
773
+ mapTableSchema((_, tableTypes2) => tableTypes2[3]),
738
774
  ' | ',
739
- )} | null, listener: ${cellListenerType}, mutator?: boolean`,
775
+ )} | null, ${LISTENER}: ${cellListenerType}, mutator?: boolean`,
740
776
  'Id',
741
777
  storeListener('addCellListener', 'tableId, rowId, cellId', 'mutator'),
742
778
  getListenerDoc('a Cell', 'a Row'),
743
779
  );
744
780
  addMethod(
745
781
  'addInvalidCellListener',
746
- `tableId: IdOrNull, rowId: IdOrNull, cellId: IdOrNull, listener: ${invalidCellListenerType}, mutator?: boolean`,
782
+ `tableId: IdOrNull, rowId: IdOrNull, cellId: IdOrNull, ${LISTENER}: ${invalidCellListenerType}, mutator?: boolean`,
747
783
  'Id',
748
784
  storeListener('addCellListener', 'tableId, rowId, cellId', 'mutator'),
749
785
  `${REGISTERS_A_LISTENER} whenever an invalid Cell change was attempted`,
750
786
  );
751
787
  addMethod(
752
788
  'addWillFinishTransactionListener',
753
- `listener: ${transactionListenerType}`,
789
+ `${LISTENER}: ${transactionListenerType}`,
754
790
  'Id',
755
791
  storeListener('addWillFinishTransactionListener'),
756
- `${REGISTERS_A_LISTENER} just before the end of the transaction`,
792
+ `${REGISTERS_A_LISTENER} just before ${THE_END_OF_THE_TRANSACTION}`,
757
793
  );
758
794
  addMethod(
759
795
  'addDidFinishTransactionListener',
760
- `listener: ${transactionListenerType}`,
796
+ `${LISTENER}: ${transactionListenerType}`,
761
797
  'Id',
762
798
  storeListener('addDidFinishTransactionListener'),
763
- `${REGISTERS_A_LISTENER} just after the end of the transaction`,
799
+ `${REGISTERS_A_LISTENER} just after ${THE_END_OF_THE_TRANSACTION}`,
764
800
  );
765
801
  addMethod(
766
802
  'callListener',
767
- 'listenerId: Id',
803
+ `${LISTENER}Id: Id`,
768
804
  storeType,
769
- fluentStoreMethod('callListener', 'listenerId'),
770
- 'Manually provoke a listener to be called',
805
+ fluentStoreMethod('callListener', `${LISTENER}Id`),
806
+ `Manually provoke a ${LISTENER} to be called`,
771
807
  );
772
808
  addMethod(
773
809
  'delListener',
774
- 'listenerId: Id',
810
+ `${LISTENER}Id: Id`,
775
811
  storeType,
776
- fluentStoreMethod('delListener', 'listenerId'),
777
- `Remove a listener that was previously added to ${THE_STORE}`,
812
+ fluentStoreMethod('delListener', `${LISTENER}Id`),
813
+ `Remove a ${LISTENER} that was previously added to ${THE_STORE}`,
778
814
  );
779
815
  addMethod(
780
816
  'getStore',
781
- '',
817
+ EMPTY_STRING,
782
818
  'Store',
783
819
  'store',
784
- 'Gets the underlying Store object',
785
- );
786
- updateType(
787
- tablesType,
788
- `{${join(tablesTypes, ' ')}}`,
789
- `${REPRESENTS} ${THE_CONTENT_OF_THE_STORE}`,
790
- );
791
- updateType(
792
- tableIdType,
793
- join(tableIdTypes, ' | '),
794
- `A Table Id in ${THE_STORE}`,
795
- );
796
- updateType(
797
- tableCallbackType,
798
- `(...[tableId, rowCallback]: ${join(
799
- tableCallbackArgTypes,
800
- ' | ',
801
- )}) => void`,
802
- getCallbackDoc('a Table Id, and a Row iterator'),
803
- );
804
- updateType(
805
- getCellChangeType,
806
- `(...[tableId, rowId, cellId]: ${join(
807
- getCellChangeArgTypes,
808
- ' | ',
809
- )}) => CellChange`,
810
- `A function that returns information about any Cell's changes during a transaction`,
811
- );
812
- updateType(
813
- cellListenerType,
814
- `(...[${storeInstance}, tableId, rowId, cellId, newCell, oldCell, getCellChange]: ${join(
815
- cellListenerArgTypes,
816
- ' | ',
817
- )}) => void`,
818
- `A function for listening to changes to a Cell in ${THE_STORE}`,
820
+ `${verbs[0]} the underlying Store object`,
819
821
  );
820
822
  mapForEach(mapCellTypes, (type, mapCellType) =>
821
823
  addType(
822
824
  mapCellType,
823
- `(cell: ${type} | undefined) => ${type}`,
825
+ `(cell: ${type}${OR_UNDEFINED}) => ${type}`,
824
826
  `Takes a ${type} Cell value and returns another`,
825
827
  ),
826
828
  );
827
- addImport(1, `./${moduleName}.d`, ...collValues(mapCellTypes));
828
- addConstant('store', ['createStore().setSchema({', ...schemaLines, '})']);
829
+ addImport(0, 'tinybase', 'CellChange', ...COMMON_IMPORTS);
830
+ addImport(1, 'tinybase', 'createStore', ...COMMON_IMPORTS);
831
+ addImport(
832
+ 1,
833
+ moduleDefinition,
834
+ storeType,
835
+ `create${storeType} as create${storeType}Decl`,
836
+ tablesType,
837
+ tableIdType,
838
+ tableCallbackType,
839
+ tablesListenerType,
840
+ tableIdsListenerType,
841
+ tableListenerType,
842
+ rowIdsListenerType,
843
+ rowListenerType,
844
+ cellIdsListenerType,
845
+ cellListenerType,
846
+ invalidCellListenerType,
847
+ transactionListenerType,
848
+ ...collValues(mapCellTypes),
849
+ );
850
+ addConstant('store', [
851
+ 'createStore().setSchema({',
852
+ flat(
853
+ mapTableSchema((tableId, _, __, TABLE_ID) => [
854
+ `[${TABLE_ID}]: {`,
855
+ ...mapCellSchema(
856
+ tableId,
857
+ (_2, type, defaultValue, CELL_ID) =>
858
+ `[${CELL_ID}]: {[${addConstant(
859
+ snake(TYPE),
860
+ `'${TYPE}'`,
861
+ )}]: ${addConstant(snake(type), `'${type}'`)}${
862
+ isUndefined(defaultValue)
863
+ ? EMPTY_STRING
864
+ : `, [${addConstant(snake(DEFAULT), `'${DEFAULT}'`)}]: ${
865
+ isString(defaultValue)
866
+ ? addConstant(snake(defaultValue), `'${defaultValue}'`)
867
+ : defaultValue
868
+ }`
869
+ }},`,
870
+ ),
871
+ `},`,
872
+ ]),
873
+ ),
874
+ '})',
875
+ ]);
876
+ addFunction('fluent', 'actions: () => Store', [
877
+ 'actions();',
878
+ `return ${storeInstance};`,
879
+ ]);
880
+ addFunction(
881
+ 'proxy',
882
+ `${LISTENER}: any`,
883
+ `(_: Store, ...args: any[]) => ${LISTENER}(${storeInstance}, ...args)`,
884
+ );
829
885
  addConstant(storeInstance, ['{', ...getMethods(1), '}']);
830
886
  return [
831
887
  build(
832
888
  ...getImports(0),
833
889
  ...getTypes(),
834
- `export interface ${storeType} {`,
890
+ `${EXPORT} interface ${storeType} {`,
835
891
  ...getMethods(0),
836
892
  `}`,
837
- '',
893
+ EMPTY_STRING,
838
894
  comment(`Creates a ${storeType} object`),
839
- `export function create${storeType}(): ${storeType};`,
895
+ `${EXPORT} function create${storeType}(): ${storeType};`,
840
896
  ),
841
897
  build(
842
898
  ...getImports(1),
843
- `export const create${storeType}: typeof create${storeType}Decl = () => {`,
899
+ `${EXPORT} const create${storeType}: typeof create${storeType}Decl = () => {`,
844
900
  ...getConstants(),
845
901
  `return Object.freeze(${storeInstance});`,
846
902
  `};`,
@@ -934,14 +990,15 @@ const createTools = getCreateFunction((store) => {
934
990
  };
935
991
  const getStoreApi$1 = (module) => getStoreApi(getStoreSchema(), module);
936
992
  const getPrettyStoreApi = async (module) => {
937
- const files = getStoreApi$1(module);
993
+ let format;
938
994
  try {
939
- const {format} = await import('prettier');
940
- return arrayMap(files, (file) =>
941
- formatJsDoc(format(file, prettierConfig)),
942
- );
943
- } catch {}
944
- return files;
995
+ format = (await import('prettier')).format;
996
+ } catch {
997
+ format = (str) => str;
998
+ }
999
+ return arrayMap(getStoreApi$1(module), (file) =>
1000
+ formatJsDoc(format(file, prettierConfig)),
1001
+ );
945
1002
  };
946
1003
  const tools = {
947
1004
  getStoreStats,