@triptease/sql-template-postgres 0.3.4 → 0.6.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.
@@ -0,0 +1,9 @@
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
+ }
@@ -0,0 +1,33 @@
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 ADDED
@@ -0,0 +1,5 @@
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 ADDED
@@ -0,0 +1,33 @@
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
package/index.d.ts CHANGED
@@ -1,15 +1,2 @@
1
- import { SqlClient, SqlTemplate, SimpleObject } from '@triptease/sql-template';
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
- }
10
- export declare function postgresText(sql: SqlTemplate): string;
11
- export declare function postgresQuery(sql: SqlTemplate): string;
12
- export declare const escapeIdentifier: (str: string) => string;
13
- export declare const escapeLiteral: (str: string) => string;
14
- export declare function id(identifier: string): SqlTemplate;
15
- export declare function ids(identifiers: string[]): SqlTemplate;
1
+ export * from "./api";
2
+ export { PostgresClient } from "./PostgresClient";
package/index.js CHANGED
@@ -1,60 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ids = exports.id = exports.escapeLiteral = exports.escapeIdentifier = exports.postgresQuery = exports.postgresText = exports.PostgresClient = void 0;
3
+ exports.PostgresClient = void 0;
4
4
  const tslib_1 = require("tslib");
5
- const sql_template_1 = require("@triptease/sql-template");
6
- const CompositeTemplate_1 = require("@triptease/sql-template/CompositeTemplate");
7
- const pg_1 = require("pg");
8
- class PostgresClient {
9
- constructor(connectionDetails, logger = (sql) => console.log('PostgresClient.query', sql)) {
10
- this.logger = logger;
11
- this.pool = new pg_1.Pool(connectionDetails);
12
- }
13
- query(sqlFragment) {
14
- return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
15
- const client = yield this.pool.connect();
16
- try {
17
- const sql = postgresText(sqlFragment);
18
- this.logger(sql);
19
- const { rows } = yield client.query(sql, [...sqlFragment.values]);
20
- return rows;
21
- }
22
- finally {
23
- client.release();
24
- }
25
- });
26
- }
27
- close() {
28
- return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
29
- return this.pool.end();
30
- });
31
- }
32
- }
33
- exports.PostgresClient = PostgresClient;
34
- function postgresText(sql) {
35
- let count = 1;
36
- return sql.text.replace(/\?/g, () => `\$${count++}`);
37
- }
38
- exports.postgresText = postgresText;
39
- // Debug only
40
- function postgresQuery(sql) {
41
- const values = [...sql.values];
42
- return sql.text.replace(/\?/g, () => {
43
- const value = values.shift();
44
- if (typeof value === 'string')
45
- return (0, exports.escapeLiteral)(value);
46
- return value;
47
- });
48
- }
49
- exports.postgresQuery = postgresQuery;
50
- exports.escapeIdentifier = pg_1.Client.prototype.escapeIdentifier;
51
- exports.escapeLiteral = pg_1.Client.prototype.escapeLiteral;
52
- function id(identifier) {
53
- return (0, sql_template_1.sqlTemplate)((0, exports.escapeIdentifier)(identifier), []);
54
- }
55
- exports.id = id;
56
- function ids(identifiers) {
57
- return (0, CompositeTemplate_1.compose)(identifiers.map(id));
58
- }
59
- exports.ids = ids;
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; } });
60
8
  //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@triptease/sql-template-postgres",
3
- "version": "0.3.4",
3
+ "version": "0.6.6",
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.3.4",
23
+ "@triptease/sql-template": "0.6.6",
24
24
  "pg": "^8.7.1",
25
25
  "tslib": "^2.3.1"
26
26
  },