@trenskow/pged 4.0.4 → 4.0.8

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.4",
3
+ "version": "4.0.8",
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
@@ -35,7 +35,6 @@ module.exports = exports = class QueryBuilder extends CustomPromise {
35
35
  this._having = [];
36
36
 
37
37
  this._offset = 0;
38
- this._limit = Infinity;
39
38
 
40
39
  this._executor = executor;
41
40
 
@@ -130,7 +129,7 @@ module.exports = exports = class QueryBuilder extends CustomPromise {
130
129
  return this;
131
130
  }
132
131
 
133
- limitTo(limit = Infinity) {
132
+ limitTo(limit) {
134
133
  this._limit = limit;
135
134
  return this;
136
135
  }
@@ -214,6 +213,22 @@ module.exports = exports = class QueryBuilder extends CustomPromise {
214
213
  return this;
215
214
  }
216
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
+
217
232
  onConflict(keys, action) {
218
233
 
219
234
  if (this._command !== 'insert') throw new Error('`onConflict` is only available when inserting.');
@@ -259,7 +274,7 @@ module.exports = exports = class QueryBuilder extends CustomPromise {
259
274
  let as = key.split(':');
260
275
  if (as.length == 1) return this._dbCase(as[0], quote && this._canQuote(key));
261
276
  return `${this._dbCase(as[0], quote)} as ${this._dbCase(as[1])}`;
262
- }).concat(this._paginated ? 'count(*) over() as total' : []).join(', ');
277
+ }).concat(this._paginated ? `count(${this._table}.*) over() as total` : []).join(', ');
263
278
  }
264
279
 
265
280
  get _operatorMap() {
@@ -421,7 +436,7 @@ module.exports = exports = class QueryBuilder extends CustomPromise {
421
436
  }
422
437
 
423
438
  _buildLimit() {
424
- if ((this._limit || Infinity) == Infinity) return;
439
+ if (typeof this._limit === 'undefined') return;
425
440
  return `limit ${this._limit}`;
426
441
  }
427
442