@rljson/rljson 0.0.80 → 0.0.82

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.
@@ -140,6 +140,9 @@ German (`de`):
140
140
  }
141
141
  ```
142
142
 
143
+ In TypeScript, the interface `Row` describes a row. Once hashed, a row carries
144
+ its hash in the field `_hash`.
145
+
143
146
  ### Column Data Types
144
147
 
145
148
  Columns can contain any of the Rljson-supported data types: `string | number |
@@ -190,6 +193,19 @@ table `b` references table `a` using `aRef`:
190
193
 
191
194
  This reference structure enables automated denormalization of JSON data.
192
195
 
196
+ In TypeScript, `ref(row)` returns the hash that `hip` has written into a row,
197
+ ready to be stored in a reference column. `rowOf(table, hash)` follows the
198
+ reference back to the row:
199
+
200
+ ```typescript
201
+ import { hip } from '@rljson/hash';
202
+ import { ref, rowOf } from '@rljson/rljson';
203
+
204
+ const tableA = hip({ _type: 'components', _data: [{ a: 10 }] });
205
+ const rowB = { tableARef: ref(tableA._data[0]) };
206
+ rowOf(tableA, rowB.tableARef); // returns the row { a: 10, _hash: '...' }
207
+ ```
208
+
193
209
  ## Data Types
194
210
 
