orange-orm 4.3.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/docs/changelog.md +0 -2
- package/package.json +1 -1
- package/src/client/index.mjs +373 -784
- package/src/getManyDto.js +143 -86
- package/src/table/column/binary/newEncode.js +4 -0
- package/src/table/column/boolean/newEncode.js +6 -0
- package/src/table/column/date/newEncode.js +7 -2
- package/src/table/column/dateWithTimeZone/newEncode.js +4 -0
- package/src/table/column/guid/newEncode.js +4 -0
- package/src/table/column/in.js +11 -14
- package/src/table/column/json/newEncode.js +9 -0
- package/src/table/column/numeric/newEncode.js +4 -0
- package/src/table/column/string/newEncode.js +4 -1
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,7 +16,7 @@ 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
22
|
function newCreateRow(span) {
|
|
@@ -75,6 +75,63 @@ function createProto(columns, span) {
|
|
|
75
75
|
return obj;
|
|
76
76
|
}
|
|
77
77
|
|
|
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
|
+
|
|
78
135
|
function hasManyRelations(span) {
|
|
79
136
|
let result;
|
|
80
137
|
const c = {};
|
|
@@ -92,7 +149,7 @@ function hasManyRelations(span) {
|
|
|
92
149
|
}
|
|
93
150
|
}
|
|
94
151
|
|
|
95
|
-
async function decode(strategy, span, rows, keys = rows.length > 0 ? Object.keys(rows[0]) : []) {
|
|
152
|
+
async function decode(strategy, span, rows, keys = rows.length > 0 ? Object.keys(rows[0]) : [], updateParent) {
|
|
96
153
|
const table = span.table;
|
|
97
154
|
let columnsMap = span.columns;
|
|
98
155
|
const columns = table._columns.filter(column => !columnsMap || columnsMap.get(column));
|
|
@@ -132,9 +189,13 @@ async function decode(strategy, span, rows, keys = rows.length > 0 ? Object.keys
|
|
|
132
189
|
}
|
|
133
190
|
|
|
134
191
|
outRows[i] = outRow;
|
|
192
|
+
// if (parentRows)
|
|
193
|
+
// parentRows[i][parentProp] = outRow;
|
|
194
|
+
if (updateParent)
|
|
195
|
+
updateParent(outRow, i);
|
|
135
196
|
if (shouldCreateMap) {
|
|
136
197
|
fkIds[i] = getIds(outRow);
|
|
137
|
-
addToMap(rowsMap,
|
|
198
|
+
addToMap(rowsMap, fkIds[i], outRow);
|
|
138
199
|
}
|
|
139
200
|
}
|
|
140
201
|
span._rowsMap = rowsMap;
|
|
@@ -146,8 +207,12 @@ async function decode(strategy, span, rows, keys = rows.length > 0 ? Object.keys
|
|
|
146
207
|
|
|
147
208
|
const all = [];
|
|
148
209
|
|
|
149
|
-
if (shouldCreateMap)
|
|
150
|
-
all.push(decodeManyRelations(strategy, span)
|
|
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
|
+
// }
|
|
151
216
|
else
|
|
152
217
|
all.push(decodeRelations2(strategy, span, rows, outRows, keys));
|
|
153
218
|
|
|
@@ -185,18 +250,47 @@ async function decodeManyRelations(strategy, span) {
|
|
|
185
250
|
const name = leg.name;
|
|
186
251
|
const table = span.table;
|
|
187
252
|
const relation = table._relations[name];
|
|
188
|
-
const filter = createOneFilter(relation, span._ids);
|
|
189
253
|
const rowsMap = span._rowsMap;
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
254
|
+
|
|
255
|
+
const filter = createOneFilter(relation, span._ids);
|
|
256
|
+
const extractKey = createExtractKey(leg);
|
|
257
|
+
const extractFromMap = createExtractFromMap(rowsMap, table._primaryColumns);
|
|
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
|
+
|
|
197
274
|
promises.push(p);
|
|
198
275
|
};
|
|
199
276
|
|
|
277
|
+
function createExtractKey(leg) {
|
|
278
|
+
if (leg.columns.length === 1) {
|
|
279
|
+
const alias = leg.columns[0].alias;
|
|
280
|
+
return (row) => row[alias];
|
|
281
|
+
}
|
|
282
|
+
else {
|
|
283
|
+
const aliases = leg.columns.map(column => column.alias);
|
|
284
|
+
return (row) => aliases.map(alias => row[alias]);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
function createExtractFromMap(map, primaryColumns) {
|
|
288
|
+
if (primaryColumns.length === 1)
|
|
289
|
+
return (key) => map.get(key);
|
|
290
|
+
else
|
|
291
|
+
return getFromMap.bind(null, map, primaryColumns);
|
|
292
|
+
}
|
|
293
|
+
|
|
200
294
|
span.legs.forEach(onEachLeg);
|
|
201
295
|
|
|
202
296
|
function onEachLeg(leg) {
|
|
@@ -205,75 +299,30 @@ async function decodeManyRelations(strategy, span) {
|
|
|
205
299
|
|
|
206
300
|
await Promise.all(promises);
|
|
207
301
|
}
|
|
208
|
-
|
|
209
302
|
async function decodeRelations2(strategy, span, rawRows, resultRows, keys) {
|
|
210
|
-
const promises = [];
|
|
211
303
|
const c = {};
|
|
212
304
|
c.visitJoin = function(leg) {
|
|
213
305
|
const name = leg.name;
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
306
|
+
return decode(strategy[name], leg.span, rawRows, keys, updateParent);
|
|
307
|
+
|
|
308
|
+
function updateParent(subRow, i) {
|
|
309
|
+
resultRows[i][name] = subRow;
|
|
310
|
+
|
|
311
|
+
}
|
|
220
312
|
};
|
|
221
313
|
|
|
222
314
|
c.visitOne = c.visitJoin;
|
|
223
|
-
|
|
224
315
|
c.visitMany = () => { };
|
|
225
316
|
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
leg.accept(c);
|
|
317
|
+
async function processLegsSequentially(legs) {
|
|
318
|
+
for (const leg of legs.toArray()) {
|
|
319
|
+
await leg.accept(c);
|
|
320
|
+
}
|
|
231
321
|
}
|
|
232
322
|
|
|
233
|
-
await
|
|
323
|
+
await processLegsSequentially(span.legs);
|
|
234
324
|
}
|
|
235
325
|
|
|
236
|
-
|
|
237
|
-
// async function decodeRelations(strategy, span, rawRows, resultRows, keys) {
|
|
238
|
-
// const promises = [];
|
|
239
|
-
// const c = {};
|
|
240
|
-
// c.visitJoin = function (leg) {
|
|
241
|
-
// const name = leg.name;
|
|
242
|
-
// const p = decode(strategy[name], leg.span, rawRows, keys).then((rows) => {
|
|
243
|
-
// for (let i = 0; i < rows.length; i++) {
|
|
244
|
-
// resultRows[i][name] = rows[i];
|
|
245
|
-
// }
|
|
246
|
-
// });
|
|
247
|
-
// promises.push(p);
|
|
248
|
-
// };
|
|
249
|
-
|
|
250
|
-
// c.visitOne = c.visitJoin;
|
|
251
|
-
|
|
252
|
-
// c.visitMany = function (leg) {
|
|
253
|
-
// const name = leg.name;
|
|
254
|
-
// const table = span.table;
|
|
255
|
-
// const relation = table._relations[name];
|
|
256
|
-
// const filter = createOneFilter(relation, span._ids);
|
|
257
|
-
// const rowsMap = span._rowsMap;
|
|
258
|
-
// const p = getManyDto(relation.childTable, filter, strategy[name], leg.span).then(subRows => {
|
|
259
|
-
// for (let i = 0; i < subRows.length; i++) {
|
|
260
|
-
// const key = leg.columns.map(column => subRows[i][column.alias]);
|
|
261
|
-
// const parentRow = getFromMap(rowsMap, table._primaryColumns, key);
|
|
262
|
-
// parentRow[name].push(subRows[i]);
|
|
263
|
-
// }
|
|
264
|
-
// });
|
|
265
|
-
// promises.push(p);
|
|
266
|
-
// };
|
|
267
|
-
|
|
268
|
-
// span.legs.forEach(onEachLeg);
|
|
269
|
-
|
|
270
|
-
// function onEachLeg(leg) {
|
|
271
|
-
// leg.accept(c);
|
|
272
|
-
// }
|
|
273
|
-
|
|
274
|
-
// await Promise.all(promises);
|
|
275
|
-
// }
|
|
276
|
-
|
|
277
326
|
function createOneFilter(relation, ids) {
|
|
278
327
|
const columns = relation.joinRelation.columns;
|
|
279
328
|
|
|
@@ -299,28 +348,36 @@ function createOneFilter(relation, ids) {
|
|
|
299
348
|
}
|
|
300
349
|
}
|
|
301
350
|
|
|
302
|
-
function addToMap(map,
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
351
|
+
function addToMap(map, values, row) {
|
|
352
|
+
if (Array.isArray(values)) {
|
|
353
|
+
|
|
354
|
+
const lastIndex = values.length - 1;
|
|
355
|
+
for (let i = 0; i < lastIndex; i++) {
|
|
356
|
+
const id = values[i];
|
|
357
|
+
if (map.has(id))
|
|
358
|
+
map = map.get(id);
|
|
359
|
+
else {
|
|
360
|
+
const next = new Map();
|
|
361
|
+
map.set(id, next);
|
|
362
|
+
map = next;
|
|
363
|
+
}
|
|
313
364
|
}
|
|
365
|
+
map.set(values[lastIndex], row);
|
|
314
366
|
}
|
|
315
|
-
|
|
367
|
+
else
|
|
368
|
+
map.set(values, row);
|
|
316
369
|
}
|
|
317
370
|
|
|
318
371
|
function getFromMap(map, primaryColumns, values) {
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
372
|
+
if (Array.isArray(values)) {
|
|
373
|
+
const length = primaryColumns.length;
|
|
374
|
+
for (let i = 0; i < length; i++) {
|
|
375
|
+
map = map.get(values[i]);
|
|
376
|
+
}
|
|
377
|
+
return map;
|
|
322
378
|
}
|
|
323
|
-
|
|
379
|
+
else
|
|
380
|
+
return map.get(values);
|
|
324
381
|
}
|
|
325
382
|
|
|
326
383
|
|
|
@@ -21,13 +21,18 @@ function _new(column) {
|
|
|
21
21
|
value = purify(value);
|
|
22
22
|
if (value == null) {
|
|
23
23
|
if (column.dbNull === null)
|
|
24
|
-
|
|
25
|
-
return
|
|
24
|
+
'null';
|
|
25
|
+
return '\'' + column.dbNull + '\'';
|
|
26
26
|
}
|
|
27
27
|
var encodeCore = getSessionSingleton('encodeDate') || encodeDate;
|
|
28
28
|
return encodeCore(value);
|
|
29
29
|
};
|
|
30
30
|
|
|
31
|
+
encode.direct = function(value) {
|
|
32
|
+
var encodeCore = getSessionSingleton('encodeDate') || encodeDate;
|
|
33
|
+
return encodeCore(value);
|
|
34
|
+
};
|
|
35
|
+
|
|
31
36
|
return encode;
|
|
32
37
|
|
|
33
38
|
|
package/src/table/column/in.js
CHANGED
|
@@ -1,25 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var quote = require('../quote');
|
|
1
|
+
const newParameterized = require('../query/newParameterized');
|
|
2
|
+
const newBoolean = require('./newBoolean');
|
|
3
|
+
const quote = require('../quote');
|
|
5
4
|
|
|
6
5
|
function _in(column,values,alias) {
|
|
7
|
-
|
|
6
|
+
let filter;
|
|
8
7
|
if (values.length === 0) {
|
|
9
8
|
filter = newParameterized('1=2');
|
|
10
9
|
return newBoolean(filter);
|
|
11
10
|
}
|
|
12
|
-
|
|
13
|
-
var parameterized = newParameterized(firstPart);
|
|
14
|
-
var separator = '(';
|
|
11
|
+
const firstPart = `${quote(alias)}.${quote(column._dbName)} in (`;
|
|
15
12
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
13
|
+
const encode = column.encode.direct;
|
|
14
|
+
const params = new Array(values.length);
|
|
15
|
+
for (let i = 0; i < values.length; i++) {
|
|
16
|
+
params[i] = encode(values[i]);
|
|
20
17
|
}
|
|
21
|
-
|
|
22
|
-
return newBoolean(
|
|
18
|
+
const sql = `${firstPart + new Array(values.length).fill('?').join(',')})`;
|
|
19
|
+
return newBoolean(newParameterized(sql, params));
|
|
23
20
|
}
|
|
24
21
|
|
|
25
22
|
module.exports = _in;
|
|
@@ -35,6 +35,15 @@ function _new(column) {
|
|
|
35
35
|
return value;
|
|
36
36
|
};
|
|
37
37
|
|
|
38
|
+
encode.direct = function(value) {
|
|
39
|
+
var encodeCore = getSessionSingleton('encodeJSON');
|
|
40
|
+
|
|
41
|
+
if (encodeCore) {
|
|
42
|
+
value = encodeCore(value);
|
|
43
|
+
}
|
|
44
|
+
return value;
|
|
45
|
+
};
|
|
46
|
+
|
|
38
47
|
return encode;
|
|
39
48
|
}
|
|
40
49
|
|
|
@@ -14,7 +14,6 @@ function _new(column) {
|
|
|
14
14
|
};
|
|
15
15
|
|
|
16
16
|
encode.unsafe = function(value) {
|
|
17
|
-
value = purify(value);
|
|
18
17
|
value = purify(value);
|
|
19
18
|
if (value == null) {
|
|
20
19
|
if (column.dbNull === null)
|
|
@@ -24,6 +23,10 @@ function _new(column) {
|
|
|
24
23
|
return '\'' + value + '\'';
|
|
25
24
|
};
|
|
26
25
|
|
|
26
|
+
encode.direct = function(value) {
|
|
27
|
+
return value ;
|
|
28
|
+
};
|
|
29
|
+
|
|
27
30
|
return encode;
|
|
28
31
|
|
|
29
32
|
}
|