orange-orm 4.1.4 → 4.2.0-beta.1
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/README.md +2 -2
- package/package.json +1 -1
- package/src/client/cloneFromDb.js +25 -0
- package/src/client/index.js +18 -16
- package/src/getManyDto.js +3 -3
package/README.md
CHANGED
|
@@ -139,7 +139,7 @@ getRows();
|
|
|
139
139
|
async function getRows() {
|
|
140
140
|
const orders = await db.order.getAll({
|
|
141
141
|
where: x => x.lines.any(line => line.product.contains('broomstick'))
|
|
142
|
-
.and(
|
|
142
|
+
.and(x.customer.name.startsWith('Harry')),
|
|
143
143
|
lines: {
|
|
144
144
|
packages: true
|
|
145
145
|
},
|
|
@@ -831,7 +831,7 @@ async function update() {
|
|
|
831
831
|
};
|
|
832
832
|
|
|
833
833
|
const modified = JSON.parse(JSON.stringify(original));
|
|
834
|
-
deliveryAddress.name = 'Roger';
|
|
834
|
+
modified.deliveryAddress.name = 'Roger';
|
|
835
835
|
modified.lines.push({ product: 'Piano', amount: 800 });
|
|
836
836
|
|
|
837
837
|
const order = await db.order.updateChanges(modified, original, { customer: true, deliveryAddress: true, lines: true });
|
package/package.json
CHANGED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
let dateToISOString = require('../dateToISOString');
|
|
2
|
+
|
|
3
|
+
function cloneFromDb(obj) {
|
|
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] = cloneFromDb(obj[i]);
|
|
10
|
+
}
|
|
11
|
+
return arrClone;
|
|
12
|
+
}
|
|
13
|
+
else if (obj instanceof Date && !isNaN(obj))
|
|
14
|
+
return dateToISOString(obj);
|
|
15
|
+
const clone = {};
|
|
16
|
+
const keys = Object.keys(obj);
|
|
17
|
+
for (let i = 0; i < keys.length; i++) {
|
|
18
|
+
const key = keys[i];
|
|
19
|
+
clone[key] = cloneFromDb(obj[key]);
|
|
20
|
+
}
|
|
21
|
+
return clone;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
module.exports = cloneFromDb;
|
package/src/client/index.js
CHANGED
|
@@ -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();
|
|
@@ -483,7 +484,8 @@ function rdbClient(options = {}) {
|
|
|
483
484
|
|
|
484
485
|
};
|
|
485
486
|
let innerProxy = new Proxy(array, handler);
|
|
486
|
-
|
|
487
|
+
//todo
|
|
488
|
+
rootMap.set(array, { json: cloneFromDb(array), strategy, originalArray: [...array] });
|
|
487
489
|
if (strategy !== undefined) {
|
|
488
490
|
const { limit, ...cleanStrategy } = { ...strategy };
|
|
489
491
|
fetchingStrategyMap.set(array, cleanStrategy);
|
|
@@ -496,13 +498,13 @@ function rdbClient(options = {}) {
|
|
|
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
|
|
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
|
|
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
|
|
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:
|
|
521
|
+
rootMap.set(row, { json: cloneFromDb(row), 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(
|
|
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:
|
|
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 =
|
|
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 =
|
|
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, {
|
|
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:
|
|
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([
|
|
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:
|
|
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:
|
|
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:
|
|
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 =
|
|
845
|
+
let old = cloneFromDb(json);
|
|
844
846
|
for (let p in row) {
|
|
845
847
|
delete row[p];
|
|
846
848
|
}
|
package/src/getManyDto.js
CHANGED
|
@@ -123,8 +123,6 @@ async function decode(strategy, span, rows, keys = rows.length > 0 ? Object.keys
|
|
|
123
123
|
}
|
|
124
124
|
const column = columns[j];
|
|
125
125
|
outRow[column.alias] = column.decode(row[keys[j]]);
|
|
126
|
-
if (shouldCreateMap)
|
|
127
|
-
fkIds[i] = getIds(outRow);
|
|
128
126
|
}
|
|
129
127
|
|
|
130
128
|
for (let j = 0; j < aggregateKeys.length; j++) {
|
|
@@ -134,8 +132,10 @@ async function decode(strategy, span, rows, keys = rows.length > 0 ? Object.keys
|
|
|
134
132
|
}
|
|
135
133
|
|
|
136
134
|
outRows[i] = outRow;
|
|
137
|
-
if (shouldCreateMap)
|
|
135
|
+
if (shouldCreateMap) {
|
|
136
|
+
fkIds[i] = getIds(outRow);
|
|
138
137
|
addToMap(rowsMap, primaryColumns, outRow);
|
|
138
|
+
}
|
|
139
139
|
}
|
|
140
140
|
span._rowsMap = rowsMap;
|
|
141
141
|
span._ids = fkIds;
|