const db = require("../db.js"); var CryptoJS = require('crypto-js'); const BOOSTERS_SEARCH_CRITERIA = { 'lt': 'booster;', 'gt': 'booster:' }; class BoosterRepository { getByID(id) { return db.get('booster:' + id); } getAll() { return db.read(BOOSTERS_SEARCH_CRITERIA); } getBoostersByUser(userid) { return db.search(['user', userid],BOOSTERS_SEARCH_CRITERIA); } insert(id, data) { data = this.verify(data); return db.put('booster:' + id, data); } verify(data) { let { treasure, user } = data; if (!user) throw Error("This data are corrupted!"); if (!treasure) throw Error("This data are corrupted!"); return { treasure, user }; } } let boosterRepository = null; function getSingleton() { return boosterRepository = (boosterRepository === null ? new BoosterRepository(): boosterRepository); } module.exports = getSingleton();