tinybase 3.0.2 → 3.1.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 (42) hide show
  1. package/lib/cjs/tools.cjs +1 -1
  2. package/lib/cjs/tools.cjs.gz +0 -0
  3. package/lib/cjs/tools.d.ts +59 -35
  4. package/lib/cjs/ui-react.cjs +1 -1
  5. package/lib/cjs/ui-react.cjs.gz +0 -0
  6. package/lib/cjs-es6/tools.cjs +1 -1
  7. package/lib/cjs-es6/tools.cjs.gz +0 -0
  8. package/lib/cjs-es6/tools.d.ts +59 -35
  9. package/lib/cjs-es6/ui-react.cjs +1 -1
  10. package/lib/cjs-es6/ui-react.cjs.gz +0 -0
  11. package/lib/debug/queries.js +4 -3
  12. package/lib/debug/store.js +7 -6
  13. package/lib/debug/tinybase.js +8 -7
  14. package/lib/debug/tools.d.ts +59 -35
  15. package/lib/debug/tools.js +1637 -727
  16. package/lib/debug/ui-react.js +30 -28
  17. package/lib/es6/tools.d.ts +59 -35
  18. package/lib/es6/tools.js +1 -1
  19. package/lib/es6/tools.js.gz +0 -0
  20. package/lib/es6/ui-react.js +1 -1
  21. package/lib/es6/ui-react.js.gz +0 -0
  22. package/lib/tools.d.ts +59 -35
  23. package/lib/tools.js +1 -1
  24. package/lib/tools.js.gz +0 -0
  25. package/lib/ui-react.js +1 -1
  26. package/lib/ui-react.js.gz +0 -0
  27. package/lib/umd/tinybase.js +1 -1
  28. package/lib/umd/tinybase.js.gz +0 -0
  29. package/lib/umd/tools.d.ts +59 -35
  30. package/lib/umd/tools.js +1 -1
  31. package/lib/umd/tools.js.gz +0 -0
  32. package/lib/umd/ui-react.js +1 -1
  33. package/lib/umd/ui-react.js.gz +0 -0
  34. package/lib/umd-es6/tinybase.js +1 -1
  35. package/lib/umd-es6/tinybase.js.gz +0 -0
  36. package/lib/umd-es6/tools.d.ts +59 -35
  37. package/lib/umd-es6/tools.js +1 -1
  38. package/lib/umd-es6/tools.js.gz +0 -0
  39. package/lib/umd-es6/ui-react.js +1 -1
  40. package/lib/umd-es6/ui-react.js.gz +0 -0
  41. package/package.json +10 -10
  42. package/readme.md +14 -14
@@ -5,6 +5,21 @@ const BOOLEAN = getTypeOf(true);
5
5
  const NUMBER = getTypeOf(0);
6
6
  const TYPE = 'type';
7
7
  const DEFAULT = 'default';
8
+ const LISTENER = 'Listener';
9
+ const GET = 'get';
10
+ const ADD = 'add';
11
+ const IDS = 'Ids';
12
+ const TABLE = 'Table';
13
+ const TABLES = TABLE + 's';
14
+ const TABLE_IDS = TABLE + IDS;
15
+ const ROW = 'Row';
16
+ const ROW_IDS = ROW + IDS;
17
+ const SORTED_ROW_IDS = 'Sorted' + ROW + IDS;
18
+ const CELL = 'Cell';
19
+ const CELL_IDS = CELL + IDS;
20
+ const VALUE = 'Value';
21
+ const VALUES = VALUE + 's';
22
+ const VALUE_IDS = VALUE + IDS;
8
23
 
9
24
  const arrayEvery = (array, cb) => array.every(cb);
10
25
  const arraySort = (array, sorter) => array.sort(sorter);
@@ -12,6 +27,7 @@ const arrayForEach = (array, cb) => array.forEach(cb);
12
27
  const arrayMap = (array, cb) => array.map(cb);
13
28
  const arrayLength = (array) => array.length;
14
29
  const arrayIsEmpty = (array) => arrayLength(array) == 0;
30
+ const arrayFilter = (array, cb) => array.filter(cb);
15
31
  const arrayPush = (array, ...values) => array.push(...values);
16
32
  const arrayPop = (array) => array.pop();
17
33
  const arrayUnshift = (array, ...values) => array.unshift(...values);
@@ -52,6 +68,110 @@ const mapEnsure = (map, key, getDefaultValue) => {
52
68
  return mapGet(map, key);
53
69
  };
54
70
 
71
+ const upper = (str) => str.toUpperCase();
72
+ const lower = (str) => str.toLowerCase();
73
+ const A = 'a ';
74
+ const A_FUNCTION_FOR = 'A function for';
75
+ const CALLBACK = 'Callback';
76
+ const DEL = 'Del';
77
+ const EXPORT = 'export';
78
+ const ID = 'Id';
79
+ const INVALID = 'Invalid';
80
+ const JSON$1 = 'Json';
81
+ const LISTENER_ = lower(LISTENER);
82
+ const OR_UNDEFINED = ' | undefined';
83
+ const PARTIAL = 'Partial';
84
+ const REGISTERS_A_LISTENER = `Registers a ${LISTENER_} that will be called`;
85
+ const REPRESENTS = 'Represents';
86
+ const SET = 'Set';
87
+ const SQUARE_BRACKETS = '[]';
88
+ const THE_STORE = 'the Store';
89
+ const TRANSACTION = 'Transaction';
90
+ const TRANSACTION_ = lower(TRANSACTION);
91
+ const THE_END_OF_THE_TRANSACTION = 'the end of the ' + TRANSACTION_;
92
+ const VOID = 'void';
93
+ const RETURNS_VOID = ' => ' + VOID;
94
+ const A_STRING_SERIALIZATION_OF = A + 'string serialization of';
95
+ const SPACE = ' ';
96
+ const GETS_A_CALLBACK_THAT_CAN = 'Gets a callback that can ';
97
+ const THE = 'the ';
98
+ const getTheContentOfDoc = (content = 0, theStore = 0) =>
99
+ `the ${CONTENT[content]}content of` +
100
+ (theStore ? SPACE + THE_STORE : EMPTY_STRING);
101
+ const getTheContentOfTheStoreDoc = (content = 0, verb, set = 0) =>
102
+ VERBS[verb] +
103
+ SPACE +
104
+ getTheContentOfDoc(content, 1) +
105
+ (set ? ' when set' : EMPTY_STRING);
106
+ const getRowTypeDoc = (tableId, set = 0) =>
107
+ REPRESENTS +
108
+ ` a Row when ${
109
+ set ? 's' : 'g'
110
+ }etting ${getTheContentOfDoc()} the '${tableId}' ` +
111
+ TABLE;
112
+ const getIdsDoc = (idsNoun, parentNoun, sorted = 0) =>
113
+ `Gets ${sorted ? 'sorted, paginated' : 'the'} Ids of the ${idsNoun}s in ` +
114
+ parentNoun;
115
+ const getForEachDoc = (childNoun, parentNoun) =>
116
+ `Calls a function for each ${childNoun} in ` + parentNoun;
117
+ const getCallbackDoc = (takes) => 'A function that takes ' + takes;
118
+ const getListenerTypeDoc = (childNoun, parentNoun = 0) =>
119
+ A_FUNCTION_FOR +
120
+ ' listening to changes to ' +
121
+ NOUNS[childNoun] +
122
+ ' in ' +
123
+ NOUNS[parentNoun];
124
+ const getListenerDoc = (childNoun, parentNoun, pluralChild = 0) =>
125
+ REGISTERS_A_LISTENER +
126
+ ' whenever ' +
127
+ NOUNS[childNoun] +
128
+ ' in ' +
129
+ NOUNS[parentNoun] +
130
+ ' change' +
131
+ (pluralChild ? EMPTY_STRING : 's');
132
+ const getTableDoc = (tableId) => `the '${tableId}' ` + TABLE;
133
+ const getRowDoc = (tableId) => 'the specified Row in ' + getTableDoc(tableId);
134
+ const getTableContentDoc = (tableId, verb = 0) =>
135
+ VERBS[verb] + ` ${getTheContentOfDoc()} ` + getTableDoc(tableId);
136
+ const getRowContentDoc = (tableId, verb = 0) =>
137
+ VERBS[verb] + ` ${getTheContentOfDoc()} ` + getRowDoc(tableId);
138
+ const getCellContentDoc = (tableId, cellId, verb = 0) =>
139
+ VERBS[verb] + ` the '${cellId}' Cell for ` + getRowDoc(tableId);
140
+ const getValueContentDoc = (valueId, verb = 0) =>
141
+ VERBS[verb] + ` the '${valueId}' Value`;
142
+ const VERBS = [
143
+ 'Gets',
144
+ 'Checks existence of',
145
+ 'Sets',
146
+ 'Deletes',
147
+ 'Sets part of',
148
+ REPRESENTS,
149
+ 'Gets ' + A_STRING_SERIALIZATION_OF,
150
+ 'Sets ' + A_STRING_SERIALIZATION_OF,
151
+ REGISTERS_A_LISTENER + ' whenever',
152
+ GETS_A_CALLBACK_THAT_CAN + 'set',
153
+ GETS_A_CALLBACK_THAT_CAN + 'add',
154
+ GETS_A_CALLBACK_THAT_CAN + 'set part of',
155
+ GETS_A_CALLBACK_THAT_CAN + 'delete',
156
+ ];
157
+ const NOUNS = [
158
+ THE_STORE,
159
+ TABLES,
160
+ THE + TABLE + SPACE + IDS,
161
+ A + TABLE,
162
+ THE + ROW + SPACE + IDS,
163
+ A + ROW,
164
+ THE + CELL + SPACE + IDS,
165
+ A + CELL,
166
+ 'invalid Cell changes',
167
+ VALUES,
168
+ THE + VALUE + SPACE + IDS,
169
+ A + VALUE,
170
+ 'invalid Value changes',
171
+ THE + 'sorted ' + ROW + SPACE + IDS,
172
+ ];
173
+ const CONTENT = [EMPTY_STRING, 'tabular ', 'keyed value '];
174
+
55
175
  const setNew = (entryOrEntries) =>
56
176
  new Set(
57
177
  isArray(entryOrEntries) || isUndefined(entryOrEntries)
@@ -60,15 +180,11 @@ const setNew = (entryOrEntries) =>
60
180
  );
61
181
  const setAdd = (set, value) => set?.add(value);
62
182
 
63
- const pairNew = (value) => [value, value];
64
- const pairNewMap = () => [mapNew(), mapNew()];
65
-
66
183
  const NON_ALPHA = /[^A-Za-z]+/;
67
184
  const NON_ALPHANUMERIC = /[^A-Za-z0-9]+/;
68
185
  const JSDOC = /^( *)\/\*\* *(.*?) *\*\/$/gm;
69
186
  const substr = (str, start, end) => str.substring(start, end);
70
- const upper = (str) => str.toUpperCase();
71
- const lower = (str) => str.toLowerCase();
187
+ const stringHasComma = (str) => str.includes(',');
72
188
  const mapUnique = (map, id, value, index = 1) => {
73
189
  const uniqueId = `${id}${index == 1 ? '' : index}`;
74
190
  if (collHas(map, uniqueId)) {
@@ -110,20 +226,26 @@ const snake = (str) =>
110
226
  ),
111
227
  );
112
228
  const comment = (doc) => `/** ${doc}. */`;
