Browse Source

identity map and then fixes

Ivan Asmer 6 years ago
parent
commit
7617d53ce3
2 changed files with 56 additions and 32 deletions
  1. 36 29
      index.js
  2. 20 3
      mm.js

+ 36 - 29
index.js

@@ -29,6 +29,42 @@ const delay       = ms => new Promise(r => setTimeout(r.bind(ms), ms))
     Savable.addClass(Notebook)
     Savable.addClass(User)
 
+    let father = await Savable.m.User.findOne(ObjectID("5c7e830b411baf1efe9f251c"))
+    console.log(father)
+    for (let child of father.children){
+        console.log(await child)
+    }
+
+    //let person = new User({
+        //name: 'Mykola',
+        //surname: 'Silniy',
+        //phones: ['105', '1'],
+        //children: [
+            //new User({
+                //name: 'Marina',
+                //surname: 'Silnaya',
+                //phones: ['105', '1000503'],
+            //}),
+            //new User({
+                //name: 'Andrey',
+                //surname: 'Silniy',
+                //phones: ['103', '1000502'],
+            //}),
+            //new User({
+                //name: 'Fedor',
+                //surname: 'Ivanova',
+                //phones: ['102', '1000504'],
+                //notebook: new Notebook({
+                    //brand: 'dubovo'
+                //})
+            //})
+        //]
+    //})
+
+    //await person.save()
+
+
+
     //let notik = await Savable.m.Notebook.findOne(ObjectID('5c7c064d2ed0f4c9ab4cba4e'))
 
     //let SilniyeMans = await Savable.m.Savable.find({ $or: [{surname: 'Silniy'}, {surname: 'Silnaya'}]})
@@ -54,35 +90,6 @@ const delay       = ms => new Promise(r => setTimeout(r.bind(ms), ms))
 
     //while(true){
         //await (new Savable({timestamp: (new Date).getTime(), r: Math.random()})).save()
-        let person = new User({
-            name: 'Mykola',
-            surname: 'Silniy',
-            phones: ['105', '1'],
-            children: [
-                new User({
-                    name: 'Marina',
-                    surname: 'Silnaya',
-                    phones: ['105', '1000503'],
-                    parent: []
-                }),
-                new User({
-                    name: 'Andrey',
-                    surname: 'Silniy',
-                    phones: ['103', '1000502'],
-                    parent: new Set
-                }),
-                new User({
-                    name: 'Fedor',
-                    surname: 'Ivanova',
-                    phones: ['102', '1000504'],
-                    notebook: new Notebook({
-                        brand: 'dubovo'
-                    })
-                })
-            ]
-        })
-
-        await person.save()
         //console.log(person)
 
         //await delay(1000)

+ 20 - 3
mm.js

@@ -9,7 +9,16 @@ module.exports = db => {
     class Savable {
         constructor(obj, empty=false){
             //TODO check type for return right class 
-            if ((obj && obj._id) && (obj._id.toString() in identityMap)) return identityMap[obj._id]
+            if (obj && obj._id){
+                console.log('savable...')
+                if (!empty){
+                    identityMap[obj._id.toString()] = this
+                }
+                //console.log(identityMap)
+                if (obj._id.toString() in identityMap){
+                    console.log(`in identity map ${obj._id}`)
+                }
+            }
 
 
             this._id    = null
@@ -22,11 +31,12 @@ module.exports = db => {
                 this.populate(obj)
                 this._empty = empty
             }
+            if ((obj && obj._id) && (obj._id.toString() in identityMap)) return identityMap[obj._id]
         }
 
 
 
-        populate(obj, empty){
+        populate(obj){
             function convertSavables(obj){
                 for (const key in obj){
                     if (Savable.isSavable(obj[key])){
@@ -57,8 +67,9 @@ module.exports = db => {
                     delete this.then
                     if (!this._id)    err(new ReferenceError('Id is empty'))
                     if (!this._class) err(new ReferenceError('Class is empty'))
+                    //TODO identityMap check for already loaded object into memory
 
-                    this.collection.findOne(_id).then( data => {
+                    this.collection.findOne(this._id).then( data => {
                         if (!data){
                             err(new ReferenceError('Document Not Found'))
                         }
@@ -86,6 +97,7 @@ module.exports = db => {
             if (this.empty) return;
 
             const syncRelations = async () => {
+                //TODO: remove refs if some ref detached since load from db
                 if (noRefs) return
 
                 if (!(this && this.__proto__ && this.__proto__.constructor && this.__proto__.constructor.relations)) return 
@@ -239,6 +251,11 @@ module.exports = db => {
             //field and foreign field can be Savable, Array or Set
             //both fields can be specified as "field", "field.subfield" 
             //or field: {subfield: foreignField} //TODO later if needed
+            //TODO: move it into object instead of class to give more flexibility, for example
+            //if person has children, it can have backRef father or mother depending on sex:
+            //return {
+            //    children: this.sex === 'male' ? 'father': 'mother'
+            //}
             return {}
         }
     }