@sprucelabs/postgres-data-store 2.0.3 → 2.0.5
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.
|
@@ -45,7 +45,7 @@ class PostgresDatabase {
|
|
|
45
45
|
}
|
|
46
46
|
async executeUpdateAndThrowIfNoResults(collection, query, updates, action) {
|
|
47
47
|
const { sql, values } = this.query.update(collection, query, updates);
|
|
48
|
-
const results = await this.executeQuery(action, sql, values);
|
|
48
|
+
const results = await this.executeQuery(action, sql, values, collection);
|
|
49
49
|
if (results.rowCount === 0) {
|
|
50
50
|
throw new data_stores_1.DataStoresError({
|
|
51
51
|
code: 'RECORD_NOT_FOUND',
|
|
@@ -139,7 +139,7 @@ class PostgresDatabase {
|
|
|
139
139
|
AND table_type = 'BASE TABLE';
|
|
140
140
|
`);
|
|
141
141
|
const tableNames = res.rows.map((row) => row.table_name);
|
|
142
|
-
await Promise.all(tableNames.map((tableName) => this.client.query(`TRUNCATE TABLE
|
|
142
|
+
await Promise.all(tableNames.map((tableName) => this.client.query(`TRUNCATE TABLE public.${tableName} RESTART IDENTITY CASCADE`)));
|
|
143
143
|
}
|
|
144
144
|
async createOne(collection, values) {
|
|
145
145
|
const rows = await this.create(collection, [values]);
|
|
@@ -150,10 +150,10 @@ class PostgresDatabase {
|
|
|
150
150
|
return [];
|
|
151
151
|
}
|
|
152
152
|
const { sql, values } = this.query.create(collection, records);
|
|
153
|
-
const { rows } = await this.executeQuery('create', sql, values);
|
|
153
|
+
const { rows } = await this.executeQuery('create', sql, values, collection);
|
|
154
154
|
return rows;
|
|
155
155
|
}
|
|
156
|
-
async executeQuery(action, sql, values) {
|
|
156
|
+
async executeQuery(action, sql, values, tableName) {
|
|
157
157
|
try {
|
|
158
158
|
const results = await this.client.query({
|
|
159
159
|
text: sql,
|
|
@@ -169,7 +169,7 @@ class PostgresDatabase {
|
|
|
169
169
|
code: 'DUPLICATE_RECORD',
|
|
170
170
|
duplicateFields: fields,
|
|
171
171
|
duplicateValues: values,
|
|
172
|
-
collectionName:
|
|
172
|
+
collectionName: tableName,
|
|
173
173
|
action,
|
|
174
174
|
});
|
|
175
175
|
}
|
|
@@ -237,7 +237,7 @@ class PostgresDatabase {
|
|
|
237
237
|
throw new data_stores_1.DataStoresError({
|
|
238
238
|
code: 'INDEX_NOT_FOUND',
|
|
239
239
|
missingIndex: fields,
|
|
240
|
-
collectionName
|
|
240
|
+
collectionName,
|
|
241
241
|
});
|
|
242
242
|
}
|
|
243
243
|
}
|
|
@@ -282,7 +282,7 @@ class PostgresDatabase {
|
|
|
282
282
|
var _a, _b;
|
|
283
283
|
const indexName = this.generateIndexName(collection, fields);
|
|
284
284
|
const keys = this.generateKeyExpressions(fields);
|
|
285
|
-
const query = `CREATE ${isUnique ? `UNIQUE` : ''} INDEX ${indexName} ON
|
|
285
|
+
const query = `CREATE ${isUnique ? `UNIQUE` : ''} INDEX ${indexName} ON public.${collection} (${keys})`;
|
|
286
286
|
try {
|
|
287
287
|
await this.client.query({
|
|
288
288
|
text: query,
|
|
@@ -292,7 +292,7 @@ class PostgresDatabase {
|
|
|
292
292
|
if ((_b = (_a = err.message) === null || _a === void 0 ? void 0 : _a.includes) === null || _b === void 0 ? void 0 : _b.call(_a, 'already exists')) {
|
|
293
293
|
throw new data_stores_1.DataStoresError({
|
|
294
294
|
code: 'INDEX_EXISTS',
|
|
295
|
-
collectionName:
|
|
295
|
+
collectionName: collection,
|
|
296
296
|
index: ['uniqueField'],
|
|
297
297
|
});
|
|
298
298
|
}
|
package/build/QueryBuilder.js
CHANGED
|
@@ -8,7 +8,7 @@ class QueryBuilder {
|
|
|
8
8
|
find(tableName, query, options) {
|
|
9
9
|
const { includeFields, limit, skip, sort } = options !== null && options !== void 0 ? options : {};
|
|
10
10
|
const fields = this.buildColumnListFromIncludeFields(includeFields);
|
|
11
|
-
let sql = `SELECT ${fields} FROM
|
|
11
|
+
let sql = `SELECT ${fields} FROM public.${tableName}`;
|
|
12
12
|
const { values, sql: where } = this.optionallyBuildWhere(query);
|
|
13
13
|
sql += where;
|
|
14
14
|
sql += this.optionallyBuildSkip(skip);
|
|
@@ -132,7 +132,7 @@ class QueryBuilder {
|
|
|
132
132
|
}
|
|
133
133
|
createWithoutReturning(tableName, records) {
|
|
134
134
|
const { fields, placeholders, values } = this.splitRecordsIntoFieldsPlaceholdersAndValues(records);
|
|
135
|
-
const sql = `INSERT INTO
|
|
135
|
+
const sql = `INSERT INTO public.${tableName} (${fields.join(', ')}) VALUES ${placeholders.join(', ')}`;
|
|
136
136
|
return { sql, values };
|
|
137
137
|
}
|
|
138
138
|
splitRecordsIntoFieldsPlaceholdersAndValues(records) {
|
|
@@ -201,7 +201,7 @@ class QueryBuilder {
|
|
|
201
201
|
query: updates,
|
|
202
202
|
startingCount: 0,
|
|
203
203
|
});
|
|
204
|
-
let sql = `UPDATE
|
|
204
|
+
let sql = `UPDATE public.${tableName} SET ${set.join(', ')}`;
|
|
205
205
|
const { sql: where, values: whereValues } = this.optionallyBuildWhere(query, values.length);
|
|
206
206
|
sql += where;
|
|
207
207
|
if (shouldReturnUpdatedRecords) {
|
|
@@ -213,7 +213,7 @@ class QueryBuilder {
|
|
|
213
213
|
};
|
|
214
214
|
}
|
|
215
215
|
delete(tableName, query) {
|
|
216
|
-
let sql = `DELETE FROM
|
|
216
|
+
let sql = `DELETE FROM public.${tableName}`;
|
|
217
217
|
const { values, sql: where } = this.optionallyBuildWhere(query !== null && query !== void 0 ? query : {});
|
|
218
218
|
sql += where;
|
|
219
219
|
return {
|
|
@@ -56,7 +56,7 @@ export default class PostgresDatabase {
|
|
|
56
56
|
executeUpdateAndThrowIfNoResults(collection, query, updates, action) {
|
|
57
57
|
return __awaiter(this, void 0, void 0, function* () {
|
|
58
58
|
const { sql, values } = this.query.update(collection, query, updates);
|
|
59
|
-
const results = yield this.executeQuery(action, sql, values);
|
|
59
|
+
const results = yield this.executeQuery(action, sql, values, collection);
|
|
60
60
|
if (results.rowCount === 0) {
|
|
61
61
|
throw new DataStoresError({
|
|
62
62
|
code: 'RECORD_NOT_FOUND',
|
|
@@ -174,7 +174,7 @@ export default class PostgresDatabase {
|
|
|
174
174
|
AND table_type = 'BASE TABLE';
|
|
175
175
|
`);
|
|
176
176
|
const tableNames = res.rows.map((row) => row.table_name);
|
|
177
|
-
yield Promise.all(tableNames.map((tableName) => this.client.query(`TRUNCATE TABLE
|
|
177
|
+
yield Promise.all(tableNames.map((tableName) => this.client.query(`TRUNCATE TABLE public.${tableName} RESTART IDENTITY CASCADE`)));
|
|
178
178
|
});
|
|
179
179
|
}
|
|
180
180
|
createOne(collection, values) {
|
|
@@ -189,11 +189,11 @@ export default class PostgresDatabase {
|
|
|
189
189
|
return [];
|
|
190
190
|
}
|
|
191
191
|
const { sql, values } = this.query.create(collection, records);
|
|
192
|
-
const { rows } = yield this.executeQuery('create', sql, values);
|
|
192
|
+
const { rows } = yield this.executeQuery('create', sql, values, collection);
|
|
193
193
|
return rows;
|
|
194
194
|
});
|
|
195
195
|
}
|
|
196
|
-
executeQuery(action, sql, values) {
|
|
196
|
+
executeQuery(action, sql, values, tableName) {
|
|
197
197
|
return __awaiter(this, void 0, void 0, function* () {
|
|
198
198
|
try {
|
|
199
199
|
const results = yield this.client.query({
|
|
@@ -210,7 +210,7 @@ export default class PostgresDatabase {
|
|
|
210
210
|
code: 'DUPLICATE_RECORD',
|
|
211
211
|
duplicateFields: fields,
|
|
212
212
|
duplicateValues: values,
|
|
213
|
-
collectionName:
|
|
213
|
+
collectionName: tableName,
|
|
214
214
|
action,
|
|
215
215
|
});
|
|
216
216
|
}
|
|
@@ -286,7 +286,7 @@ export default class PostgresDatabase {
|
|
|
286
286
|
throw new DataStoresError({
|
|
287
287
|
code: 'INDEX_NOT_FOUND',
|
|
288
288
|
missingIndex: fields,
|
|
289
|
-
collectionName
|
|
289
|
+
collectionName,
|
|
290
290
|
});
|
|
291
291
|
}
|
|
292
292
|
});
|
|
@@ -341,7 +341,7 @@ export default class PostgresDatabase {
|
|
|
341
341
|
return __awaiter(this, void 0, void 0, function* () {
|
|
342
342
|
const indexName = this.generateIndexName(collection, fields);
|
|
343
343
|
const keys = this.generateKeyExpressions(fields);
|
|
344
|
-
const query = `CREATE ${isUnique ? `UNIQUE` : ''} INDEX ${indexName} ON
|
|
344
|
+
const query = `CREATE ${isUnique ? `UNIQUE` : ''} INDEX ${indexName} ON public.${collection} (${keys})`;
|
|
345
345
|
try {
|
|
346
346
|
yield this.client.query({
|
|
347
347
|
text: query,
|
|
@@ -351,7 +351,7 @@ export default class PostgresDatabase {
|
|
|
351
351
|
if ((_b = (_a = err.message) === null || _a === void 0 ? void 0 : _a.includes) === null || _b === void 0 ? void 0 : _b.call(_a, 'already exists')) {
|
|
352
352
|
throw new DataStoresError({
|
|
353
353
|
code: 'INDEX_EXISTS',
|
|
354
|
-
collectionName:
|
|
354
|
+
collectionName: collection,
|
|
355
355
|
index: ['uniqueField'],
|
|
356
356
|
});
|
|
357
357
|
}
|
|
@@ -6,7 +6,7 @@ export default class QueryBuilder {
|
|
|
6
6
|
find(tableName, query, options) {
|
|
7
7
|
const { includeFields, limit, skip, sort } = options !== null && options !== void 0 ? options : {};
|
|
8
8
|
const fields = this.buildColumnListFromIncludeFields(includeFields);
|
|
9
|
-
let sql = `SELECT ${fields} FROM
|
|
9
|
+
let sql = `SELECT ${fields} FROM public.${tableName}`;
|
|
10
10
|
const { values, sql: where } = this.optionallyBuildWhere(query);
|
|
11
11
|
sql += where;
|
|
12
12
|
sql += this.optionallyBuildSkip(skip);
|
|
@@ -130,7 +130,7 @@ export default class QueryBuilder {
|
|
|
130
130
|
}
|
|
131
131
|
createWithoutReturning(tableName, records) {
|
|
132
132
|
const { fields, placeholders, values } = this.splitRecordsIntoFieldsPlaceholdersAndValues(records);
|
|
133
|
-
const sql = `INSERT INTO
|
|
133
|
+
const sql = `INSERT INTO public.${tableName} (${fields.join(', ')}) VALUES ${placeholders.join(', ')}`;
|
|
134
134
|
return { sql, values };
|
|
135
135
|
}
|
|
136
136
|
splitRecordsIntoFieldsPlaceholdersAndValues(records) {
|
|
@@ -199,7 +199,7 @@ export default class QueryBuilder {
|
|
|
199
199
|
query: updates,
|
|
200
200
|
startingCount: 0,
|
|
201
201
|
});
|
|
202
|
-
let sql = `UPDATE
|
|
202
|
+
let sql = `UPDATE public.${tableName} SET ${set.join(', ')}`;
|
|
203
203
|
const { sql: where, values: whereValues } = this.optionallyBuildWhere(query, values.length);
|
|
204
204
|
sql += where;
|
|
205
205
|
if (shouldReturnUpdatedRecords) {
|
|
@@ -211,7 +211,7 @@ export default class QueryBuilder {
|
|
|
211
211
|
};
|
|
212
212
|
}
|
|
213
213
|
delete(tableName, query) {
|
|
214
|
-
let sql = `DELETE FROM
|
|
214
|
+
let sql = `DELETE FROM public.${tableName}`;
|
|
215
215
|
const { values, sql: where } = this.optionallyBuildWhere(query !== null && query !== void 0 ? query : {});
|
|
216
216
|
sql += where;
|
|
217
217
|
return {
|
package/package.json
CHANGED
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"sprucebot",
|
|
24
24
|
"sprucelabs"
|
|
25
25
|
],
|
|
26
|
-
"version": "2.0.
|
|
26
|
+
"version": "2.0.5",
|
|
27
27
|
"scripts": {
|
|
28
28
|
"build.ci": "yarn build.tsc && yarn build.resolve-paths && yarn lint",
|
|
29
29
|
"build.dev": "yarn build.tsc --sourceMap ; yarn resolve-paths.lint",
|
|
@@ -52,9 +52,9 @@
|
|
|
52
52
|
"watch.tsc": "tsc -w"
|
|
53
53
|
},
|
|
54
54
|
"dependencies": {
|
|
55
|
-
"@sprucelabs/data-stores": "^22.0.
|
|
55
|
+
"@sprucelabs/data-stores": "^22.0.3",
|
|
56
56
|
"@sprucelabs/schema": "^28.5.141",
|
|
57
|
-
"pg": "^8.
|
|
57
|
+
"pg": "^8.10.0"
|
|
58
58
|
},
|
|
59
59
|
"devDependencies": {
|
|
60
60
|
"@sprucelabs/esm-postbuild": "^3.0.2",
|