@sprucelabs/postgres-data-store 2.0.4 → 2.0.6
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',
|
|
@@ -84,10 +84,17 @@ class PostgresDatabase {
|
|
|
84
84
|
return results.rowCount;
|
|
85
85
|
}
|
|
86
86
|
async dropDatabase() {
|
|
87
|
+
var _a;
|
|
87
88
|
await this.truncateTables();
|
|
88
89
|
const names = await this.getTables();
|
|
89
90
|
for (const name of names) {
|
|
90
|
-
|
|
91
|
+
try {
|
|
92
|
+
await this.dropAllNonPrimaryKeyIndexes(name);
|
|
93
|
+
}
|
|
94
|
+
catch (err) {
|
|
95
|
+
console.error('Failed to drop indexe1');
|
|
96
|
+
console.error((_a = err.stack) !== null && _a !== void 0 ? _a : err.message);
|
|
97
|
+
}
|
|
91
98
|
}
|
|
92
99
|
}
|
|
93
100
|
async dropAllNonPrimaryKeyIndexes(name) {
|
|
@@ -139,7 +146,7 @@ class PostgresDatabase {
|
|
|
139
146
|
AND table_type = 'BASE TABLE';
|
|
140
147
|
`);
|
|
141
148
|
const tableNames = res.rows.map((row) => row.table_name);
|
|
142
|
-
await Promise.all(tableNames.map((tableName) => this.client.query(`TRUNCATE TABLE
|
|
149
|
+
await Promise.all(tableNames.map((tableName) => this.client.query(`TRUNCATE TABLE public.${tableName} RESTART IDENTITY CASCADE`)));
|
|
143
150
|
}
|
|
144
151
|
async createOne(collection, values) {
|
|
145
152
|
const rows = await this.create(collection, [values]);
|
|
@@ -150,10 +157,10 @@ class PostgresDatabase {
|
|
|
150
157
|
return [];
|
|
151
158
|
}
|
|
152
159
|
const { sql, values } = this.query.create(collection, records);
|
|
153
|
-
const { rows } = await this.executeQuery('create', sql, values);
|
|
160
|
+
const { rows } = await this.executeQuery('create', sql, values, collection);
|
|
154
161
|
return rows;
|
|
155
162
|
}
|
|
156
|
-
async executeQuery(action, sql, values) {
|
|
163
|
+
async executeQuery(action, sql, values, tableName) {
|
|
157
164
|
try {
|
|
158
165
|
const results = await this.client.query({
|
|
159
166
|
text: sql,
|
|
@@ -169,7 +176,7 @@ class PostgresDatabase {
|
|
|
169
176
|
code: 'DUPLICATE_RECORD',
|
|
170
177
|
duplicateFields: fields,
|
|
171
178
|
duplicateValues: values,
|
|
172
|
-
collectionName:
|
|
179
|
+
collectionName: tableName,
|
|
173
180
|
action,
|
|
174
181
|
});
|
|
175
182
|
}
|
|
@@ -237,7 +244,7 @@ class PostgresDatabase {
|
|
|
237
244
|
throw new data_stores_1.DataStoresError({
|
|
238
245
|
code: 'INDEX_NOT_FOUND',
|
|
239
246
|
missingIndex: fields,
|
|
240
|
-
collectionName
|
|
247
|
+
collectionName,
|
|
241
248
|
});
|
|
242
249
|
}
|
|
243
250
|
}
|
|
@@ -282,7 +289,7 @@ class PostgresDatabase {
|
|
|
282
289
|
var _a, _b;
|
|
283
290
|
const indexName = this.generateIndexName(collection, fields);
|
|
284
291
|
const keys = this.generateKeyExpressions(fields);
|
|
285
|
-
const query = `CREATE ${isUnique ? `UNIQUE` : ''} INDEX ${indexName} ON
|
|
292
|
+
const query = `CREATE ${isUnique ? `UNIQUE` : ''} INDEX ${indexName} ON public.${collection} (${keys})`;
|
|
286
293
|
try {
|
|
287
294
|
await this.client.query({
|
|
288
295
|
text: query,
|
|
@@ -292,7 +299,7 @@ class PostgresDatabase {
|
|
|
292
299
|
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
300
|
throw new data_stores_1.DataStoresError({
|
|
294
301
|
code: 'INDEX_EXISTS',
|
|
295
|
-
collectionName:
|
|
302
|
+
collectionName: collection,
|
|
296
303
|
index: ['uniqueField'],
|
|
297
304
|
});
|
|
298
305
|
}
|
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',
|
|
@@ -106,11 +106,18 @@ export default class PostgresDatabase {
|
|
|
106
106
|
});
|
|
107
107
|
}
|
|
108
108
|
dropDatabase() {
|
|
109
|
+
var _a;
|
|
109
110
|
return __awaiter(this, void 0, void 0, function* () {
|
|
110
111
|
yield this.truncateTables();
|
|
111
112
|
const names = yield this.getTables();
|
|
112
113
|
for (const name of names) {
|
|
113
|
-
|
|
114
|
+
try {
|
|
115
|
+
yield this.dropAllNonPrimaryKeyIndexes(name);
|
|
116
|
+
}
|
|
117
|
+
catch (err) {
|
|
118
|
+
console.error('Failed to drop indexe1');
|
|
119
|
+
console.error((_a = err.stack) !== null && _a !== void 0 ? _a : err.message);
|
|
120
|
+
}
|
|
114
121
|
}
|
|
115
122
|
});
|
|
116
123
|
}
|
|
@@ -174,7 +181,7 @@ export default class PostgresDatabase {
|
|
|
174
181
|
AND table_type = 'BASE TABLE';
|
|
175
182
|
`);
|
|
176
183
|
const tableNames = res.rows.map((row) => row.table_name);
|
|
177
|
-
yield Promise.all(tableNames.map((tableName) => this.client.query(`TRUNCATE TABLE
|
|
184
|
+
yield Promise.all(tableNames.map((tableName) => this.client.query(`TRUNCATE TABLE public.${tableName} RESTART IDENTITY CASCADE`)));
|
|
178
185
|
});
|
|
179
186
|
}
|
|
180
187
|
createOne(collection, values) {
|
|
@@ -189,11 +196,11 @@ export default class PostgresDatabase {
|
|
|
189
196
|
return [];
|
|
190
197
|
}
|
|
191
198
|
const { sql, values } = this.query.create(collection, records);
|
|
192
|
-
const { rows } = yield this.executeQuery('create', sql, values);
|
|
199
|
+
const { rows } = yield this.executeQuery('create', sql, values, collection);
|
|
193
200
|
return rows;
|
|
194
201
|
});
|
|
195
202
|
}
|
|
196
|
-
executeQuery(action, sql, values) {
|
|
203
|
+
executeQuery(action, sql, values, tableName) {
|
|
197
204
|
return __awaiter(this, void 0, void 0, function* () {
|
|
198
205
|
try {
|
|
199
206
|
const results = yield this.client.query({
|
|
@@ -210,7 +217,7 @@ export default class PostgresDatabase {
|
|
|
210
217
|
code: 'DUPLICATE_RECORD',
|
|
211
218
|
duplicateFields: fields,
|
|
212
219
|
duplicateValues: values,
|
|
213
|
-
collectionName:
|
|
220
|
+
collectionName: tableName,
|
|
214
221
|
action,
|
|
215
222
|
});
|
|
216
223
|
}
|
|
@@ -286,7 +293,7 @@ export default class PostgresDatabase {
|
|
|
286
293
|
throw new DataStoresError({
|
|
287
294
|
code: 'INDEX_NOT_FOUND',
|
|
288
295
|
missingIndex: fields,
|
|
289
|
-
collectionName
|
|
296
|
+
collectionName,
|
|
290
297
|
});
|
|
291
298
|
}
|
|
292
299
|
});
|
|
@@ -341,7 +348,7 @@ export default class PostgresDatabase {
|
|
|
341
348
|
return __awaiter(this, void 0, void 0, function* () {
|
|
342
349
|
const indexName = this.generateIndexName(collection, fields);
|
|
343
350
|
const keys = this.generateKeyExpressions(fields);
|
|
344
|
-
const query = `CREATE ${isUnique ? `UNIQUE` : ''} INDEX ${indexName} ON
|
|
351
|
+
const query = `CREATE ${isUnique ? `UNIQUE` : ''} INDEX ${indexName} ON public.${collection} (${keys})`;
|
|
345
352
|
try {
|
|
346
353
|
yield this.client.query({
|
|
347
354
|
text: query,
|
|
@@ -351,7 +358,7 @@ export default class PostgresDatabase {
|
|
|
351
358
|
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
359
|
throw new DataStoresError({
|
|
353
360
|
code: 'INDEX_EXISTS',
|
|
354
|
-
collectionName:
|
|
361
|
+
collectionName: collection,
|
|
355
362
|
index: ['uniqueField'],
|
|
356
363
|
});
|
|
357
364
|
}
|
|
@@ -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