I work with NodeJS , for a while, I've been working with JSON files to store information from many users, but now I'm facing a problem, the corruption of the data (the file is being rewritten almost constantly), so I want to move all functions to SQLite to avoid data corruption.
My JSON file has the following fields:
{
"161104821108080640": {
"points": 0,
"level": 0,
"time": 1483466910038,
"color": "ff239d",
"money": 0,
"rep": 0,
"timerep": 1483466910038,
"timemoney": 1483466910038,
"id": "161104821108080640"
}
}
I want the SQLite file to have the same data, for this, a new entry must be created when a user's ID is not found. I'll also need to know how I could access that data later.
At the moment, this is the way I work with my JSON file:
let points = JSON.parse(fs.readFileSync('./points.json', 'utf8'));
let userData = points[msg.author.id];
if (!userData) {
userData = {
points: 0,
level: 0,
time: msg.createdTimestamp,
color: "ff239d",
money: 0,
rep: 0,
timerep: msg.createdTimestamp,
timemoney: msg.createdTimestamp
};
points[msg.author.id] = userData;
}
In which, your information is updated automatically with the following code:
if (msg.createdTimestamp - userData.time <= 60000) return;
var addin = Math.max(Math.ceil(Math.random() * 10), 5);
userData.points = userData.points + addin;
userData.time = msg.createdTimestamp;
What it does is, verify the timestamp
of the last time you received the points, if you have spent more than 1 minuto (60000 ms)
, then add a random number of points defined by the variable addin
.
How could I do the same, but using SQLite instead of JSON ?