@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 +1 -1
- package/SqlRite.js +52 -22
- package/package.json +1 -1
- package/SqlRite.d.ts +0 -22
- package/biome.json +0 -30
package/README.md
CHANGED
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
|
-
|
|
16
|
-
|
|
17
|
-
const
|
|
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
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
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
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
|
-
}
|