@sprucelabs/postgres-data-store 2.0.7 → 2.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.
@@ -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
- private client;
5
+ protected client: Client;
5
6
  private idCount;
6
- private query;
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.query = QueryBuilder_1.default.Builder();
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.query.update(collection, query, updates, false);
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.query.find(collection, query !== null && query !== void 0 ? query : {}, {
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.query.update(collection, query, updates);
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.query.find(collection, query !== null && query !== void 0 ? query : {}, options);
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.query.delete(collection, query);
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.query.delete(collection, query);
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.query.create(collection, records);
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;
@@ -65,9 +65,10 @@ class QueryBuilder {
65
65
  values.push(this.normalizeValue(value.$gt));
66
66
  set.push(`${k} > $${++placeholderCount}`);
67
67
  }
68
- else if (value === null || value === void 0 ? void 0 : value.$ne) {
69
- values.push(this.normalizeValue(value.$ne));
70
- set.push(`${k} != $${++placeholderCount}`);
68
+ else if (typeof (value === null || value === void 0 ? void 0 : value.$ne) !== 'undefined') {
69
+ const v = value.$ne;
70
+ v !== null && values.push(this.normalizeValue(v));
71
+ set.push(`${k} ${v === null ? 'IS NOT NULL' : `!= $${++placeholderCount}`}`);
71
72
  }
72
73
  else if (k === '$or') {
73
74
  const { set: orWheres, values: orValues } = this.buildSetClausFor$Or(value, placeholderCount);
@@ -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
- private client;
5
+ protected client: Client;
5
6
  private idCount;
6
- private query;
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.query = QueryBuilder.Builder();
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.query.update(collection, query, updates, false);
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.query.find(collection, query !== null && query !== void 0 ? query : {}, {
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.query.update(collection, query, updates);
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.query.find(collection, query !== null && query !== void 0 ? query : {}, options);
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.query.delete(collection, query);
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.query.delete(collection, query);
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.query.create(collection, records);
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
  }
@@ -63,9 +63,10 @@ export default class QueryBuilder {
63
63
  values.push(this.normalizeValue(value.$gt));
64
64
  set.push(`${k} > $${++placeholderCount}`);
65
65
  }
66
- else if (value === null || value === void 0 ? void 0 : value.$ne) {
67
- values.push(this.normalizeValue(value.$ne));
68
- set.push(`${k} != $${++placeholderCount}`);
66
+ else if (typeof (value === null || value === void 0 ? void 0 : value.$ne) !== 'undefined') {
67
+ const v = value.$ne;
68
+ v !== null && values.push(this.normalizeValue(v));
69
+ set.push(`${k} ${v === null ? 'IS NOT NULL' : `!= $${++placeholderCount}`}`);
69
70
  }
70
71
  else if (k === '$or') {
71
72
  const { set: orWheres, values: orValues } = this.buildSetClausFor$Or(value, placeholderCount);
package/package.json CHANGED
@@ -23,7 +23,7 @@
23
23
  "sprucebot",
24
24
  "sprucelabs"
25
25
  ],
26
- "version": "2.0.7",
26
+ "version": "2.1.0",
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,7 +52,7 @@
52
52
  "watch.tsc": "tsc -w"
53
53
  },
54
54
  "dependencies": {
55
- "@sprucelabs/data-stores": "^22.0.3",
55
+ "@sprucelabs/data-stores": "^22.3.0",
56
56
  "@sprucelabs/schema": "^28.5.141",
57
57
  "pg": "^8.10.0"
58
58
  },