Ivan Asmer 6 lat temu
rodzic
commit
43558d3584
2 zmienionych plików z 59 dodań i 39 usunięć
  1. 43 36
      index.js
  2. 16 3
      mm.js

+ 43 - 36
index.js

@@ -11,41 +11,41 @@ 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 Savable({
-            //name: 'Mykola',
-            //surname: 'Silniy',
-            //phones: ['105', '1'],
-            //children: [
-                //new Savable({
-                    //name: 'Marina',
-                    //surname: 'Silnaya',
-                    //phones: ['105', '1000503']
-                //}),
-                //new Savable({
-                    //name: 'Andrey',
-                    //surname: 'Silniy',
-                    //phones: ['103', '1000502']
-                //}),
-                //new Savable({
-                    //name: 'Fedor',
-                    //surname: 'Ivanova',
-                    //phones: ['102', '1000504'],
-                    //notebook: new Savable({
-                        //brand: 'dubovo'
-                    //})
-                //})
-            //]
-        //})
+        let person = new Savable({
+            name: 'Mykola',
+            surname: 'Silniy',
+            phones: ['105', '1'],
+            children: [
+                new Savable({
+                    name: 'Marina',
+                    surname: 'Silnaya',
+                    phones: ['105', '1000503']
+                }),
+                new Savable({
+                    name: 'Andrey',
+                    surname: 'Silniy',
+                    phones: ['103', '1000502']
+                }),
+                new Savable({
+                    name: 'Fedor',
+                    surname: 'Ivanova',
+                    phones: ['102', '1000504'],
+                    notebook: new Savable({
+                        brand: 'dubovo'
+                    })
+                })
+            ]
+        })
 
-        //await person.save()
-        //console.log(person)
+        await person.save()
+        console.log(person)
 
-        //await delay(3000)
+        await delay(3000)
     //}
 
-    let person = new Savable()
-    person._id = ObjectID('5c7bd603ce3cbc409978203e');
-    console.log(person)
+    //let person = new Savable()
+    //person._id = ObjectID('5c7bd603ce3cbc409978203e');
+    //console.log(person)
 
     let child = new Savable({
         name: 'New One Child',
@@ -53,12 +53,9 @@ const delay       = ms => new Promise(r => setTimeout(r.bind(ms), ms))
         phones: ['105', '1000506']
     });
 
-    console.log(await person)
-    console.log(await person.children[10])
-    console.log(await person.children[10].father)
-    console.log(await person.children[10].father.children[9])
+    //console.log(await person)
     //console.log(await person.children[1])
-    ;(await person).children.push(child)
+    person.children.push(child)
     child.father = person
 
     //console.log(person)
@@ -66,6 +63,16 @@ const delay       = ms => new Promise(r => setTimeout(r.bind(ms), ms))
 
     await person.save()
 
+
+    //console.log(await person.children[3])
+    let p2 =new Savable({_id: ObjectID('5c7bf8f04a3a3299f7deda0d' )}, true) //check for cache hit
+    ;(await new Savable({_id: ObjectID('5c7bf8f04a3a3299f7deda0d' )}, true)) //check for cache hit
+    ;(await p2)
+    console.log('parent 2', p2)
+    console.log(await     p2.children[3]) //check for other hit
+    console.log(await person.children[3].father)
+    console.log(await person.children[3].father.children[1])
+
     //let obj = {
         //then(cb){
             //process.nextTick(() => cb(obj))

+ 16 - 3
mm.js

@@ -1,7 +1,16 @@
 const ObjectID    = require("mongodb").ObjectID;
+
 module.exports = db => {
+    const identityMap = {}
     class Savable {
         constructor(obj, empty=false){
+            if (obj && obj._id){ 
+                if (obj._id.toString() in identityMap){
+                    return identityMap[obj._id]
+                }
+            }
+
+
             this._id    = null
             this._class = this.__proto__.constructor.name
             this._empty = true
@@ -29,8 +38,6 @@ module.exports = db => {
                 }
             }
 
-
-
             for (const key in obj) this[key] = obj[key]   
 
             convertSavables(this)
@@ -55,7 +62,10 @@ module.exports = db => {
                         if (!data){
                             err(new ReferenceError('Document Not Found'))
                         }
+                        console.log('load', this)
                         this.populate(data)
+                        console.log('caching in await', this._id)
+                        identityMap[this._id.toString()] = this
                         cb(this)
                     })
                     return this
@@ -79,7 +89,7 @@ module.exports = db => {
                     if (obj[key] && typeof obj[key] === 'object'){
                         if (obj[key] instanceof Savable){
                             if (!(obj[key]._id)){
-                                await obj[key].save().catch(err => console.log('ERR', ERR))
+                                await obj[key].save().catch(err => console.log('ERR', err))
                             }
                             result[key] = {_id: obj[key]._id, _class: obj[key]._class}
                         }
@@ -99,10 +109,13 @@ module.exports = db => {
             if (!this._id){ //first time
                 const { insertedId } = await this.collection.insertOne(toSave)
                 this._id = insertedId
+
             }
             else { //update
                 await this.collection.updateOne({_id: this._id},  {$set: toSave}).catch(err => console.log('UPDATE ERR', err))
             }
+            console.log('caching in save', this._id)
+            identityMap[this._id.toString()] = this
         }
 
         static isSavable(obj){