@triptease/sql-template-postgres 0.16.17 → 0.17.18

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.
Files changed (3) hide show
  1. package/index.d.ts +2 -1
  2. package/index.js +28 -15
  3. package/package.json +2 -2
package/index.d.ts CHANGED
@@ -2,4 +2,5 @@ import { QueryConfig } from "pg";
2
2
  import { Template } from '@triptease/sql-template';
3
3
  export declare function debugQuery(sql: Template): string;
4
4
  export declare function postgresSql(sql: Template): string;
5
- export declare function query(template: Template): QueryConfig;
5
+ export declare function statement(template: Template): QueryConfig;
6
+ export declare function prepareStatement(template: Template, name?: string): QueryConfig;
package/index.js CHANGED
@@ -1,40 +1,53 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.query = exports.postgresSql = exports.debugQuery = void 0;
3
+ exports.prepareStatement = exports.statement = exports.postgresSql = exports.debugQuery = void 0;
4
4
  const pg_1 = require("pg");
5
+ const crypto_1 = require("crypto");
5
6
  const sql_template_1 = require("@triptease/sql-template");
6
7
  const escapeIdentifier = pg_1.Client.prototype.escapeIdentifier;
7
8
  const escapeLiteral = pg_1.Client.prototype.escapeLiteral;
8
9
  function debugQuery(sql) {
9
- return sql.expressions.flatMap(e => {
10
+ return sql.expressions.reduce((a, e) => {
10
11
  if (e instanceof sql_template_1.Text)
11
- return [e.text];
12
+ return a + e.text;
12
13
  if (e instanceof sql_template_1.Identifier)
13
- return [escapeIdentifier(e.identifier)];
14
+ return a + escapeIdentifier(e.identifier);
14
15
  if (e instanceof sql_template_1.Value)
15
- return [typeof e.value === 'string' ? escapeLiteral(e.value) : e.value];
16
- return [];
17
- }).join('');
16
+ return a + (typeof e.value === 'string' ? escapeLiteral(e.value) : e.value);
17
+ return a;
18
+ }, '');
18
19
  }
19
20
  exports.debugQuery = debugQuery;
20
21
  function postgresSql(sql) {
21
22
  let count = 1;
22
- return sql.expressions.flatMap(e => {
23
+ return sql.expressions.reduce((a, e) => {
23
24
  if (e instanceof sql_template_1.Text)
24
- return [e.text];
25
+ return a + e.text;
25
26
  if (e instanceof sql_template_1.Identifier)
26
- return [escapeIdentifier(e.identifier)];
27
+ return a + escapeIdentifier(e.identifier);
27
28
  if (e instanceof sql_template_1.Value)
28
- return '$' + count++;
29
- return [];
30
- }).join('');
29
+ return a + '$' + count++;
30
+ return a;
31
+ }, '');
31
32
  }
32
33
  exports.postgresSql = postgresSql;
33
- function query(template) {
34
+ function statement(template) {
34
35
  return {
35
36
  text: postgresSql(template),
36
37
  values: template.expressions.flatMap(e => e instanceof sql_template_1.Value ? [e.value] : [])
37
38
  };
38
39
  }
39
- exports.query = query;
40
+ exports.statement = statement;
41
+ function hashSHA256(value) {
42
+ return (0, crypto_1.createHash)('sha256').update(value).digest('hex');
43
+ }
44
+ function prepareStatement(template, name) {
45
+ const { text, values } = statement(template);
46
+ return {
47
+ name: name !== null && name !== void 0 ? name : `urn:hash::sha256:${hashSHA256(text)}`,
48
+ text,
49
+ values
50
+ };
51
+ }
52
+ exports.prepareStatement = prepareStatement;
40
53
  //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@triptease/sql-template-postgres",
3
- "version": "0.16.17",
3
+ "version": "0.17.18",
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.16.17",
23
+ "@triptease/sql-template": "0.17.18",
24
24
  "pg": "^8.7.1",
25
25
  "tslib": "^2.3.1"
26
26
  },