@sprucelabs/postgres-data-store 2.0.8 → 2.1.1
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.
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { Database, Index, QueryOptions, UniqueIndex } from '@sprucelabs/data-stores';
|
|
2
|
+
import { Client, QueryResult } from 'pg';
|
|
2
3
|
export default class PostgresDatabase implements Database {
|
|
3
4
|
private connectionString;
|
|
4
|
-
|
|
5
|
+
protected client: Client;
|
|
5
6
|
private idCount;
|
|
6
|
-
private
|
|
7
|
+
private queries;
|
|
7
8
|
constructor(connectionString: string);
|
|
8
9
|
dropCollection(_name: string): Promise<void>;
|
|
9
10
|
generateId(): string;
|
|
@@ -42,4 +43,5 @@ export default class PostgresDatabase implements Database {
|
|
|
42
43
|
close(): Promise<void>;
|
|
43
44
|
isConnected(): boolean;
|
|
44
45
|
private parseIndexViolatedForFieldsAndValues;
|
|
46
|
+
query<T = QueryResult>(query: string): Promise<T>;
|
|
45
47
|
}
|
|
@@ -12,7 +12,7 @@ class PostgresDatabase {
|
|
|
12
12
|
this.idCount = 1;
|
|
13
13
|
(0, schema_1.assertOptions)({ connectionString }, ['connectionString']);
|
|
14
14
|
this.connectionString = connectionString;
|
|
15
|
-
this.
|
|
15
|
+
this.queries = QueryBuilder_1.default.Builder();
|
|
16
16
|
}
|
|
17
17
|
dropCollection(_name) {
|
|
18
18
|
throw new Error('Method not implemented.');
|
|
@@ -21,7 +21,7 @@ class PostgresDatabase {
|
|
|
21
21
|
return `${this.idCount++}`;
|
|
22
22
|
}
|
|
23
23
|
async update(collection, query, updates) {
|
|
24
|
-
const { sql, values } = this.
|
|
24
|
+
const { sql, values } = this.queries.update(collection, query, updates, false);
|
|
25
25
|
const results = await this.client.query({
|
|
26
26
|
text: sql,
|
|
27
27
|
values,
|
|
@@ -29,7 +29,7 @@ class PostgresDatabase {
|
|
|
29
29
|
return results.rowCount;
|
|
30
30
|
}
|
|
31
31
|
async count(collection, query) {
|
|
32
|
-
const { sql, values } = this.
|
|
32
|
+
const { sql, values } = this.queries.find(collection, query !== null && query !== void 0 ? query : {}, {
|
|
33
33
|
includeFields: ['count(*) as count'],
|
|
34
34
|
});
|
|
35
35
|
const results = await this.client.query({
|
|
@@ -44,7 +44,7 @@ class PostgresDatabase {
|
|
|
44
44
|
return record;
|
|
45
45
|
}
|
|
46
46
|
async executeUpdateAndThrowIfNoResults(collection, query, updates, action) {
|
|
47
|
-
const { sql, values } = this.
|
|
47
|
+
const { sql, values } = this.queries.update(collection, query, updates);
|
|
48
48
|
const results = await this.executeQuery(action, sql, values, collection);
|
|
49
49
|
if (results.rowCount === 0) {
|
|
50
50
|
throw new data_stores_1.DataStoresError({
|
|
@@ -57,7 +57,7 @@ class PostgresDatabase {
|
|
|
57
57
|
return record;
|
|
58
58
|
}
|
|
59
59
|
async find(collection, query, options) {
|
|
60
|
-
const { sql, values } = this.
|
|
60
|
+
const { sql, values } = this.queries.find(collection, query !== null && query !== void 0 ? query : {}, options);
|
|
61
61
|
const results = await this.client.query({
|
|
62
62
|
text: sql,
|
|
63
63
|
values,
|
|
@@ -76,7 +76,7 @@ class PostgresDatabase {
|
|
|
76
76
|
return (_a = results[0]) !== null && _a !== void 0 ? _a : null;
|
|
77
77
|
}
|
|
78
78
|
async delete(collection, query) {
|
|
79
|
-
const { sql, values } = this.
|
|
79
|
+
const { sql, values } = this.queries.delete(collection, query);
|
|
80
80
|
const results = await this.client.query({
|
|
81
81
|
text: sql,
|
|
82
82
|
values,
|
|
@@ -129,7 +129,7 @@ class PostgresDatabase {
|
|
|
129
129
|
});
|
|
130
130
|
query = { id: match === null || match === void 0 ? void 0 : match.id };
|
|
131
131
|
}
|
|
132
|
-
let { sql, values } = this.
|
|
132
|
+
let { sql, values } = this.queries.delete(collection, query);
|
|
133
133
|
const results = await this.client.query({
|
|
134
134
|
text: sql,
|
|
135
135
|
values,
|
|
@@ -154,7 +154,7 @@ class PostgresDatabase {
|
|
|
154
154
|
if (records.length === 0) {
|
|
155
155
|
return [];
|
|
156
156
|
}
|
|
157
|
-
const { sql, values } = this.
|
|
157
|
+
const { sql, values } = this.queries.create(collection, records);
|
|
158
158
|
const { rows } = await this.executeQuery('create', sql, values, collection);
|
|
159
159
|
return rows;
|
|
160
160
|
}
|
|
@@ -344,5 +344,10 @@ class PostgresDatabase {
|
|
|
344
344
|
const result = { fields: fixedFields, values };
|
|
345
345
|
return result;
|
|
346
346
|
}
|
|
347
|
+
async query(query) {
|
|
348
|
+
return (await this.client.query({
|
|
349
|
+
text: query,
|
|
350
|
+
}));
|
|
351
|
+
}
|
|
347
352
|
}
|
|
348
353
|
exports.default = PostgresDatabase;
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { Database, Index, QueryOptions, UniqueIndex } from '@sprucelabs/data-stores';
|
|
2
|
+
import { Client, QueryResult } from 'pg';
|
|
2
3
|
export default class PostgresDatabase implements Database {
|
|
3
4
|
private connectionString;
|
|
4
|
-
|
|
5
|
+
protected client: Client;
|
|
5
6
|
private idCount;
|
|
6
|
-
private
|
|
7
|
+
private queries;
|
|
7
8
|
constructor(connectionString: string);
|
|
8
9
|
dropCollection(_name: string): Promise<void>;
|
|
9
10
|
generateId(): string;
|
|
@@ -42,4 +43,5 @@ export default class PostgresDatabase implements Database {
|
|
|
42
43
|
close(): Promise<void>;
|
|
43
44
|
isConnected(): boolean;
|
|
44
45
|
private parseIndexViolatedForFieldsAndValues;
|
|
46
|
+
query<T = QueryResult>(query: string): Promise<T>;
|
|
45
47
|
}
|
|
@@ -16,7 +16,7 @@ export default class PostgresDatabase {
|
|
|
16
16
|
this.idCount = 1;
|
|
17
17
|
assertOptions({ connectionString }, ['connectionString']);
|
|
18
18
|
this.connectionString = connectionString;
|
|
19
|
-
this.
|
|
19
|
+
this.queries = QueryBuilder.Builder();
|
|
20
20
|
}
|
|
21
21
|
dropCollection(_name) {
|
|
22
22
|
throw new Error('Method not implemented.');
|
|
@@ -26,7 +26,7 @@ export default class PostgresDatabase {
|
|
|
26
26
|
}
|
|
27
27
|
update(collection, query, updates) {
|
|
28
28
|
return __awaiter(this, void 0, void 0, function* () {
|
|
29
|
-
const { sql, values } = this.
|
|
29
|
+
const { sql, values } = this.queries.update(collection, query, updates, false);
|
|
30
30
|
const results = yield this.client.query({
|
|
31
31
|
text: sql,
|
|
32
32
|
values,
|
|
@@ -36,7 +36,7 @@ export default class PostgresDatabase {
|
|
|
36
36
|
}
|
|
37
37
|
count(collection, query) {
|
|
38
38
|
return __awaiter(this, void 0, void 0, function* () {
|
|
39
|
-
const { sql, values } = this.
|
|
39
|
+
const { sql, values } = this.queries.find(collection, query !== null && query !== void 0 ? query : {}, {
|
|
40
40
|
includeFields: ['count(*) as count'],
|
|
41
41
|
});
|
|
42
42
|
const results = yield this.client.query({
|
|
@@ -55,7 +55,7 @@ export default class PostgresDatabase {
|
|
|
55
55
|
}
|
|
56
56
|
executeUpdateAndThrowIfNoResults(collection, query, updates, action) {
|
|
57
57
|
return __awaiter(this, void 0, void 0, function* () {
|
|
58
|
-
const { sql, values } = this.
|
|
58
|
+
const { sql, values } = this.queries.update(collection, query, updates);
|
|
59
59
|
const results = yield this.executeQuery(action, sql, values, collection);
|
|
60
60
|
if (results.rowCount === 0) {
|
|
61
61
|
throw new DataStoresError({
|
|
@@ -70,7 +70,7 @@ export default class PostgresDatabase {
|
|
|
70
70
|
}
|
|
71
71
|
find(collection, query, options) {
|
|
72
72
|
return __awaiter(this, void 0, void 0, function* () {
|
|
73
|
-
const { sql, values } = this.
|
|
73
|
+
const { sql, values } = this.queries.find(collection, query !== null && query !== void 0 ? query : {}, options);
|
|
74
74
|
const results = yield this.client.query({
|
|
75
75
|
text: sql,
|
|
76
76
|
values,
|
|
@@ -97,7 +97,7 @@ export default class PostgresDatabase {
|
|
|
97
97
|
}
|
|
98
98
|
delete(collection, query) {
|
|
99
99
|
return __awaiter(this, void 0, void 0, function* () {
|
|
100
|
-
const { sql, values } = this.
|
|
100
|
+
const { sql, values } = this.queries.delete(collection, query);
|
|
101
101
|
const results = yield this.client.query({
|
|
102
102
|
text: sql,
|
|
103
103
|
values,
|
|
@@ -162,7 +162,7 @@ export default class PostgresDatabase {
|
|
|
162
162
|
});
|
|
163
163
|
query = { id: match === null || match === void 0 ? void 0 : match.id };
|
|
164
164
|
}
|
|
165
|
-
let { sql, values } = this.
|
|
165
|
+
let { sql, values } = this.queries.delete(collection, query);
|
|
166
166
|
const results = yield this.client.query({
|
|
167
167
|
text: sql,
|
|
168
168
|
values,
|
|
@@ -193,7 +193,7 @@ export default class PostgresDatabase {
|
|
|
193
193
|
if (records.length === 0) {
|
|
194
194
|
return [];
|
|
195
195
|
}
|
|
196
|
-
const { sql, values } = this.
|
|
196
|
+
const { sql, values } = this.queries.create(collection, records);
|
|
197
197
|
const { rows } = yield this.executeQuery('create', sql, values, collection);
|
|
198
198
|
return rows;
|
|
199
199
|
});
|
|
@@ -406,4 +406,11 @@ export default class PostgresDatabase {
|
|
|
406
406
|
const result = { fields: fixedFields, values };
|
|
407
407
|
return result;
|
|
408
408
|
}
|
|
409
|
+
query(query) {
|
|
410
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
411
|
+
return (yield this.client.query({
|
|
412
|
+
text: query,
|
|
413
|
+
}));
|
|
414
|
+
});
|
|
415
|
+
}
|
|
409
416
|
}
|
package/package.json
CHANGED
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"sprucebot",
|
|
24
24
|
"sprucelabs"
|
|
25
25
|
],
|
|
26
|
-
"version": "2.
|
|
26
|
+
"version": "2.1.1",
|
|
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,14 +52,14 @@
|
|
|
52
52
|
"watch.tsc": "tsc -w"
|
|
53
53
|
},
|
|
54
54
|
"dependencies": {
|
|
55
|
-
"@sprucelabs/data-stores": "^22.
|
|
55
|
+
"@sprucelabs/data-stores": "^22.3.1",
|
|
56
56
|
"@sprucelabs/schema": "^28.5.141",
|
|
57
57
|
"pg": "^8.10.0"
|
|
58
58
|
},
|
|
59
59
|
"devDependencies": {
|
|
60
60
|
"@sprucelabs/esm-postbuild": "^3.0.2",
|
|
61
61
|
"@sprucelabs/jest-json-reporter": "^7.0.2",
|
|
62
|
-
"@sprucelabs/resolve-path-aliases": "^1.1.
|
|
62
|
+
"@sprucelabs/resolve-path-aliases": "^1.1.188",
|
|
63
63
|
"@sprucelabs/semantic-release": "^4.0.8",
|
|
64
64
|
"@sprucelabs/spruce-test-fixtures": "^52.8.0",
|
|
65
65
|
"@sprucelabs/test": "^7.7.424",
|