orange-orm 4.4.0-beta.0 → 4.4.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.
@@ -1,293 +0,0 @@
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
- const promises = [];
112
- for (let i = 0; i < rowsLength; i++) {
113
- const row = rows[i];
114
- let outRow = createRow();
115
- let pkWithNullCount = 0;
116
- for (let j = 0; j < primaryColumnsLength; j++) {
117
- if (row[keys[j]] === null)
118
- pkWithNullCount++;
119
- if (pkWithNullCount === primaryColumnsLength) {
120
- outRow = null;
121
- break;
122
- }
123
- const column = columns[j];
124
- outRow[column.alias] = column.decode(row[keys[j]]);
125
- }
126
-
127
- outRows[i] = outRow;
128
- if (outRow === null)
129
- continue;
130
- if (shouldCreateMap) {
131
- fkIds[i] = getIds(outRow);
132
- addToMap(rowsMap, primaryColumns, outRow);
133
- }
134
-
135
- // Execute code inside start and stop in the next event loop and as a promise
136
- promises.push(new Promise((resolve) => {
137
- ((currentRow, currentOutRow) => {
138
- setTimeout(async () => {
139
- for (let j = primaryColumnsLength; j < columnsLength; j++) {
140
- const column = columns[j];
141
- currentOutRow[column.alias] = column.decode(currentRow[keys[j]]);
142
- }
143
-
144
- for (let j = 0; j < aggregateKeys.length; j++) {
145
- const key = aggregateKeys[j];
146
- const parse = span.aggregates[key].column?.decode || Number.parseFloat;
147
- currentOutRow[key] = parse(currentRow[keys[j + columnsLength]]);
148
- }
149
-
150
- resolve();
151
- }, 0);
152
- })(row, outRow);
153
- }));
154
- }
155
- span._rowsMap = rowsMap;
156
- span._ids = fkIds;
157
-
158
- const many = decodeManyRelations(strategy, span);
159
-
160
- const p = Promise.all(promises).then(() => {
161
- keys.splice(0, columnsLength + aggregateKeys.length);
162
- return decodeRelations(strategy, span, rows, outRows, keys)
163
- });
164
- await Promise.all([many, p]);
165
- return outRows;
166
-
167
-
168
- function createGetIds() {
169
- const primaryColumns = table._primaryColumns;
170
- const length = primaryColumns.length;
171
- if (length === 1) {
172
- const alias = table._primaryColumns[0].alias;
173
- return (row) => row[alias];
174
- }
175
- else
176
- return (row) => {
177
- const result = new Array(length);
178
- for (let i = 0; i < length; i++) {
179
- result[i] = row[primaryColumns[i].alias];
180
- }
181
- return result;
182
- };
183
- }
184
-
185
- }
186
-
187
- async function decodeManyRelations(strategy, span) {
188
- const promises = [];
189
- const c = {};
190
- c.visitJoin = () => { };
191
- c.visitOne = c.visitJoin;
192
-
193
- c.visitMany = function (leg) {
194
- const name = leg.name;
195
- const table = span.table;
196
- const relation = table._relations[name];
197
- const filter = createOneFilter(relation, span._ids);
198
- const rowsMap = span._rowsMap;
199
- const p = getManyDto(relation.childTable, filter, strategy[name], leg.span).then(subRows => {
200
- for (let i = 0; i < subRows.length; i++) {
201
- const key = leg.columns.map(column => subRows[i][column.alias]);
202
- const parentRow = getFromMap(rowsMap, table._primaryColumns, key);
203
- parentRow[name].push(subRows[i]);
204
- }
205
- });
206
- promises.push(p);
207
- };
208
-
209
- span.legs.forEach(onEachLeg);
210
-
211
- function onEachLeg(leg) {
212
- leg.accept(c);
213
- }
214
-
215
- await Promise.all(promises);
216
- }
217
- async function decodeRelations(strategy, span, rawRows, resultRows, keys) {
218
- const promises = [];
219
- const c = {};
220
- c.visitJoin = function (leg) {
221
- const name = leg.name;
222
- const p = decode(strategy[name], leg.span, rawRows, keys).then((rows) => {
223
- for (let i = 0; i < rows.length; i++) {
224
- resultRows[i][name] = rows[i];
225
- }
226
- });
227
- promises.push(p);
228
- };
229
-
230
- c.visitOne = c.visitJoin;
231
-
232
- c.visitMany = () => { };
233
-
234
- span.legs.forEach(onEachLeg);
235
-
236
- function onEachLeg(leg) {
237
- leg.accept(c);
238
- }
239
-
240
- await Promise.all(promises);
241
- }
242
-
243
- function createOneFilter(relation, ids) {
244
- const columns = relation.joinRelation.columns;
245
-
246
- if (columns.length === 1)
247
- return columns[0].in(ids);
248
-
249
- else
250
- return createCompositeFilter();
251
-
252
- function createCompositeFilter() {
253
- let filter = emptyFilter;
254
- for (let id of ids) {
255
- let nextFilter;
256
- for (let i = 0; i < columns.length; i++) {
257
- if (nextFilter)
258
- nextFilter = nextFilter.and(columns[i].eq(id[i]));
259
- else
260
- nextFilter = columns[i].eq(id[i]);
261
- }
262
- filter = filter.or(nextFilter);
263
- }
264
- return filter;
265
- }
266
- }
267
-
268
- function addToMap(map, primaryColumns, row) {
269
-
270
- const lastIndex = primaryColumns.length - 1;
271
- for (let i = 0; i < lastIndex; i++) {
272
- const id = row[primaryColumns[i].alias];
273
- if (map.has(id))
274
- map = map.get(id);
275
- else {
276
- const next = new Map();
277
- map.set(id, next);
278
- map = next;
279
- }
280
- }
281
- map.set(row[primaryColumns[lastIndex].alias], row);
282
- }
283
-
284
- function getFromMap(map, primaryColumns, values) {
285
- const length = primaryColumns.length;
286
- for (let i = 0; i < length; i++) {
287
- map = map.get(values[i]);
288
- }
289
- return map;
290
- }
291
-
292
-
293
- module.exports = getManyDto;