12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- const level = require('level');
- const sub = require('level-sublevel');
- const search = require('level-search');
- class Database {
- constructor(){
- console.log("Constructing database!")
- this.db = sub(level("./database/mafaldadb", {valueEncoding: 'json'}));
- this.index = search(this.db, 'search');
- }
- open(callback){
- return this.db.open(callback);
- }
- close(callback){
- return this.db.close(callback);
- }
- put(key, value){
- return new Promise( (res,rej) => {
- this.db.put(key, value, (err,values) => {
- if(err)
- return rej(err);
- return res(values);
- });
- });
- }
- get(key){
- return new Promise( (res,rej) => {
- this.db.get(key, (err,values) => {
- if(err)
- return rej(err);
- return res(values);
- });
- });
- }
- del(key, options, callback){
- return this.db.del(key, options, callback);
- }
- batch(array, options, callback){
- return this.db.batch(array, options, callback);
- }
- isOpen(){
- return this.db.isOpen();
- }
- isClosed(){
- return this.db.isClosed();
- }
- createReadStream(options){
- return this.db.createReadStream(options);
- }
- createKeyStream(options){
- return this.db.createKeyStream(options);
- }
- createValueStream(options){
- return this.db.createValueStream(options);
- }
- createSearchStream(keys, options){
- return this.index.createSearchStream(keys, options);
- }
- search(keys, options) {
- return this.createSearchStream(keys, options);
- }
- };
- let database = null;
- function getSingleton() {
- return database = (database === null ? new Database() : database);
- }
- module.exports = getSingleton();
|