orange-orm 4.2.0-beta.4 → 4.2.0-beta.6

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "orange-orm",
3
- "version": "4.2.0-beta.4",
3
+ "version": "4.2.0-beta.6",
4
4
  "main": "./src/index.js",
5
5
  "browser": "./src/client/index.mjs",
6
6
  "bin": {
@@ -1,9 +1,22 @@
1
1
  let dateToISOString = require('../dateToISOString');
2
2
 
3
3
  function cloneFromDbFast(obj) {
4
- if (obj === null || typeof obj !== 'object') return obj;
5
- if (Array.isArray(obj)) return obj.map(cloneFromDbFast);
6
- return Object.fromEntries(Object.entries(obj).map(([key, value]) => [key, cloneFromDbFast(value)]));
4
+ if (obj === null || typeof obj !== 'object')
5
+ return obj;
6
+ if (Array.isArray(obj)) {
7
+ const arrClone = [];
8
+ for (let i = 0; i < obj.length; i++) {
9
+ arrClone[i] = cloneFromDbFast(obj[i]);
10
+ }
11
+ return arrClone;
12
+ }
13
+ const clone = {};
14
+ const keys = Object.keys(obj);
15
+ for (let i = 0; i < keys.length; i++) {
16
+ const key = keys[i];
17
+ clone[key] = cloneFromDbFast(obj[key]);
18
+ }
19
+ return clone;
7
20
  }
8
21
 
9
22
  function cloneRegular(obj) {
@@ -483,9 +483,11 @@ function rdbClient(options = {}) {
483
483
  }
484
484
 
485
485
  };
486
- let innerProxy = new Proxy(array, handler);
487
- //todo
488
- rootMap.set(array, { json: cloneFromDb(array, fast), strategy, originalArray: [...array] });
486
+
487
+ let watcher = onChange(array, () => {
488
+ rootMap.set(array, { json: cloneFromDb(array, fast), strategy, originalArray: [...array] });
489
+ });
490
+ let innerProxy = new Proxy(watcher, handler);
489
491
  if (strategy !== undefined) {
490
492
  const { limit, ...cleanStrategy } = { ...strategy };
491
493
  fetchingStrategyMap.set(array, cleanStrategy);
@@ -517,8 +519,10 @@ function rdbClient(options = {}) {
517
519
  }
518
520
 
519
521
  };
520
- let innerProxy = new Proxy(row, handler);
521
- rootMap.set(row, { json: cloneFromDb(row, fast), strategy });
522
+ let watcher = onChange(row, () => {
523
+ rootMap.set(row, { json: cloneFromDb(row, fast), strategy });
524
+ });
525
+ let innerProxy = new Proxy(watcher, handler);
522
526
  fetchingStrategyMap.set(row, strategy);
523
527
  return innerProxy;
524
528
  }
@@ -588,7 +592,9 @@ function rdbClient(options = {}) {
588
592
 
589
593
  async function saveArray(array, concurrencyOptions, strategy) {
590
594
  let deduceStrategy = false;
591
- let { json } = rootMap.get(array);
595
+ let json = rootMap.get(array)?.json;
596
+ if (!json)
597
+ return;
592
598
  strategy = extractStrategy({ strategy }, array);
593
599
  strategy = extractFetchingStrategy(array, strategy);
594
600
 
@@ -668,7 +674,7 @@ function rdbClient(options = {}) {
668
674
  return options.strategy;
669
675
  if (obj) {
670
676
  let context = rootMap.get(obj);
671
- if (context.strategy !== undefined) {
677
+ if (context?.strategy !== undefined) {
672
678
  // @ts-ignore
673
679
  let { limit, ...strategy } = { ...context.strategy };
674
680
  return strategy;
@@ -687,13 +693,17 @@ function rdbClient(options = {}) {
687
693
  }
688
694
 
689
695
  function clearChangesArray(array) {
690
- let { json } = rootMap.get(array);
696
+ let json = rootMap.get(array)?.json;
697
+ if (!json)
698
+ return;
691
699
  let old = cloneFromDb(json);
692
700
  array.splice(0, old.length, ...old);
693
701
  }
694
702
 
695
703
  function acceptChangesArray(array) {
696
704
  const map = rootMap.get(array);
705
+ if (!map)
706
+ return;
697
707
  map.json = cloneFromDb(array);
698
708
  map.originalArray = [...array];
699
709
  }
@@ -791,7 +801,7 @@ function rdbClient(options = {}) {
791
801
  strategy = extractStrategy({ strategy }, row);
792
802
  strategy = extractFetchingStrategy(row, strategy);
793
803
 
794
- let { json } = rootMap.get(row);
804
+ let json = rootMap.get(row)?.json;
795
805
  if (!json)
796
806
  return;
797
807
  let meta = await getMeta();
@@ -834,12 +844,15 @@ function rdbClient(options = {}) {
834
844
  }
835
845
 
836
846
  function acceptChangesRow(row) {
837
- const { strategy } = rootMap.get(row);
847
+ const data = rootMap.get(row);
848
+ if (!data)
849
+ return;
850
+ const { strategy } = data;
838
851
  rootMap.set(row, { json: cloneFromDb(row), strategy });
839
852
  }
840
853
 
841
854
  function clearChangesRow(row) {
842
- let { json } = rootMap.get(row);
855
+ let json = rootMap.get(row)?.json;
843
856
  if (!json)
844
857
  return;
845
858
  let old = cloneFromDb(json);
@@ -1004,4 +1017,36 @@ function column(path, ...previous) {
1004
1017
 
1005
1018
  }
1006
1019
 
1020
+ function onChange(target, onChange) {
1021
+
1022
+ let notified = false;
1023
+ const handler = {
1024
+ get(target, prop, receiver) {
1025
+ const value = Reflect.get(target, prop, receiver);
1026
+ if (typeof value === 'object' && value !== null) {
1027
+ return new Proxy(value, handler);
1028
+ }
1029
+ return value;
1030
+ },
1031
+ set(target, prop, value, receiver) {
1032
+ if (!notified) {
1033
+ notified = true;
1034
+ onChange(JSON.stringify(target));
1035
+ }
1036
+ return Reflect.set(target, prop, value, receiver);
1037
+
1038
+ },
1039
+ deleteProperty(target, prop) {
1040
+ if (!notified) {
1041
+ notified = true;
1042
+ onChange(JSON.stringify(target));
1043
+ }
1044
+ return Reflect.deleteProperty(target, prop);
1045
+ }
1046
+ };
1047
+
1048
+ return new Proxy(target, handler);
1049
+ }
1050
+
1051
+
1007
1052
  module.exports = rdbClient();