Przeglądaj źródła

+ref saving on load to remove when it changes later

Ivan Asmer 6 lat temu
rodzic
commit
c13af3f7ca
2 zmienionych plików z 41 dodań i 22 usunięć
  1. 13 6
      index.js
  2. 28 16
      mm.js

+ 13 - 6
index.js

@@ -30,12 +30,18 @@ const delay       = ms => new Promise(r => setTimeout(r.bind(ms), ms))
     Savable.addClass(User)
     
 
-    let father = await Savable.m.User.findOne(ObjectID("5c9559dfbbf3d0049529f70b"))
-    console.log(father)
-    for (let child of father.children){
-        console.log(await child)
-        console.log(child.name, child.dirty)
-    }
+    //for (let child of father.children){
+        //console.log(await child)
+        //console.log(child.name, child.dirty)
+    //}
+
+    let father = await Savable.m.User.findOne(ObjectID("5c9571219be797377361c65a"))
+    console.log(father);
+    (await father.children[0]).parent = null;
+    await (await father.children[0]).save();
+    console.log(father);
+    
+
 
     //let person = new User({
         //name: 'Mykola',
@@ -67,6 +73,7 @@ const delay       = ms => new Promise(r => setTimeout(r.bind(ms), ms))
 
 
 
+
     //let notik = await Savable.m.Notebook.findOne(ObjectID('5c7c064d2ed0f4c9ab4cba4e'))
 
     //let SilniyeMans = await Savable.m.Savable.find({ $or: [{surname: 'Silniy'}, {surname: 'Silnaya'}]})

+ 28 - 16
mm.js

@@ -43,9 +43,15 @@ module.exports = db => {
                 }
             }
 
-            for (const key in obj) this[key] = obj[key]   
+            Object.assign(this, obj)
+
 
             convertSavables(this)
+
+            this._loadRelations = {};
+            for (const relation in this.__proto__.constructor.relations){
+                this._loadRelations[relation] = this[relation] instanceof Array ? [...this[relation]] : this[relation]
+            }
         }
 
         get _empty(){
@@ -118,10 +124,6 @@ module.exports = db => {
                             backRefValue.push(this)
                         }
                     }
-                    //else if (backRefValue instanceof Set){
-                        //console.log('backref -to-many set')
-                        //backRefValue.add(this)
-                    //}
                     else {
                         console.log('backref -to-one')
                         backRefObj[backRefKey] = this
@@ -130,21 +132,31 @@ module.exports = db => {
                 }
 
 
-
+                
                 for (const relation in this.__proto__.constructor.relations){
                     const backRef = this.__proto__.constructor.relations[relation]
 
+                    const loadRelation = this._loadRelations[relation]
+                    const loadRelationAsArray = loadRelation instanceof Savable ? [loadRelation] : loadRelation
+
                     let {value, obj, lastKey: key} = await getValueByField(relation, this)
-                    if (value){
-                        if (value instanceof Savable){
-                            console.log('one-to-*')
-                            await setBackRef(backRef, value)
-                        }
-                        if (value instanceof Array /*|| value instanceof Set*/){
-                            console.log('many-to-*')
-                            for (const foreignSavable of value){
-                                await setBackRef(backRef, foreignSavable)
+                    const valueAsArray = value instanceof Savable ? [value] : value
+                    if (loadRelationAsArray){
+                        const removedRefs = valueAsArray ? loadRelationAsArray.filter(ref => !valueAsArray.includes(ref)) : loadRelationAsArray
+                        for (const ref of removedRefs){
+                            await ref;
+                            if (ref[backRef] instanceof Array){
+                                ref[backRef] = ref[backRef].filter(br => br._id !== this._id)
+                            }
+                            else {
+                                ref[backRef] = null
                             }
+                            await ref.save(true)
+                        }
+                    }
+                    if (valueAsArray){
+                        for (const foreignSavable of valueAsArray){
+                            await setBackRef(backRef, foreignSavable)
                         }
                     }
                 }
@@ -172,7 +184,7 @@ module.exports = db => {
                 return result;
             }
 
-            const {_id, _empty, _ref, then, ...toSave} = await recursiveSlicer(this)
+            const {_id, _empty, _ref, _loadRelations, then, ...toSave} = await recursiveSlicer(this)
 
             //TODO: UPSERT
             if (!this._id){ //first time