Module code:
const fs = require('fs');
class database {
constructor(database, object) {
typeof database === "object" && (object = database);
this.file = (typeof database === "string" ? database : 'db') + '.json';
object && fs.writeFileSync(this.file, JSON.stringify(object));
this.db = fs.existsSync(this.file) ? JSON.parse(fs.readFileSync(this.file, 'utf-8')) : {};
return new Proxy(this.db, this)
}
set(target, key, value) {
this.db[key] = value;
fs.writeFileSync(this.file, JSON.stringify(this.db));
}
}
module.exports = database;
Example Script:
var db = require('./index.js')
var test = new db();
test.a = []; // Proxy is called here
test.a.push('a'); // It's not called here, despite test.a being set.
I'm expecting the set handler to fire anytime the object is updated, but when pushing to an array inside the object, it doesn't happen. Why is it not firing?
I'm using node v8.1.4. Supposedly all proxy issues were fixed after v6.