@triptease/sql-template 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.
@@ -0,0 +1,2 @@
1
+ export declare class Expression {
2
+ }
package/Expression.js ADDED
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Expression = void 0;
4
+ class Expression {
5
+ }
6
+ exports.Expression = Expression;
7
+ //# sourceMappingURL=Expression.js.map
@@ -0,0 +1,8 @@
1
+ import { Expression } from "./Expression";
2
+ import { Template } from "./Template";
3
+ export declare class Identifier extends Expression {
4
+ identifier: string;
5
+ constructor(identifier: string);
6
+ }
7
+ export declare function id(identifier: string): Identifier;
8
+ export declare function ids(identifiers: any[], separator?: string): Template;
package/Identifier.js ADDED
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ids = exports.id = exports.Identifier = void 0;
4
+ const Expression_1 = require("./Expression");
5
+ const Template_1 = require("./Template");
6
+ const Text_1 = require("./Text");
7
+ class Identifier extends Expression_1.Expression {
8
+ constructor(identifier) {
9
+ super();
10
+ this.identifier = identifier;
11
+ }
12
+ }
13
+ exports.Identifier = Identifier;
14
+ function id(identifier) {
15
+ return new Identifier(identifier);
16
+ }
17
+ exports.id = id;
18
+ function ids(identifiers, separator = ', ') {
19
+ return (0, Template_1.template)(...identifiers.flatMap((v, i) => i > 0 ? [(0, Text_1.text)(separator), id(v)] : [id(v)]));
20
+ }
21
+ exports.ids = ids;
22
+ //# sourceMappingURL=Identifier.js.map
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/SQL.d.ts CHANGED
@@ -1,4 +1,2 @@
1
- import { SqlTemplate } from "./api";
2
- export declare function SQL(text: TemplateStringsArray, ...values: any[]): SqlTemplate;
3
- export declare function spread(values: any[]): SqlTemplate;
4
- export declare function raw(value: string): SqlTemplate;
1
+ import { Template } from "./Template";
2
+ export declare function SQL(chunks: TemplateStringsArray, ...values: any[]): Template;
package/SQL.js CHANGED
@@ -1,38 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.raw = exports.spread = exports.SQL = void 0;
4
- const CompositeTemplate_1 = require("./CompositeTemplate");
5
- const api_1 = require("./api");
6
- const placeholder = `?`;
7
- function calculateAdditionalText(value) {
8
- if ((0, api_1.isSqlTemplate)(value))
9
- return value.text;
10
- return placeholder;
11
- }
12
- function calculateNewValues(value) {
13
- if (value === undefined)
14
- return [null];
15
- if ((0, api_1.isSqlTemplate)(value))
16
- return value.values;
17
- return [value];
18
- }
19
- function SQL(text, ...values) {
20
- return text.reduce((a, t, i) => {
21
- if (i === values.length)
22
- return (0, api_1.sqlTemplate)(a.text + t, a.values);
23
- const value = values[i];
24
- const newText = t + calculateAdditionalText(value);
25
- const newValues = calculateNewValues(value);
26
- return (0, api_1.sqlTemplate)(a.text + newText, [...a.values, ...newValues]);
27
- }, (0, api_1.sqlTemplate)());
3
+ exports.SQL = void 0;
4
+ const Template_1 = require("./Template");
5
+ const Text_1 = require("./Text");
6
+ const Value_1 = require("./Value");
7
+ function SQL(chunks, ...values) {
8
+ return (0, Template_1.template)(...chunks.flatMap((chunk, index) => {
9
+ if (index > (values.length - 1))
10
+ return [(0, Text_1.text)(chunk)];
11
+ return [(0, Text_1.text)(chunk), (0, Value_1.value)(values[index])];
12
+ }));
28
13
  }
29
14
  exports.SQL = SQL;
30
- function spread(values) {
31
- return (0, CompositeTemplate_1.compose)(values.map(v => (0, api_1.sqlTemplate)(placeholder, calculateNewValues(v))));
32
- }
33
- exports.spread = spread;
34
- function raw(value) {
35
- return (0, api_1.sqlTemplate)(value, []);
36
- }
37
- exports.raw = raw;
38
15
  //# sourceMappingURL=SQL.js.map
package/Template.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ import { Expression } from "./Expression";
2
+ export declare class Template extends Expression {
3
+ expressions: Expression[];
4
+ constructor(expressions: Expression[]);
5
+ }
6
+ export declare function template(...expressions: Expression[]): Template;
package/Template.js ADDED
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.template = exports.Template = void 0;
4
+ const Expression_1 = require("./Expression");
5
+ const Text_1 = require("./Text");
6
+ class Template extends Expression_1.Expression {
7
+ constructor(expressions) {
8
+ super();
9
+ this.expressions = expressions;
10
+ }
11
+ }
12
+ exports.Template = Template;
13
+ function template(...expressions) {
14
+ return new Template(expressions.flatMap(e => {
15
+ if (e instanceof Text_1.Text && e.text === '')
16
+ return [];
17
+ if (e instanceof Template)
18
+ return e.expressions;
19
+ return [e];
20
+ }));
21
+ }
22
+ exports.template = template;
23
+ //# sourceMappingURL=Template.js.map
package/Text.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ import { Expression } from "./Expression";
2
+ export declare class Text extends Expression {
3
+ text: string;
4
+ constructor(text: string);
5
+ }
6
+ export declare function text(text: string): Text;
7
+ export declare const raw: typeof text;
package/Text.js ADDED
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.raw = exports.text = exports.Text = void 0;
4
+ const Expression_1 = require("./Expression");
5
+ class Text extends Expression_1.Expression {
6
+ constructor(text) {
7
+ super();
8
+ this.text = text;
9
+ }
10
+ }
11
+ exports.Text = Text;
12
+ function text(text) {
13
+ return new Text(text);
14
+ }
15
+ exports.text = text;
16
+ exports.raw = text;
17
+ //# sourceMappingURL=Text.js.map
package/Value.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ import { Expression } from "./Expression";
2
+ import { Template } from "./Template";
3
+ export declare class Value extends Expression {
4
+ value: any;
5
+ constructor(value: any);
6
+ }
7
+ export declare function value(value: any): Expression;
8
+ export declare function values(values: any[], separator?: string): Template;
9
+ export declare const spread: typeof values;
package/Value.js ADDED
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.spread = exports.values = exports.value = exports.Value = void 0;
4
+ const Expression_1 = require("./Expression");
5
+ const Template_1 = require("./Template");
6
+ const Text_1 = require("./Text");
7
+ class Value extends Expression_1.Expression {
8
+ constructor(value) {
9
+ super();
10
+ this.value = value;
11
+ }
12
+ }
13
+ exports.Value = Value;
14
+ function value(value) {
15
+ if (value instanceof Expression_1.Expression)
16
+ return value;
17
+ if (value === undefined)
18
+ return new Value(null);
19
+ return new Value(value);
20
+ }
21
+ exports.value = value;
22
+ function values(values, separator = ', ') {
23
+ return (0, Template_1.template)(...values.flatMap((v, i) => i > 0 ? [(0, Text_1.text)(separator), value(v)] : [value(v)]));
24
+ }
25
+ exports.values = values;
26
+ exports.spread = values;
27
+ //# sourceMappingURL=Value.js.map
package/index.d.ts CHANGED
@@ -1,2 +1,6 @@
1
- export * from "./api";
2
- export { SQL, spread, raw } from "./SQL";
1
+ export * from "./Expression";
2
+ export * from "./Identifier";
3
+ export * from "./SQL";
4
+ export * from "./Template";
5
+ export * from "./Text";
6
+ export * from "./Value";
package/index.js CHANGED
@@ -1,10 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.raw = exports.spread = exports.SQL = void 0;
4
3
  const tslib_1 = require("tslib");
5
- (0, tslib_1.__exportStar)(require("./api"), exports);
6
- var SQL_1 = require("./SQL");
7
- Object.defineProperty(exports, "SQL", { enumerable: true, get: function () { return SQL_1.SQL; } });
8
- Object.defineProperty(exports, "spread", { enumerable: true, get: function () { return SQL_1.spread; } });
9
- Object.defineProperty(exports, "raw", { enumerable: true, get: function () { return SQL_1.raw; } });
4
+ (0, tslib_1.__exportStar)(require("./Expression"), exports);
5
+ (0, tslib_1.__exportStar)(require("./Identifier"), exports);
6
+ (0, tslib_1.__exportStar)(require("./SQL"), exports);
7
+ (0, tslib_1.__exportStar)(require("./Template"), exports);
8
+ (0, tslib_1.__exportStar)(require("./Text"), exports);
9
+ (0, tslib_1.__exportStar)(require("./Value"), exports);
10
10
  //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@triptease/sql-template",
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"
@@ -22,5 +22,6 @@
22
22
  "dependencies": {
23
23
  "tslib": "^2.3.1"
24
24
  },
25
- "scripts": {}
25
+ "scripts": {},
26
+ "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"
26
27
  }
