Przeglądaj źródła

delete with relation cleanup fixes

Ivan Asmer 6 lat temu
rodzic
commit
e31007d9cd
2 zmienionych plików z 31 dodań i 31 usunięć
  1. 9 3
      index.js
  2. 22 28
      mm.js

+ 9 - 3
index.js

@@ -77,9 +77,15 @@ const delay       = ms => new Promise(r => setTimeout(r.bind(ms), ms))
 
         let prevI = 0
         let person = await Savable.m.User.findOne()
+        let prevPerson;
         for (var i=0;i<limit;i++){
-            let prevPerson = person
-            person = await rndItem(person.friends) //walking in graph: go to random friend
+            prevPerson = person || prevPerson
+            try {
+                person = person.friends ? await rndItem(person.friends) : await Savable.m.User.findOne() //walking in graph: go to random friend
+            }
+            catch(e){
+                person = await Savable.m.User.findOne()
+            }
 
             console.log(prevPerson._id)
             await prevPerson.delete()
@@ -104,7 +110,7 @@ const delay       = ms => new Promise(r => setTimeout(r.bind(ms), ms))
         return now - start
     }
 
-    await walker()
+    await walker(8531)
 
 
     //console.log(await Promise.all([walker(), walker()]))

+ 22 - 28
mm.js

@@ -7,12 +7,6 @@ module.exports = db => {
 
     class Savable {
         constructor(obj, ref, empty=false){
-            //TODO check type for return right class 
-            //if (obj && obj._id){
-                //console.log('savable...')
-            //}
-
-
             this._id    = null
             this._ref   = ref
             this._class = this.__proto__.constructor.name
@@ -68,23 +62,16 @@ module.exports = db => {
             if (value){
                 this.then = (cb, err) => {
                     let stamp = (new Date()).getTime()
-                    //if (!this._empty){ //it can't (?) works
-                        //cb(this)
-                        //return this
-                    //}
                     delete this.then
+
                     if (!this._id)    err(new ReferenceError('Id is empty'))
                     if (!this._class) err(new ReferenceError('Class is empty'))
 
-                    //console.log(this._id)
-
                     this.collection.findOne(this._id).then( data => {
                         if (!data){
                             err(new ReferenceError('Document Not Found'))
                         }
-                        //console.log('FIND', (new Date()).getTime() - stamp)
                         this.populate(data)
-                        //console.log('FIND AND POPULATE', (new Date()).getTime() - stamp)
                         cb(this)
                     })
                     return this
@@ -113,7 +100,7 @@ module.exports = db => {
 
                 async function getValueByField(field, savable) {
                     let path = field.split('.');
-                    await savable;
+                    await savable.catch(e => console.log('GET VALUE BY FIELD ERROR'));
                     let result = savable;
                     let prev;
                     let lastKey = path.pop()
@@ -122,19 +109,16 @@ module.exports = db => {
                 }
 
                 let setBackRef = async (backRef, foreignSavable) => {
-                    //console.log('BACKREF for', backRef, foreignSavable.name)
                     const {value: backRefValue, 
                             obj: backRefObj, 
                         lastKey: backRefKey} = await getValueByField(backRef, foreignSavable)
 
                     if (backRefValue instanceof Array){
-                        //console.log('backref -to-many array')
                         if (!backRefValue.includes(this)){
                             backRefValue.push(this)
                         }
                     }
                     else {
-                        //console.log('backref -to-one')
                         backRefObj[backRefKey] = this
                     }
                     noRefs || await foreignSavable.save(true)
@@ -153,7 +137,10 @@ module.exports = db => {
                     if (loadRelationAsArray){
                         const removedRefs = valueAsArray ? loadRelationAsArray.filter(ref => !valueAsArray.includes(ref)) : loadRelationAsArray
                         for (const ref of removedRefs){
-                            await ref;
+                            try {
+                                await ref
+                            }
+                            catch (e) {console.log('SYNC RELATIONS ERROR') }
                             if (ref[backRef] instanceof Array){
                                 ref[backRef] = ref[backRef].filter(br => br._id !== this._id)
                             }
@@ -208,17 +195,19 @@ module.exports = db => {
             this.saveRelations()
         }
 
-        async delete(){
-            for (const relation in this.__proto__.constructor.relations){
+        async delete(noRefs=false){
+            if (!noRefs) for (const relation in this.__proto__.constructor.relations){
                 const backRef = this.__proto__.constructor.relations[relation]
 
-                const loadRelation = this._loadRelations[relation]
+                const loadRelation = this._loadRelations && this._loadRelations[relation]
                 const loadRelationAsArray = loadRelation instanceof Savable ? [loadRelation] : loadRelation
 
                 if (loadRelationAsArray){
                     for (const ref of loadRelationAsArray){
-                        //console.log(ref._id)
-                        await ref;
+                        try {
+                            await ref
+                        }
+                        catch (e) {console.log('DELETE SYNC RELATIONS ERROR') }
                         if (ref[backRef] instanceof Array){
                             ref[backRef] = ref[backRef].filter(br => br._id !== this._id)
                         }
@@ -228,11 +217,17 @@ module.exports = db => {
                         await ref.save(true, true)
                     }
                 }
-                //console.log(`relation delete loop ${relation}`)
             }
-            let id = this._id
+            const id  = this._id
+            const col = this._class && this.collection
+
+            for (let key in this)
+                delete this[key]
+
+            delete this.__proto__
 
-            return await this.collection.deleteOne({_id: id})
+            if (col)
+                return await col.deleteOne({_id: id})
         }
 
 
@@ -241,7 +236,6 @@ module.exports = db => {
 
 
         static isSavable(obj){
-            //console.log(obj._id, obj._class)
             return obj && obj._id && obj._class
         }