@trenskow/pged 4.1.5 → 5.0.1

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.
@@ -5,14 +5,14 @@ module.exports = {
5
5
  'mocha': true
6
6
  },
7
7
  'parserOptions': {
8
- 'ecmaVersion': 2017
8
+ 'ecmaVersion': 2017,
9
+ 'sourceType': 'module'
9
10
  },
10
11
  'extends': 'eslint:recommended',
11
12
  'rules': {
12
13
  'indent': [
13
14
  'error',
14
- 'tab',
15
- { 'SwitchCase': 1 }
15
+ 'tab'
16
16
  ],
17
17
  'linebreak-style': [
18
18
  'error',
@@ -45,15 +45,6 @@ module.exports = {
45
45
  'allowEmptyCatch': true
46
46
  }
47
47
  ],
48
- 'no-trailing-spaces': [
49
- 'error', {
50
- 'ignoreComments': true
51
- }
52
- ],
53
- 'require-atomic-updates': 'off',
54
- 'eol-last': [
55
- 'error',
56
- 'always'
57
- ]
48
+ 'require-atomic-updates': 'off'
58
49
  }
59
50
  };
package/index.js CHANGED
@@ -1,20 +1,18 @@
1
- 'use strict';
1
+ import Puqeue from 'puqeue';
2
+ import caseit from '@trenskow/caseit';
3
+ import pg from 'pg';
4
+ import EventEmitter from '@trenskow/async-event-emitter';
2
5
 
3
- const
4
- Puqeue = require('puqeue'),
5
- caseit = require('@trenskow/caseit'),
6
- { Pool } = require('pg');
6
+ import QueryBuilder from './query-builder.js';
7
7
 
8
- const
9
- QueryBuilder = require('./query-builder'),
10
- EventEmitter = require('./event-emitter');
8
+ const { Pool } = pg;
11
9
 
12
10
  let id = 0;
13
11
 
14
12
  let pgOptions;
15
13
  let pool;
16
14
 
