Przeglądaj źródła

delete with relation cleanup

Ivan Asmer 6 lat temu
rodzic
commit
fdc4eaf38c
2 zmienionych plików z 41 dodań i 11 usunięć
  1. 8 4
      index.js
  2. 33 7
      mm.js

+ 8 - 4
index.js

@@ -70,7 +70,7 @@ const delay       = ms => new Promise(r => setTimeout(r.bind(ms), ms))
     //}
 
 
-    async function walker(limit=10000) {
+    async function walker(limit=10) {
         let start = (new Date()).getTime()
         let stamp = start
         let now   = start
@@ -78,7 +78,11 @@ const delay       = ms => new Promise(r => setTimeout(r.bind(ms), ms))
         let prevI = 0
         let person = await Savable.m.User.findOne()
         for (var i=0;i<limit;i++){
+            let prevPerson = person
             person = await rndItem(person.friends) //walking in graph: go to random friend
+
+            console.log(prevPerson._id)
+            await prevPerson.delete()
             //if (persons.includes(person)){
                 //console.log('WAS HERE',person._id, person.name, person.surname, person.createdAt)
             //}
@@ -100,11 +104,11 @@ const delay       = ms => new Promise(r => setTimeout(r.bind(ms), ms))
         return now - start
     }
 
-    //await walker()
+    await walker()
 
 
-    console.log(await Promise.all([walker(), walker()]))
-    console.log(await Promise.all([walker(), walker()]))
+    //console.log(await Promise.all([walker(), walker()]))
+    //console.log(await Promise.all([walker(), walker()]))
 
 
 

+ 33 - 7
mm.js

@@ -57,8 +57,7 @@ module.exports = db => {
             convertSavables(this)
 
             this.saveRelations()
-
-            this._id = obj._id
+            //this._id = obj._id
         }
 
         get _empty(){
@@ -104,13 +103,11 @@ module.exports = db => {
             return db.collection(this._class)
         }
 
-        async save(noRefs=false){
+        async save(noRefs=false, noSync=false){
             if (this.empty) return;
 
             const syncRelations = async () => {
-                //TODO: remove refs if some ref detached since load from db
-                //if (noRefs) return
-
+                if (noSync) return
                 if (!(this && this.__proto__ && this.__proto__.constructor && this.__proto__.constructor.relations)) return 
 
 
@@ -211,6 +208,33 @@ module.exports = db => {
             this.saveRelations()
         }
 
+        async delete(){
+            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
+
+                if (loadRelationAsArray){
+                    for (const ref of loadRelationAsArray){
+                        //console.log(ref._id)
+                        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, true)
+                    }
+                }
+                //console.log(`relation delete loop ${relation}`)
+            }
+            let id = this._id
+
+            return await this.collection.deleteOne({_id: id})
+        }
+
 
 
 
@@ -245,7 +269,9 @@ module.exports = db => {
                     return  obj[_class] = {
                         * find(query, projection){
                             let cursor = db.collection(_class).find(query, projection)
-                            let cursorGen = asynchronize({s: cursor.stream(), chunkEventName: 'data', endEventName: 'close'})
+                            let cursorGen = asynchronize({s: cursor.stream(), 
+                                                          chunkEventName: 'data', 
+                                                          endEventName: 'close'})
                             for (const pObj of cursorGen()){
                                 yield new Promise((ok, fail) => 
                                     pObj.then(obj => ok(Savable.newSavable(obj, null, false)),