orange-orm 4.3.0-beta.3 → 4.4.0-beta.0

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.
@@ -0,0 +1,530 @@
1
+ const emptyFilter = require('./emptyFilter');
2
+ const newQuery = require('./getManyDto/newQuery');
3
+ const negotiateRawSqlFilter = require('./table/column/negotiateRawSqlFilter');
4
+ const strategyToSpan = require('./table/strategyToSpan');
5
+ const executeQueries = require('./table/executeQueries');
6
+
7
+ async function getManyDto(table, filter, strategy, spanFromParent) {
8
+ filter = negotiateRawSqlFilter(filter, table);
9
+ if (strategy && strategy.where) {
10
+ let arg = typeof strategy.where === 'function' ? strategy.where(table) : strategy.where;
11
+ filter = filter.and(arg);
12
+ }
13
+
14
+ let span = spanFromParent || strategyToSpan(table, strategy);
15
+ let alias = table._dbName;
16
+
17
+ const query = newQuery(table, filter, span, alias);
18
+ const res = await executeQueries([query]);
19
+ return decode(strategy, span, await res[0]);
20
+ }
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
+ function createProto(columns, span) {
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 = {};
60
+
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
+ };
68
+
69
+ span.legs.forEach(onEachLeg);
70
+
71
+ function onEachLeg(leg) {
72
+ leg.accept(c);
73
+ }
74
+
75
+ return obj;
76
+ }
77
+
78
+ function hasManyRelations(span) {
79
+ let result;
80
+ const c = {};
81
+ c.visitJoin = () => { };
82
+ c.visitOne = c.visitJoin;
83
+ c.visitMany = function () {
84
+ result = true;
85
+ };
86
+
87
+ span.legs.forEach(onEachLeg);
88
+ return result;
89
+
90
+ function onEachLeg(leg) {
91
+ leg.accept(c);
92
+ }
93
+ }
94
+
95
+ async function decode(strategy, span, rows, keys = rows.length > 0 ? Object.keys(rows[0]) : []) {
96
+ const table = span.table;
97
+ let columnsMap = span.columns;
98
+ const columns = table._columns.filter(column => !columnsMap || columnsMap.get(column));
99
+ const rowsLength = rows.length;
100
+ const columnsLength = columns.length;
101
+ const primaryColumns = table._primaryColumns;
102
+ const primaryColumnsLength = primaryColumns.length;
103
+ const rowsMap = new Map();
104
+ const fkIds = new Array(rows.length);
105
+ const getIds = createGetIds();
106
+ const aggregateKeys = Object.keys(span.aggregates);
107
+
108
+ const outRows = new Array(rowsLength);
109
+ const createRow = newCreateRow(span);
110
+ const shouldCreateMap = hasManyRelations(span);
111
+ for (let i = 0; i < rowsLength; i++) {
112
+ const row = rows[i];
113
+ let outRow = createRow();
114
+ let pkWithNullCount = 0;
115
+ for (let j = 0; j < columnsLength; j++) {
116
+ if (j < primaryColumnsLength) {
117
+ if (row[keys[j]] === null)
118
+ pkWithNullCount++;
119
+ if (pkWithNullCount === primaryColumnsLength) {
120
+ outRow = null;
121
+ break;
122
+ }
123
+ }
124
+ const column = columns[j];
125
+ outRow[column.alias] = column.decode(row[keys[j]]);
126
+ }
127
+
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
+ }
133
+
134
+ outRows[i] = outRow;
135
+ if (shouldCreateMap) {
136
+ fkIds[i] = getIds(outRow);
137
+ addToMap(rowsMap, primaryColumns, outRow);
138
+ }
139
+ }
140
+ span._rowsMap = rowsMap;
141
+ span._ids = fkIds;
142
+
143
+ keys.splice(0, columnsLength + aggregateKeys.length);
144
+ if (span.legs.toArray().length === 0)
145
+ return outRows;
146
+
147
+ //alt 1
148
+ // 3.1 sec
149
+ // await new Promise((resolve, reject) => {
150
+ // setTimeout(() => {
151
+ // if (shouldCreateMap)
152
+ // decodeManyRelations(strategy, span, rows, outRows, keys)
153
+ // .then(() => decodeRelations2(strategy, span, rows, outRows, keys))
154
+ // .then(resolve)
155
+ // .catch(reject);
156
+ // else
157
+ // decodeRelations2(strategy, span, rows, outRows, keys)
158
+ // .then(resolve)
159
+ // .catch(reject);
160
+ // }, 0);
161
+ // });
162
+
163
+
164
+ //alt 1
165
+ // 3.2 sec
166
+ // await new Promise((resolve, reject) => {
167
+ // setTimeout(() => {
168
+ // const all = [];
169
+ // if (shouldCreateMap)
170
+ // all.push(decodeManyRelations(strategy, span, rows, outRows, keys))
171
+
172
+ // all.push(new Promise((resolve, reject) => {
173
+ // setTimeout(async () => {
174
+ // await decodeRelations2(strategy, span, rows, outRows, keys);
175
+ // resolve();
176
+
177
+ // }, 0);
178
+ // }));
179
+ // // const p2 = decodeRelations2(strategy, span, rows, outRows, keys);
180
+ // Promise.all(all).then(resolve, reject);
181
+ // }, 0);
182
+ // });
183
+
184
+
185
+ // let all = [];;
186
+ // if(shouldCreateMap) {
187
+
188
+ // await decodeManyRelations(strategy, span, rows, outRows, keys).then(() => decodeRelations2(strategy, span, rows, outRows, keys));
189
+ // }
190
+ // else
191
+ // await decodeRelations2(strategy, span, rows, outRows, keys);
192
+
193
+ // await Promise.all(all);
194
+
195
+
196
+
197
+ //alt 3
198
+ // 3.05 sec
199
+ let all = [];;
200
+
201
+ if(shouldCreateMap) {
202
+
203
+ all.push( decodeManyRelations(strategy, span, rows, outRows, keys) );
204
+ all.push( decodeRelations2(strategy, span, rows, outRows, keys) );
205
+ }
206
+ else
207
+ all.push( decodeRelations2(strategy, span, rows, outRows, keys) );
208
+
209
+ await Promise.all(all);
210
+
211
+ //alt 4
212
+ // 3.02 sec
213
+ // const all = [];
214
+ // if (shouldCreateMap)
215
+ // all.push(decodeManyRelations(strategy, span, rows, outRows, keys));
216
+ // all.push(new Promise((resolve, reject) => {
217
+ // setTimeout(async () => {
218
+ // await decodeRelations2(strategy, span, rows, outRows, keys);
219
+ // resolve();
220
+
221
+ // }, 0);
222
+ // }));
223
+ // await Promise.all(all);
224
+
225
+ // all.push(decodeRelations2(strategy, span, rows, outRows, keys));
226
+
227
+ //alt 4
228
+ // 3.3 sec
229
+ // if(shouldCreateMap)
230
+ // await decodeRelations(strategy, span, rows, outRows, keys);
231
+ // else
232
+ // await decodeRelations2(strategy, span, rows, outRows, keys);
233
+
234
+ // await decodeRelationsNext(strategy, span, rows, outRows, keys);
235
+ // await decodeRelations(strategy, span, rows, outRows, keys);
236
+
237
+ return outRows;
238
+
239
+
240
+ function createGetIds() {
241
+ const primaryColumns = table._primaryColumns;
242
+ const length = primaryColumns.length;
243
+ if (length === 1) {
244
+ const alias = table._primaryColumns[0].alias;
245
+ return (row) => row[alias];
246
+ }
247
+ else
248
+ return (row) => {
249
+ const result = new Array(length);
250
+ for (let i = 0; i < length; i++) {
251
+ result[i] = row[primaryColumns[i].alias];
252
+ }
253
+ return result;
254
+ };
255
+ }
256
+
257
+ }
258
+
259
+ async function decodeRelations2(strategy, span, rawRows, resultRows, keys) {
260
+ const promises = [];
261
+ const c = {};
262
+ c.visitJoin = function (leg) {
263
+ const name = leg.name;
264
+ const p = decode(strategy[name], leg.span, rawRows, keys).then((rows) => {
265
+ for (let i = 0; i < rows.length; i++) {
266
+ resultRows[i][name] = rows[i];
267
+ }
268
+ });
269
+ promises.push(p);
270
+ };
271
+
272
+ c.visitOne = c.visitJoin;
273
+
274
+ c.visitMany = () => {};
275
+
276
+
277
+ span.legs.forEach(onEachLeg);
278
+
279
+ function onEachLeg(leg) {
280
+ leg.accept(c);
281
+ }
282
+
283
+ await Promise.all(promises);
284
+ }
285
+
286
+
287
+ async function decodeManyRelations(strategy, span, rawRows, resultRows, keys) {
288
+ const promises = [];
289
+ const c = {};
290
+ c.visitJoin = () => { };
291
+ c.visitOne = c.visitJoin;
292
+
293
+ c.visitMany = function (leg) {
294
+ const name = leg.name;
295
+ const table = span.table;
296
+ const relation = table._relations[name];
297
+ const filter = createOneFilter(relation, span._ids);
298
+ const rowsMap = span._rowsMap;
299
+ const p = getManyDto(relation.childTable, filter, strategy[name], leg.span).then(subRows => {
300
+ for (let i = 0; i < subRows.length; i++) {
301
+ const key = leg.columns.map(column => subRows[i][column.alias]);
302
+ const parentRow = getFromMap(rowsMap, table._primaryColumns, key);
303
+ parentRow[name].push(subRows[i]);
304
+ }
305
+ });
306
+ promises.push(p);
307
+ };
308
+
309
+ span.legs.forEach(onEachLeg);
310
+
311
+ function onEachLeg(leg) {
312
+ leg.accept(c);
313
+ }
314
+
315
+ // await Promise.all(promises);
316
+ // }
317
+
318
+ // async function decodeRelations2(strategy, span, rawRows, resultRows, keys) {
319
+ // const promises = [];
320
+ // const c = {};
321
+ c.visitJoin = function (leg) {
322
+ const name = leg.name;
323
+ //todo avoid traverse twice noMany relations below
324
+ const p = decode(strategy[name], leg.span, rawRows, keys).then((rows) => {
325
+ for (let i = 0; i < rows.length; i++) {
326
+ resultRows[i][name] = rows[i];
327
+ }
328
+ });
329
+ promises.push(p);
330
+ };
331
+
332
+ c.visitOne = c.visitJoin;
333
+
334
+ c.visitMany = () => { };
335
+
336
+ span.legs.forEach(onEachLeg);
337
+
338
+ function onEachLeg(leg) {
339
+ leg.accept(c);
340
+ }
341
+
342
+ await Promise.all(promises);
343
+ }
344
+
345
+ async function decodeRelations3(strategy, span, rawRows, resultRows, keys) {
346
+
347
+ let previous = new Promise((resolve) => setImmediate(() => resolve()));
348
+ const c = {};
349
+
350
+ c.visitJoin = function (leg) {
351
+ const name = leg.name;
352
+ const p = new Promise((resolve) => {
353
+ setImmediate(() => {
354
+ decode(strategy[name], leg.span, rawRows, keys).then((rows) => {
355
+ for (let i = 0; i < rows.length; i++) {
356
+ resultRows[i][name] = rows[i];
357
+ }
358
+ resolve();
359
+ });
360
+ });
361
+ });
362
+ previous = previous.then(() => p);
363
+ };
364
+
365
+ c.visitOne = c.visitJoin;
366
+
367
+ c.visitMany = () => {};
368
+
369
+ span.legs.forEach(onEachLeg);
370
+
371
+ function onEachLeg(leg) {
372
+ leg.accept(c);
373
+ }
374
+
375
+ await previous;
376
+
377
+ }
378
+
379
+
380
+ async function decodeRelationsNext(strategy, span, rawRows, resultRows, keys) {
381
+ // const promises = [];
382
+ let previous = new Promise((resolve) => setImmediate(() => resolve()));
383
+ const c = {};
384
+
385
+ c.visitJoin = function (leg) {
386
+ const name = leg.name;
387
+ const p = new Promise((resolve) => {
388
+ setImmediate(() => {
389
+ 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
+ resolve();
394
+ });
395
+ });
396
+ });
397
+ previous = previous.then(() => p);
398
+ };
399
+
400
+ c.visitOne = c.visitJoin;
401
+
402
+ c.visitMany = function (leg) {
403
+ const p = new Promise((resolve,reject) => {
404
+ setImmediate( () => {
405
+
406
+ const name = leg.name;
407
+ const table = span.table;
408
+ const relation = table._relations[name];
409
+ const filter = createOneFilter(relation, span._ids);
410
+ const rowsMap = span._rowsMap;
411
+
412
+ getManyDto(relation.childTable, filter, strategy[name], leg.span).then(subRows => {
413
+ for (let i = 0; i < subRows.length; i++) {
414
+ const key = leg.columns.map(column => subRows[i][column.alias]);
415
+ const parentRow = getFromMap(rowsMap, table._primaryColumns, key);
416
+ parentRow[name].push(subRows[i]);
417
+ }
418
+ }).then(() => resolve(), reject);
419
+
420
+
421
+ });
422
+ });
423
+
424
+
425
+ previous = previous.then(() => p);
426
+ // promises.push(p);
427
+ };
428
+
429
+ span.legs.forEach(onEachLeg);
430
+
431
+ function onEachLeg(leg) {
432
+ leg.accept(c);
433
+ }
434
+
435
+ await previous
436
+ }
437
+
438
+
439
+ async function decodeRelations(strategy, span, rawRows, resultRows, keys) {
440
+ const promises = [];
441
+ const c = {};
442
+ c.visitJoin = function (leg) {
443
+ const name = leg.name;
444
+ const p = decode(strategy[name], leg.span, rawRows, keys).then((rows) => {
445
+ for (let i = 0; i < rows.length; i++) {
446
+ resultRows[i][name] = rows[i];
447
+ }
448
+ });
449
+ promises.push(p);
450
+ };
451
+
452
+ c.visitOne = c.visitJoin;
453
+
454
+ c.visitMany = function (leg) {
455
+ const name = leg.name;
456
+ const table = span.table;
457
+ const relation = table._relations[name];
458
+ const filter = createOneFilter(relation, span._ids);
459
+ const rowsMap = span._rowsMap;
460
+ const p = getManyDto(relation.childTable, filter, strategy[name], leg.span).then(subRows => {
461
+ for (let i = 0; i < subRows.length; i++) {
462
+ const key = leg.columns.map(column => subRows[i][column.alias]);
463
+ const parentRow = getFromMap(rowsMap, table._primaryColumns, key);
464
+ parentRow[name].push(subRows[i]);
465
+ }
466
+ });
467
+ promises.push(p);
468
+ };
469
+
470
+
471
+ span.legs.forEach(onEachLeg);
472
+
473
+ function onEachLeg(leg) {
474
+ leg.accept(c);
475
+ }
476
+
477
+ await Promise.all(promises);
478
+ }
479
+
480
+ function createOneFilter(relation, ids) {
481
+ const columns = relation.joinRelation.columns;
482
+
483
+ if (columns.length === 1)
484
+ return columns[0].in(ids);
485
+
486
+ else
487
+ return createCompositeFilter();
488
+
489
+ function createCompositeFilter() {
490
+ let filter = emptyFilter;
491
+ for (let id of ids) {
492
+ let nextFilter;
493
+ for (let i = 0; i < columns.length; i++) {
494
+ if (nextFilter)
495
+ nextFilter = nextFilter.and(columns[i].eq(id[i]));
496
+ else
497
+ nextFilter = columns[i].eq(id[i]);
498
+ }
499
+ filter = filter.or(nextFilter);
500
+ }
501
+ return filter;
502
+ }
503
+ }
504
+
505
+ function addToMap(map, primaryColumns, row) {
506
+
507
+ const lastIndex = primaryColumns.length - 1;
508
+ for (let i = 0; i < lastIndex; i++) {
509
+ const id = row[primaryColumns[i].alias];
510
+ if (map.has(id))
511
+ map = map.get(id);
512
+ else {
513
+ const next = new Map();
514
+ map.set(id, next);
515
+ map = next;
516
+ }
517
+ }
518
+ map.set(row[primaryColumns[lastIndex].alias], row);
519
+ }
520
+
521
+ function getFromMap(map, primaryColumns, values) {
522
+ const length = primaryColumns.length;
523
+ for (let i = 0; i < length; i++) {
524
+ map = map.get(values[i]);
525
+ }
526
+ return map;
527
+ }
528
+
529
+
530
+ module.exports = getManyDto;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "orange-orm",
3
- "version": "4.3.0-beta.3",
3
+ "version": "4.4.0-beta.0",
4
4
  "main": "./src/index.js",
5
5
  "browser": "./src/client/index.mjs",
6
6
  "bin": {
@@ -104,11 +104,11 @@
104
104
  "devDependencies": {
105
105
  "@rollup/plugin-commonjs": "^21.0.1",
106
106
  "@rollup/plugin-node-resolve": "^13.0.0",
107
- "@typescript-eslint/eslint-plugin": "^5.42.1",
108
- "@typescript-eslint/parser": "^5.42.1",
107
+ "@typescript-eslint/eslint-plugin": "^6.x",
108
+ "@typescript-eslint/parser": "^6.x",
109
109
  "@vitest/coverage-v8": "^0.34.1",
110
110
  "cors": "^2.8.5",
111
- "eslint": "^8.6.0",
111
+ "eslint": "^8.57.0",
112
112
  "eslint-plugin-jest": "^27.1.7",
113
113
  "express": "^4.18.2",
114
114
  "msnodesqlv8": "^4.1.0",