17
- module.exports = exports = class PGed extends EventEmitter {
15
+ export default class PGed extends EventEmitter {
18
16
 
19
17
  static get pg() {
20
18
  return pgOptions;
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "@trenskow/pged",
3
- "version": "4.1.5",
3
+ "version": "5.0.1",
4
4
  "description": "Just a silly little db management and query builder for PostgreSQL.",
5
5
  "main": "index.js",
6
+ "type": "module",
6
7
  "scripts": {
7
8
  "test": "echo \"Error: no test specified\" && exit 1"
8
9
  },
@@ -22,7 +23,8 @@
22
23
  },
23
24
  "homepage": "https://github.com/trenskow/pged#readme",
24
25
  "dependencies": {
25
- "@trenskow/caseit": "^1.1.4",
26
+ "@trenskow/async-event-emitter": "^0.0.2",
27
+ "@trenskow/caseit": "^1.3.0",
26
28
  "@trenskow/custom-promise": "^0.10.1",
27
29
  "pg": "^8.7.3",
28
30
  "puqeue": "^1.0.6"
package/query-builder.js CHANGED
@@ -1,14 +1,11 @@
1
- 'use strict';
2
-
3
- const
4
- caseit = require('@trenskow/caseit'),
5
- CustomPromise = require('@trenskow/custom-promise'),
6
- Puqeue = require('puqeue');
1
+ import caseit from '@trenskow/caseit';
2
+ import CustomPromise from '@trenskow/custom-promise';
3
+ import Puqeue from 'puqeue';
7
4
 
8
5
  const tableInformationQueue = new Puqeue();
9
6
  const tableInformation = {};
10
7
 
11
- module.exports = exports = class QueryBuilder extends CustomPromise {
8
+ export default class QueryBuilder extends CustomPromise {
12
9
 
13
10
  constructor(table, options = {}, connection) {
14
11
 
@@ -248,15 +245,15 @@ module.exports = exports = class QueryBuilder extends CustomPromise {
248
245
  if (!Array.isArray(keys)) keys = keys.split(/, ?/);
249
246
 
250
247
  switch (Object.keys(action || {})[0] || 'nothing') {
251
- case 'nothing':
252
- break;
253
- case 'update': {
254
- const [keys, values] = this._deconstructKeyValues(action.update);
255
- action.update = { keys, values };
256
- break;
257
- }
258
- default:
259
- throw new Error('Action `update` is only supported at this moment.');
248
+ case 'nothing':
249
+ break;
250
+ case 'update': {
251
+ const [keys, values] = this._deconstructKeyValues(action.update);
252
+ action.update = { keys, values };
253
+ break;
254
+ }
255
+ default:
256
+ throw new Error('Action `update` is only supported at this moment.');
260
257
  }
261
258
 
262
259
  this._onConflict = {
@@ -293,10 +290,10 @@ module.exports = exports = class QueryBuilder extends CustomPromise {
293
290
  let [table, key] = keyPath.split('.');
294
291
  if (typeof key === 'undefined') [table, key] = [this._table, table];
295
292
  switch ((tableInformation[caseit(table, this._options.casing.db)] || {})[caseit(key, this._options.casing.db)]) {
296
- case 'jsonb':
297
- return typeof value === 'string' ? value : JSON.stringify(value);
298
- default:
299
- return value;
293
+ case 'jsonb':
294
+ return typeof value === 'string' ? value : JSON.stringify(value);
295
+ default:
296
+ return value;
300
297
  }
301
298
  }
302
299
 
@@ -347,23 +344,23 @@ module.exports = exports = class QueryBuilder extends CustomPromise {
347
344
 
348
345
  if (key.substring(0, 1) == '$') {
349
346
  switch (caseit(key)) {
350
- case '$or':
351
- case '$and':
352
- return this._buildConditions(condition[key], key, comparer, true);
353
- case '$eq':
354
- case '$ne':
355
- case '$neq':
356
- case '$lt':
357
- case '$lte':
358
- case '$gt':
359
- case '$gte':
360
- case '$regexp':
361
- case '$jsonContains':
362
- case '$jsonNotContains':
363
- case '$jsonArrayContains':
364
- return this._buildConditions(condition[key], operator, key, true);
365
- default:
366
- throw new TypeError(`Unknown modifier ${caseit(key)}.`);
347
+ case '$or':
348
+ case '$and':
349
+ return this._buildConditions(condition[key], key, comparer, true);
350
+ case '$eq':
351
+ case '$ne':
352
+ case '$neq':
353
+ case '$lt':
354
+ case '$lte':
355
+ case '$gt':
356
+ case '$gte':
357
+ case '$regexp':
358
+ case '$jsonContains':
359
+ case '$jsonNotContains':
360
+ case '$jsonArrayContains':
361
+ return this._buildConditions(condition[key], operator, key, true);
362
+ default:
363
+ throw new TypeError(`Unknown modifier ${caseit(key)}.`);
367
364
  }
368
365
  }
369
366
 
@@ -386,12 +383,12 @@ module.exports = exports = class QueryBuilder extends CustomPromise {
386
383
 
387
384
  if (condition[key] == null) {
388
385
  switch (comparer) {
389
- case '$eq':
390
- return `${dbKey} is null`;
391
- case '$ne':
392
- return `${dbKey} is not null`;
393
- default:
394
- throw new TypeError(`Modifier ${comparer} is not usable with \`null\` values.`);
386
+ case '$eq':
387
+ return `${dbKey} is null`;
388
+ case '$ne':
389
+ return `${dbKey} is not null`;
390
+ default:
391
+ throw new TypeError(`Modifier ${comparer} is not usable with \`null\` values.`);
395
392
  }
396
393
  }
397
394
 
@@ -429,10 +426,10 @@ module.exports = exports = class QueryBuilder extends CustomPromise {
429
426
  if (join.conditions) {
430
427
  let type;
431
428
  switch (join.required) {
432
- case 'both': type = 'join'; break;
433
- case 'local': type = 'left join'; break;
434
- case 'foreign': type = 'right join'; break;
435
- case 'none': type = 'outer join'; break;
429
+ case 'both': type = 'join'; break;
430
+ case 'local': type = 'left join'; break;
431
+ case 'foreign': type = 'right join'; break;
432
+ case 'none': type = 'outer join'; break;
436
433
  }
437
434
  return `${type} ${this._dbCase(join.table)} on ${this._buildConditions(join.conditions)}`;
438
435
  } else {
@@ -512,14 +509,14 @@ module.exports = exports = class QueryBuilder extends CustomPromise {
512
509
  if (!this._onConflict) return '';
513
510
  let result = `on conflict (${this._buildKeys(this._onConflict.keys, true)}) do `;
514
511
  switch (Object.keys(this._onConflict.action || {})[0] || 'nothing') {
515
- case 'nothing':
516
- result += 'nothing';
517
- break;
518
- case 'update':
519
- result += `update ${this._buildUpdateKeysAndValues(this._onConflict.action.update.keys, this._onConflict.action.update.values)}`;
520
- break;
521
- default:
522
- break;
512
+ case 'nothing':
513
+ result += 'nothing';
514
+ break;
515
+ case 'update':
516
+ result += `update ${this._buildUpdateKeysAndValues(this._onConflict.action.update.keys, this._onConflict.action.update.values)}`;
517
+ break;
518
+ default:
519
+ break;
523
520
  }
524
521
  return result;
525
522
  }
@@ -533,46 +530,46 @@ module.exports = exports = class QueryBuilder extends CustomPromise {
533
530
  let parts = [command];
534
531
 
535
532
  switch (command) {
536
- case 'select':
537
- parts = parts.concat([
538
- this._buildKeys(this._selectKeys, true),
539
- 'from',
540
- this._table,
541
- this._buildJoins(),
542
- this._buildWhere(),
543
- this._buildGroup(),
544
- this._buildHaving(),
545
- this._buildSorting(),
546
- this._buildOffset(),
547
- this._buildLimit()
548
- ]);
549
- break;
550
- case 'update':
551
- parts = parts.concat([
552
- this._table,
553
- this._buildUpdate(),
554
- this._buildWhere(),
555
- 'returning',
556
- this._buildKeys(this._selectKeys, true)
557
- ]);
558
- break;
559
- case 'insert':
560
- parts = parts.concat([
561
- 'into',
562
- this._table,
563
- this._buildInsert(),
564
- this._buildOnConflict(),
565
- 'returning',
566
- this._buildKeys(this._selectKeys, true)
567
- ]);
568
- break;
569
- case 'delete':
570
- parts = parts.concat([
571
- 'from',
572
- this._table,
573
- this._buildWhere()
574
- ]);
575
- break;
533
+ case 'select':
534
+ parts = parts.concat([
535
+ this._buildKeys(this._selectKeys, true),
536
+ 'from',
537
+ this._table,
538
+ this._buildJoins(),
539
+ this._buildWhere(),
540
+ this._buildGroup(),
541
+ this._buildHaving(),
542
+ this._buildSorting(),
543
+ this._buildOffset(),
544
+ this._buildLimit()
545
+ ]);
546
+ break;
547
+ case 'update':
548
+ parts = parts.concat([
549
+ this._table,
550
+ this._buildUpdate(),
551
+ this._buildWhere(),
552
+ 'returning',
553
+ this._buildKeys(this._selectKeys, true)
554
+ ]);
555
+ break;
556
+ case 'insert':
557
+ parts = parts.concat([
558
+ 'into',
559
+ this._table,
560
+ this._buildInsert(),
561
+ this._buildOnConflict(),
562
+ 'returning',
563
+ this._buildKeys(this._selectKeys, true)
564
+ ]);
565
+ break;
566
+ case 'delete':
567
+ parts = parts.concat([
568
+ 'from',
569
+ this._table,
570
+ this._buildWhere()
571
+ ]);
572
+ break;
576
573
  }
577
574
 
578
575
  return [parts.filter((part) => part && part.length).join(' '), this._queryParameters];
package/event-emitter.js DELETED
@@ -1,43 +0,0 @@
1
- 'use strict';
2
-
3
- module.exports = exports = class EventEmitter {
4
-
5
- constructor() {
6
- this._identifier = -1;
7
- this._listeners = {};
8
- }
9
-
10
- on(name, handler) {
11
- this._listeners[name] = this._listeners[name] || [];
12
- this._listeners[name].push({
13
- handler,
14
- identifier: ++this._identifier
15
- });
16
- return this._identifier;
17
- }
18
-
19
- remove(name, identifier) {
20
- if (typeof this._listeners[name] === 'undefined') return;
21
- this._listeners[name] = this._listeners[name].filter((listener) => {
22
- return listener.handler !== identifier && listener.identifier !== identifier;
23
- });
24
- }
25
-
26
- once(name, handler) {
27
- const identifier = this.on(name, async (...args) => {
28
- this.remove(name, identifier);
29
- await handler(...args);
30
- });
31
- }
32
-
33
- removeAll(name) {
34
- this._identifier[name] = undefined;
35
- }
36
-
37
- async emit(name, ...args) {
38
- await Promise.all((this._listeners[name] || []).map(async (listener) => {
39
- await listener.handler(...args);
40
- }));
41
- }
42
-
43
- };