userData lib
This commit is contained in:
parent
dfef61caa8
commit
133e71961f
1 changed files with 45 additions and 0 deletions
45
src/lib/userData.js
Normal file
45
src/lib/userData.js
Normal 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,
|
||||
};
|
Loading…
Reference in a new issue