@sprucelabs/postgres-data-store 3.0.7 → 3.1.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.
|
@@ -23,6 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
const crypto_1 = require("crypto");
|
|
26
27
|
const data_stores_1 = require("@sprucelabs/data-stores");
|
|
27
28
|
const schema_1 = require("@sprucelabs/schema");
|
|
28
29
|
const pg_1 = require("pg");
|
|
@@ -38,7 +39,9 @@ class PostgresDatabase {
|
|
|
38
39
|
throw new Error('Method not implemented.');
|
|
39
40
|
}
|
|
40
41
|
generateId() {
|
|
41
|
-
return
|
|
42
|
+
return process.env.POSTGRES_ID_FORMAT === 'uuid'
|
|
43
|
+
? (0, crypto_1.randomUUID)()
|
|
44
|
+
: `${this.idCount++}`;
|
|
42
45
|
}
|
|
43
46
|
async update(collection, query, updates) {
|
|
44
47
|
const { sql, values } = this.queries.update(collection, query, updates, false);
|
|
@@ -164,7 +167,7 @@ class PostgresDatabase {
|
|
|
164
167
|
AND table_type = 'BASE TABLE';
|
|
165
168
|
`);
|
|
166
169
|
const tableNames = res.rows.map((row) => row.table_name);
|
|
167
|
-
await Promise.all(tableNames.map((tableName) => this.client.query(`TRUNCATE TABLE
|
|
170
|
+
await Promise.all(tableNames.map((tableName) => this.client.query(`TRUNCATE TABLE "${tableName}" RESTART IDENTITY CASCADE`)));
|
|
168
171
|
}
|
|
169
172
|
async createOne(collection, values) {
|
|
170
173
|
const rows = await this.create(collection, [values]);
|
|
@@ -306,7 +309,7 @@ class PostgresDatabase {
|
|
|
306
309
|
var _a, _b;
|
|
307
310
|
const indexName = this.generateIndexName(collection, fields);
|
|
308
311
|
const keys = this.generateKeyExpressions(fields);
|
|
309
|
-
const query = `CREATE ${isUnique ? `UNIQUE` : ''} INDEX ${indexName} ON
|
|
312
|
+
const query = `CREATE ${isUnique ? `UNIQUE` : ''} INDEX ${indexName} ON "${collection}" (${keys})`;
|
|
310
313
|
try {
|
|
311
314
|
await this.client.query({
|
|
312
315
|
text: query,
|
|
@@ -7,6 +7,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
+
import { randomUUID } from 'crypto';
|
|
10
11
|
import { DataStoresError, } from '@sprucelabs/data-stores';
|
|
11
12
|
import { assertOptions } from '@sprucelabs/schema';
|
|
12
13
|
import { Client } from 'pg';
|
|
@@ -22,7 +23,9 @@ export default class PostgresDatabase {
|
|
|
22
23
|
throw new Error('Method not implemented.');
|
|
23
24
|
}
|
|
24
25
|
generateId() {
|
|
25
|
-
return
|
|
26
|
+
return process.env.POSTGRES_ID_FORMAT === 'uuid'
|
|
27
|
+
? randomUUID()
|
|
28
|
+
: `${this.idCount++}`;
|
|
26
29
|
}
|
|
27
30
|
update(collection, query, updates) {
|
|
28
31
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -179,7 +182,7 @@ export default class PostgresDatabase {
|
|
|
179
182
|
AND table_type = 'BASE TABLE';
|
|
180
183
|
`);
|
|
181
184
|
const tableNames = res.rows.map((row) => row.table_name);
|
|
182
|
-
yield Promise.all(tableNames.map((tableName) => this.client.query(`TRUNCATE TABLE
|
|
185
|
+
yield Promise.all(tableNames.map((tableName) => this.client.query(`TRUNCATE TABLE "${tableName}" RESTART IDENTITY CASCADE`)));
|
|
183
186
|
});
|
|
184
187
|
}
|
|
185
188
|
createOne(collection, values) {
|
|
@@ -345,7 +348,7 @@ export default class PostgresDatabase {
|
|
|
345
348
|
return __awaiter(this, void 0, void 0, function* () {
|
|
346
349
|
const indexName = this.generateIndexName(collection, fields);
|
|
347
350
|
const keys = this.generateKeyExpressions(fields);
|
|
348
|
-
const query = `CREATE ${isUnique ? `UNIQUE` : ''} INDEX ${indexName} ON
|
|
351
|
+
const query = `CREATE ${isUnique ? `UNIQUE` : ''} INDEX ${indexName} ON "${collection}" (${keys})`;
|
|
349
352
|
try {
|
|
350
353
|
yield this.client.query({
|
|
351
354
|
text: query,
|
package/package.json
CHANGED