@sprucelabs/postgres-data-store 4.0.156 → 4.1.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,6 +5,8 @@ export default class QueryBuilder {
5
5
  static Builder(): QueryBuilder;
6
6
  find(tableName: string, query: Query, options?: QueryOptions): BuiltQuery;
7
7
  buildTableName(tableName: string): string;
8
+ private conditionalQuote;
9
+ private get shouldQuote();
8
10
  private optionallyBuildWhere;
9
11
  private buildSetClause;
10
12
  private buildSetClausFor$Or;
@@ -18,7 +18,13 @@ class QueryBuilder {
18
18
  return { sql, values };
19
19
  }
20
20
  buildTableName(tableName) {
21
- return `"${tableName}"`;
21
+ return `${this.conditionalQuote(tableName)}`;
22
+ }
23
+ conditionalQuote(fieldName) {
24
+ return this.shouldQuote ? quote(fieldName) : fieldName;
25
+ }
26
+ get shouldQuote() {
27
+ return process.env.POSTGRES_SHOULD_QUOTE_FIELD_NAMES !== 'false';
22
28
  }
23
29
  optionallyBuildWhere(query, startingPlaceholderCount = 0) {
24
30
  let sql = '';
@@ -44,36 +50,37 @@ class QueryBuilder {
44
50
  queryKeys.forEach((k) => {
45
51
  let value = query[k];
46
52
  const isNull = value === null && useIsNull;
53
+ const formattedK = this.conditionalQuote(k);
47
54
  if (value === null || value === void 0 ? void 0 : value.$in) {
48
55
  values.push(...value.$in.map((v) => this.normalizeValue(v)));
49
- set.push(`"${k}" IN (${value.$in
56
+ set.push(`${formattedK} IN (${value.$in
50
57
  .map(() => `$${++placeholderCount}`)
51
58
  .join(', ')})`);
52
59
  }
53
60
  else if (value === null || value === void 0 ? void 0 : value.$regex) {
54
61
  values.push(this.normalizeValue(value.$regex));
55
- set.push(`"${k}" ~* $${++placeholderCount}`);
62
+ set.push(`${formattedK} ~* $${++placeholderCount}`);
56
63
  }
57
64
  else if (value === null || value === void 0 ? void 0 : value.$lte) {
58
65
  values.push(this.normalizeValue(value.$lte));
59
- set.push(`"${k}" <= $${++placeholderCount}`);
66
+ set.push(`${formattedK} <= $${++placeholderCount}`);
60
67
  }
61
68
  else if (value === null || value === void 0 ? void 0 : value.$lt) {
62
69
  values.push(this.normalizeValue(value.$lt));
63
- set.push(`"${k}" < $${++placeholderCount}`);
70
+ set.push(`${formattedK} < $${++placeholderCount}`);
64
71
  }
65
72
  else if (value === null || value === void 0 ? void 0 : value.$gte) {
66
73
  values.push(this.normalizeValue(value.$gte));
67
- set.push(`"${k}" >= $${++placeholderCount}`);
74
+ set.push(`${formattedK} >= $${++placeholderCount}`);
68
75
  }
69
76
  else if (value === null || value === void 0 ? void 0 : value.$gt) {
70
77
  values.push(this.normalizeValue(value.$gt));
71
- set.push(`"${k}" > $${++placeholderCount}`);
78
+ set.push(`${formattedK} > $${++placeholderCount}`);
72
79
  }
73
80
  else if (typeof (value === null || value === void 0 ? void 0 : value.$ne) !== 'undefined') {
74
81
  const v = value.$ne;
75
82
  v !== null && values.push(this.normalizeValue(v));
76
- set.push(`"${k}" ${v === null ? 'IS NOT NULL' : `!= $${++placeholderCount}`}`);
83
+ set.push(`${formattedK} ${v === null ? 'IS NOT NULL' : `!= $${++placeholderCount}`}`);
77
84
  }
78
85
  else if (k === '$or') {
79
86
  const { set: orWheres, values: orValues } = this.buildSetClausFor$Or(value, placeholderCount);
@@ -90,7 +97,7 @@ class QueryBuilder {
90
97
  set.push(...sub.set);
91
98
  }
92
99
  else if (isNull || value === undefined) {
93
- set.push(`"${k}" IS NULL`);
100
+ set.push(`${formattedK} IS NULL`);
94
101
  }
95
102
  else {
96
103
  placeholderCount++;
@@ -110,7 +117,7 @@ class QueryBuilder {
110
117
  }
111
118
  }
112
119
  values.push(this.normalizeValue(value));
113
- set.push(`${quote(k)} = ${placeholder}`);
120
+ set.push(`${this.conditionalQuote(k)} = ${placeholder}`);
114
121
  }
115
122
  });
116
123
  return { set, values };
@@ -139,7 +146,7 @@ class QueryBuilder {
139
146
  createWithoutReturning(tableName, records) {
140
147
  const { fields, placeholders, values } = this.splitRecordsIntoFieldsPlaceholdersAndValues(records);
141
148
  const sql = `INSERT INTO ${this.buildTableName(tableName)} (${fields
142
- .map((f) => `"${f}"`)
149
+ .map((f) => `${this.conditionalQuote(f)}`)
143
150
  .join(', ')}) VALUES ${placeholders.join(', ')}`;
144
151
  return { sql, values };
145
152
  }
@@ -184,7 +191,7 @@ class QueryBuilder {
184
191
  }
185
192
  optionallyBuildSort(sort) {
186
193
  if (sort) {
187
- const sortSpecs = sort.map((s) => `"${s.field}" ${s.direction.toUpperCase()}`);
194
+ const sortSpecs = sort.map((s) => `${this.conditionalQuote(s.field)} ${s.direction.toUpperCase()}`);
188
195
  return ` ORDER BY ${sortSpecs.join(', ')}`;
189
196
  }
190
197
  return '';
@@ -5,6 +5,8 @@ export default class QueryBuilder {
5
5
  static Builder(): QueryBuilder;
6
6
  find(tableName: string, query: Query, options?: QueryOptions): BuiltQuery;
7
7
  buildTableName(tableName: string): string;
8
+ private conditionalQuote;
9
+ private get shouldQuote();
8
10
  private optionallyBuildWhere;
9
11
  private buildSetClause;
10
12
  private buildSetClausFor$Or;
@@ -15,7 +15,13 @@ export default class QueryBuilder {
15
15
  return { sql, values };
16
16
  }
17
17
  buildTableName(tableName) {
18
- return `"${tableName}"`;
18
+ return `${this.conditionalQuote(tableName)}`;
19
+ }
20
+ conditionalQuote(fieldName) {
21
+ return this.shouldQuote ? quote(fieldName) : fieldName;
22
+ }
23
+ get shouldQuote() {
24
+ return process.env.POSTGRES_SHOULD_QUOTE_FIELD_NAMES !== 'false';
19
25
  }
20
26
  optionallyBuildWhere(query, startingPlaceholderCount = 0) {
21
27
  let sql = '';
@@ -41,36 +47,37 @@ export default class QueryBuilder {
41
47
  queryKeys.forEach((k) => {
42
48
  let value = query[k];
43
49
  const isNull = value === null && useIsNull;
50
+ const formattedK = this.conditionalQuote(k);
44
51
  if (value === null || value === void 0 ? void 0 : value.$in) {
45
52
  values.push(...value.$in.map((v) => this.normalizeValue(v)));
46
- set.push(`"${k}" IN (${value.$in
53
+ set.push(`${formattedK} IN (${value.$in
47
54
  .map(() => `$${++placeholderCount}`)
48
55
  .join(', ')})`);
49
56
  }
50
57
  else if (value === null || value === void 0 ? void 0 : value.$regex) {
51
58
  values.push(this.normalizeValue(value.$regex));
52
- set.push(`"${k}" ~* $${++placeholderCount}`);
59
+ set.push(`${formattedK} ~* $${++placeholderCount}`);
53
60
  }
54
61
  else if (value === null || value === void 0 ? void 0 : value.$lte) {
55
62
  values.push(this.normalizeValue(value.$lte));
56
- set.push(`"${k}" <= $${++placeholderCount}`);
63
+ set.push(`${formattedK} <= $${++placeholderCount}`);
57
64
  }
58
65
  else if (value === null || value === void 0 ? void 0 : value.$lt) {
59
66
  values.push(this.normalizeValue(value.$lt));
60
- set.push(`"${k}" < $${++placeholderCount}`);
67
+ set.push(`${formattedK} < $${++placeholderCount}`);
61
68
  }
62
69
  else if (value === null || value === void 0 ? void 0 : value.$gte) {
63
70
  values.push(this.normalizeValue(value.$gte));
64
- set.push(`"${k}" >= $${++placeholderCount}`);
71
+ set.push(`${formattedK} >= $${++placeholderCount}`);
65
72
  }
66
73
  else if (value === null || value === void 0 ? void 0 : value.$gt) {
67
74
  values.push(this.normalizeValue(value.$gt));
68
- set.push(`"${k}" > $${++placeholderCount}`);
75
+ set.push(`${formattedK} > $${++placeholderCount}`);
69
76
  }
70
77
  else if (typeof (value === null || value === void 0 ? void 0 : value.$ne) !== 'undefined') {
71
78
  const v = value.$ne;
72
79
  v !== null && values.push(this.normalizeValue(v));
73
- set.push(`"${k}" ${v === null ? 'IS NOT NULL' : `!= $${++placeholderCount}`}`);
80
+ set.push(`${formattedK} ${v === null ? 'IS NOT NULL' : `!= $${++placeholderCount}`}`);
74
81
  }
75
82
  else if (k === '$or') {
76
83
  const { set: orWheres, values: orValues } = this.buildSetClausFor$Or(value, placeholderCount);
@@ -87,7 +94,7 @@ export default class QueryBuilder {
87
94
  set.push(...sub.set);
88
95
  }
89
96
  else if (isNull || value === undefined) {
90
- set.push(`"${k}" IS NULL`);
97
+ set.push(`${formattedK} IS NULL`);
91
98
  }
92
99
  else {
93
100
  placeholderCount++;
@@ -107,7 +114,7 @@ export default class QueryBuilder {
107
114
  }
108
115
  }
109
116
  values.push(this.normalizeValue(value));
110
- set.push(`${quote(k)} = ${placeholder}`);
117
+ set.push(`${this.conditionalQuote(k)} = ${placeholder}`);
111
118
  }
112
119
  });
113
120
  return { set, values };
@@ -136,7 +143,7 @@ export default class QueryBuilder {
136
143
  createWithoutReturning(tableName, records) {
137
144
  const { fields, placeholders, values } = this.splitRecordsIntoFieldsPlaceholdersAndValues(records);
138
145
  const sql = `INSERT INTO ${this.buildTableName(tableName)} (${fields
139
- .map((f) => `"${f}"`)
146
+ .map((f) => `${this.conditionalQuote(f)}`)
140
147
  .join(', ')}) VALUES ${placeholders.join(', ')}`;
141
148
  return { sql, values };
142
149
  }
@@ -181,7 +188,7 @@ export default class QueryBuilder {
181
188
  }
182
189
  optionallyBuildSort(sort) {
183
190
  if (sort) {
184
- const sortSpecs = sort.map((s) => `"${s.field}" ${s.direction.toUpperCase()}`);
191
+ const sortSpecs = sort.map((s) => `${this.conditionalQuote(s.field)} ${s.direction.toUpperCase()}`);
185
192
  return ` ORDER BY ${sortSpecs.join(', ')}`;
186
193
  }
187
194
  return '';
package/package.json CHANGED
@@ -22,7 +22,7 @@
22
22
  "sprucebot",
23
23
  "sprucelabs"
24
24
  ],
25
- "version": "4.0.156",
25
+ "version": "4.1.0",
26
26
  "scripts": {
27
27
  "build.ci": "yarn build.tsc && yarn build.resolve-paths && yarn lint",
28
28
  "build.dev": "yarn build.tsc --sourceMap ; yarn resolve-paths.lint",
@@ -51,18 +51,18 @@
51
51
  "watch.tsc": "tsc -w"
52
52
  },
53
53
  "dependencies": {
54
- "@sprucelabs/data-stores": "^25.2.2",
55
- "@sprucelabs/schema": "^29.0.72",
54
+ "@sprucelabs/data-stores": "^25.3.1",
55
+ "@sprucelabs/schema": "^29.0.73",
56
56
  "pg": "^8.11.3"
57
57
  },
58
58
  "devDependencies": {
59
- "@sprucelabs/esm-postbuild": "^5.0.84",
60
- "@sprucelabs/jest-json-reporter": "^7.0.111",
59
+ "@sprucelabs/esm-postbuild": "^5.0.85",
60
+ "@sprucelabs/jest-json-reporter": "^7.0.112",
61
61
  "@sprucelabs/resolve-path-aliases": "^1.1.254",
62
62
  "@sprucelabs/semantic-release": "^4.0.8",
63
63
  "@sprucelabs/spruce-test-fixtures": "^52.8.0",
64
64
  "@sprucelabs/test": "^8.0.22",
65
- "@sprucelabs/test-utils": "^4.0.61",
65
+ "@sprucelabs/test-utils": "^4.0.62",
66
66
  "@types/node": "^18.14.0",
67
67
  "@types/pg": "^8.10.7",
68
68
  "chokidar-cli": "^3.0.0",