Guest User

Untitled

a guest
Feb 22nd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. // create instances list
  2. const instances = {};
  3.  
  4. class ODM {
  5.  
  6. constructor(document) {
  7. // get class name
  8. const name = this.constructor.name;
  9.  
  10. // add unique id
  11. if (!document._id) document._id = Math.random().toString();
  12.  
  13. // create document
  14. if (!db[name]) db[name] = {};
  15. db[name][document._id] = document;
  16.  
  17. // define accessors
  18. const configuration = {};
  19. Object.keys(document).forEach((prop) => {
  20. configuration[prop] = {
  21. get() {
  22. return db[name][document._id][prop];
  23. },
  24. set(value) {
  25. db[name][document._id][prop] = value;
  26. }
  27. };
  28. });
  29.  
  30. // set accessors
  31. Object.defineProperties(this, configuration);
  32.  
  33. // add it to the list of instances
  34. instances[document._id] = this;
  35. }
  36. }
Add Comment
Please, Sign In to add comment