@@ -1,9 +0,0 @@
1
- import { SqlTemplate } from "./api";
2
- export declare class CompositeTemplate implements SqlTemplate {
3
- private fragments;
4
- private joiner;
5
- constructor(fragments: SqlTemplate[], joiner: string);
6
- get text(): string;
7
- get values(): ReadonlyArray<any>;
8
- }
9
- export declare function compose(fragments: SqlTemplate[], joiner?: string): SqlTemplate;
@@ -1,21 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.compose = exports.CompositeTemplate = void 0;
4
- class CompositeTemplate {
5
- constructor(fragments, joiner) {
6
- this.fragments = fragments;
7
- this.joiner = joiner;
8
- }
9
- get text() {
10
- return this.fragments.map(f => f.text).join(this.joiner);
11
- }
12
- get values() {
13
- return this.fragments.flatMap(f => f.values);
14
- }
15
- }
16
- exports.CompositeTemplate = CompositeTemplate;
17
- function compose(fragments, joiner = ', ') {
18
- return new CompositeTemplate(fragments, joiner);
19
- }
20
- exports.compose = compose;
21
- //# sourceMappingURL=CompositeTemplate.js.map
package/api.d.ts DELETED
@@ -1,6 +0,0 @@
1
- export interface SqlTemplate {
2
- readonly text: string;
3
- readonly values: ReadonlyArray<any>;
4
- }
5
- export declare function isSqlTemplate(value: any): value is SqlTemplate;
6
- export declare function sqlTemplate(text?: string, values?: ReadonlyArray<any>): SqlTemplate;
package/api.js DELETED
@@ -1,12 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.sqlTemplate = exports.isSqlTemplate = void 0;
4
- function isSqlTemplate(value) {
5
- return value && typeof value['text'] === 'string' && Array.isArray(value['values']);
6
- }
7
- exports.isSqlTemplate = isSqlTemplate;
8
- function sqlTemplate(text = '', values = []) {
9
- return { text, values };
10
- }
11
- exports.sqlTemplate = sqlTemplate;
12
- //# sourceMappingURL=api.js.map