@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.
- package/{.eslintrc.js → .eslintrc.cjs} +4 -13
- package/index.js +7 -9
- package/package.json +4 -2
- package/query-builder.js +92 -95
- package/event-emitter.js +0 -43
|
@@ -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
|
-
'
|
|
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
|
+
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
|
-
|
|
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
|
-
|
|
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": "
|
|
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/
|
|
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
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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
|
-
|
|
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
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
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
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
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
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
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
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
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
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
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
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
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
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
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
|
-
};
|