@rljson/rljson 0.0.64 → 0.0.66

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/rljson.js CHANGED
@@ -131,6 +131,7 @@ class Route {
131
131
  const refKey = Object.keys(segment).find(
132
132
  (k) => k.endsWith("Ref") && k !== "tableKey"
133
133
  );
134
+ /* v8 ignore next -- @preserve */
134
135
  if (refKey) {
135
136
  return segment[refKey];
136
137
  }
@@ -534,26 +535,172 @@ class Example {
534
535
  };
535
536
  },
536
537
  singleRef: () => {
538
+ const tableCfgs = hip({
539
+ _type: "tableCfgs",
540
+ _data: [
541
+ {
542
+ key: "tableA",
543
+ type: "components",
544
+ isHead: false,
545
+ isRoot: false,
546
+ isShared: true,
547
+ columns: [
548
+ {
549
+ key: "_hash",
550
+ type: "string",
551
+ titleLong: "Hash",
552
+ titleShort: "Hash"
553
+ },
554
+ {
555
+ key: "propertyA",
556
+ type: "string",
557
+ titleLong: "Key",
558
+ titleShort: "Key"
559
+ }
560
+ ],
561
+ _hash: ""
562
+ },
563
+ {
564
+ key: "tableB",
565
+ type: "components",
566
+ isHead: false,
567
+ isRoot: false,
568
+ isShared: true,
569
+ columns: [
570
+ {
571
+ key: "_hash",
572
+ type: "string",
573
+ titleLong: "Hash",
574
+ titleShort: "Hash"
575
+ },
576
+ {
577
+ key: "propertyAFromTableA",
578
+ type: "string",
579
+ titleLong: "Table A Reference",
580
+ titleShort: "TableARef",
581
+ ref: {
582
+ tableKey: "tableA",
583
+ columnKey: "propertyA"
584
+ }
585
+ }
586
+ ],
587
+ _hash: ""
588
+ }
589
+ ]
590
+ });
591
+ const tableA = hip({
592
+ _type: "components",
593
+ _tableCfg: tableCfgs._data[0]._hash,
594
+ _data: [
595
+ {
596
+ propertyA: "a0"
597
+ },
598
+ {
599
+ propertyA: "a1"
600
+ }
601
+ ],
602
+ _hash: ""
603
+ });
604
+ const tableB = hip({
605
+ _type: "components",
606
+ _tableCfg: tableCfgs._data[1]._hash,
607
+ _data: [
608
+ {
609
+ propertyAFromTableA: tableA._data[0]._hash
610
+ }
611
+ ],
612
+ _hash: ""
613
+ });
537
614
  return {
538
- tableA: {
539
- _type: "components",
540
- _data: [
541
- {
542
- keyA0: "a0"
543
- },
544
- {
545
- keyA1: "a1"
546
- }
547
- ]
548
- },
549
- tableB: {
550
- _type: "components",
551
- _data: [
552
- {
553
- tableARef: "KFQrf4mEz0UPmUaFHwH4T6"
554
- }
555
- ]
556
- }
615
+ tableCfgs,
616
+ tableA,
617
+ tableB
618
+ };
619
+ },
620
+ multiRef: () => {
621
+ const tableCfgs = hip({
622
+ _type: "tableCfgs",
623
+ _data: [
624
+ {
625
+ key: "tableA",
626
+ type: "components",
627
+ isHead: false,
628
+ isRoot: false,
629
+ isShared: true,
630
+ columns: [
631
+ {
632
+ key: "_hash",
633
+ type: "string",
634
+ titleLong: "Hash",
635
+ titleShort: "Hash"
636
+ },
637
+ {
638
+ key: "propertyA",
639
+ type: "string",
640
+ titleLong: "Key",
641
+ titleShort: "Key"
642
+ }
643
+ ],
644
+ _hash: ""
645
+ },
646
+ {
647
+ key: "tableB",
648
+ type: "components",
649
+ isHead: false,
650
+ isRoot: false,
651
+ isShared: true,
652
+ columns: [
653
+ {
654
+ key: "_hash",
655
+ type: "string",
656
+ titleLong: "Hash",
657
+ titleShort: "Hash"
658
+ },
659
+ {
660
+ key: "propertyAFromTableA",
661
+ type: "jsonValue",
662
+ titleLong: "Table A Reference",
663
+ titleShort: "TableARef",
664
+ ref: {
665
+ tableKey: "tableA",
666
+ columnKey: "propertyA"
667
+ }
668
+ }
669
+ ],
670
+ _hash: ""
671
+ }
672
+ ]
673
+ });
674
+ const tableA = hip({
675
+ _type: "components",
676
+ _tableCfg: tableCfgs._data[0]._hash,
677
+ _data: [
678
+ {
679
+ propertyA: "a0"
680
+ },
681
+ {
682
+ propertyA: "a1"
683
+ }
684
+ ],
685
+ _hash: ""
686
+ });
687
+ const tableB = hip({
688
+ _type: "components",
689
+ _tableCfg: tableCfgs._data[1]._hash,
690
+ _data: [
691
+ {
692
+ propertyAFromTableA: [
693
+ tableA._data[0]._hash,
694
+ tableA._data[1]._hash
695
+ ]
696
+ }
697
+ ],
698
+ _hash: ""
699
+ });
700
+ return {
701
+ tableCfgs,
702
+ tableA,
703
+ tableB
557
704
  };
558
705
  },
