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); this.get_chat = function(obj){ return new Promise((resolve, reject)=>{ let res = this.db.get("chats").find({ id: obj.chat_id }).value(); if(res) return resolve(res); this.db.get("chats").push({ id:obj.chat_id, lang: 0, geo:{lon:0,lat:0}}).write(); res = this.db.get("chats").find({ id: obj.chat_id }).value(); 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){ return new Promise((resolve, reject)=>{ resolve(this.db.find({id:cid}).set('tracking',v).write()); }); }; this.get_v = function(cid){ return new Promise((resolve, reject)=>{ resolve(this.db.find({id:cid}).get('tracking').value()); }); } return this; };