HiddenPhox/src/lib/userData.js

65 lines
1.3 KiB
JavaScript
Raw Normal View History

2021-07-24 04:16:27 +00:00
hf.database.run(
2022-12-03 17:17:09 +00:00
"CREATE TABLE IF NOT EXISTS user_data (key TEXT PRIMARY KEY, value TEXT NOT NULL) WITHOUT ROWID"
2021-07-24 04:16:27 +00:00
);
function setUserData(id, key, value) {
return new Promise((resolve, reject) => {
hf.database.run(
2021-07-24 18:55:39 +00:00
"REPLACE INTO user_data VALUES ($key,$value)",
2021-07-24 04:16:27 +00:00
{
$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);
}
}
);
});
}
2021-07-24 04:19:54 +00:00
function deleteUserData(id, key) {
return new Promise((resolve, reject) => {
hf.database.run(
"DELETE FROM user_data WHERE key = $key",
{
$key: `${id}[${key}]`,
},
(err) => {
if (err == null) {
resolve(true);
} else {
reject(err);
}
}
);
});
}
2021-07-24 04:16:27 +00:00
module.exports = {
setUserData,
getUserData,
2021-07-24 04:19:54 +00:00
deleteUserData,
2021-07-24 04:16:27 +00:00
};