-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql.js
95 lines (74 loc) · 2.18 KB
/
sql.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// file from mariluu site by andreweathan (aka mariluu) (me :))
// rewritten on 16/11/2022
// file actions:
// + global.SQL (object)
// + global.SQL.Client (sqlite3 Database)
// + global.SQL.IsValid (bool)
// + global.SQL.GetSiteData (function (string) -> promise -> err / row)
// + global.SQL.Execute (function (string, array) -> promise -> err / row)
await import ("dotenv/config");
import chalk from "chalk";
import sqlite3 from "sqlite3";
let SQL = {}
export let OpenDatabases = [];
SQL.OpenFile = filename => {
return new Promise((resolveTop, rejectTop) => {
let obj = {
IsValid: false,
FileName: filename
}
obj.Client = new sqlite3.Database(filename, async err => {
if (err) {
console.error(err, import.meta.url);
rejectTop(err)
return;
}
obj.IsValid = true
OpenDatabases.push(obj);
obj.Execute = async (query, params) => {
if (!obj.IsValid)
return console.log("Tried to execute SQL query on an invalid database.\nIgnore this if you're seeing this after forcefully closing the tool!")
let promise = new Promise((resolve, reject) => {
let fn = (err, row) => {
if (err) {
reject(err)
return;
}
resolve(row)
}
obj.Client.get(query, params ?? [], fn)
})
return promise;
}
obj.ExecuteAll = async (query, params) => {
if (!obj.IsValid)
return console.log("Tried to execute SQL query on an invalid database.\nIgnore this if you're seeing this after forcefully closing the tool!")
let promise = new Promise((resolve, reject) => {
let fn = (err, row) => {
if (err) {
reject(err)
return;
}
resolve(row)
}
obj.Client.all(query, params ?? [], fn)
})
return promise;
}
obj.Close = async _ => {
if (!obj.IsValid)
return console.log("Tried to close an invalid database.\nIgnore this if you're seeing this after forcefully closing the tool!")
return new Promise((resolve, reject) => {
obj.Client.close(err => {
if (err) reject(err);
OpenDatabases.splice(OpenDatabases.indexOf(obj), 1);
obj.IsValid = false;
resolve();
});
})
}
resolveTop(obj)
})
})
}
export default SQL;