@rljson/rljson 0.0.83 → 0.0.85

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.
@@ -504,7 +504,8 @@ objects:
504
504
  - **References**: All `*Ref` columns must point to existing rows
505
505
  - **Trees**: No cycles, all child hashes must resolve, correct node types
506
506
  - **Layers**: Component and SliceId tables must exist
507
- - **Cakes**: Layer tables must exist and share the same slice structure
507
+ - **Cakes**: Layer tables and layers must exist, and every layer must use the
508
+ slice ids table and row of its cake
508
509
  - **Buffets**: Referenced items must exist in their declared tables
509
510
 
510
511
  The `BaseValidator` provides an extensible validator framework — multiple
package/README.public.md CHANGED
@@ -37,6 +37,7 @@ Core types, validation, and sync protocol for the RLJSON data format.
37
37
  - [SyncEventNames](#synceventnames)
38
38
  - [ClientId](#clientid)
39
39
  - [Utilities](#utilities)
40
+ - [Tables](#tables)
40
41
  - [Row, ref and rowOf](#row-ref-and-rowof)
41
42
  - [TimeId](#timeid)
42
43
  - [RemoveDuplicates](#removeduplicates)
@@ -389,6 +390,27 @@ const onConflict: ConflictCallback = (conflict: Conflict) => {
389
390
 
390
391
  ## Utilities
391
392
 
393
+ ### Tables
394
+
395
+ `Tables` is a read-only view on the tables of an Rljson dump. By default it
396
+ hides insert history tables and tables whose key starts with `_`.
397
+
398
+ ```ts
399
+ import { Tables } from '@rljson/rljson';
400
+
401
+ const tables = new Tables(rljson);
402
+ tables.ls(); // ['cars', 'tableCfgs'] — sorted table keys
403
+ tables.ls({ internal: true }); // also insert history and `_` tables
404
+ tables.ls({ long: true }); // aligned lines: 'cars components 2'
405
+ tables.all; // the visible tables in ls() order
406
+ tables.count; // number of visible tables
407
+ tables.get('cars'); // the table or undefined
408
+ tables.has('carsInsertHistory'); // true, also for internal tables
409
+ tables.ofType('components'); // sorted keys of that type, internal included
410
+ tables.cfg('cars'); // the latest TableCfg of the table
411
+ tables.rowCount('cars'); // 2
412
+ ```
413
+
392
414
  ### Row, ref and rowOf
393
415
 
394
416
  A `Row` is a row of an Rljson table: a JSON object whose `_hash` is written by
@@ -504,7 +504,8 @@ objects:
504
504
  - **References**: All `*Ref` columns must point to existing rows
505
505
  - **Trees**: No cycles, all child hashes must resolve, correct node types
506
506
  - **Layers**: Component and SliceId tables must exist
507
- - **Cakes**: Layer tables must exist and share the same slice structure
507
+ - **Cakes**: Layer tables and layers must exist, and every layer must use the
508
+ slice ids table and row of its cake
508
509
  - **Buffets**: Referenced items must exist in their declared tables
509
510
 
510
511
  The `BaseValidator` provides an extensible validator framework — multiple
@@ -37,6 +37,7 @@ Core types, validation, and sync protocol for the RLJSON data format.
37
37
  - [SyncEventNames](#synceventnames)
38
38
  - [ClientId](#clientid)
39
39
  - [Utilities](#utilities)
40
+ - [Tables](#tables)
40
41
  - [Row, ref and rowOf](#row-ref-and-rowof)
41
42
  - [TimeId](#timeid)
42
43
  - [RemoveDuplicates](#removeduplicates)
@@ -389,6 +390,27 @@ const onConflict: ConflictCallback = (conflict: Conflict) => {
389
390
 
390
391
  ## Utilities
391
392
 
393
+ ### Tables
394
+
395
+ `Tables` is a read-only view on the tables of an Rljson dump. By default it
396
+ hides insert history tables and tables whose key starts with `_`.
397
+
398
+ ```ts
399
+ import { Tables } from '@rljson/rljson';
400
+
401
+ const tables = new Tables(rljson);
402
+ tables.ls(); // ['cars', 'tableCfgs'] — sorted table keys
403
+ tables.ls({ internal: true }); // also insert history and `_` tables
404
+ tables.ls({ long: true }); // aligned lines: 'cars components 2'
405
+ tables.all; // the visible tables in ls() order
406
+ tables.count; // number of visible tables
407
+ tables.get('cars'); // the table or undefined
408
+ tables.has('carsInsertHistory'); // true, also for internal tables
409
+ tables.ofType('components'); // sorted keys of that type, internal included
410
+ tables.cfg('cars'); // the latest TableCfg of the table
411
+ tables.rowCount('cars'); // 2
412
+ ```
413
+
392
414
  ### Row, ref and rowOf
393
415
 
394
416
  A `Row` is a row of an Rljson table: a JSON object whose `_hash` is written by
package/dist/example.d.ts CHANGED
@@ -47,6 +47,7 @@ export declare class Example {
47
47
  missingSliceIdSet: () => Rljson;
48
48
  missingLayersTable: () => Rljson;
49
49
  missingCakeLayer: () => Rljson;
50
+ layerWithOtherSliceIds: () => Rljson;
50
51
  };
51
52
  buffets: {
52
53
  missingTable: () => Rljson;
package/dist/index.d.ts CHANGED
@@ -23,6 +23,7 @@ export * from './sync/connector-payload.ts';
23
23
  export * from './sync/gap-fill.ts';
24
24
  export * from './sync/sync-config.ts';
25
25
  export * from './sync/sync-events.ts';
26
+ export * from './tables/tables.ts';
26
27
  export * from './tools/ref.ts';
27
28
  export * from './tools/resolve-slice-ids.ts';
28
29
  export * from './tools/remove-duplicates.ts';
package/dist/rljson.js CHANGED
@@ -1435,6 +1435,30 @@ var Example = class Example {
1435
1435
  throwOnWrongHashes: false
1436
1436
  });
1437
1437
  return result;
1438
+ },
1439
+ layerWithOtherSliceIds: () => {
1440
+ const result = Example.ok.complete();
1441
+ result.sliceIds._data.push({ add: [
1442
+ "id0",
1443
+ "id1",
1444
+ "id2"
1445
+ ] });
1446
+ hip(result.sliceIds, {
1447
+ updateExistingHashes: true,
1448
+ throwOnWrongHashes: false
1449
+ });
1450
+ const cake = result.cakes._data[0];
1451
+ cake.sliceIdsRow = result.sliceIds._data[1]._hash;
1452
+ hip(result.cakes, {
1453
+ updateExistingHashes: true,
1454
+ throwOnWrongHashes: false
1455
+ });
1456
+ result.buffets._data[0].items[0].ref = cake._hash;
1457
+ hip(result.buffets, {
1458
+ updateExistingHashes: true,
1459
+ throwOnWrongHashes: false
1460
+ });
1461
+ return result;
1438
1462
  }
1439
1463
  },
1440
1464
  buffets: {
@@ -2022,6 +2046,91 @@ var syncEvents = (route) => ({
2022
2046
  bootstrap: `${route}:bootstrap`
2023
2047
  });
2024
2048
  //#endregion
2049
+ //#region src/tables/tables.ts
2050
+ // @license
2051
+ /**
2052
+ * Read only view on the tables of an Rljson dump.
2053
+ *
2054
+ * By default insert history tables and tables whose key starts with `_`
2055
+ * are hidden. Pass `internal: true` to ls() to include them.
2056
+ */
2057
+ var Tables = class Tables {
2058
+ /**
2059
+ * Creates a view on the tables of the Rljson
2060
+ * @param rljson - the Rljson dump to wrap
2061
+ */
2062
+ constructor(rljson) {
2063
+ this._rljson = rljson;
2064
+ }
2065
+ _rljson;
2066
+ /** The wrapped Rljson */
2067
+ get rljson() {
2068
+ return this._rljson;
2069
+ }
2070
+ /**
2071
+ * The sorted table keys.
2072
+ * @param options - see TablesLsOptions
2073
+ * @returns table keys, or aligned lines `name type rowCount` with `long`
2074
+ */
2075
+ ls(options = {}) {
2076
+ const keys = this._keys(options.internal === true);
2077
+ if (!options.long) return keys;
2078
+ const nameWidth = Math.max(0, ...keys.map((k) => k.length));
2079
+ const typeWidth = Math.max(0, ...keys.map((k) => this.get(k)._type.length));
2080
+ return keys.map((k) => `${k.padEnd(nameWidth)} ${this.get(k)._type.padEnd(typeWidth)} ${this.rowCount(k)}`);
2081
+ }
2082
+ /** The visible tables in ls() order */
2083
+ get all() {
2084
+ return this.ls().map((k) => this.get(k));
2085
+ }
2086
+ /** The number of visible tables */
2087
+ get count() {
2088
+ return this.ls().length;
2089
+ }
2090
+ /**
2091
+ * The table with the given key or undefined
2092
+ * @param key - the table key
2093
+ */
2094
+ get(key) {
2095
+ const table = this._rljson[key];
2096
+ return Tables._isTable(table) ? table : void 0;
2097
+ }
2098
+ /**
2099
+ * True if a table with the key exists, including internal tables
2100
+ * @param key - the table key
2101
+ */
2102
+ has(key) {
2103
+ return this.get(key) !== void 0;
2104
+ }
2105
+ /**
2106
+ * The sorted keys of all tables with the given type, including internal
2107
+ * @param type - the content type
2108
+ */
2109
+ ofType(type) {
2110
+ return this._keys(true).filter((k) => this.get(k)._type === type);
2111
+ }
2112
+ /**
2113
+ * The latest table config for the key, or undefined
2114
+ * @param key - the table key
2115
+ */
2116
+ cfg(key) {
2117
+ return (this.get("tableCfgs")?._data ?? []).filter((c) => c.key === key).pop();
2118
+ }
2119
+ /**
2120
+ * The number of rows of the table, 0 if it does not exist
2121
+ * @param key - the table key
2122
+ */
2123
+ rowCount(key) {
2124
+ return this.get(key)?._data.length ?? 0;
2125
+ }
2126
+ _keys(internal) {
2127
+ return Object.keys(this._rljson).filter((k) => this.has(k)).filter((k) => internal || !k.startsWith("_") && this.get(k)._type !== "insertHistory").sort();
2128
+ }
2129
+ static _isTable(value) {
2130
+ return typeof value === "object" && value !== null && typeof value._type === "string" && Array.isArray(value._data);
2131
+ }
2132
+ };
2133
+ //#endregion
2025
2134
  //#region src/tools/ref.ts
2026
2135
  // @license
2027
2136
  /**
@@ -2273,6 +2382,7 @@ var _BaseValidator = class {
2273
2382
  () => this._cakeSliceIdsTableNotFound(),
2274
2383
  () => this._cakeSliceIdsNotFound(),
2275
2384
  () => this._cakeLayerTablesNotFound(),
2385
+ () => this._cakeLayerSliceIdsDoNotMatch(),
2276
2386
  () => this._buffetReferencedTableNotFound()
2277
2387
  ];
2278
2388
  for (const step of steps) {
@@ -2808,6 +2918,33 @@ var _BaseValidator = class {
2808
2918
  brokenCakes: missingCakeLayers
2809
2919
  };
2810
2920
  }
2921
+ _cakeLayerSliceIdsDoNotMatch() {
2922
+ const brokenCakes = [];
2923
+ iterateTablesSync(this.rljson, (tableKey, table) => {
2924
+ if (table._type !== "cakes") return;
2925
+ const cakesTable = table;
2926
+ for (const cake of cakesTable._data) for (const layersTableKey in cake.layers) {
2927
+ if (layersTableKey.startsWith("_")) continue;
2928
+ const layersTable = this.rljsonIndexed[layersTableKey];
2929
+ const layerRef = cake.layers[layersTableKey];
2930
+ const layer = layersTable._data[layerRef];
2931
+ if (layer.sliceIdsTable !== cake.sliceIdsTable || layer.sliceIdsTableRow !== cake.sliceIdsRow) brokenCakes.push({
2932
+ cakeTable: tableKey,
2933
+ brokenCake: cake._hash,
2934
+ layersTable: layersTableKey,
2935
+ brokenLayer: layerRef,
2936
+ cakeSliceIdsTable: cake.sliceIdsTable,
2937
+ cakeSliceIdsRow: cake.sliceIdsRow,
2938
+ layerSliceIdsTable: layer.sliceIdsTable,
2939
+ layerSliceIdsRow: layer.sliceIdsTableRow
2940
+ });
2941
+ }
2942
+ });
2943
+ if (brokenCakes.length > 0) this.errors.cakeLayerSliceIdsDoNotMatch = {
2944
+ error: "Layers of cakes do not use the slice ids of the cake",
2945
+ brokenCakes
2946
+ };
2947
+ }
2811
2948
  _buffetReferencedTableNotFound() {
2812
2949
  const missingTables = [];
2813
2950
  const missingItems = [];
@@ -2880,4 +3017,4 @@ var Validate = class {
2880
3017
  //#region src/index.ts
2881
3018
  // @license
2882
3019
  //#endregion
2883
- export { BaseValidator, Example, InsertValidator, Route, Validate, ackPayloadExample, ackPayloadPartialExample, addColumnsToTableCfg, bakeryExample, clientId, clientIdExample, connectorPayloadExample, connectorPayloadFullExample, contentTypes, createCakeTableCfg, createEditHistoryTableCfg, createEditTableCfg, createInsertHistoryTableCfg, createLayerTableCfg, createMultiEditTableCfg, createSliceIdsTableCfg, createTreesTableCfg, exampleBuffetsTable, exampleCakesTable, exampleComponentsTable, exampleInsert, exampleInsertHistoryTable, exampleLayersTable, exampleRevision, exampleRljson, exampleSliceIdsTable, exampleTableCfg, exampleTableCfgTable, exampleTreesTable, exampleTypedefs, gapFillRequestExample, gapFillResponseExample, getTimeIdTimestamp, getTimeIdUniquePart, isClientId, isTimeId, isValidFieldName, iterateTables, iterateTablesSync, mergeSliceIds, ref, removeDuplicates, reservedFieldNames, reservedTableKeys, resolveSliceIds, routeRefSeperator, routeSliceIdIndicators, routeSliceIdSeperator, rowOf, syncConfigCausalOnlyExample, syncConfigDefault, syncConfigFullExample, syncEvents, throwOnInvalidTableCfg, timeId, treeFromObject, validateInsert, validateRljsonAgainstTableCfg };
3020
+ export { BaseValidator, Example, InsertValidator, Route, Tables, Validate, ackPayloadExample, ackPayloadPartialExample, addColumnsToTableCfg, bakeryExample, clientId, clientIdExample, connectorPayloadExample, connectorPayloadFullExample, contentTypes, createCakeTableCfg, createEditHistoryTableCfg, createEditTableCfg, createInsertHistoryTableCfg, createLayerTableCfg, createMultiEditTableCfg, createSliceIdsTableCfg, createTreesTableCfg, exampleBuffetsTable, exampleCakesTable, exampleComponentsTable, exampleInsert, exampleInsertHistoryTable, exampleLayersTable, exampleRevision, exampleRljson, exampleSliceIdsTable, exampleTableCfg, exampleTableCfgTable, exampleTreesTable, exampleTypedefs, gapFillRequestExample, gapFillResponseExample, getTimeIdTimestamp, getTimeIdUniquePart, isClientId, isTimeId, isValidFieldName, iterateTables, iterateTablesSync, mergeSliceIds, ref, removeDuplicates, reservedFieldNames, reservedTableKeys, resolveSliceIds, routeRefSeperator, routeSliceIdIndicators, routeSliceIdSeperator, rowOf, syncConfigCausalOnlyExample, syncConfigDefault, syncConfigFullExample, syncEvents, throwOnInvalidTableCfg, timeId, treeFromObject, validateInsert, validateRljsonAgainstTableCfg };
@@ -1,2 +1,2 @@
1
1
  /** The version of the `@rljson/rljson` package. */
2
- export declare const rljsonVersion = "0.0.83";
2
+ export declare const rljsonVersion = "0.0.85";
@@ -17,7 +17,6 @@ import { exampleTreesTable, TreesTable } from './content/tree.ts';
17
17
  import { bakeryExample } from './example/bakery-example.ts';
18
18
  import { Rljson } from './rljson.ts';
19
19
 
20
-
21
20
  export class Example {
22
21
  static readonly ok = {
23
22
  bakery: (): Rljson => bakeryExample(),
@@ -890,6 +889,32 @@ export class Example {
890
889
  });
891
890
  return result;
892
891
  },
892
+
893
+ layerWithOtherSliceIds: (): Rljson => {
894
+ const result = Example.ok.complete();
895
+
896
+ // Let the cake use another row of slice ids than its layers
897
+ result.sliceIds._data.push({ add: ['id0', 'id1', 'id2'] });
898
+ hip(result.sliceIds, {
899
+ updateExistingHashes: true,
900
+ throwOnWrongHashes: false,
901
+ });
902
+
903
+ const cake = result.cakes._data[0];
904
+ cake.sliceIdsRow = result.sliceIds._data[1]._hash;
905
+ hip(result.cakes, {
906
+ updateExistingHashes: true,
907
+ throwOnWrongHashes: false,
908
+ });
909
+
910
+ // Let the buffet refer to the changed cake
911
+ result.buffets._data[0].items[0].ref = cake._hash;
912
+ hip(result.buffets, {
913
+ updateExistingHashes: true,
914
+ throwOnWrongHashes: false,
915
+ });
916
+ return result;
917
+ },
893
918
  },
894
919
 
895
920
  buffets: {
@@ -0,0 +1,63 @@
1
+ import { TableCfg } from '../content/table-cfg.ts';
2
+ import { Rljson, TableType } from '../rljson.ts';
3
+ import { ContentType, TableKey } from '../typedefs.ts';
4
+ /** Options for Tables.ls */
5
+ export interface TablesLsOptions {
6
+ /** Return aligned lines `name type rowCount` instead of names */
7
+ long?: boolean;
8
+ /** Include insert history tables and tables starting with `_` */
9
+ internal?: boolean;
10
+ }
11
+ /**
12
+ * Read only view on the tables of an Rljson dump.
13
+ *
14
+ * By default insert history tables and tables whose key starts with `_`
15
+ * are hidden. Pass `internal: true` to ls() to include them.
16
+ */
17
+ export declare class Tables {
18
+ /**
19
+ * Creates a view on the tables of the Rljson
20
+ * @param rljson - the Rljson dump to wrap
21
+ */
22
+ constructor(rljson: Rljson);
23
+ private readonly _rljson;
24
+ /** The wrapped Rljson */
25
+ get rljson(): Rljson;
26
+ /**
27
+ * The sorted table keys.
28
+ * @param options - see TablesLsOptions
29
+ * @returns table keys, or aligned lines `name type rowCount` with `long`
30
+ */
31
+ ls(options?: TablesLsOptions): string[];
32
+ /** The visible tables in ls() order */
33
+ get all(): TableType[];
34
+ /** The number of visible tables */
35
+ get count(): number;
36
+ /**
37
+ * The table with the given key or undefined
38
+ * @param key - the table key
39
+ */
40
+ get(key: TableKey): TableType | undefined;
41
+ /**
42
+ * True if a table with the key exists, including internal tables
43
+ * @param key - the table key
44
+ */
45
+ has(key: TableKey): boolean;
46
+ /**
47
+ * The sorted keys of all tables with the given type, including internal
48
+ * @param type - the content type
49
+ */
50
+ ofType(type: ContentType): TableKey[];
51
+ /**
52
+ * The latest table config for the key, or undefined
53
+ * @param key - the table key
54
+ */
55
+ cfg(key: TableKey): TableCfg | undefined;
56
+ /**
57
+ * The number of rows of the table, 0 if it does not exist
58
+ * @param key - the table key
59
+ */
60
+ rowCount(key: TableKey): number;
61
+ private _keys;
62
+ private static _isTable;
63
+ }
@@ -32,6 +32,7 @@ export interface BaseErrors extends Errors {
32
32
  cakeSliceIdsNotFound?: Json;
33
33
  cakeLayerTablesNotFound?: Json;
34
34
  cakeLayersNotFound?: Json;
35
+ cakeLayerSliceIdsDoNotMatch?: Json;
35
36
  buffetReferencedTablesNotFound?: Json;
36
37
  buffetReferencedItemsNotFound?: Json;
37
38
  }
package/package.json CHANGED
@@ -1,7 +1,6 @@
1
1
  {
2
2
  "name": "@rljson/rljson",
3
- "version": "0.0.83",
4
- "packageManager": "pnpm@11.10.0+sha512.0b7f8b98060031904c017e3a41eb187a16d40eeb829b95c4f8cb03681761fc4ab53dd219115b9b447f4dce1a05a214764461e7d3703392a9f32f9511ce8c86c8",
3
+ "version": "0.0.85",
5
4
  "description": "The RLJSON data format specification",
6
5
  "homepage": "https://github.com/rljson/rljson",
7
6
  "bugs": "https://github.com/rljson/rljson/issues",
@@ -20,37 +19,36 @@
20
19
  "dist"
21
20
  ],
22
21
  "type": "module",
23
- "scripts": {
24
- "build": "pnpm test && vite build && tsc && cp README* dist && mkdir dist/src && cp src/example.ts dist/src",
25
- "test": "vitest run --coverage && npm run lint",
26
- "prepublishOnly": "npm run build",
27
- "lint": "eslint",
28
- "updateGoldens": "cross-env UPDATE_GOLDENS=true npm test"
29
- },
30
22
  "devDependencies": {
31
- "@types/node": "^26.6.3",
32
- "@typescript-eslint/eslint-plugin": "^8.70.1",
33
- "@typescript-eslint/parser": "^8.70.1",
34
- "@vitest/coverage-v8": "^5.0.2",
23
+ "@types/node": "^26.6.4",
24
+ "@typescript-eslint/eslint-plugin": "^8.71.1",
25
+ "@typescript-eslint/parser": "^8.71.1",
26
+ "@vitest/coverage-v8": "^5.0.3",
35
27
  "cross-env": "^10.1.0",
36
- "eslint": "^10.11.0",
37
- "eslint-plugin-jsdoc": "^64.5.4",
38
- "eslint-plugin-tsdoc": "^0.5.3",
39
- "globals": "^17.12.0",
28
+ "eslint": "^10.12.0",
29
+ "eslint-plugin-jsdoc": "^65.2.0",
30
+ "eslint-plugin-tsdoc": "^0.5.4",
31
+ "globals": "^17.13.0",
40
32
  "jsdoc": "^4.0.5",
41
33
  "read-pkg": "^10.1.0",
42
34
  "typescript": "~6.0.3",
43
- "typescript-eslint": "^8.70.1",
44
- "vite": "^8.3.1",
35
+ "typescript-eslint": "^8.71.1",
36
+ "vite": "^8.3.4",
45
37
  "vite-node": "^6.0.0",
46
- "vite-plugin-dts": "^5.1.1",
38
+ "vite-plugin-dts": "^5.1.2",
47
39
  "vite-tsconfig-paths": "^6.1.1",
48
- "vitest": "^5.0.2",
40
+ "vitest": "^5.0.3",
49
41
  "vitest-dom": "^0.1.1"
50
42
  },
51
43
  "dependencies": {
52
44
  "@rljson/hash": "0.0.19",
53
45
  "@rljson/json": "0.0.23",
54
- "nanoid": "^6.0.1"
46
+ "nanoid": "^6.0.2"
47
+ },
48
+ "scripts": {
49
+ "build": "pnpm test && vite build && tsc && cp README* dist && mkdir dist/src && cp src/example.ts dist/src",
50
+ "test": "vitest run --coverage && npm run lint",
51
+ "lint": "eslint",
52
+ "updateGoldens": "cross-env UPDATE_GOLDENS=true npm test"
55
53
  }
56
- }
54
+ }