195
211
  Rljson provides several core data structures and table types to manage and
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
+ - [Row, ref and rowOf](#row-ref-and-rowof)
40
41
  - [TimeId](#timeid)
41
42
  - [RemoveDuplicates](#removeduplicates)
42
43
  - [Ecosystem](#ecosystem)
@@ -388,6 +389,28 @@ const onConflict: ConflictCallback = (conflict: Conflict) => {
388
389
 
389
390
  ## Utilities
390
391
 
392
+ ### Row, ref and rowOf
393
+
394
+ A `Row` is a row of an Rljson table: a JSON object whose `_hash` is written by
395
+ `hip`. `ref(row)` returns that hash, the reference other rows store to point to
396
+ the row. `rowOf(table, hash)` follows a reference back to its row. Both throw
397
+ when there is nothing to return: `ref` for a row that has not been hashed,
398
+ `rowOf` for a hash the table does not contain.
399
+
400
+ ```typescript
401
+ import { hip } from '@rljson/hash';
402
+ import { ComponentsTable, ref, rowOf } from '@rljson/rljson';
403
+
404
+ const ingredients = hip<ComponentsTable<{ id: string }>>({
405
+ _type: 'components',
406
+ _data: [{ id: 'flour' }, { id: 'sugar' }],
407
+ });
408
+
409
+ const [flour] = ingredients._data;
410
+ const recipe = { ingredientsRef: ref(flour) }; // refers to the flour row
411
+ rowOf(ingredients, recipe.ingredientsRef); // returns the flour row
412
+ ```
413
+
391
414
  ### TimeId
392
415
 
393
416
  A unique, time-based identifier in the format `"timestamp:xxxx"`.
@@ -140,6 +140,9 @@ German (`de`):
140
140
  }
141
141
  ```
142
142
 
143
+ In TypeScript, the interface `Row` describes a row. Once hashed, a row carries
144
+ its hash in the field `_hash`.
145
+
143
146
  ### Column Data Types
144
147
 
145
148
  Columns can contain any of the Rljson-supported data types: `string | number |
@@ -190,6 +193,19 @@ table `b` references table `a` using `aRef`:
190
193
 
191
194
  This reference structure enables automated denormalization of JSON data.
192
195
 
196
+ In TypeScript, `ref(row)` returns the hash that `hip` has written into a row,
197
+ ready to be stored in a reference column. `rowOf(table, hash)` follows the
198
+ reference back to the row:
199
+
200
+ ```typescript
201
+ import { hip } from '@rljson/hash';
202
+ import { ref, rowOf } from '@rljson/rljson';
203
+
204
+ const tableA = hip({ _type: 'components', _data: [{ a: 10 }] });
205
+ const rowB = { tableARef: ref(tableA._data[0]) };
206
+ rowOf(tableA, rowB.tableARef); // returns the row { a: 10, _hash: '...' }
207
+ ```
208
+
193
209
  ## Data Types
194
210
 
195
211
  Rljson provides several core data structures and table types to manage and
@@ -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
+ - [Row, ref and rowOf](#row-ref-and-rowof)
40
41
  - [TimeId](#timeid)
41
42
  - [RemoveDuplicates](#removeduplicates)
42
43
  - [Ecosystem](#ecosystem)
@@ -388,6 +389,28 @@ const onConflict: ConflictCallback = (conflict: Conflict) => {
388
389
 
389
390
  ## Utilities
390
391
 
392
+ ### Row, ref and rowOf
393
+
394
+ A `Row` is a row of an Rljson table: a JSON object whose `_hash` is written by
395
+ `hip`. `ref(row)` returns that hash, the reference other rows store to point to
396
+ the row. `rowOf(table, hash)` follows a reference back to its row. Both throw
397
+ when there is nothing to return: `ref` for a row that has not been hashed,
398
+ `rowOf` for a hash the table does not contain.
399
+
400
+ ```typescript
401
+ import { hip } from '@rljson/hash';
402
+ import { ComponentsTable, ref, rowOf } from '@rljson/rljson';
403
+
404
+ const ingredients = hip<ComponentsTable<{ id: string }>>({
405
+ _type: 'components',
406
+ _data: [{ id: 'flour' }, { id: 'sugar' }],
407
+ });
408
+
409
+ const [flour] = ingredients._data;
410
+ const recipe = { ingredientsRef: ref(flour) }; // refers to the flour row
411
+ rowOf(ingredients, recipe.ingredientsRef); // returns the flour row
412
+ ```
413
+
391
414
  ### TimeId
392
415
 
393
416
  A unique, time-based identifier in the format `"timestamp:xxxx"`.
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 './tools/ref.ts';
26
27
  export * from './tools/remove-duplicates.ts';
27
28
  export * from './tools/time-id.ts';
28
29
  export * from './typedefs.ts';
package/dist/rljson.js CHANGED
@@ -405,6 +405,12 @@ var bakeryExample = () => {
405
405
  meta: { description: "A tasty cake recipe" },
406
406
  children: null
407
407
  });
408
+ const recipesTreeRoot = hip({
409
+ id: "root",
410
+ isParent: true,
411
+ meta: { description: "Root of the recipes tree" },
412
+ children: [recipesTreeChildren._hash]
413
+ });
408
414
  return {
409
415
  buffets,
410
416
  cakes,
@@ -418,12 +424,7 @@ var bakeryExample = () => {
418
424
  ingredientsInsertHistory,
419
425
  recipesTreeTable: hip({
420
426
  _type: "trees",
421
- _data: [hip({
422
- id: "root",
423
- isParent: true,
424
- meta: { description: "Root of the recipes tree" },
425
- children: [recipesTreeChildren._hash]
426
- }), recipesTreeChildren],
427
+ _data: [recipesTreeRoot, recipesTreeChildren],
427
428
  _hash: ""
428
429
  })
429
430
  };
@@ -678,39 +679,43 @@ var treeFromObject = (obj, skipRootCreation = false) => {
678
679
  if (processedIds.has(nodeId)) return;
679
680
  processedIds.add(nodeId);
680
681
  const childIds = [];
681
- if (Array.isArray(value)) if (value.length === 0 || value.every((item) => item !== null && typeof item === "object" && !Array.isArray(item) && Object.keys(item).length === 1)) {
682
- for (const item of value) {
683
- const childId = Object.keys(item)[0];
684
- childIds.push(childId);
685
- processNode(item[childId], childId);
682
+ if (Array.isArray(value)) {
683
+ if (value.length === 0 || value.every((item) => item !== null && typeof item === "object" && !Array.isArray(item) && Object.keys(item).length === 1)) {
684
+ for (const item of value) {
685
+ const childId = Object.keys(item)[0];
686
+ childIds.push(childId);
687
+ processNode(item[childId], childId);
688
+ }
689
+ /* v8 ignore next -- @preserve */
690
+ const treeNode = {
691
+ id: nodeId,
692
+ isParent: true,
693
+ meta: null,
694
+ children: childIds.length > 0 ? childIds.map((id) => idToHashMap.get(id)) : null
695
+ };
696
+ const hashedNode = hip(treeNode);
697
+ idToHashMap.set(nodeId, hashedNode._hash);
698
+ result.push(hashedNode);
699
+ } else {
700
+ const hashedNode = hip({
701
+ id: nodeId,
702
+ isParent: false,
703
+ meta: { value },
704
+ children: null
705
+ });
706
+ idToHashMap.set(nodeId, hashedNode._hash);
707
+ result.push(hashedNode);
686
708
  }
687
- const hashedNode = hip({
688
- id: nodeId,
689
- isParent: true,
690
- meta: null,
691
- children: childIds.length > 0 ? childIds.map((id) => idToHashMap.get(id)) : null
692
- });
693
- idToHashMap.set(nodeId, hashedNode._hash);
694
- result.push(hashedNode);
695
- } else {
696
- const hashedNode = hip({
697
- id: nodeId,
698
- isParent: false,
699
- meta: { value },
700
- children: null
701
- });
702
- idToHashMap.set(nodeId, hashedNode._hash);
703
- result.push(hashedNode);
704
- }
705
- else if (value !== null && typeof value === "object") {
709
+ } else if (value !== null && typeof value === "object") {
706
710
  const keys = Object.keys(value);
707
711
  if (keys.includes("meta") || keys.includes("isParent") || keys.includes("children")) {
708
- const hashedNode = hip({
712
+ const treeNode = {
709
713
  id: nodeId,
710
714
  isParent: false,
711
715
  meta: value.meta,
712
716
  children: null
713
- });
717
+ };
718
+ const hashedNode = hip(treeNode);
714
719
  idToHashMap.set(nodeId, hashedNode._hash);
715
720
  result.push(hashedNode);
716
721
  } else {
@@ -720,12 +725,14 @@ var treeFromObject = (obj, skipRootCreation = false) => {
720
725
  childIds.push(key);
721
726
  processNode(value[key], key);
722
727
  }
723
- const hashedNode = hip({
728
+ /* v8 ignore next -- @preserve */
729
+ const treeNode = {
724
730
  id: nodeId,
725
731
  isParent: true,
726
732
  meta: null,
727
733
  children: childIds.length > 0 ? childIds.map((id) => idToHashMap.get(id)) : null
728
- });
734
+ };
735
+ const hashedNode = hip(treeNode);
729
736
  idToHashMap.set(nodeId, hashedNode._hash);
730
737
  result.push(hashedNode);
731
738
  }
@@ -751,12 +758,13 @@ var treeFromObject = (obj, skipRootCreation = false) => {
751
758
  }
752
759
  }
753
760
  if (!skipRootCreation && topLevelIds.length > 0) {
754
- const hashedRootNode = hip({
761
+ const rootNode = {
755
762
  id: "root",
756
763
  isParent: true,
757
764
  meta: null,
758
765
  children: topLevelIds.map((id) => idToHashMap.get(id))
759
- });
766
+ };
767
+ const hashedRootNode = hip(rootNode);
760
768
  idToHashMap.set("root", hashedRootNode._hash);
761
769
  result.push(hashedRootNode);
762
770
  }
@@ -1102,13 +1110,14 @@ var Example = class Example {
1102
1110
  _type: "layers",
1103
1111
  _data: [abLayer0, abLayer1]
1104
1112
  });
1113
+ const cake = hip({
1114
+ sliceIdsTable: "sliceIds",
1115
+ sliceIdsRow: sliceIds._data[0]._hash,
1116
+ layers: { abLayers: abLayer1._hash }
1117
+ });
1105
1118
  const cakes = hip({
1106
1119
  _type: "cakes",
1107
- _data: [hip({
1108
- sliceIdsTable: "sliceIds",
1109
- sliceIdsRow: sliceIds._data[0]._hash,
1110
- layers: { abLayers: abLayer1._hash }
1111
- })]
1120
+ _data: [cake]
1112
1121
  });
1113
1122
  return {
1114
1123
  sliceIds,
@@ -2013,6 +2022,36 @@ var syncEvents = (route) => ({
2013
2022
  bootstrap: `${route}:bootstrap`
2014
2023
  });
2015
2024
  //#endregion
2025
+ //#region src/tools/ref.ts
2026
+ // @license
2027
+ /**
2028
+ * Returns the reference to a row: the hash that `hip` has written into it.
2029
+ *
2030
+ * Other rows refer to the row by this reference.
2031
+ * @param row - A row hashed with `hip` from `@rljson/hash`
2032
+ * @returns The hash of the row
2033
+ * @throws Error if the row has no hash
2034
+ */
2035
+ var ref = (row) => {
2036
+ const hash = row._hash;
2037
+ if (!hash) throw new Error("ref: The row has no _hash. Hash it with hip first.");
2038
+ return hash;
2039
+ };
2040
+ /**
2041
+ * Follows a reference: returns the row of a table that has the given hash.
2042
+ *
2043
+ * Searches the rows one by one.
2044
+ * @param table - The table to search
2045
+ * @param hash - The reference to the row: its hash
2046
+ * @returns The row with the given hash
2047
+ * @throws Error if the table has no row with the given hash
2048
+ */
2049
+ var rowOf = (table, hash) => {
2050
+ const row = table._data.find((row) => row._hash === hash);
2051
+ if (!row) throw new Error(`rowOf: The table has no row with hash "${hash}".`);
2052
+ return row;
2053
+ };
2054
+ //#endregion
2016
2055
  //#region src/tools/remove-duplicates.ts
2017
2056
  // @license
2018
2057
  var removeDuplicates = (rljson) => {
@@ -2803,4 +2842,4 @@ var Validate = class {
2803
2842
  //#region src/index.ts
2804
2843
  // @license
2805
2844
  //#endregion
2806
- 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, removeDuplicates, reservedFieldNames, reservedTableKeys, routeRefSeperator, routeSliceIdIndicators, routeSliceIdSeperator, syncConfigCausalOnlyExample, syncConfigDefault, syncConfigFullExample, syncEvents, throwOnInvalidTableCfg, timeId, treeFromObject, validateInsert, validateRljsonAgainstTableCfg };
2845
+ 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, ref, removeDuplicates, reservedFieldNames, reservedTableKeys, routeRefSeperator, routeSliceIdIndicators, routeSliceIdSeperator, rowOf, syncConfigCausalOnlyExample, syncConfigDefault, syncConfigFullExample, syncEvents, throwOnInvalidTableCfg, timeId, treeFromObject, validateInsert, validateRljsonAgainstTableCfg };
@@ -0,0 +1,2 @@
1
+ /** The version of the `@rljson/rljson` package. */
2
+ export declare const rljsonVersion = "0.0.82";
@@ -0,0 +1,21 @@
1
+ import { RljsonTable } from '../rljson.ts';
2
+ import { ContentType, Ref, Row } from '../typedefs.ts';
3
+ /**
4
+ * Returns the reference to a row: the hash that `hip` has written into it.
5
+ *
6
+ * Other rows refer to the row by this reference.
7
+ * @param row - A row hashed with `hip` from `@rljson/hash`
8
+ * @returns The hash of the row
9
+ * @throws Error if the row has no hash
10
+ */
11
+ export declare const ref: (row: Row) => Ref;
12
+ /**
13
+ * Follows a reference: returns the row of a table that has the given hash.
14
+ *
15
+ * Searches the rows one by one.
16
+ * @param table - The table to search
17
+ * @param hash - The reference to the row: its hash
18
+ * @returns The row with the given hash
19
+ * @throws Error if the table has no row with the given hash
20
+ */
21
+ export declare const rowOf: <T extends Row>(table: RljsonTable<T, ContentType>, hash: Ref) => T;
@@ -3,6 +3,17 @@ import { Json, JsonKey } from '@rljson/json';
3
3
  * A ref is a hash that references to another element
4
4
  */
5
5
  export type Ref = string;
6
+ /**
7
+ * A row of an Rljson table: a JSON object that maps column names to values.
8
+ *
9
+ * `hip` from `@rljson/hash` writes the hash of the row into `_hash`. The hash
10
+ * is the primary key of the row: other rows refer to the row by it. `ref(row)`
11
+ * reads it.
12
+ */
13
+ export interface Row extends Json {
14
+ /** The hash of the row, written by `hip` */
15
+ _hash?: Ref;
16
+ }
6
17
  /**
7
18
  * An `id` is a *user defined* name or identifier of an slice.
8
19
  * It exists in parallel with the auto generated `_hash`.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rljson/rljson",
3
- "version": "0.0.80",
3
+ "version": "0.0.82",
4
4
  "description": "The RLJSON data format specification",
5
5
  "homepage": "https://github.com/rljson/rljson",
6
6
  "bugs": "https://github.com/rljson/rljson/issues",
@@ -20,30 +20,30 @@
20
20
  ],
21
21
  "type": "module",
22
22
  "devDependencies": {
23
- "@types/node": "^26.1.0",
24
- "@typescript-eslint/eslint-plugin": "^8.62.1",
25
- "@typescript-eslint/parser": "^8.62.1",
26
- "@vitest/coverage-v8": "^4.1.9",
23
+ "@types/node": "^26.6.3",
24
+ "@typescript-eslint/eslint-plugin": "^8.70.1",
25
+ "@typescript-eslint/parser": "^8.70.1",
26
+ "@vitest/coverage-v8": "^5.0.2",
27
27
  "cross-env": "^10.1.0",
28
- "eslint": "^10.6.0",
29
- "eslint-plugin-jsdoc": "^63.0.11",
30
- "eslint-plugin-tsdoc": "^0.5.2",
31
- "globals": "^17.7.0",
28
+ "eslint": "^10.11.0",
29
+ "eslint-plugin-jsdoc": "^64.5.4",
30
+ "eslint-plugin-tsdoc": "^0.5.3",
31
+ "globals": "^17.12.0",
32
32
  "jsdoc": "^4.0.5",
33
33
  "read-pkg": "^10.1.0",
34
34
  "typescript": "~6.0.3",
35
- "typescript-eslint": "^8.62.1",
36
- "vite": "^8.1.3",
35
+ "typescript-eslint": "^8.70.1",
36
+ "vite": "^8.3.1",
37
37
  "vite-node": "^6.0.0",
38
- "vite-plugin-dts": "^5.0.3",
38
+ "vite-plugin-dts": "^5.1.1",
39
39
  "vite-tsconfig-paths": "^6.1.1",
40
- "vitest": "^4.1.9",
40
+ "vitest": "^5.0.2",
41
41
  "vitest-dom": "^0.1.1"
42
42
  },
43
43
  "dependencies": {
44
- "@rljson/hash": "^0.0.19",
45
- "@rljson/json": "^0.0.23",
46
- "nanoid": "^5.1.16"
44
+ "@rljson/hash": "0.0.19",
45
+ "@rljson/json": "0.0.23",
46
+ "nanoid": "^6.0.1"
47
47
  },
48
48
  "scripts": {
49
49
  "build": "pnpm test && vite build && tsc && cp README* dist && mkdir dist/src && cp src/example.ts dist/src",