559
706
  singleSliceIdRef: () => {
@@ -596,89 +743,6 @@ class Example {
596
743
  }
597
744
  };
598
745
  },
599
- singleNamedRef: () => {
600
- return {
601
- tableA: {
602
- _type: "components",
603
- _data: [
604
- {
605
- keyA0: "a0"
606
- },
607
- {
608
- keyA1: "a1"
609
- }
610
- ]
611
- },
612
- tableB: {
613
- _type: "components",
614
- _data: [
615
- {
616
- namedRef: { component: "tableA", ref: "KFQrf4mEz0UPmUaFHwH4T6" }
617
- }
618
- ]
619
- }
620
- };
621
- },
622
- multiRef: () => {
623
- return {
624
- tableA: {
625
- _type: "components",
626
- _data: [
627
- {
628
- keyA0: "a0"
629
- },
630
- {
631
- keyA1: "a1"
632
- }
633
- ]
634
- },
635
- tableB: {
636
- _type: "components",
637
- _data: [
638
- {
639
- tableARef: ["KFQrf4mEz0UPmUaFHwH4T6", "YPw-pxhqaUOWRFGramr4B1"]
640
- }
641
- ]
642
- }
643
- };
644
- },
645
- multiMixedRef: () => {
646
- return {
647
- tableA: {
648
- _type: "components",
649
- _data: [
650
- {
651
- keyA0: "a0"
652
- },
653
- {
654
- keyA1: "a1"
655
- }
656
- ]
657
- },
658
- tableB: {
659
- _type: "components",
660
- _data: [
661
- {
662
- keyB0: "b0"
663
- },
664
- {
665
- keyB1: "b1"
666
- }
667
- ]
668
- },
669
- tableC: {
670
- _type: "components",
671
- _data: [
672
- {
673
- tableRef: [
674
- { component: "tableA", ref: "KFQrf4mEz0UPmUaFHwH4T6" },
675
- { component: "tableB", ref: "dXhIygNwNMVPEqFbsFJkn6" }
676
- ]
677
- }
678
- ]
679
- }
680
- };
681
- },
682
746
  complete: () => {
683
747
  const sliceIds = hip({
684
748
  _type: "sliceIds",
@@ -776,87 +840,220 @@ class Example {
776
840
  };
777
841
  },
778
842
  missingRef: () => {
843
+ const tableCfgs = hip({
844
+ _type: "tableCfgs",
845
+ _data: [
846
+ {
847
+ key: "tableA",
848
+ type: "components",
849
+ isHead: false,
850
+ isRoot: false,
851
+ isShared: true,
852
+ columns: [
853
+ {
854
+ key: "_hash",
855
+ type: "string",
856
+ titleLong: "Hash",
857
+ titleShort: "Hash"
858
+ },
859
+ {
860
+ key: "propertyA",
861
+ type: "string",
862
+ titleLong: "Key",
863
+ titleShort: "Key"
864
+ }
865
+ ],
866
+ _hash: ""
867
+ },
868
+ {
869
+ key: "tableB",
870
+ type: "components",
871
+ isHead: false,
872
+ isRoot: false,
873
+ isShared: true,
874
+ columns: [
875
+ {
876
+ key: "_hash",
877
+ type: "string",
878
+ titleLong: "Hash",
879
+ titleShort: "Hash"
880
+ },
881
+ {
882
+ key: "propertyAFromTableA",
883
+ type: "jsonValue",
884
+ titleLong: "Table A Reference",
885
+ titleShort: "TableARef",
886
+ ref: {
887
+ tableKey: "tableA",
888
+ columnKey: "propertyA"
889
+ }
890
+ }
891
+ ],
892
+ _hash: ""
893
+ }
894
+ ]
895
+ });
896
+ const tableA = hip({
897
+ _type: "components",
898
+ _tableCfg: tableCfgs._data[0]._hash,
899
+ _data: [
900
+ {
901
+ propertyA: "a0"
902
+ },
903
+ {
904
+ propertyA: "a1"
905
+ }
906
+ ],
907
+ _hash: ""
908
+ });
909
+ const tableB = hip({
910
+ _type: "components",
911
+ _tableCfg: tableCfgs._data[1]._hash,
912
+ _data: [
913
+ {
914
+ propertyAFromTableA: "MISSINGREF"
915
+ // Missing reference
916
+ }
917
+ ],
918
+ _hash: ""
919
+ });
779
920
  return {
780
- tableA: {
781
- _type: "components",
782
- _data: [
783
- {
784
- keyA0: "a0"
785
- },
786
- {
787
- keyA1: "a1"
788
- }
789
- ]
790
- },
791
- tableB: {
792
- _type: "components",
793
- _data: [
794
- {
795
- tableARef: "MISSINGREF"
796
- // MISSINGREF does not exist in tableA
797
- }
798
- ]
799
- }
800
- };
801
- },
802
- missingNamedRef: () => {
803
- return {
804
- tableA: {
805
- _type: "components",
806
- _data: [
807
- {
808
- keyA0: "a0"
809
- },
810
- {
811
- keyA1: "a1"
812
- }
813
- ]
814
- },
815
- tableB: {
816
- _type: "components",
817
- _data: [
818
- {
819
- namedRef: { component: "tableA", ref: "MISSINGREF" }
820
- // MISSINGREF does not exist in tableA
821
- }
822
- ]
823
- }
921
+ tableCfgs,
922
+ tableA,
923
+ tableB
824
924
  };
825
925
  },
826
926
  missingMultiRef: () => {
927
+ const tableCfgs = hip({
928
+ _type: "tableCfgs",
929
+ _data: [
930
+ {
931
+ key: "tableA",
932
+ type: "components",
933
+ isHead: false,
934
+ isRoot: false,
935
+ isShared: true,
936
+ columns: [
937
+ {
938
+ key: "_hash",
939
+ type: "string",
940
+ titleLong: "Hash",
941
+ titleShort: "Hash"
942
+ },
943
+ {
944
+ key: "propertyA",
945
+ type: "string",
946
+ titleLong: "Key",
947
+ titleShort: "Key"
948
+ }
949
+ ],
950
+ _hash: ""
951
+ },
952
+ {
953
+ key: "tableB",
954
+ type: "components",
955
+ isHead: false,
956
+ isRoot: false,
957
+ isShared: true,
958
+ columns: [
959
+ {
960
+ key: "_hash",
961
+ type: "string",
962
+ titleLong: "Hash",
963
+ titleShort: "Hash"
964
+ },
965
+ {
966
+ key: "propertyAFromTableA",
967
+ type: "jsonValue",
968
+ titleLong: "Table A Reference",
969
+ titleShort: "TableARef",
970
+ ref: {
971
+ tableKey: "tableA",
972
+ columnKey: "propertyA"
973
+ }
974
+ }
975
+ ],
976
+ _hash: ""
977
+ }
978
+ ]
979
+ });
980
+ const tableA = hip({
981
+ _type: "components",
982
+ _tableCfg: tableCfgs._data[0]._hash,
983
+ _data: [
984
+ {
985
+ propertyA: "a0"
986
+ },
987
+ {
988
+ propertyA: "a1"
989
+ }
990
+ ],
991
+ _hash: ""
992
+ });
993
+ const tableB = hip({
994
+ _type: "components",
995
+ _tableCfg: tableCfgs._data[1]._hash,
996
+ _data: [
997
+ {
998
+ propertyAFromTableA: [tableA._data[0]._hash, "MISSINGREF"]
999
+ // Missing reference
1000
+ }
1001
+ ],
1002
+ _hash: ""
1003
+ });
827
1004
  return {
828
- tableA: {
829
- _type: "components",
830
- _data: [
831
- {
832
- keyA0: "a0"
833
- },
834
- {
835
- keyA1: "a1"
836
- }
837
- ]
838
- },
839
- tableB: {
840
- _type: "components",
841
- _data: [
842
- {
843
- tableARef: ["KFQrf4mEz0UPmUaFHwH4T6", "MISSING"]
844
- }
845
- ]
846
- }
1005
+ tableCfgs,
1006
+ tableA,
1007
+ tableB
847
1008
  };
848
1009
  },
849
1010
  missingReferencedTable: () => {
1011
+ const tableCfgs = hip({
1012
+ _type: "tableCfgs",
1013
+ _data: [
1014
+ {
1015
+ key: "tableB",
1016
+ type: "components",
1017
+ isHead: false,
1018
+ isRoot: false,
1019
+ isShared: true,
1020
+ columns: [
1021
+ {
1022
+ key: "_hash",
1023
+ type: "string",
1024
+ titleLong: "Hash",
1025
+ titleShort: "Hash"
1026
+ },
1027
+ {
1028
+ key: "propertyAFromTableA",
1029
+ type: "jsonValue",
1030
+ titleLong: "Table A Reference",
1031
+ titleShort: "TableARef",
1032
+ ref: {
1033
+ tableKey: "tableA",
1034
+ // Referenced table missing
1035
+ columnKey: "propertyA"
1036
+ }
1037
+ }
1038
+ ],
1039
+ _hash: ""
1040
+ }
1041
+ ]
1042
+ });
1043
+ const tableB = hip({
1044
+ _type: "components",
1045
+ _tableCfg: tableCfgs._data[0]._hash,
1046
+ _data: [
1047
+ {
1048
+ propertyAFromTableA: "MISSINGREF"
1049
+ // Missing reference
1050
+ }
1051
+ ],
1052
+ _hash: ""
1053
+ });
850
1054
  return {
851
- tableB: {
852
- _type: "components",
853
- _data: [
854
- {
855
- tableARef: "MISSINGREF"
856
- // tableA is missing
857
- }
858
- ]
859
- }
1055
+ tableCfgs,
1056
+ tableB
860
1057
  };
861
1058
  },
862
1059
  missingSliceId: () => {
@@ -1244,6 +1441,12 @@ const validateInsert = (insert) => {
1244
1441
  return InsertValidator.create(insert).validate();
1245
1442
  };
1246
1443
  // @license
1444
+ const exampleInsert = () => ({
1445
+ route: "a/b/c",
1446
+ command: "add",
1447
+ value: { x: { y: { z: true } } }
1448
+ });
1449
+ // @license
1247
1450
  const reservedFieldNames = ["_data"];
1248
1451
  const reservedTableKeys = [
1249
1452
  "_hash",
@@ -1393,7 +1596,6 @@ class _BaseValidator {
1393
1596
  () => this._rootOrHeadTableHasNoIdColumn(),
1394
1597
  // Check references
1395
1598
  () => this._refsNotFound(),
1396
- () => this._sliceIdRefsNotFound(),
1397
1599
  // Check layers
1398
1600
  () => this._layerBasesNotFound(),
1399
1601
  () => this._layerSliceIdsTableNotFound(),
@@ -1488,15 +1690,9 @@ class _BaseValidator {
1488
1690
  throwOnWrongHashes: true
1489
1691
  });
1490
1692
  } catch (error) {
1491
- if (error instanceof Error) {
1492
- this.errors.hashesNotValid = {
1493
- error: error.message
1494
- };
1495
- } else {
1496
- this.errors.hashesNotValid = {
1497
- error: "Unknown error"
1498
- };
1499
- }
1693
+ this.errors.hashesNotValid = {
1694
+ error: error.message
1695
+ };
1500
1696
  }
1501
1697
  }
1502
1698
  // ...........................................................................
@@ -1768,36 +1964,46 @@ class _BaseValidator {
1768
1964
  _refsNotFound() {
1769
1965
  const missingRefs = [];
1770
1966
  iterateTablesSync(this.rljson, (tableKey, table) => {
1771
- const tableData = table._data;
1772
- for (const item of tableData) {
1773
- for (const key of Object.keys(item)) {
1774
- if (key.endsWith("Ref")) {
1775
- const targetItemRefs = Array.isArray(item[key]) ? item[key] : [item[key]];
1776
- for (const targetItemRef of targetItemRefs) {
1777
- const targetTableKey = typeof targetItemRef !== "string" ? targetItemRef.component : key.substring(0, key.length - 3);
1778
- const targetItemHash = typeof targetItemRef !== "string" ? targetItemRef.ref : targetItemRef;
1779
- const itemHash = item._hash;
1967
+ if (tableKey === "tableCfgs") return;
1968
+ const tableCfgRef = table._tableCfg;
1969
+ if (!tableCfgRef) return;
1970
+ const tableCfg = this.rljsonIndexed.tableCfgs._data[tableCfgRef];
1971
+ /* v8 ignore next -- @preserve */
1972
+ if (!tableCfg) return;
1973
+ const tableRows = table._data;
1974
+ for (const row of tableRows) {
1975
+ for (const columnKey of Object.keys(row)) {
1976
+ if (columnKey.startsWith("_")) continue;
1977
+ const columnCfg = tableCfg.columns.find(
1978
+ (col) => col.key === columnKey
1979
+ );
1980
+ /* v8 ignore next -- @preserve */
1981
+ if (columnCfg === void 0) continue;
1982
+ if (columnCfg.ref && columnCfg.ref.tableKey) {
1983
+ const targetTableKey = columnCfg.ref.tableKey;
1984
+ const targetRefs = Array.isArray(row[columnKey]) ? row[columnKey] : [row[columnKey]];
1985
+ for (const targetRef of targetRefs) {
1780
1986
  if (this.tableKeys.indexOf(targetTableKey) === -1) {
1781
1987
  missingRefs.push({
1782
1988
  error: `Target table "${targetTableKey}" not found.`,
1783
1989
  sourceTable: tableKey,
1784
- sourceKey: key,
1785
- sourceItemHash: itemHash,
1786
- targetItemHash,
1990
+ sourceKey: columnKey,
1991
+ sourceItemHash: row._hash,
1992
+ targetItemHash: targetRef,
1787
1993
  targetTable: targetTableKey
1788
1994
  });
1789
1995
  continue;
1790
1996
  }
1791
1997
  const targetTableIndexed = this.rljsonIndexed[targetTableKey];
1792
- const referencedItem = targetTableIndexed._data[targetItemHash];
1998
+ const referencedItem = targetTableIndexed._data[targetRef];
1793
1999
  if (referencedItem === void 0) {
1794
2000
  missingRefs.push({
1795
2001
  sourceTable: tableKey,
1796
- sourceItemHash: itemHash,
1797
- sourceKey: key,
1798
- targetItemHash,
2002
+ sourceItemHash: row._hash,
2003
+ sourceKey: columnKey,
2004
+ targetItemHash: targetRef,
1799
2005
  targetTable: targetTableKey,
1800
- error: `Table "${targetTableKey}" has no item with hash "${targetItemHash}"`
2006
+ error: `Table "${targetTableKey}" has no item with hash "${targetRef}"`
1801
2007
  });
1802
2008
  }
1803
2009
  }
@@ -1813,49 +2019,6 @@ class _BaseValidator {
1813
2019
  }
1814
2020
  }
1815
2021
  // ...........................................................................
1816
- _sliceIdRefsNotFound() {
1817
- const missingSliceIdRefs = [];
1818
- iterateTablesSync(this.rljson, (tableKey, table) => {
1819
- const tableData = table._data;
1820
- for (const item of tableData) {
1821
- for (const key of Object.keys(item)) {
1822
- if (key.endsWith("SliceId")) {
1823
- const targetSliceIds = Array.isArray(item[key]) ? item[key] : [item[key]];
1824
- for (const targetSliceId of targetSliceIds) {
1825
- if (this.tableKeys.indexOf(key) === -1) {
1826
- missingSliceIdRefs.push({
1827
- sourceTable: tableKey,
1828
- targetSliceId,
1829
- targetTable: key,
1830
- error: `Target table "${targetSliceId}" not found.`
1831
- });
1832
- continue;
1833
- }
1834
- const targetSliceIdsTable = this.rljson[key];
1835
- const targetSliceIds2 = targetSliceIdsTable._data.flatMap(
1836
- (d) => [...d.add, ...d.remove ?? []]
1837
- );
1838
- if (targetSliceIds2.indexOf(targetSliceId) === -1) {
1839
- missingSliceIdRefs.push({
1840
- sourceTable: tableKey,
1841
- targetSliceId,
1842
- targetTable: key,
1843
- error: `Table "${key}" has no sliceId "${targetSliceId}"`
1844
- });
1845
- }
1846
- }
1847
- }
1848
- }
1849
- }
1850
- });
1851
- if (missingSliceIdRefs.length > 0) {
1852
- this.errors.refsNotFound = {
1853
- error: "Broken references",
1854
- missingRefs: missingSliceIdRefs
1855
- };
1856
- }
1857
- }
1858
- // ...........................................................................
1859
2022
  _layerBasesNotFound() {
1860
2023
  const brokenLayers = [];
1861
2024
  iterateTablesSync(this.rljson, (tableKey, table) => {
@@ -2219,6 +2382,7 @@ export {
2219
2382
  exampleCakesTable,
2220
2383
  exampleComponentsTable,
2221
2384
  exampleHistoryTable,
2385
+ exampleInsert,
2222
2386
  exampleLayersTable,
2223
2387
  exampleRevision,
2224
2388
  exampleRljson,