@sprucelabs/postgres-data-store 4.1.47 → 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.
@@ -11,6 +11,7 @@ export default class QueryBuilder {
11
11
  private buildSetClause;
12
12
  private buildSetClausFor$Or;
13
13
  create(tableName: string, records: Record<string, any>[]): BuiltQuery;
14
+ private log;
14
15
  createWithoutReturning(tableName: string, records: Record<string, any>[]): BuiltQuery;
15
16
  private splitRecordsIntoFieldsPlaceholdersAndValues;
16
17
  private fieldValueToSqlValue;
@@ -91,9 +91,9 @@ class QueryBuilder {
91
91
  const sub = this.buildSetClause({
92
92
  query: value,
93
93
  startingCount: placeholderCount++,
94
- placeholderTemplate: `JSONB_SET(COALESCE(names || \${{count}}::JSONB, '[]'::JSONB), '{-1}', \${{count}}::JSONB)`,
94
+ placeholderTemplate: '"{{fieldName}}" || ARRAY[${{count}}]',
95
95
  });
96
- values.push(...sub.values.map((v) => JSON.stringify(v)));
96
+ values.push(...sub.values);
97
97
  set.push(...sub.set);
98
98
  }
99
99
  else if (isNull || value === undefined) {
@@ -138,11 +138,19 @@ class QueryBuilder {
138
138
  create(tableName, records) {
139
139
  let { sql, values } = this.createWithoutReturning(tableName, records);
140
140
  sql += ` RETURNING *`;
141
+ this.log('create', sql, values);
141
142
  return {
142
143
  sql,
143
144
  values,
144
145
  };
145
146
  }
147
+ log(...args) {
148
+ if (process.env.POSTGRES_SHOULD_LOG_QUERIES === 'true') {
149
+ for (const arg of args) {
150
+ console.log(JSON.stringify(arg));
151
+ }
152
+ }
153
+ }
146
154
  createWithoutReturning(tableName, records) {
147
155
  const { fields, placeholders, values } = this.splitRecordsIntoFieldsPlaceholdersAndValues(records);
148
156
  const sql = `INSERT INTO ${this.buildTableName(tableName)} (${fields
@@ -159,7 +167,7 @@ class QueryBuilder {
159
167
  fields.forEach((f) => {
160
168
  values.push(this.fieldValueToSqlValue(record, f));
161
169
  let placeholder = `$${++placeholderCount}`;
162
- if (this.isValueObject(record[f])) {
170
+ if (this.isValueObject(record[f]) && !Array.isArray(record[f])) {
163
171
  placeholder += `::json`;
164
172
  }
165
173
  placeholders.push(placeholder);
@@ -177,7 +185,14 @@ class QueryBuilder {
177
185
  value = value.toString().replace(/\//g, '');
178
186
  }
179
187
  if (this.isValueObject(value)) {
180
- value = JSON.stringify(value);
188
+ if (Array.isArray(value)) {
189
+ //in postgres, an array is notaded like this {1,2,3}
190
+ value = JSON.stringify(value);
191
+ value = `{${value.substring(1, value.length - 1)}}`;
192
+ }
193
+ else {
194
+ value = JSON.stringify(value);
195
+ }
181
196
  }
182
197
  return value !== null && value !== void 0 ? value : null;
183
198
  }
@@ -223,15 +238,18 @@ class QueryBuilder {
223
238
  if (shouldReturnUpdatedRecords) {
224
239
  sql += ' RETURNING *';
225
240
  }
226
- return {
241
+ const results = {
227
242
  sql,
228
243
  values: [...values, ...whereValues],
229
244
  };
245
+ this.log('update', results);
246
+ return results;
230
247
  }
231
248
  delete(tableName, query) {
232
249
  let sql = `DELETE FROM ${this.buildTableName(tableName)}`;
233
250
  const { values, sql: where } = this.optionallyBuildWhere(query !== null && query !== void 0 ? query : {});
234
251
  sql += where;
252
+ this.log('delete', sql, values);
235
253
  return {
236
254
  sql,
237
255
  values,
@@ -250,6 +268,7 @@ class QueryBuilder {
250
268
  .map((f) => `${f} = EXCLUDED.${f}`)
251
269
  .join(', ')}`;
252
270
  sql += ' RETURNING *';
271
+ this.log('upsert', sql, values);
253
272
  return {
254
273
  sql,
255
274
  values: [...values, ...whereValues],
@@ -11,6 +11,7 @@ export default class QueryBuilder {
11
11
  private buildSetClause;
12
12
  private buildSetClausFor$Or;
13
13
  create(tableName: string, records: Record<string, any>[]): BuiltQuery;
14
+ private log;
14
15
  createWithoutReturning(tableName: string, records: Record<string, any>[]): BuiltQuery;
15
16
  private splitRecordsIntoFieldsPlaceholdersAndValues;
16
17
  private fieldValueToSqlValue;
@@ -88,9 +88,9 @@ export default class QueryBuilder {
88
88
  const sub = this.buildSetClause({
89
89
  query: value,
90
90
  startingCount: placeholderCount++,
91
- placeholderTemplate: `JSONB_SET(COALESCE(names || \${{count}}::JSONB, '[]'::JSONB), '{-1}', \${{count}}::JSONB)`,
91
+ placeholderTemplate: '"{{fieldName}}" || ARRAY[${{count}}]',
92
92
  });
93
- values.push(...sub.values.map((v) => JSON.stringify(v)));
93
+ values.push(...sub.values);
94
94
  set.push(...sub.set);
95
95
  }
96
96
  else if (isNull || value === undefined) {
@@ -135,11 +135,19 @@ export default class QueryBuilder {
135
135
  create(tableName, records) {
136
136
  let { sql, values } = this.createWithoutReturning(tableName, records);
137
137
  sql += ` RETURNING *`;
138
+ this.log('create', sql, values);
138
139
  return {
139
140
  sql,
140
141
  values,
141
142
  };
142
143
  }
144
+ log(...args) {
145
+ if (process.env.POSTGRES_SHOULD_LOG_QUERIES === 'true') {
146
+ for (const arg of args) {
147
+ console.log(JSON.stringify(arg));
148
+ }
149
+ }
150
+ }
143
151
  createWithoutReturning(tableName, records) {
144
152
  const { fields, placeholders, values } = this.splitRecordsIntoFieldsPlaceholdersAndValues(records);
145
153
  const sql = `INSERT INTO ${this.buildTableName(tableName)} (${fields
@@ -156,7 +164,7 @@ export default class QueryBuilder {
156
164
  fields.forEach((f) => {
157
165
  values.push(this.fieldValueToSqlValue(record, f));
158
166
  let placeholder = `$${++placeholderCount}`;
159
- if (this.isValueObject(record[f])) {
167
+ if (this.isValueObject(record[f]) && !Array.isArray(record[f])) {
160
168
  placeholder += `::json`;
161
169
  }
162
170
  placeholders.push(placeholder);
@@ -174,7 +182,14 @@ export default class QueryBuilder {
174
182
  value = value.toString().replace(/\//g, '');
175
183
  }
176
184
  if (this.isValueObject(value)) {
177
- value = JSON.stringify(value);
185
+ if (Array.isArray(value)) {
186
+ //in postgres, an array is notaded like this {1,2,3}
187
+ value = JSON.stringify(value);
188
+ value = `{${value.substring(1, value.length - 1)}}`;
189
+ }
190
+ else {
191
+ value = JSON.stringify(value);
192
+ }
178
193
  }
179
194
  return value !== null && value !== void 0 ? value : null;
180
195
  }
@@ -220,15 +235,18 @@ export default class QueryBuilder {
220
235
  if (shouldReturnUpdatedRecords) {
221
236
  sql += ' RETURNING *';
222
237
  }
223
- return {
238
+ const results = {
224
239
  sql,
225
240
  values: [...values, ...whereValues],
226
241
  };
242
+ this.log('update', results);
243
+ return results;
227
244
  }
228
245
  delete(tableName, query) {
229
246
  let sql = `DELETE FROM ${this.buildTableName(tableName)}`;
230
247
  const { values, sql: where } = this.optionallyBuildWhere(query !== null && query !== void 0 ? query : {});
231
248
  sql += where;
249
+ this.log('delete', sql, values);
232
250
  return {
233
251
  sql,
234
252
  values,
@@ -247,6 +265,7 @@ export default class QueryBuilder {
247
265
  .map((f) => `${f} = EXCLUDED.${f}`)
248
266
  .join(', ')}`;
249
267
  sql += ' RETURNING *';
268
+ this.log('upsert', sql, values);
250
269
  return {
251
270
  sql,
252
271
  values: [...values, ...whereValues],
package/package.json CHANGED
@@ -22,7 +22,7 @@
22
22
  "sprucebot",
23
23
  "sprucelabs"
24
24
  ],
25
- "version": "4.1.47",
25
+ "version": "5.0.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",