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