orange-orm 4.2.0-beta.3 → 4.2.0-beta.5

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.3",
3
+ "version": "4.2.0-beta.5",
4
4
  "main": "./src/index.js",
5
5
  "browser": "./src/client/index.mjs",
6
6
  "bin": {
@@ -1,12 +1,31 @@
1
1
  let dateToISOString = require('../dateToISOString');
2
2
 
3
- function cloneFromDb(obj) {
3
+ function cloneFromDbFast(obj) {
4
4
  if (obj === null || typeof obj !== 'object')
5
5
  return obj;
6
6
  if (Array.isArray(obj)) {
7
7
  const arrClone = [];
8
8
  for (let i = 0; i < obj.length; i++) {
9
- arrClone[i] = cloneFromDb(obj[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;
20
+ }
21
+
22
+ function cloneRegular(obj) {
23
+ if (obj === null || typeof obj !== 'object')
24
+ return obj;
25
+ if (Array.isArray(obj)) {
26
+ const arrClone = [];
27
+ for (let i = 0; i < obj.length; i++) {
28
+ arrClone[i] = cloneRegular(obj[i]);
10
29
  }
11
30
  return arrClone;
12
31
  }
@@ -16,10 +35,16 @@ function cloneFromDb(obj) {
16
35
  const keys = Object.keys(obj);
17
36
  for (let i = 0; i < keys.length; i++) {
18
37
  const key = keys[i];
19
- clone[key] = cloneFromDb(obj[key]);
38
+ clone[key] = cloneRegular(obj[key]);
20
39
  }
21
40
  return clone;
22
41
  }
23
42
 
43
+ function cloneFromDb(obj, isFast) {
44
+ if (isFast)
45
+ return cloneFromDbFast(obj);
46
+ else
47
+ return cloneRegular(obj);
48
+ }
24
49
 
25
50
  module.exports = cloneFromDb;
@@ -229,7 +229,7 @@ function rdbClient(options = {}) {
229
229
  let args = [_, strategy].concat(Array.prototype.slice.call(arguments).slice(2));
230
230
  let rows = await getManyCore.apply(null, args);
231
231
  await metaPromise;
232
- return proxify(rows, strategy, {fast: true});
232
+ return proxify(rows, strategy, true);
233
233
  }
234
234
 
235
235
  async function groupBy(strategy) {
@@ -261,7 +261,7 @@ function rdbClient(options = {}) {
261
261
  await metaPromise;
262
262
  if (rows.length === 0)
263
263
  return;
264
- return proxify(rows[0], strategy, {fast: true});
264
+ return proxify(rows[0], strategy, true);
265
265
  }
266
266
 
267
267
  async function getById() {
@@ -449,14 +449,14 @@ function rdbClient(options = {}) {
449
449
  }
450
450
 
451
451
 
452
- function proxify(itemOrArray, strategy, options) {
452
+ function proxify(itemOrArray, strategy, fast) {
453
453
  if (Array.isArray(itemOrArray))
454
- return proxifyArray(itemOrArray, strategy, options);
454
+ return proxifyArray(itemOrArray, strategy, fast);
455
455
  else
456
- return proxifyRow(itemOrArray, strategy, options);
456
+ return proxifyRow(itemOrArray, strategy, fast);
457
457
  }
458
458
 
459
- function proxifyArray(array, strategy, { fast } = { }) {
459
+ function proxifyArray(array, strategy, fast) {
460
460
  let _array = array;
461
461
  if (_reactive)
462
462
  array = _reactive(array);
@@ -483,9 +483,12 @@ function rdbClient(options = {}) {
483
483
  }
484
484
 
485
485
  };
486
- let innerProxy = new Proxy(array, handler);
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);
487
491
  //todo
488
- rootMap.set(array, { json: fast ? structuredClone(array) : cloneFromDb(array), strategy, originalArray: [...array] });
489
492
  if (strategy !== undefined) {
490
493
  const { limit, ...cleanStrategy } = { ...strategy };
491
494
  fetchingStrategyMap.set(array, cleanStrategy);
@@ -493,7 +496,7 @@ function rdbClient(options = {}) {
493
496
  return innerProxy;
494
497
  }
495
498
 
496
- function proxifyRow(row, strategy, { fast } = {}) {
499
+ function proxifyRow(row, strategy, fast) {
497
500
  let handler = {
498
501
  get(_target, property,) {
499
502
  if (property === 'save' || property === 'saveChanges') //call server then acceptChanges
@@ -517,8 +520,10 @@ function rdbClient(options = {}) {
517
520
  }
518
521
 
519
522
  };
520
- let innerProxy = new Proxy(row, handler);
521
- rootMap.set(row, { json: fast ? structuredClone(row) : cloneFromDb(row), strategy });
523
+ let watcher = onChange(row, () => {
524
+ rootMap.set(row, { json: cloneFromDb(row, fast), strategy });
525
+ });
526
+ let innerProxy = new Proxy(watcher, handler);
522
527
  fetchingStrategyMap.set(row, strategy);
523
528
  return innerProxy;
524
529
  }
@@ -1004,4 +1009,31 @@ function column(path, ...previous) {
1004
1009
 
1005
1010
  }
1006
1011
 
1012
+ function onChange(target, onChange) {
1013
+
1014
+ let notified = false;
1015
+ const handler = {
1016
+ get(target, prop, receiver) {
1017
+ const value = Reflect.get(target, prop, receiver);
1018
+ if (typeof value === 'object' && value !== null) {
1019
+ return new Proxy(value, handler);
1020
+ }
1021
+ return value;
1022
+ },
1023
+ set(target, prop, value, receiver) {
1024
+ if (!notified) {
1025
+ notified = true;
1026
+ onChange(JSON.stringify(target));
1027
+ }
1028
+ return Reflect.set(target, prop, value, receiver);
1029
+
1030
+ }
1031
+
1032
+
1033
+ };
1034
+
1035
+ return new Proxy(target, handler);
1036
+ }
1037
+
1038
+
1007
1039
  module.exports = rdbClient();