const level = require('level'); const sub = require('level-sublevel'); const search = require('level-search'); const database = { init: function(){ this.db = sub(level("./mydb", {valueEncoding: 'json'})); this.index = search(this.db, 'search'); }, open: function(callback){ return this.db.open(callback); }, close: function(callback){ return this.db.close(callback); }, put: function(key, value){ return new Promise( (res,rej) => { this.db.put(key, value, (err,values) => { if(err) return rej(err); return res(values); }); }); }, get: function(key){ return new Promise( (res,rej) => { this.db.get(key, (err,values) => { if(err) return rej(err); return res(values); }); }); }, del: function(key, options, callback){ return this.db.del(key, options, callback); }, batch: function(array, options, callback){ return this.db.batch(array, options, callback); }, isOpen: function(){ return this.db.isOpen(); }, isClosed: function(){ return this.db.isClosed(); }, createReadStream: function(options){ return this.db.createReadStream(options); }, createKeyStream: function(options){ return this.db.createKeyStream(options); }, createValueStream: function(options){ return this.db.createValueStream(options); }, createSearchStream: function(keys, options){ return this.index.createSearchStream(keys, options); }, search: this.createSearchStream }; module.exports = database;