229
+ const getParameterList = (...params) =>
230
+ join(
231
+ arrayFilter(params, (param) => param),
232
+ ', ',
233
+ );
113
234
  const getCodeFunctions = () => {
114
- const allImports = pairNewMap();
235
+ const allImports = [mapNew(), mapNew(), mapNew(), mapNew()];
115
236
  const types = mapNew();
116
- const methods = mapNew();
117
237
  const constants = mapNew();
118
238
  const build = (...lines) => join(flat(lines), '\n');
119
239
  const addImport = (location, source, ...items) =>
120
240
  arrayForEach(items, (item) =>
121
- setAdd(mapEnsure(allImports[location], source, setNew), item),
241
+ arrayForEach([0, 1], (eachLocation) =>
242
+ (location ?? eachLocation) == eachLocation
243
+ ? setAdd(mapEnsure(allImports[eachLocation], source, setNew), item)
244
+ : 0,
245
+ ),
122
246
  );
123
247
  const addType = (name, body, doc) => mapUnique(types, name, [body, doc]);
124
- const addMethod = (name, parameters, returnType, body, doc, generic = '') =>
125
- mapUnique(methods, name, [parameters, returnType, body, doc, generic]);
126
- const addFunction = (name, parameters, body) =>
248
+ const addInternalFunction = (name, parameters, body) =>
127
249
  mapUnique(
128
250
  constants,
129
251
  name,
@@ -133,36 +255,38 @@ const getCodeFunctions = () => {
133
255
  );
134
256
  const addConstant = (name, body) =>
135
257
  mapGet(constants, name) === body ? name : mapUnique(constants, name, body);
136
- const getImports = (location) => [
137
- ...arraySort(
138
- mapMap(
139
- allImports[location],
140
- (items, source) =>
141
- `import {${join(
142
- arraySort(collValues(items)),
143
- ', ',
144
- )}} from '${source}';`,
145
- ),
146
- ),
147
- EMPTY_STRING,
148
- ];
258
+ const getImports = (location = 0) =>
259
+ arrayMap(
260
+ [
261
+ ...arraySort(
262
+ mapMap(
263
+ allImports[location],
264
+ (items, source) =>
265
+ `import {${join(
266
+ arraySort(collValues(items)),
267
+ ', ',
268
+ )}} from '${source}';`,
269
+ ),
270
+ (import1, import2) =>
271
+ stringHasComma(import1) != stringHasComma(import2)
272
+ ? stringHasComma(import1)
273
+ ? -1
274
+ : 1
275
+ : import1 > import2
276
+ ? 1
277
+ : -1,
278
+ ),
279
+ EMPTY_STRING,
280
+ ],
281
+ (line) => line.replace('{React}', 'React'),
282
+ // sigh
283
+ );
149
284
  const getTypes = () =>
150
285
  mapMap(types, ([body, doc], name) => [
151
286
  comment(doc),
152
- `export type ${name} = ${body};`,
287
+ `${EXPORT} type ${name} = ${body};`,
153
288
  EMPTY_STRING,
154
289
  ]);
155
- const getMethods = (location) =>
156
- mapMap(methods, ([parameters, returnType, body, doc, generic], name) => {
157
- const lines = location
158
- ? [`${name}: ${generic}(${parameters}): ${returnType} => ${body},`]
159
- : [`${name}${generic}(${parameters}): ${returnType};`];
160
- if (!location) {
161
- arrayUnshift(lines, comment(doc));
162
- }
163
- arrayPush(lines, EMPTY_STRING);
164
- return lines;
165
- });
166
290
  const getConstants = () =>
167
291
  mapMap(constants, (body, name) => {
168
292
  body = isArray(body) ? body : [body];
@@ -173,12 +297,10 @@ const getCodeFunctions = () => {
173
297
  build,
174
298
  addImport,
175
299
  addType,
176
- addMethod,
177
- addFunction,
300
+ addInternalFunction,
178
301
  addConstant,
179
302
  getImports,
180
303
  getTypes,
181
- getMethods,
182
304
  getConstants,
183
305
  ];
184
306
  };
@@ -202,182 +324,11 @@ const getCreateFunction = (getFunction) => {
202
324
  };
203
325
  };
204
326
 
205
- const THE_STORE = 'the Store';
206
- const A_FUNCTION_FOR = 'A function for';
207
- const EXPORT = 'export';
208
- const LISTENER = 'listener';
209
- const OR_UNDEFINED = ' | undefined';
210
- const REGISTERS_A_LISTENER = `Registers a ${LISTENER} that will be called`;
211
- const REPRESENTS = 'Represents';
212
- const RETURNS_VOID = ' => void';
213
- const THE_END_OF_THE_TRANSACTION = 'the end of the transaction';
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' : ''}`;
220
- const getRowTypeDoc = (tableId, set = 0) =>
221
- `${REPRESENTS} a Row when ${
222
- set ? 's' : 'g'
223
- }etting ${getTheContentOfDoc()} the '${tableId}' Table`;
224
- const getIdsDoc = (idsNoun, parentNoun, sorted = 0) =>
225
- `Gets ${
226
- sorted ? 'sorted, paginated' : 'the'
227
- } Ids of the ${idsNoun}s in ${parentNoun}`;
228
- const getForEachDoc = (childNoun, parentNoun) =>
229
- `Calls a function for each ${childNoun} in ${parentNoun}`;
230
- const getHasDoc = (childNoun, parentNoun = THE_STORE) =>
231
- `Gets whether ${childNoun} exists in ${parentNoun}`;
232
- const getCallbackDoc = (takes) => `A function that takes ${takes}`;
233
- const getListenerTypeDoc = (childNoun, parentNoun = 0) =>
234
- `${A_FUNCTION_FOR} listening to changes to ${NOUNS[childNoun]} in ${NOUNS[parentNoun]}`;
235
- const getListenerDoc = (childNoun, parentNoun, pluralChild = 0) =>
236
- `${REGISTERS_A_LISTENER} whenever ${childNoun} in ${parentNoun} change` +
237
- (pluralChild ? EMPTY_STRING : 's');
238
- const getTableDoc = (tableId) => `the '${tableId}' Table`;
239
- const getRowDoc = (tableId) =>
240
- `${THE_SPECIFIED_ROW} in ${getTableDoc(tableId)}`;
241
- const getCellDoc = (cellId) => `the '${cellId}' Cell`;
242
- const getValueDoc = (valueId) => `the '${valueId}' Value`;
243
- const getTableContentDoc = (tableId, verb = 0) =>
244
- `${VERBS[verb]} ${getTheContentOfDoc()} ${getTableDoc(tableId)}`;
245
- const getRowContentDoc = (tableId, verb = 0) =>
246
- `${VERBS[verb]} ${getTheContentOfDoc()} ${getRowDoc(tableId)}`;
247
- const getCellContentDoc = (tableId, cellId, verb = 0) =>
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 = [
262
- THE_STORE,
263
- 'Tables',
264
- 'Table Ids',
265
- 'a Table',
266
- 'Row Ids',
267
- 'a Row',
268
- 'Cell Ids',
269
- 'a Cell',
270
- 'invalid Cell changes',
271
- 'Values',
272
- 'Value Ids',
273
- 'a Value',
274
- 'invalid Value changes',
275
- ];
276
- const CONTENT = ['', 'tabular ', 'keyed value '];
277
-
278
- const COMMON_IMPORTS = ['DoRollback', 'Id', 'IdOrNull', 'Ids', 'Json', 'Store'];
279
- const storeMethod = (method, parameters = EMPTY_STRING, cast = EMPTY_STRING) =>
280
- `store.${method}(${parameters})${cast ? ` as ${cast}` : EMPTY_STRING}`;
281
- const fluentStoreMethod = (method, parameters = EMPTY_STRING) =>
282
- `fluent(() => ${storeMethod(method, parameters)})`;
283
- const storeListener = (
284
- method,
285
- beforeParameters = EMPTY_STRING,
286
- afterParameters = EMPTY_STRING,
287
- ) =>
288
- `store.${method}(${
289
- beforeParameters ? `${beforeParameters}, ` : EMPTY_STRING
290
- }proxy(${LISTENER})${
291
- afterParameters ? `, ${afterParameters}` : EMPTY_STRING
292
- })`;
293
- const getStoreApi = (tablesSchema, valuesSchema, module) => {
294
- if (objIsEmpty(tablesSchema) && objIsEmpty(valuesSchema)) {
295
- return pairNew(EMPTY_STRING);
296
- }
297
- const [
298
- build,
299
- addImport,
300
- addType,
301
- addMethod,
302
- addFunction,
303
- addConstant,
304
- getImports,
305
- getTypes,
306
- getMethods,
307
- getConstants,
308
- ] = getCodeFunctions();
309
- const moduleDefinition = `./${camel(module)}.d`;
310
- const storeType = camel(module, 1);
311
- const storeInstance = camel(storeType);
312
- const createSteps = [];
313
- const tableTypes = mapNew();
327
+ const getSchemaFunctions = (tablesSchema, valuesSchema, addConstant) => {
314
328
  const mapTablesSchema = (callback) =>
315
329
  objMap(tablesSchema, (_, tableId) => {
316
330
  return callback(
317
331
  tableId,
318
- mapEnsure(tableTypes, tableId, () => {
319
- const table = camel(tableId, 1);
320
- return [
321
- addType(
322
- `${table}Table`,
323
- `{[rowId: Id]: ${table}Row}`,
324
- `${REPRESENTS} the '${tableId}' Table`,
325
- ),
326
- addType(
327
- `${table}Row`,
328
- `{${join(
329
- mapCellSchema(
330
- tableId,
331
- (cellId, type, defaultValue) =>
332
- `'${cellId}'${
333
- isUndefined(defaultValue) ? '?' : EMPTY_STRING
334
- }: ${type};`,
335
- ),
336
- ' ',
337
- )}}`,
338
- getRowTypeDoc(tableId),
339
- ),
340
- addType(
341
- `${table}RowWhenSet`,
342
- `{${join(
343
- mapCellSchema(
344
- tableId,
345
- (cellId, type) => `'${cellId}'?: ${type};`,
346
- ),
347
- ' ',
348
- )}}`,
349
- getRowTypeDoc(tableId, 1),
350
- ),
351
- addType(
352
- `${table}CellId`,
353
- join(
354
- mapCellSchema(tableId, (cellId) => `'${cellId}'`),
355
- ' | ',
356
- ),
357
- `A Cell Id for the '${tableId}' Table`,
358
- ),
359
- addType(
360
- `${table}CellCallback`,
361
- `(...[cellId, cell]: ${join(
362
- mapCellSchema(
363
- tableId,
364
- (cellId, type) => `[cellId: '${cellId}', cell: ${type}]`,
365
- ),
366
- ' | ',
367
- )})${RETURNS_VOID}`,
368
- getCallbackDoc(
369
- `a Cell Id and value from a Row in the '${tableId}' Table`,
370
- ),
371
- ),
372
- addType(
373
- `${table}RowCallback`,
374
- `(rowId: Id, forEachCell: (cellCallback: ${table}CellCallback)${RETURNS_VOID})${RETURNS_VOID}`,
375
- getCallbackDoc(
376
- `a Row Id from the '${tableId}' Table, and a Cell iterator`,
377
- ),
378
- ),
379
- ];
380
- }),
381
332
  camel(tableId, 1),
382
333
  addConstant(snake(tableId), `'${tableId}'`),
383
334
  );
@@ -402,6 +353,124 @@ const getStoreApi = (tablesSchema, valuesSchema, module) => {
402
353
  camel(valueId, 1),
403
354
  ),
404
355
  );
356
+ return [mapTablesSchema, mapCellSchema, mapValuesSchema];
357
+ };
358
+
359
+ const METHOD_PREFIX_VERBS = [
360
+ GET,
361
+ 'has',
362
+ 'set',
363
+ 'del',
364
+ 'set',
365
+ 'forEach',
366
+ ADD,
367
+ EMPTY_STRING,
368
+ ];
369
+ const storeMethod = (method, parameters = EMPTY_STRING, cast = EMPTY_STRING) =>
370
+ `store.${method}(${parameters})` + (cast ? ' as ' + cast : EMPTY_STRING);
371
+ const fluentStoreMethod = (method, parameters = EMPTY_STRING) =>
372
+ `fluent(() => ${storeMethod(method, parameters)})`;
373
+ const storeListener = (
374
+ method,
375
+ beforeParameters = EMPTY_STRING,
376
+ afterParameters = EMPTY_STRING,
377
+ ) =>
378
+ `store.${method}(${
379
+ beforeParameters ? beforeParameters + ', ' : EMPTY_STRING
380
+ }proxy(listener)${afterParameters ? ', ' + afterParameters : EMPTY_STRING})`;
381
+ const getStoreCoreApi = (tablesSchema, valuesSchema, module) => {
382
+ const [
383
+ build,
384
+ addImport,
385
+ addType,
386
+ addInternalFunction,
387
+ addConstant,
388
+ getImports,
389
+ getTypes,
390
+ getConstants,
391
+ ] = getCodeFunctions();
392
+ const [mapTablesSchema, mapCellSchema, mapValuesSchema] = getSchemaFunctions(
393
+ tablesSchema,
394
+ valuesSchema,
395
+ addConstant,
396
+ );
397
+ const methods = mapNew();
398
+ const getMethods = (location = 0) =>
399
+ mapMap(methods, ([parameters, returnType, body, doc, generic], name) => {
400
+ const lines = location
401
+ ? [name + `: ${generic}(${parameters}): ${returnType} => ${body},`]
402
+ : [name + generic + `(${parameters}): ${returnType};`];
403
+ if (!location) {
404
+ arrayUnshift(lines, comment(doc));
405
+ }
406
+ arrayPush(lines, EMPTY_STRING);
407
+ return lines;
408
+ });
409
+ const addMethod = (
410
+ name,
411
+ parameters,
412
+ returnType,
413
+ body,
414
+ doc,
415
+ generic = EMPTY_STRING,
416
+ ) => mapUnique(methods, name, [parameters, returnType, body, doc, generic]);
417
+ const addProxyMethod = (
418
+ prefixVerb,
419
+ prefix,
420
+ underlyingName,
421
+ returnType,
422
+ doc,
423
+ params = EMPTY_STRING,
424
+ paramsInCall = EMPTY_STRING,
425
+ generic = EMPTY_STRING,
426
+ ) =>
427
+ addMethod(
428
+ METHOD_PREFIX_VERBS[prefixVerb] +
429
+ prefix +
430
+ (prefixVerb == 4 ? PARTIAL : EMPTY_STRING) +
431
+ underlyingName,
432
+ params,
433
+ returnType,
434
+ (returnType == storeType ? fluentStoreMethod : storeMethod)(
435
+ METHOD_PREFIX_VERBS[prefixVerb] +
436
+ (prefixVerb == 4 ? PARTIAL : EMPTY_STRING) +
437
+ underlyingName,
438
+ paramsInCall,
439
+ prefixVerb ? void 0 : returnType,
440
+ ),
441
+ doc,
442
+ generic,
443
+ );
444
+ const addProxyListener = (
445
+ underlyingName,
446
+ listenerType,
447
+ doc,
448
+ params = EMPTY_STRING,
449
+ paramsInCall = EMPTY_STRING,
450
+ mutator = 1,
451
+ ) =>
452
+ addMethod(
453
+ ADD + underlyingName + LISTENER,
454
+ (params ? params + ', ' : EMPTY_STRING) +
455
+ LISTENER_ +
456
+ ': ' +
457
+ listenerType +
458
+ (mutator ? ', mutator?: boolean' : EMPTY_STRING),
459
+ ID,
460
+ storeListener(
461
+ ADD + underlyingName + LISTENER,
462
+ paramsInCall,
463
+ mutator ? 'mutator' : EMPTY_STRING,
464
+ ),
465
+ doc,
466
+ );
467
+ const moduleDefinition = `./${camel(module)}.d`;
468
+ const storeType = camel(module, 1);
469
+ const storeInstance = camel(storeType);
470
+ const createSteps = [];
471
+ const mapCellOrValueTypes = mapNew();
472
+ let sharedTableTypes = [];
473
+ let sharedValueTypes = [];
405
474
  addImport(
406
475
  1,
407
476
  moduleDefinition,
@@ -409,80 +478,170 @@ const getStoreApi = (tablesSchema, valuesSchema, module) => {
409
478
  `create${storeType} as create${storeType}Decl`,
410
479
  );
411
480
  if (!objIsEmpty(tablesSchema)) {
481
+ const tablesTypes = mapNew();
482
+ mapTablesSchema((tableId, tableName) => {
483
+ const tableTypes = [
484
+ addType(
485
+ tableName + TABLE,
486
+ `{[rowId: Id]: ${tableName}Row}`,
487
+ REPRESENTS + ` the '${tableId}' ` + TABLE,
488
+ ),
489
+ addType(
490
+ tableName + ROW,
491
+ `{${join(
492
+ mapCellSchema(
493
+ tableId,
494
+ (cellId, type, defaultValue) =>
495
+ `'${cellId}'${
496
+ isUndefined(defaultValue) ? '?' : EMPTY_STRING
497
+ }: ${type};`,
498
+ ),
499
+ ' ',
500
+ )}}`,
501
+ getRowTypeDoc(tableId),
502
+ ),
503
+ addType(
504
+ tableName + ROW + 'WhenSet',
505
+ `{${join(
506
+ mapCellSchema(tableId, (cellId, type) => `'${cellId}'?: ${type};`),
507
+ ' ',
508
+ )}}`,
509
+ getRowTypeDoc(tableId, 1),
510
+ ),
511
+ addType(
512
+ tableName + CELL + ID,
513
+ join(
514
+ mapCellSchema(tableId, (cellId) => `'${cellId}'`),
515
+ ' | ',
516
+ ),
517
+ `A Cell Id for the '${tableId}' ` + TABLE,
518
+ ),
519
+ addType(
520
+ tableName + CELL + CALLBACK,
521
+ `(...[cellId, cell]: ${join(
522
+ mapCellSchema(
523
+ tableId,
524
+ (cellId, type) => `[cellId: '${cellId}', cell: ${type}]`,
525
+ ),
526
+ ' | ',
527
+ )})` + RETURNS_VOID,
528
+ getCallbackDoc(
529
+ `a Cell Id and value from a Row in the '${tableId}' ` + TABLE,
530
+ ),
531
+ ),
532
+ addType(
533
+ tableName + ROW + CALLBACK,
534
+ `(rowId: Id, forEachCell: (cellCallback: ${tableName}CellCallback)` +
535
+ RETURNS_VOID +
536
+ ')' +
537
+ RETURNS_VOID,
538
+ getCallbackDoc(
539
+ `a Row Id from the '${tableId}' Table, and a Cell iterator`,
540
+ ),
541
+ ),
542
+ ];
543
+ mapSet(tablesTypes, tableId, tableTypes);
544
+ addImport(1, moduleDefinition, ...tableTypes);
545
+ });
412
546
  const tablesType = addType(
413
- 'Tables',
547
+ TABLES,
414
548
  `{${join(
415
549
  mapTablesSchema(
416
- (tableId, tableTypes2) => `'${tableId}'?: ${tableTypes2[0]};`,
550
+ (tableId) => `'${tableId}'?: ${mapGet(tablesTypes, tableId)?.[0]};`,
417
551
  ),
418
552
  ' ',
419
553
  )}}`,
420
- getTheContentOfTheStoreDoc(4, 1),
554
+ getTheContentOfTheStoreDoc(1, 5),
421
555
  );
422
556
  const tableIdType = addType(
423
- 'TableId',
557
+ TABLE + ID,
424
558
  join(
425
559
  mapTablesSchema((tableId) => `'${tableId}'`),
426
560
  ' | ',
427
561
  ),
428
- `A Table Id in ${THE_STORE}`,
562
+ 'A ' + TABLE + ' Id in ' + THE_STORE,
429
563
  );
430
564
  const tableCallbackType = addType(
431
- 'TableCallback',
565
+ TABLE + CALLBACK,
432
566
  `(...[tableId, rowCallback]: ${join(
433
567
  mapTablesSchema(
434
- (tableId, tableTypes2) =>
435
- `[tableId: '${tableId}', forEachRow: (rowCallback: ${tableTypes2[5]})${RETURNS_VOID}]`,
568
+ (tableId) =>
569
+ `[tableId: '${tableId}', forEachRow: (rowCallback: ${
570
+ mapGet(tablesTypes, tableId)?.[5]
571
+ })${RETURNS_VOID}]`,
436
572
  ),
437
573
  ' | ',
438
- )})${RETURNS_VOID}`,
439
- getCallbackDoc('a Table Id, and a Row iterator'),
574
+ )})` + RETURNS_VOID,
575
+ getCallbackDoc(A + TABLE + ' Id, and a Row iterator'),
440
576
  );
441
577
  const getCellChangeType = addType(
442
578
  'GetCellChange',
443
579
  `(...[tableId, rowId, cellId]: ${join(
444
580
  mapTablesSchema(
445
- (tableId, tableTypes2) =>
446
- `[tableId: '${tableId}', rowId: Id, cellId: ${tableTypes2[3]}]`,
581
+ (tableId) =>
582
+ `[tableId: '${tableId}', rowId: Id, cellId: ${
583
+ mapGet(tablesTypes, tableId)?.[3]
584
+ }]`,
447
585
  ),
448
586
  ' | ',
449
587
  )}) => CellChange`,
450
- `${A_FUNCTION_FOR} returning information about any Cell's changes during a transaction`,
588
+ A_FUNCTION_FOR +
589
+ ` returning information about any Cell's changes during a ` +
590
+ TRANSACTION_,
451
591
  );
452
592
  const tablesListenerType = addType(
453
- 'TablesListener',
454
- `(${storeInstance}: ${storeType}, getCellChange: ${getCellChangeType}${OR_UNDEFINED})${RETURNS_VOID}`,
593
+ TABLES + LISTENER,
594
+ `(${storeInstance}: ${storeType}, getCellChange: ${getCellChangeType}${OR_UNDEFINED})` +
595
+ RETURNS_VOID,
455
596
  getListenerTypeDoc(1),
456
597
  );
457
598
  const tableIdsListenerType = addType(
458
- 'TableIdsListener',
459
- `(${storeInstance}: ${storeType})${RETURNS_VOID}`,
599
+ TABLE_IDS + LISTENER,
600
+ `(${storeInstance}: ${storeType})` + RETURNS_VOID,
460
601
  getListenerTypeDoc(2),
461
602
  );
462
603
  const tableListenerType = addType(
463
- 'TableListener',
464
- `(${storeInstance}: ${storeType}, tableId: ${tableIdType}, getCellChange: ${getCellChangeType}${OR_UNDEFINED})${RETURNS_VOID}`,
604
+ TABLE + LISTENER,
605
+ `(${storeInstance}: ${storeType}, tableId: ${tableIdType}, getCellChange: ${getCellChangeType}${OR_UNDEFINED})` +
606
+ RETURNS_VOID,
465
607
  getListenerTypeDoc(3),
466
608
  );
467
609
  const rowIdsListenerType = addType(
468
- 'RowIdsListener',
610
+ ROW_IDS + LISTENER,
469
611
  `(${storeInstance}: ${storeType}, tableId: ${tableIdType})` +
470
612
  RETURNS_VOID,
471
613
  getListenerTypeDoc(4, 3),
472
614
  );
615
+ const sortedRowIdsListenerType = addType(
616
+ SORTED_ROW_IDS + LISTENER,
617
+ '(' +
618
+ getParameterList(
619
+ storeInstance + ': ' + storeType,
620
+ 'tableId: ' + tableIdType,
621
+ 'cellId: Id' + OR_UNDEFINED,
622
+ 'descending: boolean',
623
+ 'offset: number',
624
+ 'limit: number' + OR_UNDEFINED,
625
+ 'sortedRowIds: Ids',
626
+ ) +
627
+ ')' +
628
+ RETURNS_VOID,
629
+ getListenerTypeDoc(13, 3),
630
+ );
473
631
  const rowListenerType = addType(
474
- 'RowListener',
475
- `(${storeInstance}: ${storeType}, tableId: ${tableIdType}, rowId: Id, getCellChange: ${getCellChangeType}${OR_UNDEFINED})${RETURNS_VOID}`,
632
+ ROW + LISTENER,
633
+ `(${storeInstance}: ${storeType}, tableId: ${tableIdType}, rowId: Id, getCellChange: ${getCellChangeType}${OR_UNDEFINED})` +
634
+ RETURNS_VOID,
476
635
  getListenerTypeDoc(5, 3),
477
636
  );
478
637
  const cellIdsListenerType = addType(
479
- 'CellIdsListener',
638
+ CELL_IDS + LISTENER,
480
639
  `(${storeInstance}: ${storeType}, tableId: ${tableIdType}, rowId: Id)` +
481
640
  RETURNS_VOID,
482
641
  getListenerTypeDoc(6, 5),
483
642
  );
484
643
  const cellListenerType = addType(
485
- 'CellListener',
644
+ CELL + LISTENER,
486
645
  `(...[${storeInstance}, tableId, rowId, cellId, newCell, oldCell, getCellChange]: ${join(
487
646
  flat(
488
647
  mapTablesSchema((tableId) =>
@@ -494,319 +653,279 @@ const getStoreApi = (tablesSchema, valuesSchema, module) => {
494
653
  ),
495
654
  ),
496
655
  ' | ',
497
- )})${RETURNS_VOID}`,
656
+ )})` + RETURNS_VOID,
498
657
  getListenerTypeDoc(7, 5),
499
658
  );
500
659
  const invalidCellListenerType = addType(
501
- 'InvalidCellListener',
502
- `(${storeInstance}: ${storeType}, tableId: Id, rowId: Id, cellId: Id, invalidCells: any[])${RETURNS_VOID}`,
660
+ INVALID + CELL + LISTENER,
661
+ `(${storeInstance}: ${storeType}, tableId: Id, rowId: Id, cellId: Id, invalidCells: any[])` +
662
+ RETURNS_VOID,
503
663
  getListenerTypeDoc(8),
504
664
  );
505
- addMethod(
506
- `hasTables`,
507
- EMPTY_STRING,
508
- BOOLEAN,
509
- storeMethod('hasTables'),
510
- getHasDoc('any Table'),
511
- );
512
- addMethod(
513
- `getTables`,
514
- EMPTY_STRING,
665
+ sharedTableTypes = [
515
666
  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),
667
+ tableIdType,
668
+ tablesListenerType,
669
+ tableIdsListenerType,
670
+ tableListenerType,
671
+ rowIdsListenerType,
672
+ sortedRowIdsListenerType,
673
+ rowListenerType,
674
+ cellIdsListenerType,
675
+ cellListenerType,
676
+ tablesTypes,
677
+ ];
678
+ arrayForEach(
679
+ [
680
+ [tablesType],
681
+ [BOOLEAN],
682
+ [storeType, 'tables: ' + tablesType, 'tables'],
683
+ [storeType],
684
+ ],
685
+ ([returnType, params, paramsInCall], verb) =>
686
+ addProxyMethod(
687
+ verb,
688
+ EMPTY_STRING,
689
+ TABLES,
690
+ returnType,
691
+ getTheContentOfTheStoreDoc(1, verb),
692
+ params,
693
+ paramsInCall,
694
+ ),
525
695
  );
526
- addMethod(
527
- `delTables`,
696
+ addProxyMethod(
697
+ 0,
528
698
  EMPTY_STRING,
529
- storeType,
530
- fluentStoreMethod('delTables'),
531
- getTheContentOfTheStoreDoc(3, 1),
699
+ TABLE_IDS,
700
+ tableIdType + SQUARE_BRACKETS,
701
+ getIdsDoc(TABLE, THE_STORE),
532
702
  );
533
- addMethod(
534
- `getTableIds`,
703
+ addProxyMethod(
704
+ 5,
535
705
  EMPTY_STRING,
536
- `${tableIdType}[]`,
537
- storeMethod('getTableIds', EMPTY_STRING, `${tableIdType}[]`),
538
- getIdsDoc('Table', THE_STORE),
706
+ TABLE,
707
+ VOID,
708
+ getForEachDoc(TABLE, THE_STORE),
709
+ 'tableCallback: ' + tableCallbackType,
710
+ 'tableCallback as any',
539
711
  );
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,
712
+ mapTablesSchema((tableId, tableName, TABLE_ID) => {
713
+ const [
714
+ tableType,
715
+ rowType,
716
+ rowWhenSetType,
717
+ cellIdType,
718
+ cellCallbackType,
719
+ rowCallbackType,
720
+ ] = mapGet(tablesTypes, tableId);
721
+ arrayForEach(
551
722
  [
552
- tableType,
553
- rowType,
554
- rowWhenSetType,
555
- cellIdType,
556
- cellCallbackType,
557
- rowCallbackType,
723
+ [tableType],
724
+ [BOOLEAN],
725
+ [storeType, 'table: ' + tableType, ', table'],
726
+ [storeType],
558
727
  ],
728
+ ([returnType, params, paramsInCall = EMPTY_STRING], verb) =>
729
+ addProxyMethod(
730
+ verb,
731
+ tableName,
732
+ TABLE,
733
+ returnType,
734
+ getTableContentDoc(tableId, verb),
735
+ params,
736
+ TABLE_ID + paramsInCall,
737
+ ),
738
+ );
739
+ addProxyMethod(
740
+ 0,
559
741
  tableName,
742
+ ROW_IDS,
743
+ IDS,
744
+ getIdsDoc(ROW, getTableDoc(tableId)),
745
+ EMPTY_STRING,
560
746
  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`,
747
+ );
748
+ addProxyMethod(
749
+ 0,
750
+ tableName,
751
+ SORTED_ROW_IDS,
752
+ IDS,
753
+ getIdsDoc(ROW, getTableDoc(tableId), 1),
754
+ 'cellId?: ' +
755
+ cellIdType +
756
+ ', descending?: boolean, offset?: number, limit?: number',
757
+ TABLE_ID + ', cellId, descending, offset, limit',
758
+ );
759
+ addProxyMethod(
760
+ 5,
761
+ tableName,
762
+ ROW,
763
+ VOID,
764
+ getForEachDoc(ROW, getTableDoc(tableId)),
765
+ 'rowCallback: ' + rowCallbackType,
766
+ TABLE_ID + ', rowCallback as any',
767
+ );
768
+ arrayForEach(
769
+ [
770
+ [rowType],
771
+ [BOOLEAN],
772
+ [storeType, ', row: ' + rowWhenSetType, ', row'],
773
+ [storeType],
774
+ [storeType, ', partialRow: ' + rowWhenSetType, ', partialRow'],
775
+ ],
776
+ (
777
+ [returnType, params = EMPTY_STRING, paramsInCall = EMPTY_STRING],
778
+ verb,
779
+ ) =>
780
+ addProxyMethod(
781
+ verb,
782
+ tableName,
783
+ ROW,
784
+ returnType,
785
+ getRowContentDoc(tableId, verb),
786
+ 'rowId: Id' + params,
787
+ TABLE_ID + ', rowId' + paramsInCall,
614
788
  ),
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`,
789
+ );
790
+ addProxyMethod(
791
+ 6,
792
+ tableName,
793
+ ROW,
794
+ ID + OR_UNDEFINED,
795
+ 'Adds a new Row to ' + getTableDoc(tableId),
796
+ 'row: ' + rowWhenSetType,
797
+ TABLE_ID + ', row',
798
+ );
799
+ addProxyMethod(
800
+ 0,
801
+ tableName,
802
+ CELL_IDS,
803
+ cellIdType + SQUARE_BRACKETS,
804
+ getIdsDoc(CELL, getRowDoc(tableId)),
805
+ 'rowId: ' + ID,
806
+ TABLE_ID + ', rowId',
807
+ );
808
+ addProxyMethod(
809
+ 5,
810
+ tableName,
811
+ CELL,
812
+ VOID,
813
+ getForEachDoc(CELL, getRowDoc(tableId)),
814
+ 'rowId: Id, cellCallback: ' + cellCallbackType,
815
+ TABLE_ID + ', rowId, cellCallback as any',
816
+ );
817
+ mapCellSchema(
818
+ tableId,
819
+ (cellId, type, defaultValue, CELL_ID, cellName) => {
820
+ const mapCellType = 'Map' + camel(type, 1);
821
+ mapSet(mapCellOrValueTypes, type, mapCellType);
822
+ const returnCellType =
823
+ type + (isUndefined(defaultValue) ? OR_UNDEFINED : EMPTY_STRING);
824
+ arrayForEach(
825
+ [
826
+ [returnCellType],
827
+ [BOOLEAN],
828
+ [storeType, `, cell: ${type} | ` + mapCellType, ', cell as any'],
829
+ [storeType],
830
+ ],
831
+ (
832
+ [returnType, params = EMPTY_STRING, paramsInCall = EMPTY_STRING],
833
+ verb,
834
+ ) =>
835
+ addProxyMethod(
836
+ verb,
837
+ tableName + cellName,
838
+ CELL,
839
+ returnType,
840
+ getCellContentDoc(tableId, cellId, verb),
841
+ 'rowId: Id' + params,
842
+ TABLE_ID + ', rowId, ' + CELL_ID + paramsInCall,
713
843
  ),
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',
844
+ );
845
+ },
846
+ );
847
+ });
848
+ addProxyMethod(
849
+ 0,
729
850
  EMPTY_STRING,
730
- 'Json',
731
- storeMethod('getTablesJson'),
732
- getTheContentOfTheStoreDoc(5, 1),
851
+ TABLES + JSON$1,
852
+ JSON$1,
853
+ getTheContentOfTheStoreDoc(1, 6),
733
854
  );
734
- addMethod(
735
- 'setTablesJson',
736
- 'tablesJson: Json',
855
+ addProxyMethod(
856
+ 2,
857
+ EMPTY_STRING,
858
+ TABLES + JSON$1,
737
859
  storeType,
738
- fluentStoreMethod('setTablesJson', 'tablesJson'),
739
- getTheContentOfTheStoreDoc(6, 1),
860
+ getTheContentOfTheStoreDoc(1, 7),
861
+ 'tablesJson: ' + JSON$1,
862
+ 'tables' + JSON$1,
740
863
  );
741
- addMethod(
742
- 'addTablesListener',
743
- `${LISTENER}: ${tablesListenerType}, mutator?: boolean`,
744
- 'Id',
745
- storeListener('addTablesListener', EMPTY_STRING, 'mutator'),
746
- getTheContentOfTheStoreDoc(7, 1) + ' changes',
864
+ addProxyListener(
865
+ TABLES,
866
+ tablesListenerType,
867
+ getTheContentOfTheStoreDoc(1, 8) + ' changes',
747
868
  );
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),
869
+ addProxyListener(TABLE_IDS, tableIdsListenerType, getListenerDoc(2, 0, 1));
870
+ addProxyListener(
871
+ TABLE,
872
+ tableListenerType,
873
+ getListenerDoc(3, 0),
874
+ `tableId: ${tableIdType} | null`,
875
+ 'tableId',
754
876
  );
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),
877
+ addProxyListener(
878
+ ROW_IDS,
879
+ rowIdsListenerType,
880
+ getListenerDoc(4, 3, 1),
881
+ `tableId: ${tableIdType} | null`,
882
+ 'tableId',
761
883
  );
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),
884
+ addProxyListener(
885
+ SORTED_ROW_IDS,
886
+ sortedRowIdsListenerType,
887
+ getListenerDoc(13, 3, 1),
888
+ getParameterList(
889
+ 'tableId: ' + tableIdType,
890
+ 'cellId: Id' + OR_UNDEFINED,
891
+ 'descending: boolean',
892
+ 'offset: number',
893
+ 'limit: number' + OR_UNDEFINED,
894
+ ),
895
+ getParameterList('tableId', 'cellId', 'descending', 'offset', 'limit'),
768
896
  );
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'),
897
+ addProxyListener(
898
+ ROW,
899
+ rowListenerType,
900
+ getListenerDoc(5, 3),
901
+ `tableId: ${tableIdType} | null, rowId: IdOrNull`,
902
+ 'tableId, rowId',
775
903
  );
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),
904
+ addProxyListener(
905
+ CELL_IDS,
906
+ cellIdsListenerType,
907
+ getListenerDoc(6, 5, 1),
908
+ `tableId: ${tableIdType} | null, rowId: IdOrNull`,
909
+ 'tableId, rowId',
782
910
  );
783
- addMethod(
784
- 'addCellListener',
911
+ addProxyListener(
912
+ CELL,
913
+ cellListenerType,
914
+ getListenerDoc(7, 5),
785
915
  `tableId: ${tableIdType} | null, rowId: IdOrNull, cellId: ${join(
786
- mapTablesSchema((_, tableTypes2) => tableTypes2[3]),
916
+ mapTablesSchema(
917
+ (tableId) => mapGet(tablesTypes, tableId)?.[3] ?? EMPTY_STRING,
918
+ ),
787
919
  ' | ',
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',
801
- ),
802
- `${REGISTERS_A_LISTENER} whenever an invalid Cell change was attempted`,
920
+ )} | null`,
921
+ 'tableId, rowId, cellId',
803
922
  );
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
- ),
923
+ addProxyListener(
924
+ INVALID + CELL,
925
+ invalidCellListenerType,
926
+ REGISTERS_A_LISTENER + ' whenever an invalid Cell change was attempted',
927
+ `tableId: IdOrNull, rowId: IdOrNull, cellId: IdOrNull`,
928
+ 'tableId, rowId, cellId',
810
929
  );
811
930
  addImport(
812
931
  1,
@@ -818,18 +937,19 @@ const getStoreApi = (tablesSchema, valuesSchema, module) => {
818
937
  tableIdsListenerType,
819
938
  tableListenerType,
820
939
  rowIdsListenerType,
940
+ sortedRowIdsListenerType,
821
941
  rowListenerType,
822
942
  cellIdsListenerType,
823
943
  cellListenerType,
824
944
  invalidCellListenerType,
825
- ...collValues(mapCellTypes),
945
+ ...collValues(mapCellOrValueTypes),
826
946
  );
827
947
  addImport(0, 'tinybase', 'CellChange');
828
948
  arrayPush(
829
949
  createSteps,
830
950
  '.setTablesSchema({',
831
951
  flat(
832
- mapTablesSchema((tableId, _, __, TABLE_ID) => [
952
+ mapTablesSchema((tableId, _, TABLE_ID) => [
833
953
  `[${TABLE_ID}]: {`,
834
954
  ...mapCellSchema(
835
955
  tableId,
@@ -840,11 +960,10 @@ const getStoreApi = (tablesSchema, valuesSchema, module) => {
840
960
  )}]: ${addConstant(snake(type), `'${type}'`)}${
841
961
  isUndefined(defaultValue)
842
962
  ? EMPTY_STRING
843
- : `, [${addConstant(snake(DEFAULT), `'${DEFAULT}'`)}]: ${
844
- isString(defaultValue)
845
- ? addConstant(snake(defaultValue), `'${defaultValue}'`)
846
- : defaultValue
847
- }`
963
+ : `, [${addConstant(snake(DEFAULT), `'${DEFAULT}'`)}]: ` +
964
+ (isString(defaultValue)
965
+ ? addConstant(snake(defaultValue), `'${defaultValue}'`)
966
+ : defaultValue)
848
967
  }},`,
849
968
  ),
850
969
  `},`,
@@ -855,196 +974,183 @@ const getStoreApi = (tablesSchema, valuesSchema, module) => {
855
974
  }
856
975
  if (!objIsEmpty(valuesSchema)) {
857
976
  const valuesType = addType(
858
- 'Values',
859
- `{${join(
860
- mapValuesSchema(
861
- (valueId, type, defaultValue) =>
862
- `'${valueId}'${
863
- isUndefined(defaultValue) ? '?' : EMPTY_STRING
864
- }: ${type};`,
865
- ),
866
- ' ',
867
- )}}`,
868
- getTheContentOfTheStoreDoc(4, 2),
977
+ VALUES,
978
+ '{' +
979
+ join(
980
+ mapValuesSchema(
981
+ (valueId, type, defaultValue) =>
982
+ `'${valueId}'${
983
+ isUndefined(defaultValue) ? '?' : EMPTY_STRING
984
+ }: ${type};`,
985
+ ),
986
+ ' ',
987
+ ) +
988
+ '}',
989
+ getTheContentOfTheStoreDoc(2, 5),
869
990
  );
870
991
  const valuesWhenSetType = addType(
871
- 'ValuesWhenSet',
872
- `{${join(
873
- mapValuesSchema((valueId, type) => `'${valueId}'?: ${type};`),
874
- ' ',
875
- )}}`,
876
- getTheContentOfTheStoreDoc(4, 2, 1),
992
+ VALUES + 'WhenSet',
993
+ '{' +
994
+ join(
995
+ mapValuesSchema((valueId, type) => `'${valueId}'?: ${type};`),
996
+ ' ',
997
+ ) +
998
+ '}',
999
+ getTheContentOfTheStoreDoc(2, 5, 1),
877
1000
  );
878
1001
  const valueIdType = addType(
879
- 'ValueId',
1002
+ VALUE + ID,
880
1003
  join(
881
1004
  mapValuesSchema((valueId) => `'${valueId}'`),
882
1005
  ' | ',
883
1006
  ),
884
- `A Value Id in ${THE_STORE}`,
1007
+ 'A Value Id in ' + THE_STORE,
885
1008
  );
886
1009
  const valueCallbackType = addType(
887
- 'ValueCallback',
1010
+ VALUE + CALLBACK,
888
1011
  `(...[valueId, rowCallback]: ${join(
889
1012
  mapValuesSchema(
890
1013
  (valueId, type) => `[valueId: '${valueId}', value: ${type}]`,
891
1014
  ),
892
1015
  ' | ',
893
- )})${RETURNS_VOID}`,
1016
+ )})` + RETURNS_VOID,
894
1017
  getCallbackDoc('a Value Id, and value'),
895
1018
  );
896
1019
  const getValueChangeType = addType(
897
1020
  'GetValueChange',
898
1021
  `(valueId: ${valueIdType}) => ValueChange`,
899
- `${A_FUNCTION_FOR} returning information about any Value's changes during a transaction`,
1022
+ A_FUNCTION_FOR +
1023
+ ` returning information about any Value's changes during a ` +
1024
+ TRANSACTION_,
900
1025
  );
