@triptease/sql-template-postgres 0.18.16 → 0.21.22
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 +56 -0
- package/index.d.ts +2 -2
- package/index.js +30 -18
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -0,0 +1,56 @@
|
|
|
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 implementation (the main SQL function is [3 lines of code](https://github.com/triptease/sql-template/blob/master/sql-template/src/SQL.ts#L5))
|
|
10
|
+
* Full escaping of identifiers and values
|
|
11
|
+
* Plugable to any DB (currently only Postgres @triptease\sql-template-postgres )
|
|
12
|
+
* Automatic support for prepareStatement naming (Postgres)
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```shell
|
|
17
|
+
npm install @triptease\sql-template @triptease\sql-template-postgres
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
```javascript
|
|
23
|
+
import {SQL, id} from "@triptease/sql-template";
|
|
24
|
+
import {statement} from "@triptease/sql-template-postgres";
|
|
25
|
+
|
|
26
|
+
client.query(statement(SQL`select * from ${id(table)} where name = ${name}`));
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Cheatsheet
|
|
30
|
+
|
|
31
|
+
### Core (@triptease/sql-template)
|
|
32
|
+
|
|
33
|
+
| function | Description |
|
|
34
|
+
|------------------------------------------------|----------------------------------------------------------------------|
|
|
35
|
+
| `SQL` | The main function to create tagged templates for SQL (*DB agnostic*) |
|
|
36
|
+
| `text` (alias `raw`) | Input raw SQL without any escaping (*use with care*) |
|
|
37
|
+
| `id` / `ids` | Input dynamic identifiers into SQL (*escaped as needed*) |
|
|
38
|
+
| `value` (optional) / `values` (alias `spread`) | Input one or more values into SQL (*escaped as needed*) |
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
### Postgres (@triptease/sql-template-postgres)
|
|
42
|
+
|
|
43
|
+
| function | Description |
|
|
44
|
+
|------------------------------------------------|---------------------------------------------------------------------|
|
|
45
|
+
| `statement` | Converts DB agnostic `SQL` template into postgres statement |
|
|
46
|
+
| `prepareStatement` | Converts DB agnostic `SQL` template into postgres prepare statement |
|
|
47
|
+
| `debugQuery` | Used to debug a query (*use with care*) |
|
|
48
|
+
|
|
49
|
+
*use with care -> Used incorrectly you can open yourself up to SQL injection*
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
### Extending
|
|
53
|
+
|
|
54
|
+
It is incredibly simple to extend to other DBs, have a look at the [postgres implementation](https://github.com/triptease/sql-template/blob/master/sql-template-postgres/src/index.ts#L17).
|
|
55
|
+
|
|
56
|
+
|
package/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { QueryConfig } from "pg";
|
|
2
2
|
import { Template } from '@triptease/sql-template';
|
|
3
3
|
export declare function debugQuery(sql: Template): string;
|
|
4
|
-
export declare function
|
|
5
|
-
export declare function
|
|
4
|
+
export declare function statement(template: Template): QueryConfig;
|
|
5
|
+
export declare function prepareStatement(template: Template, name?: string): QueryConfig;
|
package/index.js
CHANGED
|
@@ -1,40 +1,52 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.prepareStatement = exports.statement = 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.
|
|
10
|
+
return sql.expressions.reduce((a, e) => {
|
|
10
11
|
if (e instanceof sql_template_1.Text)
|
|
11
|
-
return
|
|
12
|
+
return a + e.text;
|
|
12
13
|
if (e instanceof sql_template_1.Identifier)
|
|
13
|
-
return
|
|
14
|
+
return a + escapeIdentifier(e.identifier);
|
|
14
15
|
if (e instanceof sql_template_1.Value)
|
|
15
|
-
return
|
|
16
|
-
return
|
|
17
|
-
}
|
|
16
|
+
return a + (typeof e.value === 'string' ? escapeLiteral(e.value) : e.value);
|
|
17
|
+
return a;
|
|
18
|
+
}, '');
|
|
18
19
|
}
|
|
19
20
|
exports.debugQuery = debugQuery;
|
|
20
|
-
function
|
|
21
|
+
function toSql(sql) {
|
|
21
22
|
let count = 1;
|
|
22
|
-
return sql.expressions.
|
|
23
|
+
return sql.expressions.reduce((a, e) => {
|
|
23
24
|
if (e instanceof sql_template_1.Text)
|
|
24
|
-
return
|
|
25
|
+
return a + e.text;
|
|
25
26
|
if (e instanceof sql_template_1.Identifier)
|
|
26
|
-
return
|
|
27
|
+
return a + escapeIdentifier(e.identifier);
|
|
27
28
|
if (e instanceof sql_template_1.Value)
|
|
28
|
-
return '$' + count++;
|
|
29
|
-
return
|
|
30
|
-
}
|
|
29
|
+
return a + '$' + count++;
|
|
30
|
+
return a;
|
|
31
|
+
}, '');
|
|
31
32
|
}
|
|
32
|
-
|
|
33
|
-
function query(template) {
|
|
33
|
+
function statement(template) {
|
|
34
34
|
return {
|
|
35
|
-
text:
|
|
35
|
+
text: toSql(template),
|
|
36
36
|
values: template.expressions.flatMap(e => e instanceof sql_template_1.Value ? [e.value] : [])
|
|
37
37
|
};
|
|
38
38
|
}
|
|
39
|
-
exports.
|
|
39
|
+
exports.statement = statement;
|
|
40
|
+
function hashSHA256(value) {
|
|
41
|
+
return (0, crypto_1.createHash)('sha256').update(value).digest('hex');
|
|
42
|
+
}
|
|
43
|
+
function prepareStatement(template, name) {
|
|
44
|
+
const { text, values } = statement(template);
|
|
45
|
+
return {
|
|
46
|
+
name: name !== null && name !== void 0 ? name : `urn:hash::sha256:${hashSHA256(text)}`,
|
|
47
|
+
text,
|
|
48
|
+
values
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
exports.prepareStatement = prepareStatement;
|
|
40
52
|
//# 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
|
+
"version": "0.21.22",
|
|
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.
|
|
23
|
+
"@triptease/sql-template": "0.21.22",
|
|
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 implementation (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* Full escaping of identifiers and values\r\n* Plugable to any DB (currently only Postgres @triptease\\sql-template-postgres )\r\n* Automatic support for prepareStatement naming (Postgres)\r\n\r\n## Installation\r\n\r\n```shell\r\nnpm install @triptease\\sql-template @triptease\\sql-template-postgres\r\n```\r\n\r\n## Usage\r\n\r\n```javascript\r\nimport {SQL, id} from \"@triptease/sql-template\";\r\nimport {statement} from \"@triptease/sql-template-postgres\";\r\n\r\nclient.query(statement(SQL`select * from ${id(table)} where name = ${name}`));\r\n```\r\n\r\n## Cheatsheet\r\n\r\n### Core (@triptease/sql-template) \r\n\r\n| function | Description |\r\n|------------------------------------------------|----------------------------------------------------------------------|\r\n| `SQL` | The main function to create tagged templates for SQL (*DB agnostic*) |\r\n| `text` (alias `raw`) | Input raw SQL without any escaping (*use with care*) |\r\n | `id` / `ids` | Input dynamic identifiers into SQL (*escaped as needed*) |\r\n | `value` (optional) / `values` (alias `spread`) | Input one or more values into SQL (*escaped as needed*) |\r\n\r\n\r\n### Postgres (@triptease/sql-template-postgres)\r\n\r\n| function | Description |\r\n|------------------------------------------------|---------------------------------------------------------------------|\r\n| `statement` | Converts DB agnostic `SQL` template into postgres statement |\r\n| `prepareStatement` | Converts DB agnostic `SQL` template into postgres prepare statement |\r\n| `debugQuery` | Used to debug a query (*use with care*) |\r\n\r\n*use with care -> Used incorrectly you can open yourself up to SQL injection*\r\n\r\n\r\n### Extending\r\n\r\nIt is incredibly simple to extend to other DBs, have a look at the [postgres implementation](https://github.com/triptease/sql-template/blob/master/sql-template-postgres/src/index.ts#L17). \r\n\r\n\r\n"
|
|
31
32
|
}
|