@trenskow/pged 5.0.33 → 5.1.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.
Files changed (2) hide show
  1. package/package.json +2 -1
  2. package/query-builder.js +28 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trenskow/pged",
3
- "version": "5.0.33",
3
+ "version": "5.1.1",
4
4
  "description": "Just a silly little db management and query builder for PostgreSQL.",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -26,6 +26,7 @@
26
26
  "@trenskow/async-event-emitter": "^0.1.13",
27
27
  "@trenskow/caseit": "^1.3.10",
28
28
  "@trenskow/custom-promise": "^0.10.3",
29
+ "@trenskow/parse": "^0.1.16",
29
30
  "pg": "^8.11.3",
30
31
  "puqeue": "^1.0.17"
31
32
  }
package/query-builder.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import caseit from '@trenskow/caseit';
2
2
  import CustomPromise from '@trenskow/custom-promise';
3
3
  import Puqeue from 'puqeue';
4
+ import parse from '@trenskow/parse';
4
5
 
5
6
  const tableInformationQueue = new Puqeue();
6
7
  const tableInformation = {};
@@ -41,7 +42,7 @@ export default class QueryBuilder extends CustomPromise {
41
42
 
42
43
  }
43
44
 
44
- _dbCase(input, quote) {
45
+ _dbCasePart(input, quote) {
45
46
  return input
46
47
  .split(/"|'/)
47
48
  .map((part, idx) => {
@@ -66,6 +67,31 @@ export default class QueryBuilder extends CustomPromise {
66
67
  .join('');
67
68
  }
68
69
 
70
+ _dbCase(input, quote) {
71
+
72
+ const parts = parse(['(', ')'], { maxDepth: 1, boundaries: 'include' }).do(input);
73
+
74
+ if (Array.isArray(parts)) {
75
+ return parts
76
+ .filter((part) => part.length)
77
+ .map((part) => this._dbCase(part, quote))
78
+ .join('');
79
+ }
80
+
81
+ let pre = input[0] === '(' ? '(' : '';
82
+ let post = input[input.length - 1] === ')' ? ')': '';
83
+
84
+ return `${pre}${input
85
+ .substring(pre.length, input.length - post.length)
86
+ .split(/, ?/)
87
+ .map((part) => part
88
+ .split(' ')
89
+ .map((part) => /[a-z]/i.test(part) ? this._dbCasePart(part, quote) : part)
90
+ .join(' '))
91
+ .join(', ')}${post}`;
92
+
93
+ }
94
+
69
95
  select(keys = ['*']) {
70
96
  if (typeof keys !== 'string' || keys[0] !== ':') {
71
97
  if (!Array.isArray(keys)) {
@@ -218,7 +244,7 @@ export default class QueryBuilder extends CustomPromise {
218
244
  first(key, options = { select: true }) {
219
245
  this._limit = 1;
220
246
  if (key) {
221
- if (options.select) this.select(key);
247
+ if (options.select) this.select(`${this._table}.${key}`);
222
248
  this._first = key;
223
249
  }
224
250
  else this._first = true;