Ivan Asmer преди 5 години
родител
ревизия
faf7f975c8
променени са 2 файла, в които са добавени 99 реда и са изтрити 100 реда
  1. 1 95
      index.js
  2. 98 5
      models.js

+ 1 - 95
index.js

@@ -7,7 +7,7 @@ const anonResolvers = ['login', 'createUser'];
 
 
 ;(async () => {
-    const {Savable, slice} = await require('./models.js')('cb')
+    const {Savable, slice, getModels} = await require('./models.js')('cb')
 
     class User extends Savable {
         static get relations(){
@@ -221,100 +221,6 @@ query login($login:String!, $password:String!){
  */
 
 
-    function getModels(id){
-        const SlicedSavable = slice([id, 'user'])
-
-        class User extends SlicedSavable {
-            constructor(...params){
-                super(...params)
-                
-                if (this.moneyEvents instanceof Array)
-                    return
-
-                if (this.moneyEvents instanceof SlicedSavable)
-                    this.moneyEvents = [this.moneyEvents];
-
-                this.moneyEvents = [];
-            }
-
-            static get relations(){
-                return {
-                    moneyEvents: "user"
-                }
-            }
-        }
-        SlicedSavable.addClass(User)
-
-        class Event extends SlicedSavable {
-            constructor(...params){
-                super(...params)
-
-                if (this.moneyEvents instanceof Array)
-                    return
-
-                if (this.moneyEvents instanceof SlicedSavable)
-                    this.moneyEvents = [this.moneyEvents];
-
-                this.moneyEvents = [];
-            }
-
-            get owner(){
-                return this.___owner
-            }
-
-            get usersSum(){
-                return (async  () => {
-                    var result = 0;
-                    for (let money of this.moneyEvents){
-                        result += (await money).amount
-                        console.log('MONEY SUM', money.amount, money._id)
-                    }
-                    return result;
-                })()
-            }
-
-
-            get moneyDiff(){
-                return (async  () => {
-                    return (await this.usersSum) - (this.total || 0);
-                })()
-            }
-
-            get avg(){
-                return this.total/((this.moneyEvents && this.moneyEvents.length) || 1)
-            }
-
-
-            static get relations(){
-                return {
-                    moneyEvents: "event"
-                }
-            }
-        }
-        SlicedSavable.addClass(Event)
-
-        class EventMoney extends SlicedSavable {
-            get owner(){
-                return this.___owner
-            }
-
-            get avgDiff(){
-                return (async () =>  this.amount - (await this.event).avg)()
-            }
-
-            static get relations(){
-                return {
-                    user: "moneyEvents",
-                    event: "moneyEvents"
-                }
-            }
-        }
-        SlicedSavable.addClass(EventMoney)
-
-        return {
-            SlicedSavable, User, Event, EventMoney
-        }
-    }
 
 
 

+ 98 - 5
models.js

@@ -10,13 +10,106 @@ module.exports = async (dbName, dsn="mongodb://localhost:27017/") => {
     const client      = await mongoClient.connect()
     const db          = client.db(dbName)
     const Savable     = mm(db).Savable
-    const sliceSavable = mm(db).sliceSavable 
+    const slice       = mm(db).sliceSavable 
 
-    return {
-        Savable, 
+    function getModels(id){
+        const SlicedSavable = slice([id, 'user'])
+
+        class User extends SlicedSavable {
+            constructor(...params){
+                super(...params)
+                
+                if (this.moneyEvents instanceof Array)
+                    return
+
+                if (this.moneyEvents instanceof SlicedSavable)
+                    this.moneyEvents = [this.moneyEvents];
 
-        slice(...params){
-            return sliceSavable(...params)
+                this.moneyEvents = [];
+            }
+
+            static get relations(){
+                return {
+                    moneyEvents: "user"
+                }
+            }
         }
+        SlicedSavable.addClass(User)
+
+        class Event extends SlicedSavable {
+            constructor(...params){
+                super(...params)
+
+                if (this.moneyEvents instanceof Array)
+                    return
+
+                if (this.moneyEvents instanceof SlicedSavable)
+                    this.moneyEvents = [this.moneyEvents];
+
+                this.moneyEvents = [];
+            }
+
+            get owner(){
+                return this.___owner
+            }
+
+            get usersSum(){
+                return (async  () => {
+                    var result = 0;
+                    for (let money of this.moneyEvents){
+                        result += (await money).amount
+                        console.log('MONEY SUM', money.amount, money._id)
+                    }
+                    return result;
+                })()
+            }
+
+
+            get moneyDiff(){
+                return (async  () => {
+                    return (await this.usersSum) - (this.total || 0);
+                })()
+            }
+
+            get avg(){
+                return this.total/((this.moneyEvents && this.moneyEvents.length) || 1)
+            }
+
+
+            static get relations(){
+                return {
+                    moneyEvents: "event"
+                }
+            }
+        }
+        SlicedSavable.addClass(Event)
+
+        class EventMoney extends SlicedSavable {
+            get owner(){
+                return this.___owner
+            }
+
+            get avgDiff(){
+                return (async () =>  this.amount - (await this.event).avg)()
+            }
+
+            static get relations(){
+                return {
+                    user: "moneyEvents",
+                    event: "moneyEvents"
+                }
+            }
+        }
+        SlicedSavable.addClass(EventMoney)
+
+        return {
+            SlicedSavable, User, Event, EventMoney
+        }
+    }
+
+    return {
+        Savable, 
+        slice,
+        getModels
     }
 }