123456789101112131415161718192021222324252627282930313233343536 |
- var level = require('level');
- var path = require('path');
- var DIR = path.dirname(__filename);
- // 1) Create our store
- const db = level(DIR + '/mydb')
- class Database {
- constructor(){
- }
- get(name) {
- return db.get(name).then((r) => JSON.parse(r)).catch((e) => console.log(e));
- }
- put(name, value){
- return db.put(name, JSON.stringify(value)).catch((e) => console.log(e));
- }
- del(name) {
- return db.del(name);
- }
- getAll() {
- return new Promise((resolve, reject) => {
- let array = {};
- db.createReadStream()
- .on('data', function (data) {
- array[data.key] = JSON.parse(data.value);
- })
- .on('end', function () {
- resolve(array);
- })
- });
- }
- }
- module.exports = new Database();
|