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,313 +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
- 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
-
145
- await decodeRelations(strategy, span, rows, outRows, keys);
146
- return outRows;
147
-
148
-
149
- function createGetIds() {
150
- const primaryColumns = table._primaryColumns;
151
- const length = primaryColumns.length;
152
- if (length === 1) {
153
- const alias = table._primaryColumns[0].alias;
154
- return (row) => row[alias];
155
- }
156
- else
157
- return (row) => {
158
- const result = new Array(length);
159
- for (let i = 0; i < length; i++) {
160
- result[i] = row[primaryColumns[i].alias];
161
- }
162
- return result;
163
- };
164
- }
165
-
166
- }
167
-
168
- async function decodeRelations(strategy, span, rawRows, resultRows, keys) {
169
- const rowsMap = span._rowsMap;
170
- const table = span.table;
171
- const oneRelations = [];
172
- const manyPromises = [];
173
-
174
- span.legs.forEach(leg => {
175
-
176
- const name = leg.name;
177
- const relation = table._relations[name];
178
-
179
- if (relation.isMany) {
180
- const filter = createOneFilter(relation, span._ids);
181
- const p = getManyDto(relation.childTable, filter, strategy[name], leg.span).then(subRows => {
182
- for (const subRow of subRows) {
183
- const key = leg.columns.map(column => subRow[column.alias]);
184
- const parentRow = getFromMap(rowsMap, table._primaryColumns, key);
185
- if (!parentRow[name]) {
186
- parentRow[name] = [];
187
- }
188
- parentRow[name].push(subRow);
189
- }
190
- });
191
- manyPromises.push(p);
192
- }
193
-
194
- else
195
- oneRelations.push({ leg, promise: decode(strategy[name], leg.span, rawRows, keys) });
196
- });
197
-
198
-
199
-
200
-
201
- // Wait for all 'one' relations to be decoded
202
- const oneResults = await Promise.all(oneRelations.map(relation => relation.promise));
203
-
204
- // Update resultRows with 'one' relation results
205
- for (let i = 0; i < rawRows.length; i++) {
206
- for (let j = 0; j < oneRelations.length; j++) {
207
- const leg = oneRelations[j].leg;
208
-
209
- const name = leg.name;
210
- const rows = oneResults[j];
211
- resultRows[i][name] = rows[i];
212
-
213
- }
214
-
215
- }
216
-
217
- // Wait for all 'many' relation promises to be resolved
218
- await Promise.all(manyPromises);
219
- }
220
-
221
-
222
- async function decodeRelations2(strategy, span, rawRows, resultRows, keys) {
223
- const promises = [];
224
-
225
- const c = {};
226
- c.visitJoin = function (leg) {
227
- const name = leg.name;
228
- const p = decode(strategy[name], leg.span, rawRows, keys).then((rows) => {
229
- for (let i = 0; i < rows.length; i++) {
230
- resultRows[i][name] = rows[i];
231
- }
232
- });
233
- promises.push(p);
234
- };
235
-
236
- c.visitOne = c.visitJoin;
237
-
238
- c.visitMany = function (leg) {
239
- const name = leg.name;
240
- const table = span.table;
241
- const relation = table._relations[name];
242
- const filter = createOneFilter(relation, span._ids);
243
- const rowsMap = span._rowsMap;
244
- const p = getManyDto(relation.childTable, filter, strategy[name], leg.span).then(subRows => {
245
- for (let i = 0; i < subRows.length; i++) {
246
- const key = leg.columns.map(column => subRows[i][column.alias]);
247
- const parentRow = getFromMap(rowsMap, table._primaryColumns, key);
248
- parentRow[name].push(subRows[i]);
249
- }
250
- });
251
- promises.push(p);
252
- };
253
-
254
- span.legs.forEach(onEachLeg);
255
-
256
- function onEachLeg(leg) {
257
- leg.accept(c);
258
- }
259
-
260
- await Promise.all(promises);
261
- }
262
-
263
- function createOneFilter(relation, ids) {
264
- const columns = relation.joinRelation.columns;
265
-
266
- if (columns.length === 1)
267
- return columns[0].in(ids);
268
-
269
- else
270
- return createCompositeFilter();
271
-
272
- function createCompositeFilter() {
273
- let filter = emptyFilter;
274
- for (let id of ids) {
275
- let nextFilter;
276
- for (let i = 0; i < columns.length; i++) {
277
- if (nextFilter)
278
- nextFilter = nextFilter.and(columns[i].eq(id[i]));
279
- else
280
- nextFilter = columns[i].eq(id[i]);
281
- }
282
- filter = filter.or(nextFilter);
283
- }
284
- return filter;
285
- }
286
- }
287
-
288
- function addToMap(map, primaryColumns, row) {
289
-
290
- const lastIndex = primaryColumns.length - 1;
291
- for (let i = 0; i < lastIndex; i++) {
292
- const id = row[primaryColumns[i].alias];
293
- if (map.has(id))
294
- map = map.get(id);
295
- else {
296
- const next = new Map();
297
- map.set(id, next);
298
- map = next;
299
- }
300
- }
301
- map.set(row[primaryColumns[lastIndex].alias], row);
302
- }
303
-
304
- function getFromMap(map, primaryColumns, values) {
305
- const length = primaryColumns.length;
306
- for (let i = 0; i < length; i++) {
307
- map = map.get(values[i]);
308
- }
309
- return map;
310
- }
311
-
312
-
313
- module.exports = getManyDto;