userData lib

This commit is contained in:
Cynthia Foxwell 2021-07-23 22:16:27 -06:00
parent dfef61caa8
commit 133e71961f

45
src/lib/userData.js Normal file
View file

@ -0,0 +1,45 @@
hf.database.run(
"CREATE TABLE IF NOT EXISTS user_data (key STRING PRIMARY KEY, value STRING NOT NULL) WITHOUT ROWID"
);
function setUserData(id, key, value) {
return new Promise((resolve, reject) => {
hf.database.run(
"INSERT INTO user_data VALUES ($key,$value)",
{
$value: value,
$key: `${id}[${key}]`,
},
(err) => {
if (err == null) {
resolve(true);
} else {
reject(err);
}
}
);
});
}
function getUserData(id, key, fallback = null) {
return new Promise((resolve, reject) => {
hf.database.get(
"SELECT value FROM user_data WHERE key = $key",
{
$key: `${id}[${key}]`,
},
(err, row) => {
if (err == null) {
resolve(row?.value || fallback);
} else {
reject(err);
}
}
);
});
}
module.exports = {
setUserData,
getUserData,
};