@rljson/io 0.0.22 → 0.0.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,2 +1,4 @@
1
1
  export * from './io-mem.ts';
2
+ export * from './io-tools.ts';
2
3
  export * from './io.ts';
4
+ export * from './reverse-ref.ts';
package/dist/io-mem.d.ts CHANGED
@@ -29,7 +29,6 @@ export declare class IoMem implements Io {
29
29
  private _ioTools;
30
30
  private _isReady;
31
31
  private _mem;
32
- private _ioInit;
33
32
  private _init;
34
33
  private _initTableCfgs;
35
34
  private _createOrExtendTable;
@@ -10,6 +10,14 @@ export declare class IoTools {
10
10
  * @param io The Io interface to use
11
11
  */
12
12
  constructor(io: Io);
13
+ /**
14
+ * Initializes the revisions table.
15
+ */
16
+ initRevisionsTable: () => Promise<void>;
17
+ /**
18
+ * Returns the table configuration of the tableCfgs table.
19
+ */
20
+ get tableCfgsTableCfg(): TableCfg;
13
21
  /**
14
22
  * Example object for test purposes
15
23
  * @returns An instance of io tools
@@ -18,7 +26,7 @@ export declare class IoTools {
18
26
  /**
19
27
  * Returns a list with all table names
20
28
  */
21
- allTableNames(): Promise<string[]>;
29
+ allTableKeys(): Promise<string[]>;
22
30
  /**
23
31
  * Returns the configuration of a given table
24
32
  */
package/dist/io.js CHANGED
@@ -4,17 +4,25 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
4
4
  import { hip, hsh } from "@rljson/hash";
5
5
  import { IsReady } from "@rljson/is-ready";
6
6
  import { jsonValueTypes, copy, equals } from "@rljson/json";
7
+ import { iterateTablesSync } from "@rljson/rljson";
7
8
  // @license
8
- class IoInit {
9
+ const _IoTools = class _IoTools {
10
+ /**
11
+ * Constructor
12
+ * @param io The Io interface to use
13
+ */
9
14
  constructor(io) {
15
+ /**
16
+ * Initializes the revisions table.
17
+ */
10
18
  __publicField(this, "initRevisionsTable", async () => {
11
19
  const tableCfg = {
12
20
  version: 1,
13
21
  key: "revisions",
14
22
  type: "ingredients",
15
- isHead: false,
16
- isRoot: false,
17
- isShared: true,
23
+ isHead: true,
24
+ isRoot: true,
25
+ isShared: false,
18
26
  columns: [
19
27
  { key: "table", type: "string" },
20
28
  { key: "predecessor", type: "string" },
@@ -27,35 +35,35 @@ class IoInit {
27
35
  });
28
36
  this.io = io;
29
37
  }
30
- get tableCfg() {
38
+ /**
39
+ * Returns the table configuration of the tableCfgs table.
40
+ */
41
+ get tableCfgsTableCfg() {
31
42
  const tableCfg = hip({
32
- version: 1,
43
+ _hash: "",
33
44
  key: "tableCfgs",
34
45
  type: "ingredients",
35
46
  isHead: false,
36
47
  isRoot: false,
37
48
  isShared: true,
49
+ version: 1,
38
50
  columns: [
51
+ { key: "_hash", type: "string" },
39
52
  { key: "key", type: "string" },
40
- { key: "type", type: "string" }
53
+ { key: "type", type: "string" },
54
+ { key: "isHead", type: "boolean" },
55
+ { key: "isRoot", type: "boolean" },
56
+ { key: "isShared", type: "boolean" },
57
+ { key: "version", type: "number" },
58
+ { key: "columns", type: "jsonArray" }
41
59
  ]
42
60
  });
43
61
  return tableCfg;
44
62
  }
45
- }
46
- // @license
47
- const _IoTools = class _IoTools {
48
- /**
49
- * Constructor
50
- * @param io The Io interface to use
51
- */
52
- constructor(io) {
53
- this.io = io;
54
- }
55
63
  /**
56
64
  * Returns a list with all table names
57
65
  */
58
- async allTableNames() {
66
+ async allTableKeys() {
59
67
  const result = (await this.io.tableCfgs()).tableCfgs._data.map(
60
68
  (e) => e.key
61
69
  );
@@ -156,10 +164,9 @@ const _IoMem = class _IoMem {
156
164
  __publicField(this, "_ioTools");
157
165
  __publicField(this, "_isReady", new IsReady());
158
166
  __publicField(this, "_mem", hip({}));
159
- __publicField(this, "_ioInit");
160
167
  // ...........................................................................
161
168
  __publicField(this, "_initTableCfgs", () => {
162
- const tableCfg = this._ioInit.tableCfg;
169
+ const tableCfg = this._ioTools.tableCfgsTableCfg;
163
170
  this._mem.tableCfgs = hip({
164
171
  _data: [tableCfg],
165
172
  _type: "ingredients",
@@ -226,9 +233,8 @@ const _IoMem = class _IoMem {
226
233
  // ...........................................................................
227
234
  async _init() {
228
235
  this._ioTools = new IoTools(this);
229
- this._ioInit = new IoInit(this);
230
236
  this._initTableCfgs();
231
- await this._ioInit.initRevisionsTable();
237
+ await this._ioTools.initRevisionsTable();
232
238
  this._isReady.resolve();
233
239
  }
234
240
  // ...........................................................................
@@ -328,7 +334,111 @@ __publicField(_IoMem, "example", async () => {
328
334
  let IoMem = _IoMem;
329
335
  // @license
330
336
  const exampleIo = "Checkout @rljson/io-mem for an example implementation";
337
+ // @license
338
+ const calcReverseRefs = (rljson) => {
339
+ const result = {};
340
+ iterateTablesSync(rljson, (childTableKey, table) => {
341
+ const childTable = {};
342
+ result[childTableKey] = childTable;
343
+ for (const childRow of table._data) {
344
+ childTable[childRow._hash] = {};
345
+ }
346
+ });
347
+ iterateTablesSync(rljson, (parentTableKey, parentTable) => {
348
+ for (const parentTableRow of parentTable._data) {
349
+ switch (parentTable._type) {
350
+ case "ingredients":
351
+ _writeIngredientRefs(parentTableKey, parentTableRow, result);
352
+ break;
353
+ case "layers": {
354
+ _writeLayerRefs(parentTableKey, parentTableRow, result);
355
+ break;
356
+ }
357
+ case "sliceIds": {
358
+ break;
359
+ }
360
+ case "cakes": {
361
+ _writeCakeRefs(parentTableKey, parentTableRow, result);
362
+ break;
363
+ }
364
+ case "buffets": {
365
+ _writeBuffetRefs(parentTableKey, parentTableRow, result);
366
+ break;
367
+ }
368
+ }
369
+ }
370
+ });
371
+ return result;
372
+ };
373
+ const _writeIngredientRefs = (parentTableName, parentRow, result) => {
374
+ const parentRowHash = parentRow._hash;
375
+ for (const parentColumnName in parentRow) {
376
+ if (parentColumnName.startsWith("_")) {
377
+ continue;
378
+ }
379
+ if (!parentColumnName.endsWith("Ref")) {
380
+ continue;
381
+ }
382
+ const childTableName = parentColumnName.slice(0, -3);
383
+ const childRowHash = parentRow[parentColumnName];
384
+ _write(
385
+ result,
386
+ childTableName,
387
+ childRowHash,
388
+ parentTableName,
389
+ parentRowHash
390
+ );
391
+ }
392
+ };
393
+ const _writeLayerRefs = (parentTableName, parentRow, result) => {
394
+ const childTableName = parentRow.ingredientsTable;
395
+ const parentRowHash = parentRow._hash;
396
+ for (const sliceId in parentRow.assign) {
397
+ if (sliceId.startsWith("_")) {
398
+ continue;
399
+ }
400
+ const sliceHash = parentRow.assign[sliceId];
401
+ _write(result, childTableName, sliceHash, parentTableName, parentRowHash);
402
+ }
403
+ };
404
+ const _writeCakeRefs = (parentTableName, parentRow, result) => {
405
+ const parentRowHash = parentRow._hash;
406
+ for (const layer in parentRow.layers) {
407
+ const childTableName = parentRow.layersTable;
408
+ const childRowHash = parentRow.layers[layer];
409
+ _write(
410
+ result,
411
+ childTableName,
412
+ childRowHash,
413
+ parentTableName,
414
+ parentRowHash
415
+ );
416
+ }
417
+ };
418
+ const _writeBuffetRefs = (parentTableName, parentRow, result) => {
419
+ const parentRowHash = parentRow._hash;
420
+ for (const item of parentRow.items) {
421
+ const childTableName = item.table;
422
+ const childRowHash = item.ref;
423
+ _write(
424
+ result,
425
+ childTableName,
426
+ childRowHash,
427
+ parentTableName,
428
+ parentRowHash
429
+ );
430
+ }
431
+ };
432
+ const _write = (result, childTableName, childRowHash, parentTableName, parentRowHash) => {
433
+ var _a;
434
+ const referencesForChildTable = result[childTableName] ?? (result[childTableName] = {});
435
+ const referencesForChildTableRow = referencesForChildTable[childRowHash] ?? (referencesForChildTable[childRowHash] = {});
436
+ referencesForChildTableRow[parentTableName] ?? (referencesForChildTableRow[parentTableName] = {});
437
+ (_a = referencesForChildTableRow[parentTableName])[parentRowHash] ?? (_a[parentRowHash] = {});
438
+ };
331
439
  export {
332
440
  IoMem,
441
+ IoTools,
442
+ calcReverseRefs,
333
443
  exampleIo
334
444
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rljson/io",
3
- "version": "0.0.22",
3
+ "version": "0.0.23",
4
4
  "packageManager": "pnpm@10.6.2",
5
5
  "description": "Low level interface for reading and writing RLJSON data",
6
6
  "homepage": "https://github.com/rljson/io",
@@ -56,10 +56,10 @@
56
56
  "overrides": {}
57
57
  },
58
58
  "dependencies": {
59
- "@rljson/hash": "^0.0.13",
59
+ "@rljson/hash": "^0.0.15",
60
60
  "@rljson/is-ready": "^0.0.17",
61
- "@rljson/json": "^0.0.18",
62
- "@rljson/rljson": "^0.0.40",
61
+ "@rljson/json": "^0.0.21",
62
+ "@rljson/rljson": "^0.0.42",
63
63
  "@rljson/validate": "^0.0.10"
64
64
  }
65
65
  }
package/dist/io-init.d.ts DELETED
@@ -1,11 +0,0 @@
1
- import { TableCfg } from '@rljson/rljson';
2
- import { Io } from './io.ts';
3
- /**
4
- * Initialization tools for Io
5
- */
6
- export declare class IoInit {
7
- readonly io: Io;
8
- constructor(io: Io);
9
- get tableCfg(): TableCfg;
10
- initRevisionsTable: () => Promise<void>;
11
- }