@trenskow/pged 4.0.6 → 4.0.9

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.6",
3
+ "version": "4.0.9",
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
@@ -189,8 +189,8 @@ module.exports = exports = class QueryBuilder extends CustomPromise {
189
189
  options.conditions = {};
190
190
  options.local = options.local || this._defaultPrimaryKey;
191
191
  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)}`;
192
+ let local = options.local.substring(0, 1) == ':' ? this._dbCase(options.local) : `:${this._table}.${this._dbCase(options.local)}`;
193
+ let foreign = options.foreign.substring(0, 1) == ':' ? this._dbCase(options.foreign.substring(1)) : `${this._dbCase(options.table)}.${this._dbCase(options.foreign)}`;
194
194
  options.conditions[local] = foreign;
195
195
  }
196
196
  options.conditions = this._formalizeConditions(options.conditions);
@@ -213,6 +213,22 @@ module.exports = exports = class QueryBuilder extends CustomPromise {
213
213
  return this;
214
214
  }
215
215
 
216
+ sum(key) {
217
+
218
+ if (key.key) {
219
+ if (key.table) key = `"${this._dbCase(key.table)}"."${this._dbCase(key.key)}"`;
220
+ else key = `"${this._dbCase(key.key)}"`;
221
+ }
222
+
223
+ this._selectKeys = [`:sum(${key}) AS sum`];
224
+ this._limit = 1;
225
+ this._first = 'sum';
226
+ this._defaultResult = 0;
227
+
228
+ return this;
229
+
230
+ }
231
+
216
232
  onConflict(keys, action) {
217
233
 
218
234
  if (this._command !== 'insert') throw new Error('`onConflict` is only available when inserting.');
@@ -254,11 +270,11 @@ module.exports = exports = class QueryBuilder extends CustomPromise {
254
270
 
255
271
  _buildKeys(keys = ['*'], quote) {
256
272
  return keys.map((key) => {
257
- if (key.substr(0, 1) == ':') return key.substr(1);
273
+ if (key.substring(0, 1) == ':') return key.substring(1);
258
274
  let as = key.split(':');
259
275
  if (as.length == 1) return this._dbCase(as[0], quote && this._canQuote(key));
260
276
  return `${this._dbCase(as[0], quote)} as ${this._dbCase(as[1])}`;
261
- }).concat(this._paginated ? 'count(*) over() as total' : []).join(', ');
277
+ }).concat(this._paginated ? `count(${this._table}.*) over() as total` : []).join(', ');
262
278
  }
263
279
 
264
280
  get _operatorMap() {
@@ -306,7 +322,7 @@ module.exports = exports = class QueryBuilder extends CustomPromise {
306
322
 
307
323
  let key = Object.keys(condition)[0];
308
324
 
309
- if (key.substr(0, 1) == '$') {
325
+ if (key.substring(0, 1) == '$') {
310
326
  switch (caseit(key)) {
311
327
  case '$or':
312
328
  case '$and':
@@ -328,15 +344,15 @@ module.exports = exports = class QueryBuilder extends CustomPromise {
328
344
  }
329
345
  }
330
346
 
331
- if (key.substr(0, 1) == ':') {
332
- return this._buildCondition(key.substr(1), comparer, this._dbCase(condition[key]));
347
+ if (key.substring(0, 1) == ':') {
348
+ return this._buildCondition(key.substring(1), comparer, this._dbCase(condition[key]));
333
349
  }
334
350
 
335
351
  let dbKey = key;
336
352
 
337
353
  if (dbKey.indexOf('.') == -1) {
338
- if (dbKey.substr(0, 1) === '!') {
339
- dbKey = dbKey.substr(1);
354
+ if (dbKey.substring(0, 1) === '!') {
355
+ dbKey = dbKey.substring(1);
340
356
  } else {
341
357
  dbKey = dbKey
342
358
  .split('->')
@@ -405,11 +421,11 @@ module.exports = exports = class QueryBuilder extends CustomPromise {
405
421
  _buildSorting() {
406
422
  if (!this._sortingKeys.length) return;
407
423
  const escapeIfNeeded = (value) => {
408
- if (value.substr(0, 1) == ':') return value.substr(1);
424
+ if (value.substring(0, 1) == ':') return value.substring(1);
409
425
  return this._dbCase(value, true);
410
426
  };
411
427
  return `order by ${this._sortingKeys.map((key) => {
412
- if (key.substr(0, 1) == '-') return `${escapeIfNeeded(key.substr(1))} desc`;
428
+ if (key.substring(0, 1) == '-') return `${escapeIfNeeded(key.substring(1))} desc`;
413
429
  return escapeIfNeeded(key);
414
430
  }).join(', ')}`;
415
431
  }
@@ -430,7 +446,7 @@ module.exports = exports = class QueryBuilder extends CustomPromise {
430
446
  if (value == null) {
431
447
  value = 'null';
432
448
  } else if (/^:/.test(value)) {
433
- value = value.substr(1);
449
+ value = value.substring(1);
434
450
  } else {
435
451
  this._queryParameters.push(value);
436
452
  value = `$${this._queryParameters.length}`;