@triptease/sql-template-postgres 0.16.17 → 0.19.20

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/README.md CHANGED
@@ -0,0 +1,29 @@
1
+ # Sql Template
2
+
3
+ This is yet another SQL tagged template for Typescript/Javascript
4
+
5
+ ## Why another library?
6
+
7
+ * Typescript first
8
+ * Functional/Immutable
9
+ * Super simple implementaion (the main SQL function is 3 lines of code - https://github.com/triptease/sql-template/blob/master/sql-template/src/SQL.ts#L5)
10
+ * Plugable to any DB (currently has a Postgres function in @triptease\sql-template-postgres)
11
+ * Automatic support for prepareStatement naming (Postgres)
12
+
13
+ ## Installation
14
+
15
+ ```
16
+ npm install @triptease\sql-template @triptease\sql-template-postgres
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ ```
22
+ import {SQL} from "@triptease/sql-template";
23
+ import {statement} from "@triptease/sql-template-postgres";
24
+
25
+ pg.query(statement(SQL`select * from user where name = ${name}`));
26
+
27
+ ```
28
+
29
+ WIP more to come
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.19.20",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+ssh://git@github.com/triptease/sql-template.git"
@@ -20,12 +20,13 @@
20
20
  "*.d.ts"
21
21
  ],
22
22
  "dependencies": {
23
- "@triptease/sql-template": "0.16.17",
23
+ "@triptease/sql-template": "0.19.20",
24
24
  "pg": "^8.7.1",
25
25
  "tslib": "^2.3.1"
26
26
  },
27
27
  "devDependencies": {
28
28
  "@types/pg": "^8.6.4"
29
29
  },
30
- "scripts": {}
30
+ "scripts": {},
31
+ "readme": "# Sql Template\r\n\r\nThis is yet another SQL tagged template for Typescript/Javascript\r\n\r\n## Why another library?\r\n\r\n* Typescript first\r\n* Functional/Immutable\r\n* Super simple implementaion (the main SQL function is 3 lines of code - https://github.com/triptease/sql-template/blob/master/sql-template/src/SQL.ts#L5)\r\n* Plugable to any DB (currently has a Postgres function in @triptease\\sql-template-postgres)\r\n* Automatic support for prepareStatement naming (Postgres)\r\n\r\n## Installation\r\n\r\n```\r\nnpm install @triptease\\sql-template @triptease\\sql-template-postgres\r\n```\r\n\r\n## Usage\r\n\r\n```\r\nimport {SQL} from \"@triptease/sql-template\";\r\nimport {statement} from \"@triptease/sql-template-postgres\";\r\n\r\npg.query(statement(SQL`select * from user where name = ${name}`));\r\n\r\n```\r\n\r\nWIP more to come\r\n"
31
32
  }