|
@@ -4,12 +4,14 @@ const search = require('level-search');
|
|
|
|
|
|
var path = require('path');
|
|
var path = require('path');
|
|
var DIR = path.dirname(__filename);
|
|
var DIR = path.dirname(__filename);
|
|
-console.log(DIR);
|
|
|
|
class Database {
|
|
class Database {
|
|
constructor(){
|
|
constructor(){
|
|
console.log("Constructing database!")
|
|
console.log("Constructing database!")
|
|
this.db = sub(level(DIR + "/mafaldadb", {valueEncoding: 'json'}));
|
|
this.db = sub(level(DIR + "/mafaldadb", {valueEncoding: 'json'}));
|
|
this.index = search(this.db, 'search');
|
|
this.index = search(this.db, 'search');
|
|
|
|
+
|
|
|
|
+ // this.createSearchStream = this.createSearchStream.bind(this);
|
|
|
|
+
|
|
}
|
|
}
|
|
open(callback){
|
|
open(callback){
|
|
return this.db.open(callback);
|
|
return this.db.open(callback);
|
|
@@ -47,8 +49,22 @@ class Database {
|
|
isClosed(){
|
|
isClosed(){
|
|
return this.db.isClosed();
|
|
return this.db.isClosed();
|
|
}
|
|
}
|
|
- createReadStream(options){
|
|
|
|
- return this.db.createReadStream(options);
|
|
|
|
|
|
+ createReadStream(options) {
|
|
|
|
+ const results = [];
|
|
|
|
+ return new Promise((res,rej) => {
|
|
|
|
+ this.db.createReadStream(options)
|
|
|
|
+ .on('data', function (data) {
|
|
|
|
+ results.push(data.value);
|
|
|
|
+ })
|
|
|
|
+ .on('error', function (err) {
|
|
|
|
+ rej(err);
|
|
|
|
+ })
|
|
|
|
+ .on('close', function () {
|
|
|
|
+ })
|
|
|
|
+ .on('end', function () {
|
|
|
|
+ res(results);
|
|
|
|
+ });
|
|
|
|
+ });
|
|
}
|
|
}
|
|
createKeyStream(options){
|
|
createKeyStream(options){
|
|
return this.db.createKeyStream(options);
|
|
return this.db.createKeyStream(options);
|
|
@@ -56,8 +72,25 @@ class Database {
|
|
createValueStream(options){
|
|
createValueStream(options){
|
|
return this.db.createValueStream(options);
|
|
return this.db.createValueStream(options);
|
|
}
|
|
}
|
|
- createSearchStream(keys, options){
|
|
|
|
- return this.index.createSearchStream(keys, options);
|
|
|
|
|
|
+ createSearchStream(keys, options) {
|
|
|
|
+ const results = [];
|
|
|
|
+ return new Promise((res,rej) => {
|
|
|
|
+ this.index.createSearchStream(keys,options)
|
|
|
|
+ .on('data', function (data) {
|
|
|
|
+ results.push(data.value);
|
|
|
|
+ })
|
|
|
|
+ .on('error', function (err) {
|
|
|
|
+ rej(err);
|
|
|
|
+ })
|
|
|
|
+ .on('close', function () {
|
|
|
|
+ })
|
|
|
|
+ .on('end', function () {
|
|
|
|
+ res(results);
|
|
|
|
+ });
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+ read(options) {
|
|
|
|
+ return this.createReadStream(options);
|
|
}
|
|
}
|
|
search(keys, options) {
|
|
search(keys, options) {
|
|
return this.createSearchStream(keys, options);
|
|
return this.createSearchStream(keys, options);
|