query-weaver 0.0.0

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 ADDED
@@ -0,0 +1,142 @@
1
+ # Query Weaver
2
+
3
+ Compose SQL statements safely by leveraging template string literals
4
+
5
+ ## Usage
6
+
7
+ ### As a SQL Builder
8
+ ```js
9
+ import { sql } from 'query-weaver';
10
+ import pg from 'pg';
11
+
12
+ const foo = 1, bar = 'Bar';
13
+ const query = sql`SELECT * FROM foobar WHERE foo = ${ foo } AND bar = ${ bar }`;
14
+
15
+ console.log(query.toString())
16
+ // SELECT * FROM foobar WHERE foo = '1' AND bar = 'Bar'
17
+
18
+ const db = new pg.Pool();
19
+ const { rows } = await db.query(query);
20
+
21
+ console.log(rows)
22
+ // [ { foo: 1, bar: 'Bar' } ]
23
+
24
+ console.log(JSON.stringify(query, null, 2));
25
+ // {
26
+ // "text": "SELECT * FROM foobar WHERE foo = $1 AND bar = $2",
27
+ // "values": [
28
+ // 1,
29
+ // "Bar"
30
+ // ],
31
+ // "embed": "SELECT * FROM foobar WHERE foo = '1' AND bar = 'Bar'"
32
+ // }
33
+
34
+ db.end();
35
+ ```
36
+
37
+ As you can see, the query is executed using **placeholder** on the database. This makes string-value concatenation safe.
38
+ You can also get a string embed version of the query, so that you can debug the query easily.
39
+
40
+
41
+ ### As a Query Helper (with `node-postgres` for example)
42
+ ```js
43
+ import { useQueryHelper } from 'query-weaver';
44
+ import pg from 'pg';
45
+
46
+ const db = useQueryHelper(new pg.Pool());
47
+
48
+ const foo = 1, bar = 'Bar';
49
+ const { rows } = await db.query`SELECT * FROM foobar WHERE foo = ${ foo } AND bar = ${ bar }`;
50
+
51
+ console.log(rows);
52
+ // [ { foo: 1, bar: 'Bar' } ]
53
+
54
+ db.end(); // this call will be proxied to the original pg.Pool() instance
55
+ ```
56
+
57
+ Almost the same as above, but you can directly pass the template string to the `query` function.
58
+
59
+
60
+ ### WHERE builder
61
+ ```js
62
+ import { sql, WHERE, OR } from 'query-weaver';
63
+
64
+ const a = 1, b = "string", c = null, d = 5, e = false;
65
+ console.log(String(sql`SELECT * FROM foobar ${WHERE({ a, b, c }, OR({ d, e }))}`));
66
+ // SELECT * FROM foobar WHERE ((a = '1') AND (b = 'string') AND (c IS NULL) AND (((d = '5') OR (e = false))))
67
+
68
+ const q = sql`SELECT * FROM foobar ${WHERE(
69
+ {
70
+ a: 10,
71
+ b: 'string',
72
+ c: sql`IS UNKNOWN`,
73
+ d: sql`BETWEEN ${a} AND ${d}`
74
+ },
75
+ "e IS NULL"
76
+ sql`f IN (${f})`,
77
+ )}`
78
+ console.log(q.text);
79
+ // SELECT * FROM foobar WHERE ((a = $1) AND (b = $2) AND (c IS UNKNOWN) AND (d BETWEEN $3 AND $4) AND (e IS NULL) AND (f IN ($5)))
80
+
81
+ console.log(q.embed);
82
+ // SELECT * FROM foobar WHERE ((a = '10') AND (b = 'string') AND (c IS UNKNOWN) AND (d BETWEEN '1' AND '5') AND (e IS NULL) AND (f IN (ARRAY['1','2','3','4','5'])))
83
+ ```
84
+
85
+
86
+ ### JSON builder
87
+ ```js
88
+ import pg from 'pg';
89
+ import { useQueryHelper, json } from 'query-weaver';
90
+
91
+ const db = useQueryHelper(new pg.Pool());
92
+
93
+ const id = 10;
94
+ const obj = { b: 'string', c: [1, 2, 'X'], d: { e: null, f: undefined } }
95
+
96
+ const row = await db.getRow`SELECT * FROM jsonb_to_record(${json`{ "a": ${ obj }, "b": ${id} }`}) AS (a jsonb, b int);`
97
+
98
+ console.log(row);
99
+ // {
100
+ // a: { b: 'string', c: [ 1, 2, 'X' ], d: { e: null } },
101
+ // b: 10,
102
+ // }
103
+
104
+ db.end();
105
+ ```
106
+
107
+
108
+ ### VALUES builder
109
+ `buildValues` (or `sql.values`)
110
+
111
+ ```js
112
+ sql.values([[ ... values ], ...]); // => VALUES (...), (...), ...
113
+ ```
114
+
115
+
116
+ ### Simple INSERT builder and executor
117
+ `buildInsert` (or `sql.insert`) and `insert` executor
118
+
119
+ ```js
120
+ sql.insert(tableName, { ... fieldValuePairs }); // => sql`INSERT INTO ...`
121
+ db.insert(tableName, { ... fieldValuePairs }); // => db.query`INSERT INTO ...`
122
+
123
+ // bulk insert
124
+ sql.insert(tableName, [{ ... fieldValuePairs }, ... ]); // => sql`INSERT INTO ... VALUES (...), (...), ...`
125
+ db.insert(tableName, [{ ... fieldValuePairs }, ... ]); // => db.query`INSERT INTO ... VALUES (...), (...), ...`
126
+ ```
127
+
128
+
129
+ ### Simple UPDATE builder and executor
130
+ `buildUpdate` (or `sql.update`) and `update` executor
131
+ ```js
132
+ sql.update(tableName, { ... fieldValuePairs }, { ... whereCondition }); // => sql`UPDATE ...`
133
+ db.update(tableName, { ... fieldValuePairs }, { ... whereCondition }); // => db.query`UPDATE ...`
134
+ ```
135
+
136
+
137
+ ### Simple DELETE builder and executor
138
+ `buildDelete` (or `sql.delete`) and `delete` executor
139
+ ```js
140
+ sql.delete(tableName, { ... whereCondition }); // => sql`DELETE FROM ...`
141
+ db.delete(tableName, { ... whereCondition }); // => db.query`DELETE FROM ...`
142
+ ```
@@ -0,0 +1,2 @@
1
+ export * from './query-weaver';
2
+ export * from './query-helper';