901
1026
  const valuesListenerType = addType(
902
- 'ValuesListener',
1027
+ VALUES + LISTENER,
903
1028
  `(${storeInstance}: ${storeType}, getValueChange: ${getValueChangeType}${OR_UNDEFINED})` +
904
1029
  RETURNS_VOID,
905
1030
  getListenerTypeDoc(9),
906
1031
  );
907
1032
  const valueIdsListenerType = addType(
908
- 'ValueIdsListener',
909
- `(${storeInstance}: ${storeType})${RETURNS_VOID}`,
1033
+ VALUE_IDS + LISTENER,
1034
+ `(${storeInstance}: ${storeType})` + RETURNS_VOID,
910
1035
  getListenerTypeDoc(10),
911
1036
  );
912
1037
  const valueListenerType = addType(
913
- 'ValueListener',
1038
+ VALUE + LISTENER,
914
1039
  `(...[${storeInstance}, valueId, newValue, oldValue, getValueChange]: ${join(
915
1040
  mapValuesSchema(
916
1041
  (valueId, type) =>
917
1042
  `[${storeInstance}: ${storeType}, valueId: '${valueId}', newValue: ${type}${OR_UNDEFINED}, oldValue: ${type}${OR_UNDEFINED}, getValueChange: ${getValueChangeType} | undefined]`,
918
1043
  ),
919
1044
  ' | ',
920
- )})${RETURNS_VOID}`,
1045
+ )})` + RETURNS_VOID,
921
1046
  getListenerTypeDoc(11),
922
1047
  );
923
1048
  const invalidValueListenerType = addType(
924
- 'InvalidValueListener',
925
- `(${storeInstance}: ${storeType}, valueId: Id, invalidValues: any[])${RETURNS_VOID}`,
1049
+ INVALID + VALUE + LISTENER,
1050
+ `(${storeInstance}: ${storeType}, valueId: Id, invalidValues: any[])` +
1051
+ RETURNS_VOID,
926
1052
  getListenerTypeDoc(12),
927
1053
  );
928
- addMethod(
929
- `hasValues`,
930
- EMPTY_STRING,
931
- BOOLEAN,
932
- storeMethod('hasValues'),
933
- getHasDoc('any Value'),
934
- );
935
- addMethod(
936
- `getValues`,
937
- EMPTY_STRING,
1054
+ sharedValueTypes = [
938
1055
  valuesType,
939
- storeMethod('getValues', EMPTY_STRING, valuesType),
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),
1056
+ valuesWhenSetType,
1057
+ valueIdType,
1058
+ valuesListenerType,
1059
+ valueIdsListenerType,
1060
+ valueListenerType,
1061
+ ];
1062
+ arrayForEach(
1063
+ [
1064
+ [valuesType],
1065
+ [BOOLEAN],
1066
+ [storeType, 'values: ' + valuesWhenSetType, 'values'],
1067
+ [storeType],
1068
+ [storeType, 'partialValues: ' + valuesWhenSetType, 'partialValues'],
1069
+ ],
1070
+ ([returnType, params, paramsInCall], verb) =>
1071
+ addProxyMethod(
1072
+ verb,
1073
+ EMPTY_STRING,
1074
+ VALUES,
1075
+ returnType,
1076
+ getTheContentOfTheStoreDoc(2, verb),
1077
+ params,
1078
+ paramsInCall,
1079
+ ),
955
1080
  );
956
- addMethod(
957
- `delValues`,
1081
+ addProxyMethod(
1082
+ 0,
958
1083
  EMPTY_STRING,
959
- storeType,
960
- fluentStoreMethod('delValues'),
961
- getTheContentOfTheStoreDoc(3, 2),
1084
+ VALUE_IDS,
1085
+ valueIdType + SQUARE_BRACKETS,
1086
+ getIdsDoc(VALUE, THE_STORE),
962
1087
  );
963
- addMethod(
964
- `getValueIds`,
1088
+ addProxyMethod(
1089
+ 5,
965
1090
  EMPTY_STRING,
966
- `${valueIdType}[]`,
967
- storeMethod('getValueIds', EMPTY_STRING, `${valueIdType}[]`),
968
- getIdsDoc('Value', THE_STORE),
969
- );
970
- addMethod(
971
- 'forEachValue',
1091
+ VALUE,
1092
+ `void`,
1093
+ getForEachDoc(VALUE, THE_STORE),
972
1094
  `valueCallback: ${valueCallbackType}`,
973
- 'void',
974
- storeMethod('forEachValue', 'valueCallback as any'),
975
- getForEachDoc('Value', THE_STORE),
1095
+ 'valueCallback as any',
976
1096
  );
977
1097
  mapValuesSchema((valueId, type, _, VALUE_ID, valueName) => {
978
- addMethod(
979
- `has${valueName}Value`,
980
- EMPTY_STRING,
981
- BOOLEAN,
982
- storeMethod('hasValue', VALUE_ID),
983
- getHasDoc(getValueDoc(valueId)),
984
- );
985
- addMethod(
986
- `get${valueName}Value`,
987
- EMPTY_STRING,
988
- type,
989
- storeMethod('getValue', VALUE_ID, type),
990
- getValueContentDoc(valueId),
991
- );
992
- addMethod(
993
- `set${valueName}Value`,
994
- `value: ${type}`,
995
- storeType,
996
- fluentStoreMethod('setValue', `${VALUE_ID}, value`),
997
- getValueContentDoc(valueId, 1),
998
- );
999
- addMethod(
1000
- `del${valueName}Value`,
1001
- EMPTY_STRING,
1002
- storeType,
1003
- fluentStoreMethod('delValue', VALUE_ID),
1004
- getValueContentDoc(valueId, 3),
1098
+ const mapValueType = 'Map' + camel(type, 1);
1099
+ mapSet(mapCellOrValueTypes, type, mapValueType);
1100
+ arrayForEach(
1101
+ [
1102
+ [type],
1103
+ [BOOLEAN],
1104
+ [storeType, `value: ${type} | ` + mapValueType, ', value as any'],
1105
+ [storeType],
1106
+ ],
1107
+ ([returnType, params, paramsInCall = EMPTY_STRING], verb) =>
1108
+ addProxyMethod(
1109
+ verb,
1110
+ valueName,
1111
+ VALUE,
1112
+ returnType,
1113
+ getValueContentDoc(valueId, verb),
1114
+ params,
1115
+ VALUE_ID + paramsInCall,
1116
+ ),
1005
1117
  );
1006
1118
  });
1007
- addMethod(
1008
- 'getValuesJson',
1119
+ addProxyMethod(
1120
+ 0,
1009
1121
  EMPTY_STRING,
1010
- 'Json',
1011
- storeMethod('getValuesJson'),
1012
- getTheContentOfTheStoreDoc(5, 2),
1122
+ VALUES + JSON$1,
1123
+ JSON$1,
1124
+ getTheContentOfTheStoreDoc(2, 6),
1013
1125
  );
1014
- addMethod(
1015
- 'setValuesJson',
1016
- 'valuesJson: Json',
1126
+ addProxyMethod(
1127
+ 2,
1128
+ EMPTY_STRING,
1129
+ VALUES + JSON$1,
1017
1130
  storeType,
1018
- fluentStoreMethod('setValuesJson', 'valuesJson'),
1019
- getTheContentOfTheStoreDoc(6, 2),
1131
+ getTheContentOfTheStoreDoc(2, 7),
1132
+ 'valuesJson: ' + JSON$1,
1133
+ 'values' + JSON$1,
1020
1134
  );
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),
1135
+ addProxyListener(
1136
+ VALUES,
1137
+ valuesListenerType,
1138
+ getTheContentOfTheStoreDoc(2, 8) + ' changes',
1034
1139
  );
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),
1140
+ addProxyListener(VALUE_IDS, valueIdsListenerType, getListenerDoc(10, 0, 1));
1141
+ addProxyListener(
1142
+ VALUE,
1143
+ valueListenerType,
1144
+ getListenerDoc(11, 0),
1145
+ `valueId: ${valueIdType} | null`,
1146
+ 'valueId',
1041
1147
  );
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`,
1148
+ addProxyListener(
1149
+ INVALID + VALUE,
1150
+ invalidValueListenerType,
1151
+ REGISTERS_A_LISTENER + ' whenever an invalid Value change was attempted',
1152
+ `valueId: IdOrNull`,
1153
+ 'valueId',
1048
1154
  );
