@sprucelabs/postgres-data-store 6.1.444 → 6.1.446
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/build/PostgresDatabase.js +15 -23
- package/build/QueryBuilder.js +13 -13
- package/package.json +9 -9
|
@@ -25,16 +25,15 @@ class PostgresDatabase {
|
|
|
25
25
|
: `${this.idCount++}`;
|
|
26
26
|
}
|
|
27
27
|
async update(collection, query, updates) {
|
|
28
|
-
var _a;
|
|
29
28
|
const { sql, values } = this.queries.update(collection, query, updates, false);
|
|
30
29
|
const results = await this.client.query({
|
|
31
30
|
text: sql,
|
|
32
31
|
values,
|
|
33
32
|
});
|
|
34
|
-
return
|
|
33
|
+
return results.rowCount ?? 0;
|
|
35
34
|
}
|
|
36
35
|
async count(collection, query) {
|
|
37
|
-
const { sql, values } = this.queries.find(collection, query
|
|
36
|
+
const { sql, values } = this.queries.find(collection, query ?? {}, {
|
|
38
37
|
includeFields: ['count(*) as count'],
|
|
39
38
|
});
|
|
40
39
|
const results = await this.client.query({
|
|
@@ -62,7 +61,7 @@ class PostgresDatabase {
|
|
|
62
61
|
return record;
|
|
63
62
|
}
|
|
64
63
|
async find(collection, query, options) {
|
|
65
|
-
const { sql, values } = this.queries.find(collection, query
|
|
64
|
+
const { sql, values } = this.queries.find(collection, query ?? {}, options);
|
|
66
65
|
const results = await this.client.query({
|
|
67
66
|
text: sql,
|
|
68
67
|
values,
|
|
@@ -76,21 +75,19 @@ class PostgresDatabase {
|
|
|
76
75
|
return this.executeGetIndexes(collectionName, false);
|
|
77
76
|
}
|
|
78
77
|
async findOne(collection, query, options) {
|
|
79
|
-
var _a;
|
|
80
78
|
const results = await this.find(collection, query, {
|
|
81
79
|
...options,
|
|
82
80
|
limit: 1,
|
|
83
81
|
});
|
|
84
|
-
return
|
|
82
|
+
return results[0] ?? null;
|
|
85
83
|
}
|
|
86
84
|
async delete(collection, query) {
|
|
87
|
-
var _a;
|
|
88
85
|
const { sql, values } = this.queries.delete(collection, query);
|
|
89
86
|
const results = await this.client.query({
|
|
90
87
|
text: sql,
|
|
91
88
|
values,
|
|
92
89
|
});
|
|
93
|
-
return
|
|
90
|
+
return results.rowCount ?? 0;
|
|
94
91
|
}
|
|
95
92
|
async dropDatabase() {
|
|
96
93
|
await this.truncateTables();
|
|
@@ -132,19 +129,18 @@ class PostgresDatabase {
|
|
|
132
129
|
}
|
|
133
130
|
}
|
|
134
131
|
async deleteOne(collection, query) {
|
|
135
|
-
var _a;
|
|
136
132
|
if (!query.id) {
|
|
137
133
|
const match = await this.findOne(collection, query, {
|
|
138
134
|
includeFields: ['id'],
|
|
139
135
|
});
|
|
140
|
-
query = { id: match
|
|
136
|
+
query = { id: match?.id };
|
|
141
137
|
}
|
|
142
138
|
let { sql, values } = this.queries.delete(collection, query);
|
|
143
139
|
const results = await this.client.query({
|
|
144
140
|
text: sql,
|
|
145
141
|
values,
|
|
146
142
|
});
|
|
147
|
-
return
|
|
143
|
+
return results.rowCount ?? 0;
|
|
148
144
|
}
|
|
149
145
|
async truncateTables() {
|
|
150
146
|
const res = await this.client.query(`
|
|
@@ -177,7 +173,7 @@ class PostgresDatabase {
|
|
|
177
173
|
return results;
|
|
178
174
|
}
|
|
179
175
|
catch (err) {
|
|
180
|
-
const parsed = this.parseIndexViolatedForFieldsAndValues(err
|
|
176
|
+
const parsed = this.parseIndexViolatedForFieldsAndValues(err?.detail);
|
|
181
177
|
if (parsed) {
|
|
182
178
|
const { fields, values } = parsed;
|
|
183
179
|
throw new data_stores_1.DataStoresError({
|
|
@@ -192,7 +188,6 @@ class PostgresDatabase {
|
|
|
192
188
|
}
|
|
193
189
|
}
|
|
194
190
|
async connect() {
|
|
195
|
-
var _a, _b, _c;
|
|
196
191
|
this.client = new pg_1.Client({
|
|
197
192
|
connectionString: this.connectionString,
|
|
198
193
|
});
|
|
@@ -201,14 +196,14 @@ class PostgresDatabase {
|
|
|
201
196
|
}
|
|
202
197
|
catch (err) {
|
|
203
198
|
const message = err.message;
|
|
204
|
-
if ((
|
|
199
|
+
if ((err.code ?? message)?.includes('ECONNREFUSED')) {
|
|
205
200
|
throw new data_stores_1.DataStoresError({
|
|
206
201
|
code: 'UNABLE_TO_CONNECT_TO_DB',
|
|
207
202
|
originalError: err,
|
|
208
203
|
});
|
|
209
204
|
}
|
|
210
|
-
if (message
|
|
211
|
-
const match =
|
|
205
|
+
if (message?.includes('does not exist')) {
|
|
206
|
+
const match = message.match(/"([^"]*)"/) ?? ['', ''];
|
|
212
207
|
throw new data_stores_1.DataStoresError({
|
|
213
208
|
code: 'INVALID_DATABASE_NAME',
|
|
214
209
|
suppliedName: match[1],
|
|
@@ -268,12 +263,11 @@ class PostgresDatabase {
|
|
|
268
263
|
await Promise.all(indexesToRemove.map((index) => this.dropIndex(collectionName, index)));
|
|
269
264
|
await Promise.all([
|
|
270
265
|
...indexesToAdd.map(async (index) => {
|
|
271
|
-
var _a;
|
|
272
266
|
try {
|
|
273
267
|
await this.executeCreateIndex(collectionName, index, isUnique);
|
|
274
268
|
}
|
|
275
269
|
catch (err) {
|
|
276
|
-
if (
|
|
270
|
+
if (err.options?.code !== 'INDEX_EXISTS') {
|
|
277
271
|
throw new data_stores_1.DataStoresError({
|
|
278
272
|
code: 'DUPLICATE_KEY',
|
|
279
273
|
originalError: err,
|
|
@@ -288,7 +282,6 @@ class PostgresDatabase {
|
|
|
288
282
|
await this.executeCreateIndex(collection, fields, true);
|
|
289
283
|
}
|
|
290
284
|
async executeCreateIndex(collection, index, isUnique) {
|
|
291
|
-
var _a, _b;
|
|
292
285
|
const { sql: query } = this.queries.createIndex(collection, index, isUnique);
|
|
293
286
|
try {
|
|
294
287
|
await this.client.query({
|
|
@@ -296,7 +289,7 @@ class PostgresDatabase {
|
|
|
296
289
|
});
|
|
297
290
|
}
|
|
298
291
|
catch (err) {
|
|
299
|
-
if (
|
|
292
|
+
if (err.message?.includes?.('already exists')) {
|
|
300
293
|
throw new data_stores_1.DataStoresError({
|
|
301
294
|
code: 'INDEX_EXISTS',
|
|
302
295
|
collectionName: collection,
|
|
@@ -320,7 +313,7 @@ class PostgresDatabase {
|
|
|
320
313
|
}
|
|
321
314
|
parseIndexViolatedForFieldsAndValues(input) {
|
|
322
315
|
const regex = /Key \((.*)\)=\((.*)\) already exists\./;
|
|
323
|
-
const matches = input
|
|
316
|
+
const matches = input?.match(regex);
|
|
324
317
|
if (!matches) {
|
|
325
318
|
return null;
|
|
326
319
|
}
|
|
@@ -337,12 +330,11 @@ class PostgresDatabase {
|
|
|
337
330
|
return result;
|
|
338
331
|
}
|
|
339
332
|
async query(query, params) {
|
|
340
|
-
var _a;
|
|
341
333
|
const results = await this.client.query({
|
|
342
334
|
text: query,
|
|
343
335
|
values: params,
|
|
344
336
|
});
|
|
345
|
-
return
|
|
337
|
+
return results?.rows ?? [];
|
|
346
338
|
}
|
|
347
339
|
}
|
|
348
340
|
exports.default = PostgresDatabase;
|
package/build/QueryBuilder.js
CHANGED
|
@@ -27,7 +27,7 @@ class QueryBuilder {
|
|
|
27
27
|
return { sql: query, values };
|
|
28
28
|
}
|
|
29
29
|
find(tableName, query, options) {
|
|
30
|
-
const { includeFields, limit, skip, sort } = options
|
|
30
|
+
const { includeFields, limit, skip, sort } = options ?? {};
|
|
31
31
|
const fields = this.buildColumnListFromIncludeFields(includeFields);
|
|
32
32
|
let sql = `SELECT ${fields} FROM ${this.buildTableName(tableName)}`;
|
|
33
33
|
const { values, sql: where } = this.optionallyBuildWhere(query);
|
|
@@ -51,7 +51,7 @@ class QueryBuilder {
|
|
|
51
51
|
let sql = '';
|
|
52
52
|
const values = [];
|
|
53
53
|
const queryKeys = Object.keys(query);
|
|
54
|
-
if ((queryKeys
|
|
54
|
+
if ((queryKeys ?? []).length > 0) {
|
|
55
55
|
const { set: columnSpecs, values: whereValues } = this.buildEqualityClause({
|
|
56
56
|
query,
|
|
57
57
|
startingCount: startingPlaceholderCount,
|
|
@@ -72,39 +72,39 @@ class QueryBuilder {
|
|
|
72
72
|
let value = query[k];
|
|
73
73
|
const isNull = value === null && useIsNull;
|
|
74
74
|
const formattedK = this.conditionalQuote(k);
|
|
75
|
-
if (value
|
|
75
|
+
if (value?.$in) {
|
|
76
76
|
values.push(...value.$in.map((v) => this.normalizeValue(v)));
|
|
77
77
|
set.push(`${formattedK} IN (${value.$in
|
|
78
78
|
.map(() => `$${++placeholderCount}`)
|
|
79
79
|
.join(', ')})`);
|
|
80
80
|
}
|
|
81
|
-
else if (value
|
|
81
|
+
else if (value?.$exists) {
|
|
82
82
|
set.push(`${formattedK} IS NOT NULL`);
|
|
83
83
|
}
|
|
84
|
-
else if (
|
|
84
|
+
else if (value?.$type === 'string') {
|
|
85
85
|
set.push(`${formattedK} ~ '[A-Za-z]+'`);
|
|
86
86
|
}
|
|
87
|
-
else if (value
|
|
87
|
+
else if (value?.$regex) {
|
|
88
88
|
values.push(this.normalizeValue(value.$regex));
|
|
89
89
|
set.push(`${formattedK} ~* $${++placeholderCount}`);
|
|
90
90
|
}
|
|
91
|
-
else if (value
|
|
91
|
+
else if (value?.$lte) {
|
|
92
92
|
values.push(this.normalizeValue(value.$lte));
|
|
93
93
|
set.push(`${formattedK} <= $${++placeholderCount}`);
|
|
94
94
|
}
|
|
95
|
-
else if (value
|
|
95
|
+
else if (value?.$lt) {
|
|
96
96
|
values.push(this.normalizeValue(value.$lt));
|
|
97
97
|
set.push(`${formattedK} < $${++placeholderCount}`);
|
|
98
98
|
}
|
|
99
|
-
else if (value
|
|
99
|
+
else if (value?.$gte) {
|
|
100
100
|
values.push(this.normalizeValue(value.$gte));
|
|
101
101
|
set.push(`${formattedK} >= $${++placeholderCount}`);
|
|
102
102
|
}
|
|
103
|
-
else if (value
|
|
103
|
+
else if (value?.$gt) {
|
|
104
104
|
values.push(this.normalizeValue(value.$gt));
|
|
105
105
|
set.push(`${formattedK} > $${++placeholderCount}`);
|
|
106
106
|
}
|
|
107
|
-
else if (typeof
|
|
107
|
+
else if (typeof value?.$ne !== 'undefined') {
|
|
108
108
|
const v = value.$ne;
|
|
109
109
|
v !== null && values.push(this.normalizeValue(v));
|
|
110
110
|
set.push(`${formattedK} ${v === null ? 'IS NOT NULL' : `!= $${++placeholderCount}`}`);
|
|
@@ -222,7 +222,7 @@ class QueryBuilder {
|
|
|
222
222
|
value = JSON.stringify(value);
|
|
223
223
|
}
|
|
224
224
|
}
|
|
225
|
-
return value
|
|
225
|
+
return value ?? null;
|
|
226
226
|
}
|
|
227
227
|
isValueObject(value) {
|
|
228
228
|
return (value !== null &&
|
|
@@ -278,7 +278,7 @@ class QueryBuilder {
|
|
|
278
278
|
}
|
|
279
279
|
delete(tableName, query) {
|
|
280
280
|
let sql = `DELETE FROM ${this.buildTableName(tableName)}`;
|
|
281
|
-
const { values, sql: where } = this.optionallyBuildWhere(query
|
|
281
|
+
const { values, sql: where } = this.optionallyBuildWhere(query ?? {});
|
|
282
282
|
sql += where;
|
|
283
283
|
this.log('delete', sql, values);
|
|
284
284
|
return {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sprucelabs/postgres-data-store",
|
|
3
|
-
"version": "6.1.
|
|
3
|
+
"version": "6.1.446",
|
|
4
4
|
"description": "Postgres production and test adapters for @sprucelabs/data-stores",
|
|
5
5
|
"skill": {
|
|
6
6
|
"namespace": "postgres-data-store",
|
|
@@ -49,18 +49,18 @@
|
|
|
49
49
|
"watch.tsc": "tsc -w"
|
|
50
50
|
},
|
|
51
51
|
"dependencies": {
|
|
52
|
-
"@sprucelabs/data-stores": "^28.3.
|
|
53
|
-
"@sprucelabs/schema": "^30.0.
|
|
52
|
+
"@sprucelabs/data-stores": "^28.3.142",
|
|
53
|
+
"@sprucelabs/schema": "^30.0.489",
|
|
54
54
|
"pg": "^8.13.0"
|
|
55
55
|
},
|
|
56
56
|
"devDependencies": {
|
|
57
|
-
"@sprucelabs/esm-postbuild": "^6.0.
|
|
58
|
-
"@sprucelabs/jest-json-reporter": "^8.0.
|
|
59
|
-
"@sprucelabs/resolve-path-aliases": "^2.0.
|
|
57
|
+
"@sprucelabs/esm-postbuild": "^6.0.463",
|
|
58
|
+
"@sprucelabs/jest-json-reporter": "^8.0.481",
|
|
59
|
+
"@sprucelabs/resolve-path-aliases": "^2.0.458",
|
|
60
60
|
"@sprucelabs/semantic-release": "^5.0.2",
|
|
61
|
-
"@sprucelabs/test": "^9.0.
|
|
62
|
-
"@sprucelabs/test-utils": "^5.1.
|
|
63
|
-
"@types/node": "^22.7.
|
|
61
|
+
"@sprucelabs/test": "^9.0.51",
|
|
62
|
+
"@sprucelabs/test-utils": "^5.1.433",
|
|
63
|
+
"@types/node": "^22.7.6",
|
|
64
64
|
"@types/pg": "^8.11.10",
|
|
65
65
|
"chokidar-cli": "^3.0.0",
|
|
66
66
|
"eslint": "^9.12.0",
|