@triptease/sql-template-postgres 0.15.13 → 0.18.19

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)
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
@@ -1 +1,6 @@
1
- export * from "./api";
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 statement(template: Template): QueryConfig;
6
+ export declare function prepareStatement(template: Template, name?: string): QueryConfig;
package/index.js CHANGED
@@ -1,5 +1,53 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const tslib_1 = require("tslib");
4
- (0, tslib_1.__exportStar)(require("./api"), exports);
3
+ exports.prepareStatement = exports.statement = exports.postgresSql = exports.debugQuery = void 0;
4
+ const pg_1 = require("pg");
5
+ const crypto_1 = require("crypto");
6
+ const sql_template_1 = require("@triptease/sql-template");
7
+ const escapeIdentifier = pg_1.Client.prototype.escapeIdentifier;
8
+ const escapeLiteral = pg_1.Client.prototype.escapeLiteral;
9
+ function debugQuery(sql) {
10
+ return sql.expressions.reduce((a, e) => {
11
+ if (e instanceof sql_template_1.Text)
12
+ return a + e.text;
13
+ if (e instanceof sql_template_1.Identifier)
14
+ return a + escapeIdentifier(e.identifier);
15
+ if (e instanceof sql_template_1.Value)
16
+ return a + (typeof e.value === 'string' ? escapeLiteral(e.value) : e.value);
17
+ return a;
18
+ }, '');
19
+ }
20
+ exports.debugQuery = debugQuery;
21
+ function postgresSql(sql) {
22
+ let count = 1;
23
+ return sql.expressions.reduce((a, e) => {
24
+ if (e instanceof sql_template_1.Text)
25
+ return a + e.text;
26
+ if (e instanceof sql_template_1.Identifier)
27
+ return a + escapeIdentifier(e.identifier);
28
+ if (e instanceof sql_template_1.Value)
29
+ return a + '$' + count++;
30
+ return a;
31
+ }, '');
32
+ }
33
+ exports.postgresSql = postgresSql;
34
+ function statement(template) {
35
+ return {
36
+ text: postgresSql(template),
37
+ values: template.expressions.flatMap(e => e instanceof sql_template_1.Value ? [e.value] : [])
38
+ };
39
+ }
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;
5
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.15.13",
3
+ "version": "0.18.19",
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.15.13",
23
+ "@triptease/sql-template": "0.18.19",
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)\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
  }
package/api.d.ts DELETED
@@ -1,7 +0,0 @@
1
- import { SqlTemplate } from "../../sql-template/src";
2
- import { QueryConfig } from "pg";
3
- export declare function postgresText(sql: SqlTemplate): string;
4
- export declare function postgresQuery(sql: SqlTemplate): string;
5
- export declare function id(identifier: string): SqlTemplate;
6
- export declare function ids(identifiers: string[]): SqlTemplate;
7
- export declare function queryConfig(template: SqlTemplate): QueryConfig;
package/api.js DELETED
@@ -1,40 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.queryConfig = 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
- function queryConfig(template) {
34
- return {
35
- text: postgresText(template),
36
- values: [...template.values]
37
- };
38
- }
39
- exports.queryConfig = queryConfig;
40
- //# sourceMappingURL=api.js.map