orange-orm 4.3.0-beta.1 → 4.3.0-beta.3
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 +1 -1
- package/src/getManyDto.js +360 -23
package/package.json
CHANGED
package/src/getManyDto.js
CHANGED
|
@@ -28,7 +28,7 @@ function newCreateRow(span) {
|
|
|
28
28
|
const c = {};
|
|
29
29
|
c.visitJoin = () => { };
|
|
30
30
|
c.visitOne = () => { };
|
|
31
|
-
c.visitMany = function(leg) {
|
|
31
|
+
c.visitMany = function (leg) {
|
|
32
32
|
manyNames.push(leg.name);
|
|
33
33
|
};
|
|
34
34
|
|
|
@@ -58,11 +58,11 @@ function createProto(columns, span) {
|
|
|
58
58
|
}
|
|
59
59
|
const c = {};
|
|
60
60
|
|
|
61
|
-
c.visitJoin = function(leg) {
|
|
61
|
+
c.visitJoin = function (leg) {
|
|
62
62
|
obj[leg.name] = null;
|
|
63
63
|
};
|
|
64
64
|
c.visitOne = c.visitJoin;
|
|
65
|
-
c.visitMany = function(leg) {
|
|
65
|
+
c.visitMany = function (leg) {
|
|
66
66
|
obj[leg.name] = null;
|
|
67
67
|
};
|
|
68
68
|
|
|
@@ -80,7 +80,7 @@ function hasManyRelations(span) {
|
|
|
80
80
|
const c = {};
|
|
81
81
|
c.visitJoin = () => { };
|
|
82
82
|
c.visitOne = c.visitJoin;
|
|
83
|
-
c.visitMany = function() {
|
|
83
|
+
c.visitMany = function () {
|
|
84
84
|
result = true;
|
|
85
85
|
};
|
|
86
86
|
|
|
@@ -108,41 +108,160 @@ async function decode(strategy, span, rows, keys = rows.length > 0 ? Object.keys
|
|
|
108
108
|
const outRows = new Array(rowsLength);
|
|
109
109
|
const createRow = newCreateRow(span);
|
|
110
110
|
const shouldCreateMap = hasManyRelations(span);
|
|
111
|
+
let all = [];;
|
|
112
|
+
|
|
111
113
|
for (let i = 0; i < rowsLength; i++) {
|
|
112
114
|
const row = rows[i];
|
|
113
115
|
let outRow = createRow();
|
|
114
116
|
let pkWithNullCount = 0;
|
|
115
|
-
for (let j = 0; j <
|
|
116
|
-
if (j
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
break;
|
|
122
|
-
}
|
|
117
|
+
for (let j = 0; j < primaryColumnsLength; j++) {
|
|
118
|
+
if (row[keys[j]] === null)
|
|
119
|
+
pkWithNullCount++;
|
|
120
|
+
if (pkWithNullCount === primaryColumnsLength) {
|
|
121
|
+
outRow = null;
|
|
122
|
+
break;
|
|
123
123
|
}
|
|
124
124
|
const column = columns[j];
|
|
125
125
|
outRow[column.alias] = column.decode(row[keys[j]]);
|
|
126
|
-
}
|
|
127
126
|
|
|
128
|
-
for (let j = 0; j < aggregateKeys.length; j++) {
|
|
129
|
-
const key = aggregateKeys[j];
|
|
130
|
-
const parse = span.aggregates[key].column?.decode || Number.parseFloat;
|
|
131
|
-
outRow[key] = parse(row[keys[j+columnsLength]]);
|
|
132
127
|
}
|
|
133
128
|
|
|
134
|
-
|
|
135
|
-
if (shouldCreateMap) {
|
|
129
|
+
if (shouldCreateMap && outRow) {
|
|
136
130
|
fkIds[i] = getIds(outRow);
|
|
137
131
|
addToMap(rowsMap, primaryColumns, outRow);
|
|
138
132
|
}
|
|
133
|
+
outRows[i] = outRow;
|
|
139
134
|
}
|
|
135
|
+
|
|
136
|
+
all.push(new Promise((resolve) => {
|
|
137
|
+
setImmediate(async () => {
|
|
138
|
+
for (let i = 0; i < rowsLength; i++) {
|
|
139
|
+
const currentOutRow = outRows[i];
|
|
140
|
+
const currentRow = rows[i];
|
|
141
|
+
for (let j = primaryColumnsLength; j < columnsLength; j++) {
|
|
142
|
+
const column = columns[j];
|
|
143
|
+
currentOutRow[column.alias] = column.decode(currentRow[keys[j]]);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
for (let j = 0; j < aggregateKeys.length; j++) {
|
|
147
|
+
const key = aggregateKeys[j];
|
|
148
|
+
const parse = span.aggregates[key].column?.decode || Number.parseFloat;
|
|
149
|
+
currentOutRow[key] = parse(currentRow[keys[j + columnsLength]]);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
resolve();
|
|
153
|
+
});
|
|
154
|
+
}));
|
|
155
|
+
|
|
156
|
+
|
|
140
157
|
span._rowsMap = rowsMap;
|
|
141
158
|
span._ids = fkIds;
|
|
142
159
|
|
|
143
160
|
keys.splice(0, columnsLength + aggregateKeys.length);
|
|
161
|
+
if (span.legs.toArray().length === 0)
|
|
162
|
+
return outRows;
|
|
163
|
+
|
|
164
|
+
//alt 1
|
|
165
|
+
// 3.1 sec
|
|
166
|
+
// await new Promise((resolve, reject) => {
|
|
167
|
+
// setTimeout(() => {
|
|
168
|
+
// if (shouldCreateMap)
|
|
169
|
+
// decodeManyRelations(strategy, span, rows, outRows, keys)
|
|
170
|
+
// .then(() => decodeRelations2(strategy, span, rows, outRows, keys))
|
|
171
|
+
// .then(resolve)
|
|
172
|
+
// .catch(reject);
|
|
173
|
+
// else
|
|
174
|
+
// decodeRelations2(strategy, span, rows, outRows, keys)
|
|
175
|
+
// .then(resolve)
|
|
176
|
+
// .catch(reject);
|
|
177
|
+
// }, 0);
|
|
178
|
+
// });
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
//alt 1
|
|
182
|
+
// 3.2 sec
|
|
183
|
+
// await new Promise((resolve, reject) => {
|
|
184
|
+
// setTimeout(() => {
|
|
185
|
+
// const all = [];
|
|
186
|
+
// if (shouldCreateMap)
|
|
187
|
+
// all.push(decodeManyRelations(strategy, span, rows, outRows, keys))
|
|
188
|
+
|
|
189
|
+
// all.push(new Promise((resolve, reject) => {
|
|
190
|
+
// setTimeout(async () => {
|
|
191
|
+
// await decodeRelations2(strategy, span, rows, outRows, keys);
|
|
192
|
+
// resolve();
|
|
193
|
+
|
|
194
|
+
// }, 0);
|
|
195
|
+
// }));
|
|
196
|
+
// // const p2 = decodeRelations2(strategy, span, rows, outRows, keys);
|
|
197
|
+
// Promise.all(all).then(resolve, reject);
|
|
198
|
+
// }, 0);
|
|
199
|
+
// });
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
// let all = [];;
|
|
203
|
+
// if(shouldCreateMap) {
|
|
204
|
+
|
|
205
|
+
// await decodeManyRelations(strategy, span, rows, outRows, keys).then(() => decodeRelations2(strategy, span, rows, outRows, keys));
|
|
206
|
+
// }
|
|
207
|
+
// else
|
|
208
|
+
// await decodeRelations2(strategy, span, rows, outRows, keys);
|
|
209
|
+
|
|
210
|
+
// await Promise.all(all);
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
//alt 3
|
|
215
|
+
// 3.05 sec
|
|
216
|
+
|
|
217
|
+
if (shouldCreateMap) {
|
|
218
|
+
// all.push( decodeRelations2(strategy, span, rows, outRows, keys).then(() => decodeManyRelations(strategy, span)));
|
|
219
|
+
all.push(decodeManyRelations(strategy, span));
|
|
220
|
+
all.push(decodeRelations2(strategy, span, rows, outRows, keys));
|
|
221
|
+
// all.push( decodeManyRelations(strategy, span).then(() => decodeRelations2(strategy, span, rows, outRows, keys)));
|
|
222
|
+
// all.push( decodeManyRelations(strategy, span));
|
|
223
|
+
// all.push(decodeRelations2(strategy, span, rows, outRows, keys));
|
|
224
|
+
// all.push( decodeRelations2(strategy, span, rows, outRows, keys) );
|
|
225
|
+
}
|
|
226
|
+
else
|
|
227
|
+
all.push(decodeRelations2(strategy, span, rows, outRows, keys));
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
await Promise.all(all);
|
|
231
|
+
|
|
232
|
+
//alt 4
|
|
233
|
+
// 3.02 sec
|
|
234
|
+
// const all = [];
|
|
235
|
+
// if (shouldCreateMap)
|
|
236
|
+
// // all.push(new Promise((resolve, reject) => {
|
|
237
|
+
// // setTimeout(async () => {
|
|
238
|
+
// // await decodeManyRelations(strategy, span);
|
|
239
|
+
// // resolve();
|
|
240
|
+
|
|
241
|
+
// // }, 0)
|
|
242
|
+
// // }));
|
|
243
|
+
// all.push(decodeManyRelations(strategy, span));
|
|
244
|
+
// all.push(new Promise((resolve, reject) => {
|
|
245
|
+
// setTimeout(async () => {
|
|
246
|
+
// await decodeRelations2(strategy, span, rows, outRows, keys);
|
|
247
|
+
// resolve();
|
|
248
|
+
|
|
249
|
+
// }, 0);
|
|
250
|
+
// }));
|
|
251
|
+
// await Promise.all(all);
|
|
252
|
+
|
|
253
|
+
// all.push(decodeRelations2(strategy, span, rows, outRows, keys));
|
|
254
|
+
|
|
255
|
+
//alt 4
|
|
256
|
+
// 3.3 sec
|
|
257
|
+
// if(shouldCreateMap)
|
|
258
|
+
// await decodeRelations(strategy, span, rows, outRows, keys);
|
|
259
|
+
// else
|
|
260
|
+
// await decodeRelations2(strategy, span, rows, outRows, keys);
|
|
261
|
+
|
|
262
|
+
// await decodeRelationsNext(strategy, span, rows, outRows, keys);
|
|
263
|
+
// await decodeRelations(strategy, span, rows, outRows, keys);
|
|
144
264
|
|
|
145
|
-
await decodeRelations(strategy, span, rows, outRows, keys);
|
|
146
265
|
return outRows;
|
|
147
266
|
|
|
148
267
|
|
|
@@ -164,11 +283,228 @@ async function decode(strategy, span, rows, keys = rows.length > 0 ? Object.keys
|
|
|
164
283
|
}
|
|
165
284
|
|
|
166
285
|
}
|
|
286
|
+
async function decodeRelations2(strategy, span, rawRows, resultRows, keys) {
|
|
287
|
+
const legs = span.legs.toArray();
|
|
288
|
+
if (legs.length === 0)
|
|
289
|
+
return;
|
|
290
|
+
const promises = [];
|
|
291
|
+
const c = {};
|
|
292
|
+
c.visitJoin = function (leg) {
|
|
293
|
+
const name = leg.name;
|
|
294
|
+
const p = decode(strategy[name], leg.span, rawRows, keys).then((rows) => {
|
|
295
|
+
for (let i = 0; i < rows.length; i++) {
|
|
296
|
+
resultRows[i][name] = rows[i];
|
|
297
|
+
}
|
|
298
|
+
});
|
|
299
|
+
promises.push(p);
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
c.visitOne = c.visitJoin;
|
|
303
|
+
|
|
304
|
+
c.visitMany = () => { };
|
|
305
|
+
|
|
306
|
+
function processLegs(index) {
|
|
307
|
+
if (index >= legs.length) {
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
const leg = legs[index];
|
|
311
|
+
leg.accept(c);
|
|
312
|
+
Promise.all(promises).then(() => {
|
|
313
|
+
setImmediate(() => {
|
|
314
|
+
processLegs(index + 1);
|
|
315
|
+
});
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
processLegs(0);
|
|
320
|
+
|
|
321
|
+
await Promise.all(promises);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
async function decodeRelations3(strategy, span, rawRows, resultRows, keys) {
|
|
325
|
+
const promises = [];
|
|
326
|
+
const c = {};
|
|
327
|
+
c.visitJoin = function (leg) {
|
|
328
|
+
const name = leg.name;
|
|
329
|
+
const p = decode(strategy[name], leg.span, rawRows, keys).then((rows) => {
|
|
330
|
+
for (let i = 0; i < rows.length; i++) {
|
|
331
|
+
resultRows[i][name] = rows[i];
|
|
332
|
+
}
|
|
333
|
+
});
|
|
334
|
+
promises.push(p);
|
|
335
|
+
};
|
|
336
|
+
|
|
337
|
+
c.visitOne = c.visitJoin;
|
|
338
|
+
|
|
339
|
+
c.visitMany = () => { };
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
span.legs.forEach(onEachLeg);
|
|
343
|
+
|
|
344
|
+
function onEachLeg(leg) {
|
|
345
|
+
leg.accept(c);
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
await Promise.all(promises);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
async function decodeManyRelations(strategy, span, rawRows, resultRows, keys) {
|
|
353
|
+
const promises = [];
|
|
354
|
+
const c = {};
|
|
355
|
+
c.visitJoin = () => { };
|
|
356
|
+
c.visitOne = c.visitJoin;
|
|
357
|
+
|
|
358
|
+
c.visitMany = function (leg) {
|
|
359
|
+
const name = leg.name;
|
|
360
|
+
const table = span.table;
|
|
361
|
+
const relation = table._relations[name];
|
|
362
|
+
const filter = createOneFilter(relation, span._ids);
|
|
363
|
+
const rowsMap = span._rowsMap;
|
|
364
|
+
const p = getManyDto(relation.childTable, filter, strategy[name], leg.span).then(subRows => {
|
|
365
|
+
for (let i = 0; i < subRows.length; i++) {
|
|
366
|
+
const key = leg.columns.map(column => subRows[i][column.alias]);
|
|
367
|
+
const parentRow = getFromMap(rowsMap, table._primaryColumns, key);
|
|
368
|
+
parentRow[name].push(subRows[i]);
|
|
369
|
+
}
|
|
370
|
+
});
|
|
371
|
+
promises.push(p);
|
|
372
|
+
};
|
|
373
|
+
|
|
374
|
+
span.legs.forEach(onEachLeg);
|
|
375
|
+
|
|
376
|
+
function onEachLeg(leg) {
|
|
377
|
+
leg.accept(c);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
// await Promise.all(promises);
|
|
381
|
+
// }
|
|
382
|
+
|
|
383
|
+
// async function decodeRelations2(strategy, span, rawRows, resultRows, keys) {
|
|
384
|
+
// const promises = [];
|
|
385
|
+
// const c = {};
|
|
386
|
+
// c.visitJoin = function (leg) {
|
|
387
|
+
// const name = leg.name;
|
|
388
|
+
// //todo avoid traverse twice noMany relations below
|
|
389
|
+
// const p = decode(strategy[name], leg.span, rawRows, keys).then((rows) => {
|
|
390
|
+
// for (let i = 0; i < rows.length; i++) {
|
|
391
|
+
// resultRows[i][name] = rows[i];
|
|
392
|
+
// }
|
|
393
|
+
// });
|
|
394
|
+
// promises.push(p);
|
|
395
|
+
// };
|
|
396
|
+
|
|
397
|
+
// c.visitOne = c.visitJoin;
|
|
398
|
+
|
|
399
|
+
// c.visitMany = () => { };
|
|
400
|
+
|
|
401
|
+
// span.legs.forEach(onEachLeg);
|
|
402
|
+
|
|
403
|
+
// function onEachLeg(leg) {
|
|
404
|
+
// leg.accept(c);
|
|
405
|
+
// }
|
|
406
|
+
|
|
407
|
+
await Promise.all(promises);
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
async function decodeRelations3(strategy, span, rawRows, resultRows, keys) {
|
|
411
|
+
|
|
412
|
+
let previous = new Promise((resolve) => setImmediate(() => resolve()));
|
|
413
|
+
const c = {};
|
|
414
|
+
|
|
415
|
+
c.visitJoin = function (leg) {
|
|
416
|
+
const name = leg.name;
|
|
417
|
+
const p = new Promise((resolve) => {
|
|
418
|
+
setImmediate(() => {
|
|
419
|
+
decode(strategy[name], leg.span, rawRows, keys).then((rows) => {
|
|
420
|
+
for (let i = 0; i < rows.length; i++) {
|
|
421
|
+
resultRows[i][name] = rows[i];
|
|
422
|
+
}
|
|
423
|
+
resolve();
|
|
424
|
+
});
|
|
425
|
+
});
|
|
426
|
+
});
|
|
427
|
+
previous = previous.then(() => p);
|
|
428
|
+
};
|
|
429
|
+
|
|
430
|
+
c.visitOne = c.visitJoin;
|
|
431
|
+
|
|
432
|
+
c.visitMany = () => { };
|
|
433
|
+
|
|
434
|
+
span.legs.forEach(onEachLeg);
|
|
435
|
+
|
|
436
|
+
function onEachLeg(leg) {
|
|
437
|
+
leg.accept(c);
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
await previous;
|
|
441
|
+
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
async function decodeRelationsNext(strategy, span, rawRows, resultRows, keys) {
|
|
446
|
+
// const promises = [];
|
|
447
|
+
let previous = new Promise((resolve) => setImmediate(() => resolve()));
|
|
448
|
+
const c = {};
|
|
449
|
+
|
|
450
|
+
c.visitJoin = function (leg) {
|
|
451
|
+
const name = leg.name;
|
|
452
|
+
const p = new Promise((resolve) => {
|
|
453
|
+
setImmediate(() => {
|
|
454
|
+
decode(strategy[name], leg.span, rawRows, keys).then((rows) => {
|
|
455
|
+
for (let i = 0; i < rows.length; i++) {
|
|
456
|
+
resultRows[i][name] = rows[i];
|
|
457
|
+
}
|
|
458
|
+
resolve();
|
|
459
|
+
});
|
|
460
|
+
});
|
|
461
|
+
});
|
|
462
|
+
previous = previous.then(() => p);
|
|
463
|
+
};
|
|
464
|
+
|
|
465
|
+
c.visitOne = c.visitJoin;
|
|
466
|
+
|
|
467
|
+
c.visitMany = function (leg) {
|
|
468
|
+
const p = new Promise((resolve, reject) => {
|
|
469
|
+
setImmediate(() => {
|
|
470
|
+
|
|
471
|
+
const name = leg.name;
|
|
472
|
+
const table = span.table;
|
|
473
|
+
const relation = table._relations[name];
|
|
474
|
+
const filter = createOneFilter(relation, span._ids);
|
|
475
|
+
const rowsMap = span._rowsMap;
|
|
476
|
+
|
|
477
|
+
getManyDto(relation.childTable, filter, strategy[name], leg.span).then(subRows => {
|
|
478
|
+
for (let i = 0; i < subRows.length; i++) {
|
|
479
|
+
const key = leg.columns.map(column => subRows[i][column.alias]);
|
|
480
|
+
const parentRow = getFromMap(rowsMap, table._primaryColumns, key);
|
|
481
|
+
parentRow[name].push(subRows[i]);
|
|
482
|
+
}
|
|
483
|
+
}).then(() => resolve(), reject);
|
|
484
|
+
|
|
485
|
+
|
|
486
|
+
});
|
|
487
|
+
});
|
|
488
|
+
|
|
489
|
+
|
|
490
|
+
previous = previous.then(() => p);
|
|
491
|
+
// promises.push(p);
|
|
492
|
+
};
|
|
493
|
+
|
|
494
|
+
span.legs.forEach(onEachLeg);
|
|
495
|
+
|
|
496
|
+
function onEachLeg(leg) {
|
|
497
|
+
leg.accept(c);
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
await previous
|
|
501
|
+
}
|
|
502
|
+
|
|
167
503
|
|
|
168
504
|
async function decodeRelations(strategy, span, rawRows, resultRows, keys) {
|
|
169
505
|
const promises = [];
|
|
170
506
|
const c = {};
|
|
171
|
-
c.visitJoin = function(leg) {
|
|
507
|
+
c.visitJoin = function (leg) {
|
|
172
508
|
const name = leg.name;
|
|
173
509
|
const p = decode(strategy[name], leg.span, rawRows, keys).then((rows) => {
|
|
174
510
|
for (let i = 0; i < rows.length; i++) {
|
|
@@ -180,7 +516,7 @@ async function decodeRelations(strategy, span, rawRows, resultRows, keys) {
|
|
|
180
516
|
|
|
181
517
|
c.visitOne = c.visitJoin;
|
|
182
518
|
|
|
183
|
-
c.visitMany = function(leg) {
|
|
519
|
+
c.visitMany = function (leg) {
|
|
184
520
|
const name = leg.name;
|
|
185
521
|
const table = span.table;
|
|
186
522
|
const relation = table._relations[name];
|
|
@@ -196,6 +532,7 @@ async function decodeRelations(strategy, span, rawRows, resultRows, keys) {
|
|
|
196
532
|
promises.push(p);
|
|
197
533
|
};
|
|
198
534
|
|
|
535
|
+
|
|
199
536
|
span.legs.forEach(onEachLeg);
|
|
200
537
|
|
|
201
538
|
function onEachLeg(leg) {
|
|
@@ -216,7 +553,7 @@ function createOneFilter(relation, ids) {
|
|
|
216
553
|
|
|
217
554
|
function createCompositeFilter() {
|
|
218
555
|
let filter = emptyFilter;
|
|
219
|
-
for(let id of ids) {
|
|
556
|
+
for (let id of ids) {
|
|
220
557
|
let nextFilter;
|
|
221
558
|
for (let i = 0; i < columns.length; i++) {
|
|
222
559
|
if (nextFilter)
|