2020-07-01 11:00:51 +02:00
|
|
|
const low = require('lowdb');
|
|
|
|
const FileSync = require('lowdb/adapters/FileSync');
|
|
|
|
|
|
|
|
var db = null;
|
|
|
|
|
|
|
|
function db_init(db){
|
|
|
|
db.defaults({ chats:[]}).write();
|
|
|
|
}
|
|
|
|
|
|
|
|
var dbc = function(name){
|
|
|
|
const db_file = "./db/"+name+".json";
|
|
|
|
var db = low(new FileSync(db_file));
|
|
|
|
db_init(db);
|
|
|
|
return db;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = function(name){
|
|
|
|
this.name = name;
|
|
|
|
this.db = dbc(name);
|
|
|
|
|
2020-07-01 11:21:12 +02:00
|
|
|
this.get_chat = function(cid){
|
2020-07-01 11:00:51 +02:00
|
|
|
|
|
|
|
return new Promise((resolve, reject)=>{
|
2020-07-01 11:21:12 +02:00
|
|
|
let res = this.db.get("chats").find({id:cid}).value();
|
2020-07-01 11:00:51 +02:00
|
|
|
if(res) return resolve(res);
|
|
|
|
|
2020-07-01 11:21:12 +02:00
|
|
|
this.db.get("chats").push({id:cid, tracking:[]}).write();
|
|
|
|
res = this.db.get("chats").find({id:cid}).value();
|
2020-07-01 11:00:51 +02:00
|
|
|
if(res) return resolve(res);
|
|
|
|
|
|
|
|
return reject({error:true, msg:"Error With DB..."});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
this.dump_db = function(){
|
|
|
|
return new Promise((resolve, reject)=>{
|
|
|
|
resolve(this.db.get("chats").value());
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
this.set_v = function(cid,v){
|
2020-07-01 11:36:17 +02:00
|
|
|
this.get_chat(cid);
|
2020-07-01 11:11:56 +02:00
|
|
|
return new Promise((resolve, reject)=>{
|
2020-07-01 11:32:09 +02:00
|
|
|
resolve(this.db.get("chats").find({id:cid}).set('tracking',v).write());
|
2020-07-01 11:11:56 +02:00
|
|
|
});
|
2020-07-01 11:00:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
this.get_v = function(cid){
|
2020-07-01 11:36:17 +02:00
|
|
|
this.get_chat(cid);
|
2020-07-01 11:11:56 +02:00
|
|
|
return new Promise((resolve, reject)=>{
|
2020-07-01 11:37:29 +02:00
|
|
|
resolve(this.db.get("chats").find({id:cid}).get('tracking').value());
|
2020-07-01 11:11:56 +02:00
|
|
|
});
|
2020-07-01 11:00:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|