vybe/db.js

29 lines
616 B
JavaScript

const sqlite3 = require("sqlite3");
const fs = require("fs");
const db = new sqlite3.Database("vybe.db");
db.query = function (sql, params) {
var that = this;
return new Promise(function (resolve, reject) {
that.all(sql, params, function (error, rows) {
if (error) reject(error);
else resolve({ rows: rows });
});
});
};
(async () => {
if (!fs.existsSync("vybe.db")) {
for (let sql of fs
.readFileSync("db/1-init.sql")
.toString()
.split(";")
.filter((x) => x)) {
await db.query(sql);
}
}
})();
module.exports = db;