db.js 717 B

123456789101112131415161718192021222324252627282930313233343536
  1. var level = require('level');
  2. var path = require('path');
  3. var DIR = path.dirname(__filename);
  4. // 1) Create our store
  5. const db = level(DIR + '/mydb')
  6. class Database {
  7. constructor(){
  8. }
  9. get(name) {
  10. return db.get(name).then((r) => JSON.parse(r)).catch((e) => console.log(e));
  11. }
  12. put(name, value){
  13. return db.put(name, JSON.stringify(value)).catch((e) => console.log(e));
  14. }
  15. del(name) {
  16. return db.del(name);
  17. }
  18. getAll() {
  19. return new Promise((resolve, reject) => {
  20. let array = {};
  21. db.createReadStream()
  22. .on('data', function (data) {
  23. array[data.key] = JSON.parse(data.value);
  24. })
  25. .on('end', function () {
  26. resolve(array);
  27. })
  28. });
  29. }
  30. }
  31. module.exports = new Database();