-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.ts
36 lines (31 loc) · 848 Bytes
/
db.ts
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
import sqlite3 from "sqlite3";
const db = new sqlite3.Database("./local.db");
db.serialize(() => {
db.run(`
CREATE TABLE IF NOT EXISTS todos (
id INTEGER PRIMARY KEY,
title TEXT,
completed BOOLEAN
);
`);
})
export const sql = (strings: TemplateStringsArray, ...values: any[]) => new Promise((resolve, reject) => {
const db = new sqlite3.Database("./local.db");
let str = '';
strings.forEach((string, i) => {
str += string + (values[i] || '');
});
if (str.startsWith('SELECT')) {
db.all(str, (err, rows) => {
if (err) reject(err)
resolve(rows)
})
return;
}
db.serialize(() => {
db.run(str, err => {
if (err) { reject(err); return; }
resolve(true)
})
})
})