@trenskow/pged 4.0.7 → 4.1.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/index.js CHANGED
@@ -229,7 +229,7 @@ module.exports = exports = class PGed {
229
229
 
230
230
  const [query, parameters] = queryBuilder._build();
231
231
 
232
- return await this.exec(
232
+ let result = await this.exec(
233
233
  query,
234
234
  parameters,
235
235
  {
@@ -237,6 +237,12 @@ module.exports = exports = class PGed {
237
237
  transaction: queryBuilder._transaction
238
238
  });
239
239
 
240
+ if (['null', 'undefined'].includes(typeof result)) {
241
+ result = queryBuilder._defaultResult;
242
+ }
243
+
244
+ return result;
245
+
240
246
  });
241
247
  }
242
248
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trenskow/pged",
3
- "version": "4.0.7",
3
+ "version": "4.1.0",
4
4
  "description": "Just a silly little db management and query builder for PostgreSQL.",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/query-builder.js CHANGED
@@ -49,8 +49,8 @@ module.exports = exports = class QueryBuilder extends CustomPromise {
49
49
  .split('.')
50
50
  .map((part) => {
51
51
  let doQuote = quote;
52
- if (part.substr(0, 1) === '!' && quote) {
53
- part = part.substr(1);
52
+ if (part.substring(0, 1) === '!' && quote) {
53
+ part = part.substring(1);
54
54
  doQuote = false;
55
55
  }
56
56
  part = part
@@ -118,9 +118,17 @@ module.exports = exports = class QueryBuilder extends CustomPromise {
118
118
  return this;
119
119
  }
120
120
 
121
- sorted(keys) {
122
- if (!Array.isArray(keys)) keys = keys.split(/, ?/);
123
- this._sortingKeys = keys;
121
+ sorted(sortingKeys) {
122
+ if (typeof sortingKeys === 'string') sortingKeys = sortingKeys.split(/, ?/);
123
+ if (!Array.isArray(sortingKeys)) sortingKeys = [sortingKeys];
124
+ sortingKeys = sortingKeys.map((sortingKey) => {
125
+ if (typeof sortingKey === 'string') return {
126
+ key: sortingKey.substring(0, 1) === '-' ? sortingKey.substring(1) : sortingKey,
127
+ order: sortingKey.substring(0, 1) === '-' ? 'desc' : 'asc'
128
+ };
129
+ return sortingKey;
130
+ });
131
+ this._sortingKeys = this._sortingKeys.concat(sortingKeys);
124
132
  return this;
125
133
  }
126
134
 
@@ -189,8 +197,8 @@ module.exports = exports = class QueryBuilder extends CustomPromise {
189
197
  options.conditions = {};
190
198
  options.local = options.local || this._defaultPrimaryKey;
191
199
  options.foreign = options.foreign || this._defaultPrimaryKey;
192
- let local = options.local.substr(0, 1) == ':' ? this._dbCase(options.local) : `:${this._table}.${this._dbCase(options.local)}`;
193
- let foreign = options.foreign.substr(0, 1) == ':' ? this._dbCase(options.foreign.substr(1)) : `${this._dbCase(options.table)}.${this._dbCase(options.foreign)}`;
200
+ let local = options.local.substring(0, 1) == ':' ? this._dbCase(options.local) : `:${this._table}.${this._dbCase(options.local)}`;
201
+ let foreign = options.foreign.substring(0, 1) == ':' ? this._dbCase(options.foreign.substring(1)) : `${this._dbCase(options.table)}.${this._dbCase(options.foreign)}`;
194
202
  options.conditions[local] = foreign;
195
203
  }
196
204
  options.conditions = this._formalizeConditions(options.conditions);
@@ -213,6 +221,22 @@ module.exports = exports = class QueryBuilder extends CustomPromise {
213
221
  return this;
214
222
  }
215
223
 
224
+ sum(key) {
225
+
226
+ if (key.key) {
227
+ if (key.table) key = `"${this._dbCase(key.table)}"."${this._dbCase(key.key)}"`;
228
+ else key = `"${this._dbCase(key.key)}"`;
229
+ }
230
+
231
+ this._selectKeys = [`:sum(${key}) AS sum`];
232
+ this._limit = 1;
233
+ this._first = 'sum';
234
+ this._defaultResult = 0;
235
+
236
+ return this;
237
+
238
+ }
239
+
216
240
  onConflict(keys, action) {
217
241
 
218
242
  if (this._command !== 'insert') throw new Error('`onConflict` is only available when inserting.');
@@ -254,7 +278,7 @@ module.exports = exports = class QueryBuilder extends CustomPromise {
254
278
 
255
279
  _buildKeys(keys = ['*'], quote) {
256
280
  return keys.map((key) => {
257
- if (key.substr(0, 1) == ':') return key.substr(1);
281
+ if (key.substring(0, 1) == ':') return key.substring(1);
258
282
  let as = key.split(':');
259
283
  if (as.length == 1) return this._dbCase(as[0], quote && this._canQuote(key));
260
284
  return `${this._dbCase(as[0], quote)} as ${this._dbCase(as[1])}`;
@@ -306,7 +330,7 @@ module.exports = exports = class QueryBuilder extends CustomPromise {
306
330
 
307
331
  let key = Object.keys(condition)[0];
308
332
 
309
- if (key.substr(0, 1) == '$') {
333
+ if (key.substring(0, 1) == '$') {
310
334
  switch (caseit(key)) {
311
335
  case '$or':
312
336
  case '$and':
@@ -328,15 +352,15 @@ module.exports = exports = class QueryBuilder extends CustomPromise {
328
352
  }
329
353
  }
330
354
 
331
- if (key.substr(0, 1) == ':') {
332
- return this._buildCondition(key.substr(1), comparer, this._dbCase(condition[key]));
355
+ if (key.substring(0, 1) == ':') {
356
+ return this._buildCondition(key.substring(1), comparer, this._dbCase(condition[key]));
333
357
  }
334
358
 
335
359
  let dbKey = key;
336
360
 
337
361
  if (dbKey.indexOf('.') == -1) {
338
- if (dbKey.substr(0, 1) === '!') {
339
- dbKey = dbKey.substr(1);
362
+ if (dbKey.substring(0, 1) === '!') {
363
+ dbKey = dbKey.substring(1);
340
364
  } else {
341
365
  dbKey = dbKey
342
366
  .split('->')
@@ -403,15 +427,24 @@ module.exports = exports = class QueryBuilder extends CustomPromise {
403
427
  }
404
428
 
405
429
  _buildSorting() {
430
+
406
431
  if (!this._sortingKeys.length) return;
432
+
407
433
  const escapeIfNeeded = (value) => {
408
- if (value.substr(0, 1) == ':') return value.substr(1);
434
+ if (value.substring(0, 1) == ':') return value.substring(1);
409
435
  return this._dbCase(value, true);
410
436
  };
411
- return `order by ${this._sortingKeys.map((key) => {
412
- if (key.substr(0, 1) == '-') return `${escapeIfNeeded(key.substr(1))} desc`;
413
- return escapeIfNeeded(key);
437
+
438
+ return `order by ${this._sortingKeys.map((sortingKey) => {
439
+ let condition = escapeIfNeeded(sortingKey.key);
440
+ if (Array.isArray(sortingKey.values)) {
441
+ condition = `case ${this._dbCase(sortingKey.key)} ${sortingKey.values.map((value, idx) => {
442
+ return `when '${value}' then ${idx}`;
443
+ }).join(' ')} end`;
444
+ }
445
+ return `${condition}${sortingKey.order === 'desc' ? ' desc' : ''}`;
414
446
  }).join(', ')}`;
447
+
415
448
  }
416
449
 
417
450
  _buildOffset() {
@@ -430,7 +463,7 @@ module.exports = exports = class QueryBuilder extends CustomPromise {
430
463
  if (value == null) {
431
464
  value = 'null';
432
465
  } else if (/^:/.test(value)) {
433
- value = value.substr(1);
466
+ value = value.substring(1);
434
467
  } else {
435
468
  this._queryParameters.push(value);
436
469
  value = `$${this._queryParameters.length}`;