1049
1155
  addImport(
1050
1156
  1,
@@ -1069,94 +1175,122 @@ const getStoreApi = (tablesSchema, valuesSchema, module) => {
1069
1175
  )}]: ${addConstant(snake(type), `'${type}'`)}${
1070
1176
  isUndefined(defaultValue)
1071
1177
  ? EMPTY_STRING
1072
- : `, [${addConstant(snake(DEFAULT), `'${DEFAULT}'`)}]: ${
1073
- isString(defaultValue)
1074
- ? addConstant(snake(defaultValue), `'${defaultValue}'`)
1075
- : defaultValue
1076
- }`
1178
+ : `, [${addConstant(snake(DEFAULT), `'${DEFAULT}'`)}]: ` +
1179
+ (isString(defaultValue)
1180
+ ? addConstant(snake(defaultValue), `'${defaultValue}'`)
1181
+ : defaultValue)
1077
1182
  }},`,
1078
1183
  ]),
1079
1184
  '})',
1080
1185
  );
1081
1186
  }
1082
- addImport(0, 'tinybase', ...COMMON_IMPORTS);
1187
+ mapForEach(mapCellOrValueTypes, (type, mapCellType) =>
1188
+ addType(
1189
+ mapCellType,
1190
+ `(cell: ${type}${OR_UNDEFINED}) => ` + type,
1191
+ `Takes a ${type} Cell value and returns another`,
1192
+ ),
1193
+ );
1194
+ addImport(
1195
+ null,
1196
+ 'tinybase',
1197
+ 'DoRollback',
1198
+ ID,
1199
+ 'IdOrNull',
1200
+ IDS,
1201
+ JSON$1,
1202
+ 'Store',
1203
+ );
1083
1204
  const transactionListenerType = addType(
1084
- 'TransactionListener',
1085
- `(${storeInstance}: ${storeType}, cellsTouched: boolean, valuesTouched: boolean)${RETURNS_VOID}`,
1086
- `${A_FUNCTION_FOR} listening to the completion of a transaction`,
1205
+ TRANSACTION + LISTENER,
1206
+ `(${storeInstance}: ${storeType}, cellsTouched: boolean, valuesTouched: boolean)` +
1207
+ RETURNS_VOID,
1208
+ A_FUNCTION_FOR + ' listening to the completion of a ' + TRANSACTION_,
1087
1209
  );
1088
- addMethod(
1089
- 'getJson',
1210
+ addProxyMethod(
1211
+ 0,
1090
1212
  EMPTY_STRING,
1091
- 'Json',
1092
- storeMethod('getJson'),
1093
- getTheContentOfTheStoreDoc(5),
1213
+ JSON$1,
1214
+ JSON$1,
1215
+ getTheContentOfTheStoreDoc(0, 6),
1094
1216
  );
1095
- addMethod(
1096
- 'setJson',
1097
- 'json: Json',
1217
+ addProxyMethod(
1218
+ 2,
1219
+ EMPTY_STRING,
1220
+ JSON$1,
1098
1221
  storeType,
1099
- fluentStoreMethod('setJson', 'json'),
1100
- getTheContentOfTheStoreDoc(6),
1222
+ getTheContentOfTheStoreDoc(0, 7),
1223
+ 'json: ' + JSON$1,
1224
+ 'json',
1101
1225
  );
1102
- addMethod(
1103
- 'transaction',
1104
- 'actions: () => Return, doRollback?: DoRollback',
1226
+ addProxyMethod(
1227
+ 7,
1228
+ EMPTY_STRING,
1229
+ TRANSACTION_,
1105
1230
  'Return',
1106
- storeMethod('transaction', 'actions, doRollback'),
1107
- 'Execute a transaction to make multiple mutations',
1231
+ 'Execute a ' + TRANSACTION_ + ' to make multiple mutations',
1232
+ 'actions: () => Return, doRollback?: DoRollback',
1233
+ 'actions, doRollback',
1108
1234
  '<Return>',
1109
1235
  );
1110
- addMethod(
1111
- 'startTransaction',
1236
+ addProxyMethod(
1237
+ 7,
1112
1238
  EMPTY_STRING,
1239
+ 'start' + TRANSACTION,
1113
1240
  storeType,
1114
- fluentStoreMethod('startTransaction'),
1115
- 'Explicitly starts a transaction',
1241
+ 'Explicitly starts a ' + TRANSACTION_,
1116
1242
  );
1117
- addMethod(
1118
- 'finishTransaction',
1119
- 'doRollback?: DoRollback,',
1243
+ addProxyMethod(
1244
+ 7,
1245
+ EMPTY_STRING,
1246
+ 'finish' + TRANSACTION,
1120
1247
  storeType,
1121
- fluentStoreMethod('finishTransaction', 'doRollback'),
1122
- 'Explicitly finishes a transaction',
1248
+ 'Explicitly finishes a ' + TRANSACTION_,
1249
+ 'doRollback?: DoRollback,',
1250
+ 'doRollback',
1123
1251
  );
1124
- addMethod(
1125
- 'addWillFinishTransactionListener',
1126
- `${LISTENER}: ${transactionListenerType}`,
1127
- 'Id',
1128
- storeListener('addWillFinishTransactionListener'),
1129
- `${REGISTERS_A_LISTENER} just before ${THE_END_OF_THE_TRANSACTION}`,
1252
+ addProxyListener(
1253
+ 'WillFinish' + TRANSACTION,
1254
+ transactionListenerType,
1255
+ REGISTERS_A_LISTENER + ' just before ' + THE_END_OF_THE_TRANSACTION,
1256
+ EMPTY_STRING,
1257
+ EMPTY_STRING,
1258
+ 0,
1130
1259
  );
1131
- addMethod(
1132
- 'addDidFinishTransactionListener',
1133
- `${LISTENER}: ${transactionListenerType}`,
1134
- 'Id',
1135
- storeListener('addDidFinishTransactionListener'),
1136
- `${REGISTERS_A_LISTENER} just after ${THE_END_OF_THE_TRANSACTION}`,
1260
+ addProxyListener(
1261
+ 'DidFinish' + TRANSACTION,
1262
+ transactionListenerType,
1263
+ REGISTERS_A_LISTENER + ' just after ' + THE_END_OF_THE_TRANSACTION,
1264
+ EMPTY_STRING,
1265
+ EMPTY_STRING,
1266
+ 0,
1137
1267
  );
1138
- addMethod(
1139
- 'callListener',
1140
- `${LISTENER}Id: Id`,
1268
+ addProxyMethod(
1269
+ 7,
1270
+ EMPTY_STRING,
1271
+ 'call' + LISTENER,
1141
1272
  storeType,
1142
- fluentStoreMethod('callListener', `${LISTENER}Id`),
1143
- `Manually provoke a ${LISTENER} to be called`,
1273
+ `Manually provoke a listener to be called`,
1274
+ `listenerId: Id`,
1275
+ `listenerId`,
1144
1276
  );
1145
- addMethod(
1146
- 'delListener',
1147
- `${LISTENER}Id: Id`,
1277
+ addProxyMethod(
1278
+ 3,
1279
+ EMPTY_STRING,
1280
+ LISTENER,
1148
1281
  storeType,
1149
- fluentStoreMethod('delListener', `${LISTENER}Id`),
1150
- `Remove a ${LISTENER} that was previously added to ${THE_STORE}`,
1282
+ 'Remove a listener that was previously added to ' + THE_STORE,
1283
+ 'listenerId: Id',
1284
+ 'listenerId',
1151
1285
  );
1152
1286
  addMethod(
1153
1287
  'getStore',
1154
1288
  EMPTY_STRING,
1155
1289
  'Store',
1156
1290
  'store',
1157
- `${VERBS[0]} the underlying Store object`,
1291
+ VERBS[0] + ' the underlying Store object',
1158
1292
  );
1159
- addImport(1, 'tinybase', 'createStore', ...COMMON_IMPORTS);
1293
+ addImport(1, 'tinybase', 'createStore');
1160
1294
  addImport(
1161
1295
  1,
1162
1296
  moduleDefinition,
@@ -1165,33 +1299,806 @@ const getStoreApi = (tablesSchema, valuesSchema, module) => {
1165
1299
  transactionListenerType,
1166
1300
  );
1167
1301
  addConstant('store', ['createStore()', ...createSteps]);
1168
- addFunction('fluent', 'actions: () => Store', [
1302
+ addInternalFunction('fluent', 'actions: () => Store', [
1169
1303
  'actions();',
1170
1304
  `return ${storeInstance};`,
1171
1305
  ]);
1172
- addFunction(
1306
+ addInternalFunction(
1173
1307
  'proxy',
1174
- `${LISTENER}: any`,
1175
- `(_: Store, ...args: any[]) => ${LISTENER}(${storeInstance}, ...args)`,
1308
+ `listener: any`,
1309
+ `(_: Store, ...params: any[]) => listener(${storeInstance}, ...params)`,
1176
1310
  );
1177
1311
  addConstant(storeInstance, ['{', ...getMethods(1), '}']);
1178
1312
  return [
1179
1313
  build(
1180
1314
  ...getImports(0),
1181
1315
  ...getTypes(),
1182
- `${EXPORT} interface ${storeType} {`,
1316
+ EXPORT + ' interface ' + storeType + ' {',
1183
1317
  ...getMethods(0),
1184
- `}`,
1318
+ '}',
1185
1319
  EMPTY_STRING,
1186
1320
  comment(`Creates a ${storeType} object`),
1187
- `${EXPORT} function create${storeType}(): ${storeType};`,
1321
+ EXPORT + ' function create' + storeType + '(): ' + storeType + ';',
1188
1322
  ),
1189
1323
  build(
1190
1324
  ...getImports(1),
1191
- `${EXPORT} const create${storeType}: typeof create${storeType}Decl = () => {`,
1325
+ EXPORT +
1326
+ ' const create' +
1327
+ storeType +
1328
+ ': typeof create' +
1329
+ storeType +
1330
+ 'Decl = () => {',
1192
1331
  ...getConstants(),
1193
1332
  `return Object.freeze(${storeInstance});`,
1194
- `};`,
1333
+ '};',
1334
+ ),
1335
+ sharedTableTypes,
1336
+ sharedValueTypes,
1337
+ ];
1338
+ };
1339
+
1340
+ const DEPS = 'Deps';
1341
+ const getGet = (noun) => GET + noun;
1342
+ const getGetAndGetDeps = (noun) =>
1343
+ getParameterList(getGet(noun), getGet(noun) + DEPS);
1344
+ const PARAMETER = 'Parameter';
1345
+ const GETTER_ARGS = ': (parameter: ' + PARAMETER + ', store: Store) => ';
1346
+ const USE_CONTEXT = 'const contextValue = useContext(Context);';
1347
+ const AND_REGISTERS =
1348
+ ', and registers a listener so that any changes to that result will cause a re-render';
1349
+ const BASED_ON_A_PARAMETER = ', based on a parameter';
1350
+ const COLON_SPACE = ': ';
1351
+ const PARAMETERIZED_CALLBACK =
1352
+ PARAMETER + 'ized' + CALLBACK + '<' + PARAMETER + '>';
1353
+ const GENERIC_PARAMETER = '<' + PARAMETER + ',>';
1354
+ const DEPS_SUFFIX = DEPS + '?: React.DependencyList';
1355
+ const THEN_DEPS = 'then' + DEPS_SUFFIX;
1356
+ const THEN_PREFIX = 'then?: (store: Store';
1357
+ const THEN_AND_THEN_DEPS = getParameterList(
1358
+ THEN_PREFIX + ')' + RETURNS_VOID,
1359
+ THEN_DEPS,
1360
+ );
1361
+ const THEN_AND_THEN_DEPS_IN_CALL = 'then, then' + DEPS;
1362
+ const ROW_ID = 'rowId';
1363
+ const TYPED_ROW_ID = ROW_ID + COLON_SPACE + ID;
1364
+ const getListenerHookParams = (listenerType, ...extraParams) =>
1365
+ getParameterList(
1366
+ ...extraParams,
1367
+ LISTENER_ + ': ' + listenerType,
1368
+ LISTENER_ + DEPS_SUFFIX,
1369
+ 'mutator?: boolean',
1370
+ );
1371
+ const getListenerHookParamsInCall = (...extraParams) =>
1372
+ getParameterList(...extraParams, LISTENER_, LISTENER_ + DEPS, 'mutator');
1373
+ const getStoreUiReactApi = (
1374
+ tablesSchema,
1375
+ valuesSchema,
1376
+ module,
1377
+ sharedTableTypes,
1378
+ sharedValueTypes,
1379
+ ) => {
1380
+ const [
1381
+ build,
1382
+ addImport,
1383
+ addType,
1384
+ addInternalFunction,
1385
+ addConstant,
1386
+ getImports,
1387
+ getTypes,
1388
+ getConstants,
1389
+ ] = getCodeFunctions();
1390
+ const [mapTablesSchema, mapCellSchema, mapValuesSchema] = getSchemaFunctions(
1391
+ tablesSchema,
1392
+ valuesSchema,
1393
+ addConstant,
1394
+ );
1395
+ const moduleDefinition = `./${camel(module)}.d`;
1396
+ const uiReactModuleDefinition = `./${camel(module)}-ui-react.d`;
1397
+ const tinyBaseUiReact = 'tinybase/ui-react';
1398
+ const storeType = camel(module, 1);
1399
+ const storeInstance = camel(storeType);
1400
+ const StoreOrStoreId = storeType + 'Or' + storeType + ID;
1401
+ const storeOrStoreId = storeInstance + 'Or' + storeType + ID;
1402
+ const functions = mapNew();
1403
+ const addFunction = (
1404
+ name,
1405
+ parameters,
1406
+ returnType,
1407
+ body,
1408
+ doc,
1409
+ generic = EMPTY_STRING,
1410
+ ) => {
1411
+ addImport(1, uiReactModuleDefinition, name + ' as ' + name + 'Decl');
1412
+ return mapUnique(functions, name, [
1413
+ parameters,
1414
+ returnType,
1415
+ isArray(body) ? ['{', body, '}'] : body,
1416
+ doc,
1417
+ generic,
1418
+ ]);
1419
+ };
1420
+ const addHook = (
1421
+ name,
1422
+ parameters,
1423
+ returnType,
1424
+ body,
1425
+ doc,
1426
+ generic = EMPTY_STRING,
1427
+ ) => addFunction(`use${name}`, parameters, returnType, body, doc, generic);
1428
+ const addProxyHook = (
1429
+ name,
1430
+ underlyingName,
1431
+ returnType,
1432
+ doc,
1433
+ preParameters = EMPTY_STRING,
1434
+ preParametersInCall = EMPTY_STRING,
1435
+ generic = EMPTY_STRING,
1436
+ postParameters = EMPTY_STRING,
1437
+ postParametersInCall = EMPTY_STRING,
1438
+ ) => {
1439
+ addImport(
1440
+ 1,
1441
+ tinyBaseUiReact,
1442
+ `use${underlyingName} as use${underlyingName}Core`,
1443
+ );
1444
+ addHook(
1445
+ name,
1446
+ getParameterList(preParameters, storeOrStoreIdParameter, postParameters),
1447
+ returnType,
1448
+ useHook +
1449
+ `(${storeOrStoreId}, use${underlyingName}Core, [` +
1450
+ (preParametersInCall ? preParametersInCall : EMPTY_STRING) +
1451
+ (postParametersInCall ? '], [' + postParametersInCall : EMPTY_STRING) +
1452
+ '])',
1453
+ doc,
1454
+ generic,
1455
+ );
1456
+ };
1457
+ const addComponent = (name, parameters, body, doc) =>
1458
+ addFunction(name, parameters, 1, body, doc);
1459
+ const getFunctions = (location = 0) =>
1460
+ mapMap(functions, ([parameters, returnType, body, doc, generic], name) => {
1461
+ const lines = location
1462
+ ? [
1463
+ EXPORT +
1464
+ ` const ${name}: typeof ${name}Decl = ${generic}(${parameters}): ${
1465
+ returnType == 1 ? 'any' : returnType
1466
+ } =>`,
1467
+ body,
1468
+ ]
1469
+ : [
1470
+ EXPORT +
1471
+ ` function ${name}${generic}(${parameters}): ${
1472
+ returnType == 1 ? 'ComponentReturnType' : returnType
1473
+ };`,
1474
+ ];
1475
+ if (!location) {
1476
+ arrayUnshift(lines, comment(doc));
1477
+ }
1478
+ arrayPush(lines, EMPTY_STRING);
1479
+ return lines;
1480
+ });
1481
+ addImport(
1482
+ null,
1483
+ 'tinybase',
1484
+ ID,
1485
+ IDS,
1486
+ 'IdOrNull',
1487
+ 'Store',
1488
+ CALLBACK,
1489
+ PARAMETER + 'ized' + CALLBACK,
1490
+ );
1491
+ addImport(0, tinyBaseUiReact, 'ComponentReturnType');
1492
+ addImport(0, moduleDefinition, storeType);
1493
+ const storeOrStoreIdType = addType(
1494
+ StoreOrStoreId,
1495
+ storeType + ' | ' + ID,
1496
+ `Used when you need to refer to a ${storeType} in a React hook or component`,
1497
+ );
1498
+ const providerPropsType = addType(
1499
+ 'ProviderProps',
1500
+ `{readonly ${storeInstance}?: ${storeType}; readonly ${storeInstance}ById?: {[${storeInstance}Id: Id]: ${storeType}}}`,
1501
+ `Used with the Provider component, so that a ${storeType} can be passed into the context of an application`,
1502
+ );
1503
+ addImport(1, 'react', 'React');
1504
+ addImport(1, uiReactModuleDefinition, storeOrStoreIdType, providerPropsType);
1505
+ const storeOrStoreIdParameter = storeOrStoreId + '?: ' + storeOrStoreIdType;
1506
+ addConstant('{createContext, useContext, useMemo}', 'React');
1507
+ addConstant(
1508
+ 'Context',
1509
+ `createContext<[${storeType}?, {[${storeInstance}Id: Id]: ${storeType}}?]>([])`,
1510
+ );
1511
+ addHook(
1512
+ `Create${storeType}`,
1513
+ `create: () => ${storeType}, create` + DEPS_SUFFIX,
1514
+ storeType,
1515
+ '\n// eslint-disable-next-line react-hooks/exhaustive-deps\nuseMemo(create, createDeps)',
1516
+ `Create a ${storeType} within a React application with convenient memoization`,
1517
+ );
1518
+ const getStoreHook = addHook(
1519
+ storeType,
1520
+ `id?: Id`,
1521
+ storeType + OR_UNDEFINED,
1522
+ [
1523
+ USE_CONTEXT,
1524
+ 'return id == null ? contextValue[0] : contextValue[1]?.[id];',
1525
+ ],
1526
+ `Get a reference to a ${storeType} from within a Provider component context`,
1527
+ );
1528
+ const useHook = addInternalFunction(
1529
+ `useHook`,
1530
+ storeOrStoreId +
1531
+ `: ${storeOrStoreIdType} | undefined, hook: (...params: any[]) => any, preParams: any[], postParams: any[] = []`,
1532
+ [
1533
+ `const ${storeInstance} = ${getStoreHook}(${storeOrStoreId} as Id);`,
1534
+ `return hook(...preParams, ((${storeOrStoreId} == null || typeof ${storeOrStoreId} == 'string')`,
1535
+ `? ${storeInstance} : ${storeOrStoreId})?.getStore(), ...postParams)`,
1536
+ ],
1537
+ );
1538
+ if (!objIsEmpty(tablesSchema)) {
1539
+ const [
1540
+ tablesType,
1541
+ tableIdType,
1542
+ tablesListenerType,
1543
+ tableIdsListenerType,
1544
+ tableListenerType,
1545
+ rowIdsListenerType,
1546
+ sortedRowIdsListenerType,
1547
+ rowListenerType,
1548
+ cellIdsListenerType,
1549
+ cellListenerType,
1550
+ tablesTypes,
1551
+ ] = sharedTableTypes;
1552
+ addImport(
1553
+ null,
1554
+ moduleDefinition,
1555
+ tablesType,
1556
+ tableIdType,
1557
+ tablesListenerType,
1558
+ tableIdsListenerType,
1559
+ tableListenerType,
1560
+ rowIdsListenerType,
1561
+ sortedRowIdsListenerType,
1562
+ rowListenerType,
1563
+ cellIdsListenerType,
1564
+ cellListenerType,
1565
+ );
1566
+ addImport(1, tinyBaseUiReact);
1567
+ addImport(1, moduleDefinition, storeType);
1568
+ addProxyHook(
1569
+ TABLES,
1570
+ TABLES,
1571
+ tablesType,
1572
+ getTheContentOfTheStoreDoc(1, 0) + AND_REGISTERS,
1573
+ );
1574
+ addProxyHook(
1575
+ TABLE_IDS,
1576
+ TABLE_IDS,
1577
+ tableIdType + SQUARE_BRACKETS,
1578
+ getIdsDoc(TABLE, THE_STORE) + AND_REGISTERS,
1579
+ );
1580
+ addProxyHook(
1581
+ SET + TABLES + CALLBACK,
1582
+ SET + TABLES + CALLBACK,
1583
+ PARAMETERIZED_CALLBACK,
1584
+ getTheContentOfTheStoreDoc(1, 9) + BASED_ON_A_PARAMETER,
1585
+ getParameterList(
1586
+ getGet(TABLES) + GETTER_ARGS + tablesType,
1587
+ getGet(TABLES) + DEPS_SUFFIX,
1588
+ ),
1589
+ getGetAndGetDeps(TABLES),
1590
+ GENERIC_PARAMETER,
1591
+ getParameterList(
1592
+ THEN_PREFIX,
1593
+ `tables: ${tablesType})` + RETURNS_VOID,
1594
+ THEN_DEPS,
1595
+ ),
1596
+ THEN_AND_THEN_DEPS_IN_CALL,
1597
+ );
1598
+ addProxyHook(
1599
+ DEL + TABLES + CALLBACK,
1600
+ DEL + TABLES + CALLBACK,
1601
+ CALLBACK,
1602
+ getTheContentOfTheStoreDoc(1, 12),
1603
+ EMPTY_STRING,
1604
+ EMPTY_STRING,
1605
+ EMPTY_STRING,
1606
+ THEN_AND_THEN_DEPS,
1607
+ THEN_AND_THEN_DEPS_IN_CALL,
1608
+ );
1609
+ mapTablesSchema((tableId, tableName, TABLE_ID) => {
1610
+ const [tableType, rowType, rowWhenSetType, cellIdType] = mapGet(
1611
+ tablesTypes,
1612
+ tableId,
1613
+ );
1614
+ addImport(
1615
+ 0,
1616
+ moduleDefinition,
1617
+ tableType,
1618
+ rowType,
1619
+ rowWhenSetType,
1620
+ cellIdType,
1621
+ );
1622
+ addImport(
1623
+ 1,
1624
+ moduleDefinition,
1625
+ tableType,
1626
+ rowType,
1627
+ rowWhenSetType,
1628
+ cellIdType,
1629
+ );
1630
+ addProxyHook(
1631
+ tableName + TABLE,
1632
+ TABLE,
1633
+ tableType,
1634
+ getTableContentDoc(tableId) + AND_REGISTERS,
1635
+ EMPTY_STRING,
1636
+ TABLE_ID,
1637
+ );
1638
+ addProxyHook(
1639
+ tableName + ROW_IDS,
1640
+ ROW_IDS,
1641
+ IDS,
1642
+ getIdsDoc(ROW, getTableDoc(tableId)) + AND_REGISTERS,
1643
+ EMPTY_STRING,
1644
+ TABLE_ID,
1645
+ );
1646
+ addProxyHook(
1647
+ tableName + SORTED_ROW_IDS,
1648
+ SORTED_ROW_IDS,
1649
+ IDS,
1650
+ getIdsDoc(ROW, getTableDoc(tableId), 1) + AND_REGISTERS,
1651
+ 'cellId?: ' +
1652
+ cellIdType +
1653
+ ', descending?: boolean, offset?: number, limit?: number',
1654
+ TABLE_ID + ', cellId, descending, offset, limit',
1655
+ );
1656
+ addProxyHook(
1657
+ tableName + ROW,
1658
+ ROW,
1659
+ rowType,
1660
+ getRowContentDoc(tableId) + AND_REGISTERS,
1661
+ TYPED_ROW_ID,
1662
+ getParameterList(TABLE_ID, ROW_ID),
1663
+ );
1664
+ addProxyHook(
1665
+ tableName + CELL_IDS,
1666
+ CELL_IDS,
1667
+ cellIdType + SQUARE_BRACKETS,
1668
+ getIdsDoc(CELL, getRowDoc(tableId)) + AND_REGISTERS,
1669
+ TYPED_ROW_ID,
1670
+ getParameterList(TABLE_ID, ROW_ID),
1671
+ );
1672
+ addProxyHook(
1673
+ SET + tableName + TABLE + CALLBACK,
1674
+ SET + TABLE + CALLBACK,
1675
+ PARAMETERIZED_CALLBACK,
1676
+ getTableContentDoc(tableId, 9) + BASED_ON_A_PARAMETER,
1677
+ getParameterList(
1678
+ getGet(TABLE) + GETTER_ARGS + tableType,
1679
+ getGet(TABLE) + DEPS_SUFFIX,
1680
+ ),
1681
+ getParameterList(TABLE_ID, getGetAndGetDeps(TABLE)),
1682
+ GENERIC_PARAMETER,
1683
+ getParameterList(
1684
+ THEN_PREFIX,
1685
+ `table: ${tableType})` + RETURNS_VOID,
1686
+ THEN_DEPS,
1687
+ ),
1688
+ THEN_AND_THEN_DEPS_IN_CALL,
1689
+ );
1690
+ addProxyHook(
1691
+ DEL + tableName + TABLE + CALLBACK,
1692
+ DEL + TABLE + CALLBACK,
1693
+ CALLBACK,
1694
+ getTableContentDoc(tableId, 12),
1695
+ EMPTY_STRING,
1696
+ TABLE_ID,
1697
+ EMPTY_STRING,
1698
+ THEN_AND_THEN_DEPS,
1699
+ THEN_AND_THEN_DEPS_IN_CALL,
1700
+ );
1701
+ addProxyHook(
1702
+ SET + tableName + ROW + CALLBACK,
1703
+ SET + ROW + CALLBACK,
1704
+ PARAMETERIZED_CALLBACK,
1705
+ getRowContentDoc(tableId, 9) + BASED_ON_A_PARAMETER,
1706
+ getParameterList(
1707
+ TYPED_ROW_ID,
1708
+ getGet(ROW) + GETTER_ARGS + rowWhenSetType,
1709
+ getGet(ROW) + DEPS_SUFFIX,
1710
+ ),
1711
+ getParameterList(TABLE_ID, ROW_ID, getGetAndGetDeps(ROW)),
1712
+ GENERIC_PARAMETER,
1713
+ getParameterList(
1714
+ THEN_PREFIX,
1715
+ `row: ${rowWhenSetType})` + RETURNS_VOID,
1716
+ THEN_DEPS,
1717
+ ),
1718
+ THEN_AND_THEN_DEPS_IN_CALL,
1719
+ );
1720
+ addProxyHook(
1721
+ 'Add' + tableName + ROW + CALLBACK,
1722
+ 'Add' + ROW + CALLBACK,
1723
+ PARAMETERIZED_CALLBACK,
1724
+ getRowContentDoc(tableId, 10) + BASED_ON_A_PARAMETER,
1725
+ getParameterList(
1726
+ getGet(ROW) + GETTER_ARGS + rowWhenSetType,
1727
+ getGet(ROW) + DEPS_SUFFIX,
1728
+ ),
1729
+ getParameterList(TABLE_ID, getGetAndGetDeps(ROW)),
1730
+ GENERIC_PARAMETER,
1731
+ 'then?: (' +
1732
+ getParameterList(
1733
+ TYPED_ROW_ID + OR_UNDEFINED,
1734
+ 'store: Store',
1735
+ 'row: ' + rowWhenSetType + ')' + RETURNS_VOID,
1736
+ 'then' + DEPS_SUFFIX,
1737
+ ),
1738
+ THEN_AND_THEN_DEPS_IN_CALL,
1739
+ );
1740
+ addProxyHook(
1741
+ SET + tableName + PARTIAL + ROW + CALLBACK,
1742
+ SET + PARTIAL + ROW + CALLBACK,
1743
+ PARAMETERIZED_CALLBACK,
1744
+ getRowContentDoc(tableId, 11) + BASED_ON_A_PARAMETER,
1745
+ getParameterList(
1746
+ TYPED_ROW_ID,
1747
+ getGet(PARTIAL + ROW) + GETTER_ARGS + rowWhenSetType,
1748
+ getGet(PARTIAL + ROW) + DEPS_SUFFIX,
1749
+ ),
1750
+ getParameterList(TABLE_ID, ROW_ID, getGetAndGetDeps(PARTIAL + ROW)),
1751
+ GENERIC_PARAMETER,
1752
+ getParameterList(
1753
+ THEN_PREFIX,
1754
+ `partialRow: ${rowWhenSetType})` + RETURNS_VOID,
1755
+ THEN_DEPS,
1756
+ ),
1757
+ THEN_AND_THEN_DEPS_IN_CALL,
1758
+ );
1759
+ addProxyHook(
1760
+ DEL + tableName + ROW + CALLBACK,
1761
+ DEL + ROW + CALLBACK,
1762
+ CALLBACK,
1763
+ getRowContentDoc(tableId, 12),
1764
+ TYPED_ROW_ID,
1765
+ getParameterList(TABLE_ID, ROW_ID),
1766
+ EMPTY_STRING,
1767
+ THEN_AND_THEN_DEPS,
1768
+ THEN_AND_THEN_DEPS_IN_CALL,
1769
+ );
1770
+ mapCellSchema(
1771
+ tableId,
1772
+ (cellId, type, defaultValue, CELL_ID, cellName) => {
1773
+ const mapCellType = 'Map' + camel(type, 1);
1774
+ addImport(0, moduleDefinition, mapCellType);
1775
+ addImport(1, moduleDefinition, mapCellType);
1776
+ addProxyHook(
1777
+ tableName + cellName + CELL,
1778
+ CELL,
1779
+ type + (isUndefined(defaultValue) ? OR_UNDEFINED : EMPTY_STRING),
1780
+ getCellContentDoc(tableId, cellId) + AND_REGISTERS,
1781
+ TYPED_ROW_ID,
1782
+ getParameterList(TABLE_ID, ROW_ID, CELL_ID),
1783
+ );
1784
+ addProxyHook(
1785
+ SET + tableName + cellName + CELL + CALLBACK,
1786
+ SET + CELL + CALLBACK,
1787
+ PARAMETERIZED_CALLBACK,
1788
+ getCellContentDoc(tableId, cellId, 9) + BASED_ON_A_PARAMETER,
1789
+ getParameterList(
1790
+ TYPED_ROW_ID,
1791
+ getGet(CELL) + GETTER_ARGS + type + ' | ' + mapCellType,
1792
+ getGet(CELL) + DEPS_SUFFIX,
1793
+ ),
1794
+ getParameterList(TABLE_ID, ROW_ID, CELL_ID, getGetAndGetDeps(CELL)),
1795
+ GENERIC_PARAMETER,
1796
+ getParameterList(
1797
+ THEN_PREFIX,
1798
+ `cell: ${type} | ${mapCellType})` + RETURNS_VOID,
1799
+ THEN_DEPS,
1800
+ ),
1801
+ THEN_AND_THEN_DEPS_IN_CALL,
1802
+ );
1803
+ addProxyHook(
1804
+ DEL + tableName + cellName + CELL + CALLBACK,
1805
+ DEL + CELL + CALLBACK,
1806
+ CALLBACK,
1807
+ getCellContentDoc(tableId, cellId, 12),
1808
+ getParameterList(TYPED_ROW_ID, 'forceDel?: boolean'),
1809
+ getParameterList(TABLE_ID, ROW_ID, CELL_ID, 'forceDel'),
1810
+ EMPTY_STRING,
1811
+ THEN_AND_THEN_DEPS,
1812
+ THEN_AND_THEN_DEPS_IN_CALL,
1813
+ );
1814
+ },
1815
+ );
1816
+ });
1817
+ const cellIdsType = join(
1818
+ mapTablesSchema(
1819
+ (tableId) => mapGet(tablesTypes, tableId)?.[3] ?? EMPTY_STRING,
1820
+ ),
1821
+ ' | ',
1822
+ );
1823
+ addProxyHook(
1824
+ TABLES + LISTENER,
1825
+ TABLES + LISTENER,
1826
+ VOID,
1827
+ getTheContentOfTheStoreDoc(1, 8) + ' changes',
1828
+ getListenerHookParams(tablesListenerType),
1829
+ getListenerHookParamsInCall(),
1830
+ );
1831
+ addProxyHook(
1832
+ TABLE_IDS + LISTENER,
1833
+ TABLE_IDS + LISTENER,
1834
+ VOID,
1835
+ getListenerDoc(2, 0, 1),
1836
+ getListenerHookParams(tableIdsListenerType),
1837
+ getListenerHookParamsInCall(),
1838
+ );
1839
+ addProxyHook(
1840
+ TABLE + LISTENER,
1841
+ TABLE + LISTENER,
1842
+ VOID,
1843
+ getListenerDoc(3, 0),
1844
+ getListenerHookParams(
1845
+ tableListenerType,
1846
+ `tableId: ${tableIdType} | null`,
1847
+ ),
1848
+ getListenerHookParamsInCall('tableId'),
1849
+ );
1850
+ addProxyHook(
1851
+ ROW_IDS + LISTENER,
1852
+ ROW_IDS + LISTENER,
1853
+ VOID,
1854
+ getListenerDoc(4, 3, 1),
1855
+ getListenerHookParams(
1856
+ rowIdsListenerType,
1857
+ `tableId: ${tableIdType} | null`,
1858
+ ),
1859
+ getListenerHookParamsInCall('tableId'),
1860
+ );
1861
+ addProxyHook(
1862
+ SORTED_ROW_IDS + LISTENER,
1863
+ SORTED_ROW_IDS + LISTENER,
1864
+ VOID,
1865
+ getListenerDoc(13, 3, 1),
1866
+ getListenerHookParams(
1867
+ sortedRowIdsListenerType,
1868
+ `tableId: ${tableIdType} | null`,
1869
+ 'cellId: ' + cellIdsType + OR_UNDEFINED,
1870
+ 'descending: boolean',
1871
+ 'offset: number',
1872
+ 'limit: number' + OR_UNDEFINED,
1873
+ ),
1874
+ getListenerHookParamsInCall(
1875
+ 'tableId',
1876
+ 'cellId',
1877
+ 'descending',
1878
+ 'offset',
1879
+ 'limit',
1880
+ ),
1881
+ );
1882
+ addProxyHook(
1883
+ ROW + LISTENER,
1884
+ ROW + LISTENER,
1885
+ VOID,
1886
+ getListenerDoc(5, 3),
1887
+ getListenerHookParams(
1888
+ rowListenerType,
1889
+ `tableId: ${tableIdType} | null`,
1890
+ ROW_ID + `: IdOrNull`,
1891
+ ),
1892
+ getListenerHookParamsInCall('tableId', ROW_ID),
1893
+ );
1894
+ addProxyHook(
1895
+ CELL_IDS + LISTENER,
1896
+ CELL_IDS + LISTENER,
1897
+ VOID,
1898
+ getListenerDoc(6, 5, 1),
1899
+ getListenerHookParams(
1900
+ cellIdsListenerType,
1901
+ `tableId: ${tableIdType} | null`,
1902
+ ROW_ID + `: IdOrNull`,
1903
+ ),
1904
+ getListenerHookParamsInCall('tableId', ROW_ID),
1905
+ );
1906
+ addProxyHook(
1907
+ CELL + LISTENER,
1908
+ CELL + LISTENER,
1909
+ VOID,
1910
+ getListenerDoc(7, 5),
1911
+ getListenerHookParams(
1912
+ cellListenerType,
1913
+ `tableId: ${tableIdType} | null`,
1914
+ ROW_ID + `: IdOrNull`,
1915
+ `cellId: ${cellIdsType} | null`,
1916
+ ),
1917
+ getListenerHookParamsInCall('tableId', ROW_ID, 'cellId'),
1918
+ );
1919
+ }
1920
+ if (!objIsEmpty(valuesSchema)) {
1921
+ const [
1922
+ valuesType,
1923
+ valuesWhenSetType,
1924
+ valueIdType,
1925
+ valuesListenerType,
1926
+ valueIdsListenerType,
1927
+ valueListenerType,
1928
+ ] = sharedValueTypes;
1929
+ addImport(null, moduleDefinition, ...sharedValueTypes);
1930
+ addProxyHook(
1931
+ VALUES,
1932
+ VALUES,
1933
+ valuesType,
1934
+ getTheContentOfTheStoreDoc(2, 0) + AND_REGISTERS,
1935
+ );
1936
+ addProxyHook(
1937
+ VALUE_IDS,
1938
+ VALUE_IDS,
1939
+ valueIdType + SQUARE_BRACKETS,
1940
+ getIdsDoc(VALUE, THE_STORE) + AND_REGISTERS,
1941
+ );
1942
+ addProxyHook(
1943
+ SET + VALUES + CALLBACK,
1944
+ SET + VALUES + CALLBACK,
1945
+ PARAMETERIZED_CALLBACK,
1946
+ getTheContentOfTheStoreDoc(2, 9) + BASED_ON_A_PARAMETER,
1947
+ getParameterList(
1948
+ getGet(VALUES) + GETTER_ARGS + valuesWhenSetType,
1949
+ getGet(VALUES) + DEPS_SUFFIX,
1950
+ ),
1951
+ getGetAndGetDeps(VALUES),
1952
+ GENERIC_PARAMETER,
1953
+ getParameterList(
1954
+ THEN_PREFIX,
1955
+ `values: ${valuesWhenSetType})` + RETURNS_VOID,
1956
+ THEN_DEPS,
1957
+ ),
1958
+ THEN_AND_THEN_DEPS_IN_CALL,
1959
+ );
1960
+ addProxyHook(
1961
+ SET + PARTIAL + VALUES + CALLBACK,
1962
+ SET + PARTIAL + VALUES + CALLBACK,
1963
+ PARAMETERIZED_CALLBACK,
1964
+ getTheContentOfTheStoreDoc(2, 11) + BASED_ON_A_PARAMETER,
1965
+ getParameterList(
1966
+ getGet(PARTIAL + VALUES) + GETTER_ARGS + valuesWhenSetType,
1967
+ getGet(PARTIAL + VALUES) + DEPS_SUFFIX,
1968
+ ),
1969
+ getGetAndGetDeps(PARTIAL + VALUES),
1970
+ GENERIC_PARAMETER,
1971
+ getParameterList(
1972
+ THEN_PREFIX,
1973
+ `partialValues: ${valuesWhenSetType})` + RETURNS_VOID,
1974
+ THEN_DEPS,
1975
+ ),
1976
+ THEN_AND_THEN_DEPS_IN_CALL,
1977
+ );
1978
+ addProxyHook(
1979
+ DEL + VALUES + CALLBACK,
1980
+ DEL + VALUES + CALLBACK,
1981
+ CALLBACK,
1982
+ getTheContentOfTheStoreDoc(2, 12),
1983
+ EMPTY_STRING,
1984
+ EMPTY_STRING,
1985
+ EMPTY_STRING,
1986
+ THEN_AND_THEN_DEPS,
1987
+ THEN_AND_THEN_DEPS_IN_CALL,
1988
+ );
1989
+ mapValuesSchema((valueId, type, _, VALUE_ID, valueName) => {
1990
+ const mapValueType = 'Map' + camel(type, 1);
1991
+ addImport(0, moduleDefinition, mapValueType);
1992
+ addImport(1, moduleDefinition, mapValueType);
1993
+ addProxyHook(
1994
+ valueName + VALUE,
1995
+ VALUE,
1996
+ type,
1997
+ getValueContentDoc(valueId) + AND_REGISTERS,
1998
+ EMPTY_STRING,
1999
+ VALUE_ID,
2000
+ );
2001
+ addProxyHook(
2002
+ SET + valueName + VALUE + CALLBACK,
2003
+ SET + VALUE + CALLBACK,
2004
+ PARAMETERIZED_CALLBACK,
2005
+ getValueContentDoc(valueId, 9) + BASED_ON_A_PARAMETER,
2006
+ getParameterList(
2007
+ getGet(VALUE) + GETTER_ARGS + type + ' | ' + mapValueType,
2008
+ getGet(VALUE) + DEPS_SUFFIX,
2009
+ ),
2010
+ getParameterList(VALUE_ID, getGetAndGetDeps(VALUE)),
2011
+ GENERIC_PARAMETER,
2012
+ getParameterList(
2013
+ THEN_PREFIX,
2014
+ `value: ${type} | ${mapValueType})` + RETURNS_VOID,
2015
+ THEN_DEPS,
2016
+ ),
2017
+ THEN_AND_THEN_DEPS_IN_CALL,
2018
+ );
2019
+ addProxyHook(
2020
+ DEL + valueName + VALUE + CALLBACK,
2021
+ DEL + VALUE + CALLBACK,
2022
+ CALLBACK,
2023
+ getValueContentDoc(valueId, 12),
2024
+ EMPTY_STRING,
2025
+ VALUE_ID,
2026
+ EMPTY_STRING,
2027
+ THEN_AND_THEN_DEPS,
2028
+ THEN_AND_THEN_DEPS_IN_CALL,
2029
+ );
2030
+ });
2031
+ addProxyHook(
2032
+ VALUES + LISTENER,
2033
+ VALUES + LISTENER,
2034
+ VOID,
2035
+ getTheContentOfTheStoreDoc(2, 8) + ' changes',
2036
+ getListenerHookParams(valuesListenerType),
2037
+ getListenerHookParamsInCall(),
2038
+ );
2039
+ addProxyHook(
2040
+ VALUE_IDS + LISTENER,
2041
+ VALUE_IDS + LISTENER,
2042
+ VOID,
2043
+ getListenerDoc(10, 0, 1),
2044
+ getListenerHookParams(valueIdsListenerType),
2045
+ getListenerHookParamsInCall(),
2046
+ );
2047
+ addProxyHook(
2048
+ VALUE + LISTENER,
2049
+ VALUE + LISTENER,
2050
+ VOID,
2051
+ getListenerDoc(11, 0),
2052
+ getListenerHookParams(
2053
+ valueListenerType,
2054
+ `valueId: ${valueIdType} | null`,
2055
+ ),
2056
+ getListenerHookParamsInCall('valueId'),
2057
+ );
2058
+ }
2059
+ addComponent(
2060
+ 'Provider',
2061
+ `{${storeInstance}, ${storeInstance}ById, children}: ` +
2062
+ providerPropsType +
2063
+ ' & {children: React.ReactNode}',
2064
+ [
2065
+ USE_CONTEXT,
2066
+ 'return (',
2067
+ '<Context.Provider',
2068
+ 'value={useMemo(',
2069
+ `() => [${storeInstance} ?? contextValue[0], {...contextValue[1], ...${storeInstance}ById}],`,
2070
+ `[${storeInstance}, ${storeInstance}ById, contextValue],`,
2071
+ ')}>',
2072
+ '{children}',
2073
+ '</Context.Provider>',
2074
+ ');',
2075
+ ],
2076
+ 'Wraps part of an application in a context that provides default objects to be used by hooks and components within',
2077
+ );
2078
+ return [
2079
+ build(...getImports(0), ...getTypes(), ...getFunctions(0)),
2080
+ build(...getImports(1), ...getConstants(), ...getFunctions(1)),
2081
+ ];
2082
+ };
2083
+
2084
+ const getStoreApi = (tablesSchema, valuesSchema, module) => {
2085
+ if (objIsEmpty(tablesSchema) && objIsEmpty(valuesSchema)) {
2086
+ return [EMPTY_STRING, EMPTY_STRING, EMPTY_STRING, EMPTY_STRING];
2087
+ }
2088
+ const [dTsCore, tsCore, sharedTableTypes, sharedValueTypes] = getStoreCoreApi(
2089
+ tablesSchema,
2090
+ valuesSchema,
2091
+ module,
2092
+ );
2093
+ return [
2094
+ dTsCore,
2095
+ tsCore,
2096
+ ...getStoreUiReactApi(
2097
+ tablesSchema,
2098
+ valuesSchema,
2099
+ module,
2100
+ sharedTableTypes,
2101
+ sharedValueTypes,
1195
2102
  ),
1196
2103
  ];
1197
2104
  };
@@ -1295,14 +2202,17 @@ const createTools = getCreateFunction((store) => {
1295
2202
  const getStoreApi$1 = (module) =>
1296
2203
  getStoreApi(getStoreTablesSchema(), getStoreValuesSchema(), module);
1297
2204
  const getPrettyStoreApi = async (module) => {
2205
+ const extensions = ['d.ts', 'ts', 'd.ts', 'tsx'];
1298
2206
  let format;
1299
2207
  try {
1300
2208
  format = (await import('prettier')).format;
1301
- } catch {
2209
+ } catch (e) {
1302
2210
  format = (str) => str;
1303
2211
  }
1304
- return arrayMap(getStoreApi$1(module), (file) =>
1305
- formatJsDoc(format(file, prettierConfig)),
2212
+ return arrayMap(getStoreApi$1(module), (file, f) =>
2213
+ formatJsDoc(
2214
+ format(file, {...prettierConfig, filepath: `_.${extensions[f]}`}),
2215
+ ),
1306
2216
  );
1307
2217
  };
1308
2218
  const getStore = () => store;