@possumtech/sqlrite 0.1.4 → 0.1.5

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.
Files changed (2) hide show
  1. package/SqlRite.js +30 -17
  2. package/package.json +1 -1
package/SqlRite.js CHANGED
@@ -21,25 +21,38 @@ export default class SqlRite {
21
21
  const chunks =
22
22
  /-- (?<chunk>(?<type>INIT|EXEC|PREP): (?<name>\w+)\n(?<sql>.*?))($|(?=-- (INIT|EXEC|PREP):))/gs;
23
23
 
24
+ const initChunks = [];
25
+ const execChunks = [];
26
+ const prepChunks = [];
27
+
24
28
  for (const chunk of code.matchAll(chunks)) {
25
29
  const { type, name, sql } = chunk.groups;
26
- switch (type) {
27
- case "INIT":
28
- db.exec(sql);
29
- break;
30
- case "EXEC":
31
- this[name] = () => db.exec(sql);
32
- this.async[name] = async () => db.exec(sql);
33
- break;
34
- case "PREP":
35
- this[name] = db.prepare(sql);
36
-
37
- this.async[name] = {};
38
- this.async[name].all = async (params = {}) => this[name].all(params);
39
- this.async[name].get = async (params = {}) => this[name].get(params);
40
- this.async[name].run = async (params = {}) => this[name].run(params);
41
- break;
42
- }
30
+
31
+ if (type === "INIT") initChunks.push(chunk.groups);
32
+ if (type === "EXEC") execChunks.push(chunk.groups);
33
+ if (type === "PREP") prepChunks.push(chunk.groups);
43
34
  }
35
+
36
+ initChunks.forEach((init) => db.exec(init.sql));
37
+
38
+ execChunks.forEach((exec) => {
39
+ this[exec.name] = () => db.exec(exec.sql);
40
+ this.async[exec.name] = async () => db.exec(exec.sql);
41
+ });
42
+
43
+ prepChunks.forEach((prep) => {
44
+ this[prep.name] = db.prepare(prep.sql);
45
+
46
+ this.async[prep.name] = {};
47
+
48
+ this.async[prep.name].all = async (params = {}) =>
49
+ this[prep.name].all(params);
50
+
51
+ this.async[prep.name].get = async (params = {}) =>
52
+ this[prep.name].get(params);
53
+
54
+ this.async[prep.name].run = async (params = {}) =>
55
+ this[prep.name].run(params);
56
+ });
44
57
  }
45
58
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@possumtech/sqlrite",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "description": "SQL Done Right",
5
5
  "keywords": [
6
6
  "node",