orange-orm 4.2.0 → 4.3.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "orange-orm",
3
- "version": "4.2.0",
3
+ "version": "4.3.0-beta.0",
4
4
  "main": "./src/index.js",
5
5
  "browser": "./src/client/index.mjs",
6
6
  "bin": {
@@ -63,6 +63,7 @@
63
63
  "glob": "^10.3.4",
64
64
  "module-definition": "^4.0.0",
65
65
  "node-cls": "^1.0.5",
66
+ "postgres": "^3.4.4",
66
67
  "promise": "^8.0.3",
67
68
  "rfdc": "^1.2.0",
68
69
  "uuid": "^8.3.2"
@@ -71,6 +72,7 @@
71
72
  "msnodesqlv8": "^4.1.0",
72
73
  "mysql2": "^2.2.5 || ^3.9.4",
73
74
  "oracledb": "^6.3.0",
75
+ "postgres": "^3.4.4",
74
76
  "pg": "^8.5.1",
75
77
  "pg-native": "^3.0.0",
76
78
  "pg-query-stream": "^3.3.2",
@@ -78,6 +80,9 @@
78
80
  "tedious": "^15.1.2 || ^16.0.0 || ^18.1.0"
79
81
  },
80
82
  "peerDependenciesMeta": {
83
+ "postgres": {
84
+ "optional": true
85
+ },
81
86
  "pg": {
82
87
  "optional": true
83
88
  },
@@ -114,6 +119,7 @@
114
119
  "mysql2": "^3.9.4",
115
120
  "oracledb": "^6.3.0",
116
121
  "owasp-dependency-check": "^0.0.21",
122
+ "postgres": "^3.4.4",
117
123
  "pg": "^8.5.1",
118
124
  "pg-query-stream": "^3.3.2",
119
125
  "rollup": "^2.52.7",
@@ -0,0 +1,301 @@
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 < primaryColumnsLength; j++) {
116
+ if (row[keys[j]] === null)
117
+ pkWithNullCount++;
118
+ if (pkWithNullCount === primaryColumnsLength) {
119
+ outRow = null;
120
+ break;
121
+ }
122
+ const column = columns[j];
123
+ outRow[column.alias] = column.decode(row[keys[j]]);
124
+ }
125
+
126
+ outRows[i] = outRow;
127
+ if (shouldCreateMap) {
128
+ fkIds[i] = getIds(outRow);
129
+ addToMap(rowsMap, primaryColumns, outRow);
130
+ }
131
+ }
132
+ span._rowsMap = rowsMap;
133
+ span._ids = fkIds;
134
+ const manyPromise = decodeManyRelations(strategy, span, rows, outRows, keys);
135
+
136
+ for (let i = 0; i < rowsLength; i++) {
137
+ const row = rows[i];
138
+ let outRow = outRows[i];
139
+ if (outRow === null)
140
+ continue;
141
+ for (let j = primaryColumnsLength; j < columnsLength; j++) {
142
+ const column = columns[j];
143
+ outRow[column.alias] = column.decode(row[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
+ outRow[key] = parse(row[keys[j+columnsLength]]);
150
+ }
151
+ }
152
+
153
+
154
+ keys.splice(0, columnsLength + aggregateKeys.length);
155
+
156
+ await decodeRelations(strategy, span, rows, outRows, keys);
157
+ await manyPromise;
158
+ return outRows;
159
+
160
+
161
+ function createGetIds() {
162
+ const primaryColumns = table._primaryColumns;
163
+ const length = primaryColumns.length;
164
+ if (length === 1) {
165
+ const alias = table._primaryColumns[0].alias;
166
+ return (row) => row[alias];
167
+ }
168
+ else
169
+ return (row) => {
170
+ const result = new Array(length);
171
+ for (let i = 0; i < length; i++) {
172
+ result[i] = row[primaryColumns[i].alias];
173
+ }
174
+ return result;
175
+ };
176
+ }
177
+
178
+ }
179
+
180
+ async function decodeManyRelations(strategy, span, rawRows, resultRows, keys) {
181
+ const promises = [];
182
+ const c = {};
183
+ c.visitJoin = () => {};
184
+ c.visitOne = c.visitJoin;
185
+
186
+ c.visitMany = function(leg) {
187
+ const name = leg.name;
188
+ const table = span.table;
189
+ const relation = table._relations[name];
190
+ const filter = createOneFilter(relation, span._ids);
191
+ const rowsMap = span._rowsMap;
192
+ const p = getManyDto(relation.childTable, filter, strategy[name], leg.span).then(subRows => {
193
+ for (let i = 0; i < subRows.length; i++) {
194
+ const key = leg.columns.map(column => subRows[i][column.alias]);
195
+ const parentRow = getFromMap(rowsMap, table._primaryColumns, key);
196
+ parentRow[name].push(subRows[i]);
197
+ }
198
+ });
199
+ promises.push(p);
200
+ };
201
+
202
+ span.legs.forEach(onEachLeg);
203
+
204
+ function onEachLeg(leg) {
205
+ leg.accept(c);
206
+ }
207
+
208
+ await Promise.all(promises);
209
+ }
210
+ async function decodeRelations(strategy, span, rawRows, resultRows, keys) {
211
+ const promises = [];
212
+ const c = {};
213
+ c.visitJoin = function(leg) {
214
+ const name = leg.name;
215
+ const p = decode(strategy[name], leg.span, rawRows, keys).then((rows) => {
216
+ for (let i = 0; i < rows.length; i++) {
217
+ resultRows[i][name] = rows[i];
218
+ }
219
+ });
220
+ promises.push(p);
221
+ };
222
+
223
+ c.visitOne = c.visitJoin;
224
+
225
+ c.visitMany = () => {};
226
+ // function(leg) {
227
+ // const name = leg.name;
228
+ // const table = span.table;
229
+ // const relation = table._relations[name];
230
+ // const filter = createOneFilter(relation, span._ids);
231
+ // const rowsMap = span._rowsMap;
232
+ // const p = getManyDto(relation.childTable, filter, strategy[name], leg.span).then(subRows => {
233
+ // for (let i = 0; i < subRows.length; i++) {
234
+ // const key = leg.columns.map(column => subRows[i][column.alias]);
235
+ // const parentRow = getFromMap(rowsMap, table._primaryColumns, key);
236
+ // parentRow[name].push(subRows[i]);
237
+ // }
238
+ // });
239
+ // promises.push(p);
240
+ // };
241
+
242
+ span.legs.forEach(onEachLeg);
243
+
244
+ function onEachLeg(leg) {
245
+ leg.accept(c);
246
+ }
247
+
248
+ await Promise.all(promises);
249
+ }
250
+
251
+ function createOneFilter(relation, ids) {
252
+ const columns = relation.joinRelation.columns;
253
+
254
+ if (columns.length === 1)
255
+ return columns[0].in(ids);
256
+
257
+ else
258
+ return createCompositeFilter();
259
+
260
+ function createCompositeFilter() {
261
+ let filter = emptyFilter;
262
+ for(let id of ids) {
263
+ let nextFilter;
264
+ for (let i = 0; i < columns.length; i++) {
265
+ if (nextFilter)
266
+ nextFilter = nextFilter.and(columns[i].eq(id[i]));
267
+ else
268
+ nextFilter = columns[i].eq(id[i]);
269
+ }
270
+ filter = filter.or(nextFilter);
271
+ }
272
+ return filter;
273
+ }
274
+ }
275
+
276
+ function addToMap(map, primaryColumns, row) {
277
+
278
+ const lastIndex = primaryColumns.length - 1;
279
+ for (let i = 0; i < lastIndex; i++) {
280
+ const id = row[primaryColumns[i].alias];
281
+ if (map.has(id))
282
+ map = map.get(id);
283
+ else {
284
+ const next = new Map();
285
+ map.set(id, next);
286
+ map = next;
287
+ }
288
+ }
289
+ map.set(row[primaryColumns[lastIndex].alias], row);
290
+ }
291
+
292
+ function getFromMap(map, primaryColumns, values) {
293
+ const length = primaryColumns.length;
294
+ for (let i = 0; i < length; i++) {
295
+ map = map.get(values[i]);
296
+ }
297
+ return map;
298
+ }
299
+
300
+
301
+ module.exports = getManyDto;
@@ -1,5 +1,5 @@
1
1
  function encodeBoolean(bool) {
2
- return bool.toString();
2
+ return bool;
3
3
  }
4
4
 
5
5
  module.exports = encodeBoolean;
@@ -1,7 +1,7 @@
1
1
  function encodeDate(date) {
2
2
  if (date.toISOString)
3
- return '\'' + date.toISOString() + '\'';
4
- return '\'' + date + '\'';
3
+ return '' + date.toISOString() + '';
4
+ return '' + date + '';
5
5
  }
6
6
 
7
7
  module.exports = encodeDate;
@@ -1,15 +1,16 @@
1
1
  function encode(arg) {
2
- if (Array.isArray(arg))
3
- return new JsonBArrayParam(arg);
4
- else
5
- return arg;
2
+ // if (Array.isArray(arg))
3
+ // return new JsonBArrayParam(arg);
4
+ // else
5
+ return arg;
6
6
  }
7
7
 
8
- class JsonBArrayParam {
9
- constructor(actualArray) { this.actualArray = actualArray; }
10
- toPostgres() {
11
- return JSON.stringify(this.actualArray);
12
- }
13
- }
8
+ //todo
9
+ // class JsonBArrayParam {
10
+ // constructor(actualArray) { this.actualArray = actualArray; }
11
+ // toPostgres() {
12
+ // return JSON.stringify(this.actualArray);
13
+ // }
14
+ // }
14
15
 
15
16
  module.exports = encode;
@@ -4,45 +4,19 @@ var EventEmitter = require('events').EventEmitter;
4
4
 
5
5
  var defaults = require('./defaults');
6
6
  var genericPool = require('../../generic-pool');
7
- var _pg = require('pg');
7
+ var pg = require('postgres');
8
8
 
9
9
  function newPgPool(connectionString, poolOptions) {
10
10
  poolOptions = poolOptions || {};
11
- let pg = poolOptions.native ? _pg.native : _pg;
12
11
  var pool = genericPool.Pool({
13
12
  max: poolOptions.size || poolOptions.poolSize || defaults.poolSize,
14
13
  idleTimeoutMillis: poolOptions.idleTimeout || defaults.poolIdleTimeout,
15
14
  reapIntervalMillis: poolOptions.reapIntervalMillis || defaults.reapIntervalMillis,
16
15
  log: poolOptions.log || defaults.poolLog,
17
16
  create: function(cb) {
18
- var client = new pg.Client(connectionString);
19
- client.connect(function(err) {
20
- if (err) return cb(err, null);
21
-
22
- //handle connected client background errors by emitting event
23
- //via the pg object and then removing errored client from the pool
24
- client.on('error', function(e) {
25
- pool.emit('error', e, client);
26
-
27
- // If the client is already being destroyed, the error
28
- // occurred during stream ending. Do not attempt to destroy
29
- // the client again.
30
- if (!client._destroying) {
31
- pool.destroy(client);
32
- }
33
- });
34
-
35
- // Remove connection from pool on disconnect
36
- client.on('end', function(_e) {
37
- // Do not enter infinite loop between pool.destroy
38
- // and client 'end' event...
39
- if (!client._destroying) {
40
- pool.destroy(client);
41
- }
42
- });
43
- client.poolCount = 0;
44
- return cb(null, client);
45
- });
17
+ var client = pg(connectionString, {max: 1});
18
+ client.poolCount = 0;
19
+ cb(null, client);
46
20
  },
47
21
  destroy: function(client) {
48
22
  client._destroying = true;
@@ -2,7 +2,7 @@ var log = require('../table/log');
2
2
  var replaceParamChar = require('./replaceParamChar');
3
3
 
4
4
  function wrapQuery(connection) {
5
- var runOriginalQuery = connection.query;
5
+ // var runOriginalQuery = connection.query;
6
6
  return runQuery;
7
7
 
8
8
  function runQuery(query, onCompleted) {
@@ -13,18 +13,15 @@ function wrapQuery(connection) {
13
13
  values: params,
14
14
  types: query.types
15
15
  };
16
- log.emitQuery({sql, parameters: params});
16
+ log.emitQuery({ sql, parameters: params });
17
17
 
18
- runOriginalQuery.call(connection, query, onInnerCompleted);
18
+ connection.unsafe(sql, params).then(onInnerCompleted, onCompleted);
19
+ // runOriginalQuery.call(connection, query, onInnerCompleted);
19
20
 
20
- function onInnerCompleted(err, result) {
21
- if (err)
22
- onCompleted(err);
23
- else {
24
- if (Array.isArray(result))
25
- result = result[result.length-1];
26
- onCompleted(null, result.rows);
27
- }
21
+ function onInnerCompleted(result) {
22
+ // if (Array.isArray(result))
23
+ // result = result[result.length - 1];
24
+ onCompleted(null, result);
28
25
  }
29
26
  }
30
27