Skip to content

Instantly share code, notes, and snippets.

@SnowyPainter
Last active August 16, 2022 10:21
Show Gist options
  • Save SnowyPainter/25c35bc0ec4a4bbcc125509736f9160c to your computer and use it in GitHub Desktop.
Save SnowyPainter/25c35bc0ec4a4bbcc125509736f9160c to your computer and use it in GitHub Desktop.
quick and simple db cover for sqlite js
let sqlite3 = require('sqlite3').verbose();
let db = new sqlite3.Database('a.sqlite');
const user_table = {
"id": "INTEGER primary key autoincrement",
"email": "TEXT unique",
"pw": "TEXT",
"name": "TEXT",
"teacher": "INTEGER"
}
const student_report_table = {
"id": "INTEGER primary key",
"grade": "INTEGER",
"gpa": "INTEGER",
"detailjson": "TEXT"
}
const pair_map = (k, s, v) => {
let elements = [];
if (k.length != v.length) return null;
for (i = 0; i < k.length; i++)
elements.push(k[i] + s + v[i]);
return elements
}
const filled = (arr) => new Array(arr.length).fill("?");
function create_table(db, name, columns, dontcreateifexists = false) {
let statement = "CREATE TABLE ";
if (dontcreateifexists == true) statement += "IF NOT EXISTS "
statement += (name + " (" + pair_map(Object.keys(columns), " ", Object.values(columns)).join(', ') + ");");
db.run(statement, (err) => {
if (err) return console.log(err.message);
console.log("table " + name + " was created.")
});
}
function insert(db, table, values) {
const keys = Object.keys(values);
db.run("INSERT INTO " + table + " (" + keys.join(', ') + ")" + " VALUES(" + filled(keys).join(', ') + ")", Object.values(values), (err) => {
if (err) return console.log(err.message);
console.log("table " + table + " insertion sucessful.")
})
}
function update(db, table, values, condition_express, condition_values) {
if(condition_express != "") condition_express = " WHERE "+condition_express;
db.run("UPDATE " + table + " SET " + pair_map(Object.keys(values), "=", filled(values)) + condition_express, Object.values(values).concat(condition_values), (err) => {
if (err) return console.log(err.message);
console.log("table " + table + " update sucessful.")
})
}
function select(db, table, columns, condition_express, condition_values) {
if(condition_express != "") condition_express = " WHERE "+condition_express;
db.all("SELECT "+columns.join(', ')+" FROM "+table+condition_express, condition_values, (err, rows) => {
if (err) return console.log(err.message);
console.log(rows)
})
}
function delete_row(db, table, condition_express, condition_values) {
db.run("DELETE FROM "+table+" WHERE "+condition_express, condition_values, (err) => {
if (err) return console.log(err.message);
console.log("table "+ table+" deleted "+condition_express+condition_values);
})
}
function initialize_database() {
db.serialize(() => {
create_table(db, "User", user_table, true);
create_table(db, "StudentReport", student_report_table, true);
/* insert(db, "User", {
"pw": "1234"
})
update(db, "User", {
"pw":"4321",
}, "pw=? AND id=1", [1234])
delete_row(db, "User", "id=?", ["1"]);
select(db, "User", ["pw"], "id=?", [2]);
*/
});
db.close();
}
module.exports = { initialize_database, insert, update, delete_row, select, db };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment