-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.js
95 lines (94 loc) · 2.34 KB
/
db.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
const sqlite3 = require('sqlite3').verbose();
const { rejects } = require('assert');
const fs = require('fs');
const { resolve } = require('path');
const copyFile = async(sourcePath, destinationPath) => {
try {
await new Promise((resolve, reject) => {
fs.copyFile(sourcePath, destinationPath, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
console.log('File copied successfully.');
} catch (err) {
console.error('Error:', err);
}
}
const dbCommand = async (db, command, silent=false) => {
try {
await new Promise( async (resolve, reject) => {
await db.run(command, (err) => {
if(err) {
if(!silent)
console.log(err);
reject("DB command failed!");
}
else {
if(!silent)
console.log(command + "--> Success ✅");
resolve();
}
});
});
} catch (err) {
console.error('Error:', err);
}
}
const squery = async (db, query) => {
try {
return new Promise( async (resolve, reject)=>{
await db.get(query, (err, results) => {
if(err) {
reject(err);
}
else {
resolve(results);
}
});
});
} catch (err) {
console.error('Error:', err);
}
}
const query = async (db, query) => {
try {
return new Promise( async (resolve, reject)=>{
await db.all(query, (err, results) => {
if(err) {
reject(err);
}
else {
resolve(results);
}
});
});
} catch (err) {
console.error('Error:', err);
}
}
const initProgress = async () => {
const sqlite3 = require('sqlite3').verbose();
await copyFile("sqlite3/dictionary.db", "progress/database.db");
const db = new sqlite3.Database('progress/database.db');
// Progress DB initalization
await dbCommand(db, `
CREATE TABLE IF NOT EXISTS words (
id INTEGER PRIMARY KEY AUTOINCREMENT,
updated_at DATETIME NULL,
status INTEGER CHECK (status BETWEEN 0 AND 9),
word varchar(25) NULL,
wordtype varchar(20) NULL,
definition text NULL
)
`);
await dbCommand(db, `INSERT INTO words (word, wordtype, definition)
SELECT entries.word, entries.wordtype, entries.definition
FROM entries`);
}
module.exports = {
initProgress, dbCommand, query, squery
};