From 133e71961f342f1aa920aa0f528037b65370699b Mon Sep 17 00:00:00 2001 From: Cynthia Foxwell Date: Fri, 23 Jul 2021 22:16:27 -0600 Subject: [PATCH] userData lib --- src/lib/userData.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/lib/userData.js diff --git a/src/lib/userData.js b/src/lib/userData.js new file mode 100644 index 0000000..2b633af --- /dev/null +++ b/src/lib/userData.js @@ -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, +};