orange-orm 4.4.0-beta.0 → 4.4.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "orange-orm",
3
- "version": "4.4.0-beta.0",
3
+ "version": "4.4.0-beta.1",
4
4
  "main": "./src/index.js",
5
5
  "browser": "./src/client/index.mjs",
6
6
  "bin": {
package/src/getManyDto.js CHANGED
@@ -4,7 +4,7 @@ const negotiateRawSqlFilter = require('./table/column/negotiateRawSqlFilter');
4
4
  const strategyToSpan = require('./table/strategyToSpan');
5
5
  const executeQueries = require('./table/executeQueries');
6
6
 
7
- async function getManyDto(table, filter, strategy, spanFromParent) {
7
+ async function getManyDto(table, filter, strategy, spanFromParent, updateParent) {
8
8
  filter = negotiateRawSqlFilter(filter, table);
9
9
  if (strategy && strategy.where) {
10
10
  let arg = typeof strategy.where === 'function' ? strategy.where(table) : strategy.where;
@@ -16,95 +16,122 @@ async function getManyDto(table, filter, strategy, spanFromParent) {
16
16
 
17
17
  const query = newQuery(table, filter, span, alias);
18
18
  const res = await executeQueries([query]);
19
- return decode(strategy, span, await res[0]);
19
+ return decode(strategy, span, await res[0], undefined, updateParent);
20
20
  }
21
21
 
22
- // function newCreateRow(span) {
23
- // let columnsMap = span.columns;
24
- // const columns = span.table._columns.filter(column => !columnsMap || columnsMap.get(column));
25
- // const protoRow = createProto(columns, span);
26
- // const manyNames = [];
27
-
28
- // const c = {};
29
- // c.visitJoin = () => { };
30
- // c.visitOne = () => { };
31
- // c.visitMany = function(leg) {
32
- // manyNames.push(leg.name);
33
- // };
34
-
35
- // span.legs.forEach(onEachLeg);
36
- // return createRow;
37
-
38
- // function onEachLeg(leg) {
39
- // leg.accept(c);
40
- // }
41
-
42
- // function createRow() {
43
- // const obj = Object.create(protoRow);
44
- // for (let i = 0; i < manyNames.length; i++) {
45
- // obj[manyNames[i]] = [];
46
- // }
47
- // return obj;
48
- // }
49
- // }
50
-
51
-
52
22
  function newCreateRow(span) {
53
- const columnsMap = span.columns;
23
+ let columnsMap = span.columns;
54
24
  const columns = span.table._columns.filter(column => !columnsMap || columnsMap.get(column));
55
- const ProtoRow = createProto(columns, span);
25
+ const protoRow = createProto(columns, span);
56
26
  const manyNames = [];
57
27
 
58
- const c = {
59
- visitJoin: () => { },
60
- visitOne: () => { },
61
- visitMany: function(leg) {
62
- manyNames.push(leg.name);
63
- }
28
+ const c = {};
29
+ c.visitJoin = () => { };
30
+ c.visitOne = () => { };
31
+ c.visitMany = function(leg) {
32
+ manyNames.push(leg.name);
64
33
  };
65
34
 
66
- span.legs.forEach(leg => leg.accept(c));
67
-
35
+ span.legs.forEach(onEachLeg);
68
36
  return createRow;
69
37
 
38
+ function onEachLeg(leg) {
39
+ leg.accept(c);
40
+ }
41
+
70
42
  function createRow() {
71
- const obj = new ProtoRow();
72
- manyNames.forEach(name => {
73
- obj[name] = [];
74
- });
43
+ const obj = Object.create(protoRow);
44
+ for (let i = 0; i < manyNames.length; i++) {
45
+ obj[manyNames[i]] = [];
46
+ }
75
47
  return obj;
76
48
  }
77
49
  }
78
50
 
79
51
  function createProto(columns, span) {
80
- function ProtoRow() {
81
- columns.forEach(column => {
82
- this[column.alias] = null;
83
- });
52
+ let obj = {};
53
+ for (let i = 0; i < columns.length; i++) {
54
+ obj[columns[i].alias] = null;
55
+ }
56
+ for (let key in span.aggregates) {
57
+ obj[key] = null;
58
+ }
59
+ const c = {};
84
60
 
85
- for (const key in span.aggregates) {
86
- this[key] = null;
87
- }
61
+ c.visitJoin = function(leg) {
62
+ obj[leg.name] = null;
63
+ };
64
+ c.visitOne = c.visitJoin;
65
+ c.visitMany = function(leg) {
66
+ obj[leg.name] = null;
67
+ };
88
68
 
89
- const c = {
90
- visitJoin: (leg) => {
91
- this[leg.name] = null;
92
- },
93
- visitOne: (leg) => {
94
- this[leg.name] = null;
95
- },
96
- visitMany: (leg) => {
97
- this[leg.name] = null;
98
- }
99
- };
69
+ span.legs.forEach(onEachLeg);
100
70
 
101
- span.legs.forEach(leg => leg.accept(c));
71
+ function onEachLeg(leg) {
72
+ leg.accept(c);
102
73
  }
103
74
 
104
- return ProtoRow;
75
+ return obj;
105
76
  }
106
77
 
107
78
 
79
+ // function newCreateRow(span) {
80
+ // const columnsMap = span.columns;
81
+ // const columns = span.table._columns.filter(column => !columnsMap || columnsMap.get(column));
82
+ // const ProtoRow = createProto(columns, span);
83
+ // const manyNames = [];
84
+
85
+ // const c = {
86
+ // visitJoin: () => { },
87
+ // visitOne: () => { },
88
+ // visitMany: function(leg) {
89
+ // manyNames.push(leg.name);
90
+ // }
91
+ // };
92
+
93
+ // span.legs.forEach(leg => leg.accept(c));
94
+
95
+ // return createRow;
96
+
97
+ // function createRow() {
98
+ // const obj = new ProtoRow();
99
+ // manyNames.forEach(name => {
100
+ // obj[name] = [];
101
+ // });
102
+ // return obj;
103
+ // }
104
+ // }
105
+
106
+ // function createProto(columns, span) {
107
+ // function ProtoRow() {
108
+ // columns.forEach(column => {
109
+ // this[column.alias] = null;
110
+ // });
111
+
112
+ // for (const key in span.aggregates) {
113
+ // this[key] = null;
114
+ // }
115
+
116
+ // const c = {
117
+ // visitJoin: (leg) => {
118
+ // this[leg.name] = null;
119
+ // },
120
+ // visitOne: (leg) => {
121
+ // this[leg.name] = null;
122
+ // },
123
+ // visitMany: (leg) => {
124
+ // this[leg.name] = null;
125
+ // }
126
+ // };
127
+
128
+ // span.legs.forEach(leg => leg.accept(c));
129
+ // }
130
+
131
+ // return ProtoRow;
132
+ // }
133
+
134
+
108
135
  function hasManyRelations(span) {
109
136
  let result;
110
137
  const c = {};
@@ -122,7 +149,7 @@ function hasManyRelations(span) {
122
149
  }
123
150
  }
124
151
 
125
- async function decode(strategy, span, rows, keys = rows.length > 0 ? Object.keys(rows[0]) : [], parentRows, parentProp) {
152
+ async function decode(strategy, span, rows, keys = rows.length > 0 ? Object.keys(rows[0]) : [], updateParent) {
126
153
  const table = span.table;
127
154
  let columnsMap = span.columns;
128
155
  const columns = table._columns.filter(column => !columnsMap || columnsMap.get(column));
@@ -162,8 +189,10 @@ async function decode(strategy, span, rows, keys = rows.length > 0 ? Object.keys
162
189
  }
163
190
 
164
191
  outRows[i] = outRow;
165
- if (parentRows)
166
- parentRows[i][parentProp] = outRow;
192
+ // if (parentRows)
193
+ // parentRows[i][parentProp] = outRow;
194
+ if (updateParent)
195
+ updateParent(outRow, i);
167
196
  if (shouldCreateMap) {
168
197
  fkIds[i] = getIds(outRow);
169
198
  addToMap(rowsMap, fkIds[i], outRow);
@@ -178,8 +207,12 @@ async function decode(strategy, span, rows, keys = rows.length > 0 ? Object.keys
178
207
 
179
208
  const all = [];
180
209
 
181
- if (shouldCreateMap)
182
- all.push(decodeManyRelations(strategy, span).then(() => decodeRelations2(strategy, span, rows, outRows, keys)));
210
+ if (shouldCreateMap) {
211
+ all.push(decodeManyRelations(strategy, span));
212
+ all.push(decodeRelations2(strategy, span, rows, outRows, keys));
213
+ }
214
+ // decodeRelations2(strategy, span, rows, outRows, keys);
215
+ // }
183
216
  else
184
217
  all.push(decodeRelations2(strategy, span, rows, outRows, keys));
185
218
 
@@ -222,13 +255,22 @@ async function decodeManyRelations(strategy, span) {
222
255
  const filter = createOneFilter(relation, span._ids);
223
256
  const extractKey = createExtractKey(leg);
224
257
  const extractFromMap = createExtractFromMap(rowsMap, table._primaryColumns);
225
- const p = getManyDto(relation.childTable, filter, strategy[name], leg.span).then(subRows => {
226
- for (let i = 0; i < subRows.length; i++) {
227
- const key = extractKey(subRows[i]);
228
- const parentRow = extractFromMap(key);
229
- parentRow[name].push(subRows[i]);
230
- }
231
- });
258
+
259
+ const p = getManyDto(relation.childTable, filter, strategy[name], leg.span, updateParent);
260
+ // .then(subRows => {
261
+ // for (let i = 0; i < subRows.length; i++) {
262
+ // const key = extractKey(subRows[i]);
263
+ // const parentRow = extractFromMap(key);
264
+ // parentRow[name].push(subRows[i]);
265
+ // }
266
+ // });
267
+
268
+ function updateParent(subRow) {
269
+ const key = extractKey(subRow);
270
+ const parentRow = extractFromMap(key);
271
+ parentRow[name].push(subRow);
272
+ }
273
+
232
274
  promises.push(p);
233
275
  };
234
276
 
@@ -257,28 +299,28 @@ async function decodeManyRelations(strategy, span) {
257
299
 
258
300
  await Promise.all(promises);
259
301
  }
260
-
261
302
  async function decodeRelations2(strategy, span, rawRows, resultRows, keys) {
262
- const promises = [];
263
303
  const c = {};
264
304
  c.visitJoin = function(leg) {
265
305
  const name = leg.name;
266
- const p = decode(strategy[name], leg.span, rawRows, keys, resultRows, name);
267
- promises.push(p);
306
+ return decode(strategy[name], leg.span, rawRows, keys, updateParent);
307
+
308
+ function updateParent(subRow, i) {
309
+ resultRows[i][name] = subRow;
310
+
311
+ }
268
312
  };
269
313
 
270
314
  c.visitOne = c.visitJoin;
271
-
272
315
  c.visitMany = () => { };
273
316
 
274
-
275
- span.legs.forEach(onEachLeg);
276
-
277
- function onEachLeg(leg) {
278
- leg.accept(c);
317
+ async function processLegsSequentially(legs) {
318
+ for (const leg of legs.toArray()) {
319
+ await leg.accept(c);
320
+ }
279
321
  }
280
322
 
281
- await Promise.all(promises);
323
+ await processLegsSequentially(span.legs);
282
324
  }
283
325
 
284
326
  function createOneFilter(relation, ids) {
@@ -18,9 +18,6 @@ function _new(_column) {
18
18
  };
19
19
 
20
20
  encode.direct = function(value) {
21
- value = purify(value);
22
- if (value === null)
23
- return null;
24
21
  return Buffer.from(value, 'base64');
25
22
  };
26
23
 
@@ -31,12 +31,6 @@ function _new(column) {
31
31
  };
32
32
 
33
33
  encode.direct = function(value) {
34
- value = purify(value);
35
- if (value === null) {
36
- if (column.dbNull === null)
37
- return null;
38
- return column.dbNull ;
39
- }
40
34
  var encodeCore = getSessionSingleton('encodeBoolean');
41
35
 
42
36
  return encodeCore(value);
@@ -29,12 +29,6 @@ function _new(column) {
29
29
  };
30
30
 
31
31
  encode.direct = function(value) {
32
- value = purify(value);
33
- if (value == null) {
34
- if (column.dbNull === null)
35
- null;
36
- return column.dbNull;
37
- }
38
32
  var encodeCore = getSessionSingleton('encodeDate') || encodeDate;
39
33
  return encodeCore(value);
40
34
  };
@@ -24,12 +24,6 @@ function _new(column) {
24
24
  };
25
25
 
26
26
  encode.direct = function(value) {
27
- value = purify(value);
28
- if (value == null) {
29
- if (column.dbNull === null)
30
- return null;
31
- return column.dbNull;
32
- }
33
27
  return encodeDate(value);
34
28
  };
35
29
 
@@ -23,13 +23,7 @@ function _new(column) {
23
23
  return '\'' + value + '\'';
24
24
  };
25
25
 
26
- encode.direct = function(candidate) {
27
- var value = purify(candidate);
28
- if (value == null) {
29
- if (column.dbNull === null)
30
- return null;
31
- return column.dbNull;
32
- }
26
+ encode.direct = function(value) {
33
27
  return value ;
34
28
  };
35
29
 
@@ -35,13 +35,7 @@ function _new(column) {
35
35
  return value;
36
36
  };
37
37
 
38
- encode.direct = function(candidate) {
39
- var value = purify(candidate);
40
- if (value == null) {
41
- if(column.dbNull === null)
42
- return null;
43
- return column.dbNull;
44
- }
38
+ encode.direct = function(value) {
45
39
  var encodeCore = getSessionSingleton('encodeJSON');
46
40
 
47
41
  if (encodeCore) {
@@ -22,12 +22,6 @@ module.exports = function(column) {
22
22
  };
23
23
 
24
24
  encode.direct = function(value) {
25
- value = purify(value);
26
- if (value == null) {
27
- if (column.dbNull === null)
28
- return null;
29
- return column.dbNull;
30
- }
31
25
  return value ;
32
26
  };
33
27
 
@@ -24,12 +24,6 @@ function _new(column) {
24
24
  };
25
25
 
26
26
  encode.direct = function(value) {
27
- value = purify(value);
28
- if (value == null) {
29
- if (column.dbNull === null)
30
- return null;
31
- return column.dbNull;
32
- }
33
27
  return value ;
34
28
  };
35
29