Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ODM {
- constructor(document) {
- // get class name
- const name = this.constructor.name;
- // add unique id
- if (!document._id) document._id = Math.random().toString();
- // create document
- if (!db[name]) db[name] = {};
- db[name][document._id] = document;
- // define accessors
- const configuration = {};
- Object.keys(document).forEach((prop) => {
- configuration[prop] = {
- get() {
- const value = db[name][document._id][prop];
- // return an instance or a value
- return value.indexOf('@') !== -1 ? instances[value.replace('@', '')] : value;
- },
- set(value) {
- if (classes[value.constructor.name]) {
- // store the id of the instance
- db[name][document._id][prop] = value._id;
- } else {
- db[name][document._id][prop] = value;
- }
- }
- };
- });
- // set accessors
- Object.defineProperties(this, configuration);
- // add it to the list of instances
- instances[document._id] = this;
- }
- }
Add Comment
Please, Sign In to add comment