@trenskow/pged 4.1.6 → 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.
- package/{.eslintrc.js → .eslintrc.cjs} +4 -13
- package/index.js +6 -8
- package/package.json +3 -1
- package/query-builder.js +92 -93
- 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
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
{ Pool } = require('pg');
|
|
3
|
+
import Puqeue from 'puqeue';
|
|
4
|
+
import caseit from '@trenskow/caseit';
|
|
5
|
+
import { Pool } from 'pg';
|
|
7
6
|
|
|
8
|
-
|
|
9
|
-
|
|
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
|
-
|
|
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": "
|
|
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,6 +23,7 @@
|
|
|
22
23
|
},
|
|
23
24
|
"homepage": "https://github.com/trenskow/pged#readme",
|
|
24
25
|
"dependencies": {
|
|
26
|
+
"@trenskow/async-event-emitter": "^0.0.1",
|
|
25
27
|
"@trenskow/caseit": "^1.3.0",
|
|
26
28
|
"@trenskow/custom-promise": "^0.10.1",
|
|
27
29
|
"pg": "^8.7.3",
|
package/query-builder.js
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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
|
-
|
|
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
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
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
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
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
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
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
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
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
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
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
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
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
|
-
};
|