You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

db.js 509B

1234567891011121314151617181920212223242526272829303132
  1. var fs = require("fs");
  2. module.exports = function(file) {
  3. var schema = ["list", "words"];
  4. var self = {
  5. list: [],
  6. words: []
  7. };
  8. function toobj() {
  9. var obj = {}
  10. schema.forEach(i => obj[i] = self[i]);
  11. return obj;
  12. }
  13. self.flush = function() {
  14. fs.writeFileSync(file, JSON.stringify(toobj()));
  15. }
  16. try {
  17. db = JSON.parse(fs.readFileSync(file));
  18. schema.forEach(i => self[i] = db[i] || self[i]);
  19. } catch (err) {
  20. if (err.code !== "ENOENT")
  21. throw err;
  22. self.flush();
  23. }
  24. return self;
  25. }