@sprucelabs/postgres-data-store 6.0.8 → 6.0.10

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.
@@ -29,11 +29,8 @@ const schema_1 = require("@sprucelabs/schema");
29
29
  const pg_1 = require("pg");
30
30
  const QueryBuilder_1 = __importStar(require("./QueryBuilder"));
31
31
  class PostgresDatabase {
32
- connectionString;
33
- client;
34
- idCount = 1;
35
- queries;
36
32
  constructor(connectionString) {
33
+ this.idCount = 1;
37
34
  (0, schema_1.assertOptions)({ connectionString }, ['connectionString']);
38
35
  this.connectionString = connectionString;
39
36
  this.queries = QueryBuilder_1.default.Builder();
@@ -47,15 +44,16 @@ class PostgresDatabase {
47
44
  : `${this.idCount++}`;
48
45
  }
49
46
  async update(collection, query, updates) {
47
+ var _a;
50
48
  const { sql, values } = this.queries.update(collection, query, updates, false);
51
49
  const results = await this.client.query({
52
50
  text: sql,
53
51
  values,
54
52
  });
55
- return results.rowCount ?? 0;
53
+ return (_a = results.rowCount) !== null && _a !== void 0 ? _a : 0;
56
54
  }
57
55
  async count(collection, query) {
58
- const { sql, values } = this.queries.find(collection, query ?? {}, {
56
+ const { sql, values } = this.queries.find(collection, query !== null && query !== void 0 ? query : {}, {
59
57
  includeFields: ['count(*) as count'],
60
58
  });
61
59
  const results = await this.client.query({
@@ -83,7 +81,7 @@ class PostgresDatabase {
83
81
  return record;
84
82
  }
85
83
  async find(collection, query, options) {
86
- const { sql, values } = this.queries.find(collection, query ?? {}, options);
84
+ const { sql, values } = this.queries.find(collection, query !== null && query !== void 0 ? query : {}, options);
87
85
  const results = await this.client.query({
88
86
  text: sql,
89
87
  values,
@@ -97,19 +95,21 @@ class PostgresDatabase {
97
95
  return this.executeGetIndexes(collectionName, false);
98
96
  }
99
97
  async findOne(collection, query, options) {
98
+ var _a;
100
99
  const results = await this.find(collection, query, {
101
100
  ...options,
102
101
  limit: 1,
103
102
  });
104
- return results[0] ?? null;
103
+ return (_a = results[0]) !== null && _a !== void 0 ? _a : null;
105
104
  }
106
105
  async delete(collection, query) {
106
+ var _a;
107
107
  const { sql, values } = this.queries.delete(collection, query);
108
108
  const results = await this.client.query({
109
109
  text: sql,
110
110
  values,
111
111
  });
112
- return results.rowCount ?? 0;
112
+ return (_a = results.rowCount) !== null && _a !== void 0 ? _a : 0;
113
113
  }
114
114
  async dropDatabase() {
115
115
  await this.truncateTables();
@@ -151,18 +151,19 @@ class PostgresDatabase {
151
151
  }
152
152
  }
153
153
  async deleteOne(collection, query) {
154
+ var _a;
154
155
  if (!query.id) {
155
156
  const match = await this.findOne(collection, query, {
156
157
  includeFields: ['id'],
157
158
  });
158
- query = { id: match?.id };
159
+ query = { id: match === null || match === void 0 ? void 0 : match.id };
159
160
  }
160
161
  let { sql, values } = this.queries.delete(collection, query);
161
162
  const results = await this.client.query({
162
163
  text: sql,
163
164
  values,
164
165
  });
165
- return results.rowCount ?? 0;
166
+ return (_a = results.rowCount) !== null && _a !== void 0 ? _a : 0;
166
167
  }
167
168
  async truncateTables() {
168
169
  const res = await this.client.query(`
@@ -195,7 +196,7 @@ class PostgresDatabase {
195
196
  return results;
196
197
  }
197
198
  catch (err) {
198
- const parsed = this.parseIndexViolatedForFieldsAndValues(err?.detail);
199
+ const parsed = this.parseIndexViolatedForFieldsAndValues(err === null || err === void 0 ? void 0 : err.detail);
199
200
  if (parsed) {
200
201
  const { fields, values } = parsed;
201
202
  throw new data_stores_1.DataStoresError({
@@ -210,6 +211,7 @@ class PostgresDatabase {
210
211
  }
211
212
  }
212
213
  async connect() {
214
+ var _a, _b, _c;
213
215
  this.client = new pg_1.Client({
214
216
  connectionString: this.connectionString,
215
217
  });
@@ -218,14 +220,14 @@ class PostgresDatabase {
218
220
  }
219
221
  catch (err) {
220
222
  const message = err.message;
221
- if ((err.code ?? message)?.includes('ECONNREFUSED')) {
223
+ if ((_b = ((_a = err.code) !== null && _a !== void 0 ? _a : message)) === null || _b === void 0 ? void 0 : _b.includes('ECONNREFUSED')) {
222
224
  throw new data_stores_1.DataStoresError({
223
225
  code: 'UNABLE_TO_CONNECT_TO_DB',
224
226
  originalError: err,
225
227
  });
226
228
  }
227
- if (message?.includes('does not exist')) {
228
- const match = message.match(/"([^"]*)"/) ?? ['', ''];
229
+ if (message === null || message === void 0 ? void 0 : message.includes('does not exist')) {
230
+ const match = (_c = message.match(/"([^"]*)"/)) !== null && _c !== void 0 ? _c : ['', ''];
229
231
  throw new data_stores_1.DataStoresError({
230
232
  code: 'INVALID_DATABASE_NAME',
231
233
  suppliedName: match[1],
@@ -284,11 +286,12 @@ class PostgresDatabase {
284
286
  const indexesToRemove = existingIndexes.filter((existing) => !indexes.find((index) => this.areIndexesEqual(existing, index)));
285
287
  await Promise.all([
286
288
  ...indexesToAdd.map(async (index) => {
289
+ var _a;
287
290
  try {
288
291
  await this.executeCreateIndex(collectionName, index, isUnique);
289
292
  }
290
293
  catch (err) {
291
- if (err.options?.code !== 'INDEX_EXISTS') {
294
+ if (((_a = err.options) === null || _a === void 0 ? void 0 : _a.code) !== 'INDEX_EXISTS') {
292
295
  throw new data_stores_1.DataStoresError({
293
296
  code: 'DUPLICATE_KEY',
294
297
  originalError: err,
@@ -309,6 +312,7 @@ class PostgresDatabase {
309
312
  await this.executeCreateIndex(collection, fields, isUnique);
310
313
  }
311
314
  async executeCreateIndex(collection, fields, isUnique) {
315
+ var _a, _b;
312
316
  const indexName = this.generateIndexName(collection, fields);
313
317
  const keys = this.generateKeyExpressions(fields);
314
318
  const query = `CREATE ${isUnique ? `UNIQUE` : ''} INDEX ${indexName} ON "${collection}" (${keys})`;
@@ -318,7 +322,7 @@ class PostgresDatabase {
318
322
  });
319
323
  }
320
324
  catch (err) {
321
- if (err.message?.includes?.('already exists')) {
325
+ if ((_b = (_a = err.message) === null || _a === void 0 ? void 0 : _a.includes) === null || _b === void 0 ? void 0 : _b.call(_a, 'already exists')) {
322
326
  throw new data_stores_1.DataStoresError({
323
327
  code: 'INDEX_EXISTS',
324
328
  collectionName: collection,
@@ -354,7 +358,7 @@ class PostgresDatabase {
354
358
  }
355
359
  parseIndexViolatedForFieldsAndValues(input) {
356
360
  const regex = /Key \((.*)\)=\((.*)\) already exists\./;
357
- const matches = input?.match(regex);
361
+ const matches = input === null || input === void 0 ? void 0 : input.match(regex);
358
362
  if (!matches) {
359
363
  return null;
360
364
  }
@@ -371,11 +375,12 @@ class PostgresDatabase {
371
375
  return result;
372
376
  }
373
377
  async query(query, params) {
378
+ var _a;
374
379
  const results = await this.client.query({
375
380
  text: query,
376
381
  values: params,
377
382
  });
378
- return results?.rows ?? [];
383
+ return (_a = results === null || results === void 0 ? void 0 : results.rows) !== null && _a !== void 0 ? _a : [];
379
384
  }
380
385
  }
381
386
  exports.default = PostgresDatabase;
@@ -7,7 +7,7 @@ class QueryBuilder {
7
7
  return new this();
8
8
  }
9
9
  find(tableName, query, options) {
10
- const { includeFields, limit, skip, sort } = options ?? {};
10
+ const { includeFields, limit, skip, sort } = options !== null && options !== void 0 ? options : {};
11
11
  const fields = this.buildColumnListFromIncludeFields(includeFields);
12
12
  let sql = `SELECT ${fields} FROM ${this.buildTableName(tableName)}`;
13
13
  const { values, sql: where } = this.optionallyBuildWhere(query);
@@ -31,7 +31,7 @@ class QueryBuilder {
31
31
  let sql = '';
32
32
  const values = [];
33
33
  const queryKeys = Object.keys(query);
34
- if ((queryKeys ?? []).length > 0) {
34
+ if ((queryKeys !== null && queryKeys !== void 0 ? queryKeys : []).length > 0) {
35
35
  const { set: columnSpecs, values: whereValues } = this.buildSetClause({
36
36
  query,
37
37
  startingCount: startingPlaceholderCount,
@@ -52,33 +52,33 @@ class QueryBuilder {
52
52
  let value = query[k];
53
53
  const isNull = value === null && useIsNull;
54
54
  const formattedK = this.conditionalQuote(k);
55
- if (value?.$in) {
55
+ if (value === null || value === void 0 ? void 0 : value.$in) {
56
56
  values.push(...value.$in.map((v) => this.normalizeValue(v)));
57
57
  set.push(`${formattedK} IN (${value.$in
58
58
  .map(() => `$${++placeholderCount}`)
59
59
  .join(', ')})`);
60
60
  }
61
- else if (value?.$regex) {
61
+ else if (value === null || value === void 0 ? void 0 : value.$regex) {
62
62
  values.push(this.normalizeValue(value.$regex));
63
63
  set.push(`${formattedK} ~* $${++placeholderCount}`);
64
64
  }
65
- else if (value?.$lte) {
65
+ else if (value === null || value === void 0 ? void 0 : value.$lte) {
66
66
  values.push(this.normalizeValue(value.$lte));
67
67
  set.push(`${formattedK} <= $${++placeholderCount}`);
68
68
  }
69
- else if (value?.$lt) {
69
+ else if (value === null || value === void 0 ? void 0 : value.$lt) {
70
70
  values.push(this.normalizeValue(value.$lt));
71
71
  set.push(`${formattedK} < $${++placeholderCount}`);
72
72
  }
73
- else if (value?.$gte) {
73
+ else if (value === null || value === void 0 ? void 0 : value.$gte) {
74
74
  values.push(this.normalizeValue(value.$gte));
75
75
  set.push(`${formattedK} >= $${++placeholderCount}`);
76
76
  }
77
- else if (value?.$gt) {
77
+ else if (value === null || value === void 0 ? void 0 : value.$gt) {
78
78
  values.push(this.normalizeValue(value.$gt));
79
79
  set.push(`${formattedK} > $${++placeholderCount}`);
80
80
  }
81
- else if (typeof value?.$ne !== 'undefined') {
81
+ else if (typeof (value === null || value === void 0 ? void 0 : value.$ne) !== 'undefined') {
82
82
  const v = value.$ne;
83
83
  v !== null && values.push(this.normalizeValue(v));
84
84
  set.push(`${formattedK} ${v === null ? 'IS NOT NULL' : `!= $${++placeholderCount}`}`);
@@ -196,7 +196,7 @@ class QueryBuilder {
196
196
  value = JSON.stringify(value);
197
197
  }
198
198
  }
199
- return value ?? null;
199
+ return value !== null && value !== void 0 ? value : null;
200
200
  }
201
201
  isValueObject(value) {
202
202
  return (value !== null &&
@@ -252,7 +252,7 @@ class QueryBuilder {
252
252
  }
253
253
  delete(tableName, query) {
254
254
  let sql = `DELETE FROM ${this.buildTableName(tableName)}`;
255
- const { values, sql: where } = this.optionallyBuildWhere(query ?? {});
255
+ const { values, sql: where } = this.optionallyBuildWhere(query !== null && query !== void 0 ? query : {});
256
256
  sql += where;
257
257
  this.log('delete', sql, values);
258
258
  return {
package/package.json CHANGED
@@ -21,7 +21,7 @@
21
21
  "sprucebot",
22
22
  "sprucelabs"
23
23
  ],
24
- "version": "6.0.8",
24
+ "version": "6.0.10",
25
25
  "scripts": {
26
26
  "build.ci": "yarn run build.tsc && yarn run build.resolve-paths && yarn run lint",
27
27
  "build.dev": "yarn run build.tsc --sourceMap ; yarn run resolve-paths.lint",
@@ -50,17 +50,17 @@
50
50
  "watch.tsc": "tsc -w"
51
51
  },
52
52
  "dependencies": {
53
- "@sprucelabs/data-stores": "^27.0.7",
54
- "@sprucelabs/schema": "^30.0.10",
53
+ "@sprucelabs/data-stores": "^27.0.8",
54
+ "@sprucelabs/schema": "^30.0.12",
55
55
  "pg": "^8.11.5"
56
56
  },
57
57
  "devDependencies": {
58
58
  "@sprucelabs/esm-postbuild": "^6.0.8",
59
- "@sprucelabs/jest-json-reporter": "^8.0.6",
59
+ "@sprucelabs/jest-json-reporter": "^8.0.7",
60
60
  "@sprucelabs/resolve-path-aliases": "^2.0.10",
61
61
  "@sprucelabs/semantic-release": "^5.0.1",
62
62
  "@sprucelabs/test": "^9.0.6",
63
- "@sprucelabs/test-utils": "^5.0.8",
63
+ "@sprucelabs/test-utils": "^5.0.9",
64
64
  "@types/node": "^20.12.7",
65
65
  "@types/pg": "^8.11.5",
66
66
  "chokidar-cli": "^3.0.0",