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

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.2",
3
+ "version": "4.2.0-beta.4",
4
4
  "main": "./src/index.js",
5
5
  "browser": "./src/client/index.mjs",
6
6
  "bin": {
@@ -1,12 +1,18 @@
1
1
  let dateToISOString = require('../dateToISOString');
2
2
 
3
- function cloneFromDb(obj) {
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)]));
7
+ }
8
+
9
+ function cloneRegular(obj) {
4
10
  if (obj === null || typeof obj !== 'object')
5
11
  return obj;
6
12
  if (Array.isArray(obj)) {
7
13
  const arrClone = [];
8
14
  for (let i = 0; i < obj.length; i++) {
9
- arrClone[i] = cloneFromDb(obj[i]);
15
+ arrClone[i] = cloneRegular(obj[i]);
10
16
  }
11
17
  return arrClone;
12
18
  }
@@ -16,10 +22,16 @@ function cloneFromDb(obj) {
16
22
  const keys = Object.keys(obj);
17
23
  for (let i = 0; i < keys.length; i++) {
18
24
  const key = keys[i];
19
- clone[key] = cloneFromDb(obj[key]);
25
+ clone[key] = cloneRegular(obj[key]);
20
26
  }
21
27
  return clone;
22
28
  }
23
29
 
30
+ function cloneFromDb(obj, isFast) {
31
+ if (isFast)
32
+ return cloneFromDbFast(obj);
33
+ else
34
+ return cloneRegular(obj);
35
+ }
24
36
 
25
37
  module.exports = cloneFromDb;
@@ -1,5 +1,6 @@
1
1
  const createPatch = require('./createPatch');
2
2
  const stringify = require('./stringify');
3
+ const cloneFromDb = require('./cloneFromDb');
3
4
  const netAdapter = require('./netAdapter');
4
5
  const toKeyPositionMap = require('./toKeyPositionMap');
5
6
  const rootMap = new WeakMap();
@@ -228,7 +229,7 @@ function rdbClient(options = {}) {
228
229
  let args = [_, strategy].concat(Array.prototype.slice.call(arguments).slice(2));
229
230
  let rows = await getManyCore.apply(null, args);
230
231
  await metaPromise;
231
- return proxify(rows, strategy, {fastStringify : true});
232
+ return proxify(rows, strategy, true);
232
233
  }
233
234
 
234
235
  async function groupBy(strategy) {
@@ -260,7 +261,7 @@ function rdbClient(options = {}) {
260
261
  await metaPromise;
261
262
  if (rows.length === 0)
262
263
  return;
263
- return proxify(rows[0], strategy, {fastStringify : true});
264
+ return proxify(rows[0], strategy, true);
264
265
  }
265
266
 
266
267
  async function getById() {
@@ -448,14 +449,14 @@ function rdbClient(options = {}) {
448
449
  }
449
450
 
450
451
 
451
- function proxify(itemOrArray, strategy, options) {
452
+ function proxify(itemOrArray, strategy, fast) {
452
453
  if (Array.isArray(itemOrArray))
453
- return proxifyArray(itemOrArray, strategy, options);
454
+ return proxifyArray(itemOrArray, strategy, fast);
454
455
  else
455
- return proxifyRow(itemOrArray, strategy, options);
456
+ return proxifyRow(itemOrArray, strategy, fast);
456
457
  }
457
458
 
458
- function proxifyArray(array, strategy, { fastStringify } = {}) {
459
+ function proxifyArray(array, strategy, fast) {
459
460
  let _array = array;
460
461
  if (_reactive)
461
462
  array = _reactive(array);
@@ -483,7 +484,8 @@ function rdbClient(options = {}) {
483
484
 
484
485
  };
485
486
  let innerProxy = new Proxy(array, handler);
486
- rootMap.set(array, { json: fastStringify ? JSON.stringify(array) : stringify(array), strategy, originalArray: [...array] });
487
+ //todo
488
+ rootMap.set(array, { json: cloneFromDb(array, fast), strategy, originalArray: [...array] });
487
489
  if (strategy !== undefined) {
488
490
  const { limit, ...cleanStrategy } = { ...strategy };
489
491
  fetchingStrategyMap.set(array, cleanStrategy);
@@ -491,18 +493,18 @@ function rdbClient(options = {}) {
491
493
  return innerProxy;
492
494
  }
493
495
 
494
- function proxifyRow(row, strategy, { fastStringify } = {}) {
496
+ function proxifyRow(row, strategy, fast) {
495
497
  let handler = {
496
498
  get(_target, property,) {
497
499
  if (property === 'save' || property === 'saveChanges') //call server then acceptChanges
498
500
  return saveRow.bind(null, row);
499
- else if (property === 'delete') //call server then remove from jsonMap and original
501
+ else if (property === 'delete') //call server then remove from json and original
500
502
  return deleteRow.bind(null, row);
501
503
  else if (property === 'refresh') //refresh from server then acceptChanges
502
504
  return refreshRow.bind(null, row);
503
- else if (property === 'clearChanges') //refresh from jsonMap, update original if present
505
+ else if (property === 'clearChanges') //refresh from json, update original if present
504
506
  return clearChangesRow.bind(null, row);
505
- else if (property === 'acceptChanges') //remove from jsonMap
507
+ else if (property === 'acceptChanges') //remove from json
506
508
  return acceptChangesRow.bind(null, row);
507
509
  else if (property === 'toJSON')
508
510
  return () => {
@@ -516,7 +518,7 @@ function rdbClient(options = {}) {
516
518
 
517
519
  };
518
520
  let innerProxy = new Proxy(row, handler);
519
- rootMap.set(row, { json: fastStringify ? JSON.stringify(row) : stringify(row), strategy });
521
+ rootMap.set(row, { json: cloneFromDb(row, fast), strategy });
520
522
  fetchingStrategyMap.set(row, strategy);
521
523
  return innerProxy;
522
524
  }
@@ -591,7 +593,7 @@ function rdbClient(options = {}) {
591
593
  strategy = extractFetchingStrategy(array, strategy);
592
594
 
593
595
  let meta = await getMeta();
594
- const patch = createPatch(JSON.parse(json), array, meta);
596
+ const patch = createPatch(json, array, meta);
595
597
  if (patch.length === 0)
596
598
  return;
597
599
  let body = stringify({ patch, options: { strategy, ...tableOptions, ...concurrencyOptions, deduceStrategy } });
@@ -606,7 +608,7 @@ function rdbClient(options = {}) {
606
608
  let insertedPositions = getInsertedRowsPosition(array);
607
609
  let { changed, strategy: newStrategy } = await p;
608
610
  copyIntoArray(changed, array, [...insertedPositions, ...updatedPositions]);
609
- rootMap.set(array, { json: stringify(array), strategy: newStrategy, originalArray: [...array] });
611
+ rootMap.set(array, { json: cloneFromDb(array), strategy: newStrategy, originalArray: [...array] });
610
612
  }
611
613
 
612
614
  async function patch(patch, concurrencyOptions, strategy) {
@@ -686,13 +688,13 @@ function rdbClient(options = {}) {
686
688
 
687
689
  function clearChangesArray(array) {
688
690
  let { json } = rootMap.get(array);
689
- let old = JSON.parse(json);
691
+ let old = cloneFromDb(json);
690
692
  array.splice(0, old.length, ...old);
691
693
  }
692
694
 
693
695
  function acceptChangesArray(array) {
694
696
  const map = rootMap.get(array);
695
- map.json = stringify(array);
697
+ map.json = cloneFromDb(array);
696
698
  map.originalArray = [...array];
697
699
  }
698
700
 
@@ -705,7 +707,7 @@ function rdbClient(options = {}) {
705
707
  let adapter = netAdapter(url, tableName, { axios: axiosInterceptor, tableOptions });
706
708
  let { strategy } = await adapter.patch(body);
707
709
  array.length = 0;
708
- rootMap.set(array, { jsonMap: stringify(array), strategy });
710
+ rootMap.set(array, { json: cloneFromDb(array), strategy });
709
711
  }
710
712
 
711
713
  function setMapValue(rowsMap, keys, row, index) {
@@ -768,7 +770,7 @@ function rdbClient(options = {}) {
768
770
  array.splice(i + offset, 1);
769
771
  offset--;
770
772
  }
771
- rootMap.set(array, { json: stringify(array), strategy, originalArray: [...array] });
773
+ rootMap.set(array, { json: cloneFromDb(array), strategy, originalArray: [...array] });
772
774
  fetchingStrategyMap.set(array, strategy);
773
775
  }
774
776
 
@@ -794,7 +796,7 @@ function rdbClient(options = {}) {
794
796
  return;
795
797
  let meta = await getMeta();
796
798
 
797
- let patch = createPatch([JSON.parse(json)], [row], meta);
799
+ let patch = createPatch([json], [row], meta);
798
800
  if (patch.length === 0)
799
801
  return;
800
802
 
@@ -803,7 +805,7 @@ function rdbClient(options = {}) {
803
805
  let adapter = netAdapter(url, tableName, { axios: axiosInterceptor, tableOptions });
804
806
  let { changed, strategy: newStrategy } = await adapter.patch(body);
805
807
  copyInto(changed, [row]);
806
- rootMap.set(row, { json: stringify(row), strategy: newStrategy });
808
+ rootMap.set(row, { json: cloneFromDb(row), strategy: newStrategy });
807
809
  }
808
810
 
809
811
  async function refreshRow(row, strategy) {
@@ -827,20 +829,20 @@ function rdbClient(options = {}) {
827
829
  for (let p in rows[0]) {
828
830
  row[p] = rows[0][p];
829
831
  }
830
- rootMap.set(row, { json: stringify(row), strategy });
832
+ rootMap.set(row, { json: cloneFromDb(row), strategy });
831
833
  fetchingStrategyMap.set(row, strategy);
832
834
  }
833
835
 
834
836
  function acceptChangesRow(row) {
835
837
  const { strategy } = rootMap.get(row);
836
- rootMap.set(row, { json: stringify(row), strategy });
838
+ rootMap.set(row, { json: cloneFromDb(row), strategy });
837
839
  }
838
840
 
839
841
  function clearChangesRow(row) {
840
842
  let { json } = rootMap.get(row);
841
843
  if (!json)
842
844
  return;
843
- let old = JSON.parse(json);
845
+ let old = cloneFromDb(json);
844
846
  for (let p in row) {
845
847
  delete row[p];
846
848
  }