db.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. const level = require('level');
  2. const sub = require('level-sublevel');
  3. const search = require('level-search');
  4. const database = {
  5. init: function(){
  6. this.db = sub(level("./mydb", {valueEncoding: 'json'}));
  7. this.index = search(this.db, 'search');
  8. },
  9. open: function(callback){
  10. return this.db.open(callback);
  11. },
  12. close: function(callback){
  13. return this.db.close(callback);
  14. },
  15. put: function(key, value){
  16. return new Promise( (res,rej) => {
  17. this.db.put(key, value, (err,values) => {
  18. if(err)
  19. return rej(err);
  20. return res(values);
  21. });
  22. });
  23. },
  24. get: function(key){
  25. return new Promise( (res,rej) => {
  26. this.db.get(key, (err,values) => {
  27. if(err)
  28. return rej(err);
  29. return res(values);
  30. });
  31. });
  32. },
  33. del: function(key, options, callback){
  34. return this.db.del(key, options, callback);
  35. },
  36. batch: function(array, options, callback){
  37. return this.db.batch(array, options, callback);
  38. },
  39. isOpen: function(){
  40. return this.db.isOpen();
  41. },
  42. isClosed: function(){
  43. return this.db.isClosed();
  44. },
  45. createReadStream: function(options){
  46. return this.db.createReadStream(options);
  47. },
  48. createKeyStream: function(options){
  49. return this.db.createKeyStream(options);
  50. },
  51. createValueStream: function(options){
  52. return this.db.createValueStream(options);
  53. },
  54. createSearchStream: function(keys, options){
  55. return this.index.createSearchStream(keys, options);
  56. },
  57. search: this.createSearchStream
  58. };
  59. module.exports = database;