todo4/data.js

93 lines
2.7 KiB
JavaScript
Raw Normal View History

2024-07-26 20:02:43 -04:00
var exports = module.exports;
//var {Database} = require('sqlite3');
//var sqldb = new Database(_data+'/data.db');
var {Client} = require('cassandra-driver');
const cass = new Client({
cloud: {secureConnectBundle:'secure-connect-todo.zip'},
credentials: {
username:'',
password:''
},
});
async function startcass() {
await cass.connect();
cass.execute('use todo');
}
startcass();
(()=>{
var rankCache = new Map();
var rankReadTimeouts = new Map();
var rankWriteTimeouts = new Map();
var rankReadBounce = 1000*60*5;
var rankWriteBounce = 1000*15;
function rawgetrank(user,cb) {
cass.execute('Select json from rank where user=?',[user]).then(result=>{
if(!result || !result.rows || !result.rows.length) return cb(null,{});
cb(null,JSON.parse(result.rows[0].json));
}).catch(cb);
}
function updaterankreadtimeout(user) {
if(rankReadTimeouts.has(user)) clearTimeout(rankReadTimeouts.get(user));
rankReadTimeouts.set(user,setTimeout(()=>rankCache.delete(user),rankReadBounce));
}
function getrank(user,cb) {
updaterankreadtimeout(user);
if(rankCache.has(user)) return cb(null,rankCache.get(user));
rawgetrank(user,(err,obj)=>{
if(err) return cb(err);
rankCache.set(user,obj);
return cb(null,obj);
});
}
function rawwriterank(user,obj) {
cass.execute('Insert into rank (user,json) values (?,?)',[user,JSON.stringify(obj)]);
}
function writerank(user,obj) {
if(rankWriteTimeouts.has(user)) clearTimeout(rankWriteTimeouts.get(user));
rankWriteTimeouts.set(user,setTimeout(()=>rawwriterank(user,obj),rankWriteBounce));
}
exports.getrank=getrank;
exports.writerank=writerank;
})();
(()=>{
function rawgettitle(user,id,cb) {
cass.execute('Select title from titles where user=? and id=?',[user,id]).then(result=>{
if(!result || !result.rows || !result.rows.length) return cb('title not found');
cb(null,result.rows[0]);
}).catch(cb);
}
function rawwritetitle(user,id,title) {
cass.execute('Insert into titles (user,id,title) values (?,?,?)',[user.id,title]);
}
exports.gettitle=rawgettitle;
exports.writetitle=rawwritetitle;
})();
(()=>{
function rawgetday(user,day,cb) {
cass.execute('Select json from day where user=? and day=?',[user,day]).then(result=>{
if(!result || !result.rows || !result.rows.length) return cb(null,[]);
cb(null,JSON.parse(result.rows[0].json));
}).catch(cb);
}
function rawwriteday(user,day,list) {
cass.execute('Insert into day (user,day,json) values (?,?,?)',[user,day,JSON.stringify(list)]);
}
exports.getday=rawgetday;
exports.writeday=rawwriteday;
})();
(()=>{
function rawaddlog(user,title,id) {
cass.execute('Insert into addjournal (user,time,title,id) values (?,?,?,?)',[user,Date.now(),title,id]);
}
exports.addlogwrite=rawaddlog;
})();