@possumtech/sqlrite 0.1.4 → 0.2.1

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
@@ -169,7 +169,7 @@ const sql = new SqlRite({
169
169
  path: ":memory:",
170
170
 
171
171
  // Path to your SQL directory.
172
- dir: "sql/",
172
+ dir: "sql",
173
173
  });
174
174
  ```
175
175
 
package/SqlRite.js CHANGED
@@ -5,41 +5,71 @@ export default class SqlRite {
5
5
  constructor(options = {}) {
6
6
  const defaults = {
7
7
  path: ":memory:",
8
- dir: "sql/",
8
+ dir: "sql",
9
9
  };
10
10
 
11
11
  const merged = { ...defaults, ...options };
12
12
 
13
13
  const db = new DatabaseSync(merged.path, merged);
14
14
 
15
- const { dir } = merged;
16
- const files = fs.readdirSync(dir);
17
- const code = files.map((f) => fs.readFileSync(dir + f, "utf8")).join("");
15
+ // allow multiple directories
16
+ if (!Array.isArray(merged.dir)) merged.dir = [merged.dir];
17
+ const files = merged.dir.flatMap((d) => this.getFiles(d));
18
+
19
+ const code = files.map((f) => fs.readFileSync(f, "utf8")).join("");
18
20
 
19
21
  this.async = {};
20
22
 
21
23
  const chunks =
22
24
  /-- (?<chunk>(?<type>INIT|EXEC|PREP): (?<name>\w+)\n(?<sql>.*?))($|(?=-- (INIT|EXEC|PREP):))/gs;
23
25
 
26
+ const initChunks = [];
27
+ const execChunks = [];
28
+ const prepChunks = [];
29
+
24
30
  for (const chunk of code.matchAll(chunks)) {
25
- 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
- }
31
+ const { type } = chunk.groups;
32
+
33
+ if (type === "INIT") initChunks.push(chunk.groups);
34
+ if (type === "EXEC") execChunks.push(chunk.groups);
35
+ if (type === "PREP") prepChunks.push(chunk.groups);
36
+ }
37
+
38
+ initChunks.forEach((init) => {
39
+ db.exec(init.sql);
40
+ });
41
+
42
+ execChunks.forEach((exec) => {
43
+ this[exec.name] = () => db.exec(exec.sql);
44
+ this.async[exec.name] = async () => db.exec(exec.sql);
45
+ });
46
+
47
+ prepChunks.forEach((prep) => {
48
+ this[prep.name] = db.prepare(prep.sql);
49
+
50
+ this.async[prep.name] = {};
51
+
52
+ this.async[prep.name].all = async (params = {}) =>
53
+ this[prep.name].all(params);
54
+
55
+ this.async[prep.name].get = async (params = {}) =>
56
+ this[prep.name].get(params);
57
+
58
+ this.async[prep.name].run = async (params = {}) =>
59
+ this[prep.name].run(params);
60
+ });
61
+ }
62
+
63
+ getFiles(dir) {
64
+ const files = [];
65
+
66
+ for (const item of fs.readdirSync(dir)) {
67
+ const path = `${dir}/${item}`;
68
+
69
+ if (fs.lstatSync(path).isDirectory()) files.push(...this.getFiles(path));
70
+ else if (item.endsWith(".sql")) files.push(path);
43
71
  }
72
+
73
+ return files;
44
74
  }
45
75
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@possumtech/sqlrite",
3
- "version": "0.1.4",
3
+ "version": "0.2.1",
4
4
  "description": "SQL Done Right",
5
5
  "keywords": [
6
6
  "node",
package/SqlRite.d.ts DELETED
@@ -1,22 +0,0 @@
1
- import { Database, DatabaseSync, Statement } from 'node:sqlite';
2
-
3
- interface SqlRiteOptions {
4
- path?: string;
5
- dir?: string;
6
- }
7
-
8
- interface SqlRiteAsyncPreparedStatements {
9
- all: (params?: Record<string, any>) => Promise<any[]>;
10
- get: (params?: Record<string, any>) => Promise<any>;
11
- run: (params?: Record<string, any>) => Promise<void>;
12
- }
13
-
14
- interface SqlRiteAsyncMethods {
15
- [key: string]: (() => Promise<void>) | SqlRiteAsyncPreparedStatements;
16
- }
17
-
18
- export default class SqlRite {
19
- constructor(options?: SqlRiteOptions);
20
- async: SqlRiteAsyncMethods;
21
- [key: string]: (() => void) | Statement | SqlRiteAsyncMethods | any;
22
- }
package/biome.json DELETED
@@ -1,30 +0,0 @@
1
- {
2
- "$schema": "https://biomejs.dev/schemas/1.9.4/schema.json",
3
- "vcs": {
4
- "enabled": false,
5
- "clientKind": "git",
6
- "useIgnoreFile": false
7
- },
8
- "files": {
9
- "ignoreUnknown": false,
10
- "ignore": ["SqlRite.d.ts", "package.json", "package-lock.json"]
11
- },
12
- "formatter": {
13
- "enabled": true,
14
- "indentStyle": "tab"
15
- },
16
- "organizeImports": {
17
- "enabled": true
18
- },
19
- "linter": {
20
- "enabled": true,
21
- "rules": {
22
- "recommended": true
23
- }
24
- },
25
- "javascript": {
26
- "formatter": {
27
- "quoteStyle": "double"
28
- }
29
- }
30
- }