@trenskow/pged 4.1.4 → 5.0.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.
@@ -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
1
  'use strict';
2
2
 
3
- const
4
- Puqeue = require('puqeue'),
5
- caseit = require('@trenskow/caseit'),
6
- { Pool } = require('pg');
3
+ import Puqeue from 'puqeue';
4
+ import caseit from '@trenskow/caseit';
5
+ import { Pool } from 'pg';
7
6
 
8
- const
9
- QueryBuilder = require('./query-builder'),
10
- EventEmitter = require('./event-emitter');
7
+ import QueryBuilder from './query-builder';
8
+ import EventEmitter from '@trenskow/async-event-emitter';
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 exports = 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.4",
3
+ "version": "5.0.0",
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.1",
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,13 @@
1
1
  'use strict';
2
2
 
3
- const
4
- caseit = require('@trenskow/caseit'),
5
- CustomPromise = require('@trenskow/custom-promise'),
6
- Puqeue = require('puqeue');
3
+ import caseit from '@trenskow/caseit';
4
+ import CustomPromise from '@trenskow/custom-promise';
5
+ import Puqeue from 'puqeue';
7
6
 
8
7
  const tableInformationQueue = new Puqeue();
9
8
  const tableInformation = {};
10
9
 
11
- module.exports = exports = class QueryBuilder extends CustomPromise {
10
+ export default exports = class QueryBuilder extends CustomPromise {
12
11
 
13
12
  constructor(table, options = {}, connection) {
14
13
 
@@ -248,15 +247,15 @@ module.exports = exports = class QueryBuilder extends CustomPromise {
248
247
  if (!Array.isArray(keys)) keys = keys.split(/, ?/);
249
248
 
250
249
  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.');
250
+ case 'nothing':
251
+ break;
252
+ case 'update': {
253
+ const [keys, values] = this._deconstructKeyValues(action.update);
254
+ action.update = { keys, values };
255
+ break;
256
+ }
257
+ default:
258
+ throw new Error('Action `update` is only supported at this moment.');
260
259
  }
261
260
 
262
261
  this._onConflict = {
@@ -293,10 +292,10 @@ module.exports = exports = class QueryBuilder extends CustomPromise {
293
292
  let [table, key] = keyPath.split('.');
294
293
  if (typeof key === 'undefined') [table, key] = [this._table, table];
295
294
  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;
295
+ case 'jsonb':
296
+ return typeof value === 'string' ? value : JSON.stringify(value);
297
+ default:
298
+ return value;
300
299
  }
301
300
  }
302
301
 
@@ -347,23 +346,23 @@ module.exports = exports = class QueryBuilder extends CustomPromise {
347
346
 
348
347
  if (key.substring(0, 1) == '$') {
349
348
  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)}.`);
349
+ case '$or':
350
+ case '$and':
351
+ return this._buildConditions(condition[key], key, comparer, true);
352
+ case '$eq':
353
+ case '$ne':
354
+ case '$neq':
355
+ case '$lt':
356
+ case '$lte':
357
+ case '$gt':
358
+ case '$gte':
359
+ case '$regexp':
360
+ case '$jsonContains':
361
+ case '$jsonNotContains':
362
+ case '$jsonArrayContains':
363
+ return this._buildConditions(condition[key], operator, key, true);
364
+ default:
365
+ throw new TypeError(`Unknown modifier ${caseit(key)}.`);
367
366
  }
368
367
  }
369
368
 
@@ -386,12 +385,12 @@ module.exports = exports = class QueryBuilder extends CustomPromise {
386
385
 
387
386
  if (condition[key] == null) {
388
387
  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.`);
388
+ case '$eq':
389
+ return `${dbKey} is null`;
390
+ case '$ne':
391
+ return `${dbKey} is not null`;
392
+ default:
393
+ throw new TypeError(`Modifier ${comparer} is not usable with \`null\` values.`);
395
394
  }
396
395
  }
397
396
 
@@ -429,10 +428,10 @@ module.exports = exports = class QueryBuilder extends CustomPromise {
429
428
  if (join.conditions) {
430
429
  let type;
431
430
  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;
431
+ case 'both': type = 'join'; break;
432
+ case 'local': type = 'left join'; break;
433
+ case 'foreign': type = 'right join'; break;
434
+ case 'none': type = 'outer join'; break;
436
435
  }
437
436
  return `${type} ${this._dbCase(join.table)} on ${this._buildConditions(join.conditions)}`;
438
437
  } else {
@@ -512,14 +511,14 @@ module.exports = exports = class QueryBuilder extends CustomPromise {
512
511
  if (!this._onConflict) return '';
513
512
  let result = `on conflict (${this._buildKeys(this._onConflict.keys, true)}) do `;
514
513
  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;
514
+ case 'nothing':
515
+ result += 'nothing';
516
+ break;
517
+ case 'update':
518
+ result += `update ${this._buildUpdateKeysAndValues(this._onConflict.action.update.keys, this._onConflict.action.update.values)}`;
519
+ break;
520
+ default:
521
+ break;
523
522
  }
524
523
  return result;
525
524
  }
@@ -533,46 +532,46 @@ module.exports = exports = class QueryBuilder extends CustomPromise {
533
532
  let parts = [command];
534
533
 
535
534
  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;
535
+ case 'select':
536
+ parts = parts.concat([
537
+ this._buildKeys(this._selectKeys, true),
538
+ 'from',
539
+ this._table,
540
+ this._buildJoins(),
541
+ this._buildWhere(),
542
+ this._buildGroup(),
543
+ this._buildHaving(),
544
+ this._buildSorting(),
545
+ this._buildOffset(),
546
+ this._buildLimit()
547
+ ]);
548
+ break;
549
+ case 'update':
550
+ parts = parts.concat([
551
+ this._table,
552
+ this._buildUpdate(),
553
+ this._buildWhere(),
554
+ 'returning',
555
+ this._buildKeys(this._selectKeys, true)
556
+ ]);
557
+ break;
558
+ case 'insert':
559
+ parts = parts.concat([
560
+ 'into',
561
+ this._table,
562
+ this._buildInsert(),
563
+ this._buildOnConflict(),
564
+ 'returning',
565
+ this._buildKeys(this._selectKeys, true)
566
+ ]);
567
+ break;
568
+ case 'delete':
569
+ parts = parts.concat([
570
+ 'from',
571
+ this._table,
572
+ this._buildWhere()
573
+ ]);
574
+ break;
576
575
  }
577
576
 
578
577
  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
- await handler(...args);
29
- this.remove(name, identifier);
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
- };