@triptease/sql-template-postgres 0.10.10 → 0.18.16

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.
package/index.d.ts CHANGED
@@ -1,2 +1,5 @@
1
- export * from "./api";
2
- export { PostgresClient } from "./PostgresClient";
1
+ import { QueryConfig } from "pg";
2
+ import { Template } from '@triptease/sql-template';
3
+ export declare function debugQuery(sql: Template): string;
4
+ export declare function postgresSql(sql: Template): string;
5
+ export declare function query(template: Template): QueryConfig;
package/index.js CHANGED
@@ -1,8 +1,40 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PostgresClient = void 0;
4
- const tslib_1 = require("tslib");
5
- (0, tslib_1.__exportStar)(require("./api"), exports);
6
- var PostgresClient_1 = require("./PostgresClient");
7
- Object.defineProperty(exports, "PostgresClient", { enumerable: true, get: function () { return PostgresClient_1.PostgresClient; } });
3
+ exports.query = exports.postgresSql = exports.debugQuery = void 0;
4
+ const pg_1 = require("pg");
5
+ const sql_template_1 = require("@triptease/sql-template");
6
+ const escapeIdentifier = pg_1.Client.prototype.escapeIdentifier;
7
+ const escapeLiteral = pg_1.Client.prototype.escapeLiteral;
8
+ function debugQuery(sql) {
9
+ return sql.expressions.flatMap(e => {
10
+ if (e instanceof sql_template_1.Text)
11
+ return [e.text];
12
+ if (e instanceof sql_template_1.Identifier)
13
+ return [escapeIdentifier(e.identifier)];
14
+ if (e instanceof sql_template_1.Value)
15
+ return [typeof e.value === 'string' ? escapeLiteral(e.value) : e.value];
16
+ return [];
17
+ }).join('');
18
+ }
19
+ exports.debugQuery = debugQuery;
20
+ function postgresSql(sql) {
21
+ let count = 1;
22
+ return sql.expressions.flatMap(e => {
23
+ if (e instanceof sql_template_1.Text)
24
+ return [e.text];
25
+ if (e instanceof sql_template_1.Identifier)
26
+ return [escapeIdentifier(e.identifier)];
27
+ if (e instanceof sql_template_1.Value)
28
+ return '$' + count++;
29
+ return [];
30
+ }).join('');
31
+ }
32
+ exports.postgresSql = postgresSql;
33
+ function query(template) {
34
+ return {
35
+ text: postgresSql(template),
36
+ values: template.expressions.flatMap(e => e instanceof sql_template_1.Value ? [e.value] : [])
37
+ };
38
+ }
39
+ exports.query = query;
8
40
  //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@triptease/sql-template-postgres",
3
- "version": "0.10.10",
3
+ "version": "0.18.16",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+ssh://git@github.com/triptease/sql-template.git"
@@ -20,7 +20,7 @@
20
20
  "*.d.ts"
21
21
  ],
22
22
  "dependencies": {
23
- "@triptease/sql-template": "0.10.10",
23
+ "@triptease/sql-template": "0.18.16",
24
24
  "pg": "^8.7.1",
25
25
  "tslib": "^2.3.1"
26
26
  },
@@ -1,9 +0,0 @@
1
- import { SimpleObject, SqlClient, SqlTemplate } from "../../sql-template/src";
2
- import { PoolConfig } from "pg";
3
- export declare class PostgresClient implements SqlClient {
4
- private logger;
5
- private pool;
6
- constructor(connectionDetails: PoolConfig, logger?: (sql: string) => void);
7
- query(sqlFragment: SqlTemplate): Promise<SimpleObject[]>;
8
- close(): Promise<void>;
9
- }
package/PostgresClient.js DELETED
@@ -1,33 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PostgresClient = void 0;
4
- const tslib_1 = require("tslib");
5
- const pg_1 = require("pg");
6
- const api_1 = require("./api");
7
- class PostgresClient {
8
- constructor(connectionDetails, logger = (sql) => console.log('PostgresClient.query', sql)) {
9
- this.logger = logger;
10
- this.pool = new pg_1.Pool(connectionDetails);
11
- }
12
- query(sqlFragment) {
13
- return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
14
- const client = yield this.pool.connect();
15
- try {
16
- const sql = (0, api_1.postgresText)(sqlFragment);
17
- this.logger(sql);
18
- const { rows } = yield client.query(sql, [...sqlFragment.values]);
19
- return rows;
20
- }
21
- finally {
22
- client.release();
23
- }
24
- });
25
- }
26
- close() {
27
- return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
28
- return this.pool.end();
29
- });
30
- }
31
- }
32
- exports.PostgresClient = PostgresClient;
33
- //# sourceMappingURL=PostgresClient.js.map
package/api.d.ts DELETED
@@ -1,5 +0,0 @@
1
- import { SqlTemplate } from "../../sql-template/src";
2
- export declare function postgresText(sql: SqlTemplate): string;
3
- export declare function postgresQuery(sql: SqlTemplate): string;
4
- export declare function id(identifier: string): SqlTemplate;
5
- export declare function ids(identifiers: string[]): SqlTemplate;
package/api.js DELETED
@@ -1,33 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ids = exports.id = exports.postgresQuery = exports.postgresText = void 0;
4
- const src_1 = require("../../sql-template/src");
5
- const pg_1 = require("pg");
6
- const CompositeTemplate_1 = require("../../sql-template/src/CompositeTemplate");
7
- const escapeIdentifier = pg_1.Client.prototype.escapeIdentifier;
8
- const escapeLiteral = pg_1.Client.prototype.escapeLiteral;
9
- function postgresText(sql) {
10
- let count = 1;
11
- return sql.text.replace(/\?/g, () => `\$${count++}`);
12
- }
13
- exports.postgresText = postgresText;
14
- // Debug only
15
- function postgresQuery(sql) {
16
- const values = [...sql.values];
17
- return sql.text.replace(/\?/g, () => {
18
- const value = values.shift();
19
- if (typeof value === 'string')
20
- return escapeLiteral(value);
21
- return value;
22
- });
23
- }
24
- exports.postgresQuery = postgresQuery;
25
- function id(identifier) {
26
- return (0, src_1.sqlTemplate)(escapeIdentifier(identifier), []);
27
- }
28
- exports.id = id;
29
- function ids(identifiers) {
30
- return (0, CompositeTemplate_1.compose)(identifiers.map(id));
31
- }
32
- exports.ids = ids;
33
- //# sourceMappingURL=api.js.map