const db = require("../db.js"); const CryptoJS = require('crypto-js'); const CARDS_SEARCH_CRITERIA = { 'lt': 'card;', //TODO card; h card:? 'gt': 'card:' }; class CardsRepository { getByID(id) { return db.get('card:' + id); } getAll() { return db.read(CARDS_SEARCH_CRITERIA); } getCardsByUser(userid) { return db.search(['user', userid],CARDS_SEARCH_CRITERIA); } insert(id, data) { data = this.verify(data); return db.put('card:' + id, data); } verify(data) { let { user } = data; if (!user) throw Error("This data are corrupted!"); return { user }; } } let cardsRepository = null; function getSingleton() { return cardsRepository = (cardsRepository === null ? new CardsRepository(): cardsRepository); } module.exports = getSingleton();