Ver código fonte

+owner getter

Ivan Asmer 4 anos atrás
pai
commit
b895bc2cbb
2 arquivos alterados com 28 adições e 7 exclusões
  1. 6 1
      index.js
  2. 22 6
      models.js

+ 6 - 1
index.js

@@ -41,7 +41,7 @@ const upload  = require('multer')({ dest: uploadPath })
             post: Post,
             comment: Comment,
             direct: Direct,
-            user: User,
+            owner: User
         }
 
         input LikeInput {
@@ -62,6 +62,7 @@ const upload  = require('multer')({ dest: uploadPath })
             collections: [Collection]
             likes: [Like]
             likesCount: Int
+            owner: User
         }
 
         input PostInput {
@@ -81,6 +82,7 @@ const upload  = require('multer')({ dest: uploadPath })
             userAvatar: User,
             posts: [Post],
             directs: [Direct]
+            owner: User
         }
 
         input ImageInput {
@@ -100,6 +102,7 @@ const upload  = require('multer')({ dest: uploadPath })
             answerTo: Comment
             likes: [Like]
             likesCount: Int
+            owner: User
         }
 
         input CommentInput {
@@ -119,6 +122,7 @@ const upload  = require('multer')({ dest: uploadPath })
             likes: [Like]
             likesCount: Int
             to: User
+            owner: User
         }
 
         input DirectInput {
@@ -134,6 +138,7 @@ const upload  = require('multer')({ dest: uploadPath })
             _id: ID,
             text: String,
             posts: [Post]
+            owner: User
         }
 
         input CollectionInput {

+ 22 - 6
models.js

@@ -26,7 +26,15 @@ module.exports = async (dbName='hipstagram') => {
         }
         SlicedSavable.addClass(User)
 
-        class Like extends SlicedSavable {
+        class OwnerSlicedSavable extends SlicedSavable {
+            get owner(){
+                if (!this.___owner) return this.___owner
+
+                return SlicedSavable.m.User.findOne({_id: ObjectID(this.___owner)})
+            }
+        }
+
+        class Like extends OwnerSlicedSavable {
             constructor(...params){
                 super(...params)
                 //TODO: 
@@ -34,6 +42,8 @@ module.exports = async (dbName='hipstagram') => {
                 //entityLikesCount?
             }
 
+
+
             static get relations(){
                 return {
                 }
@@ -41,7 +51,7 @@ module.exports = async (dbName='hipstagram') => {
         }
         SlicedSavable.addClass(Like)
 
-        class Post extends SlicedSavable {
+        class Post extends OwnerSlicedSavable {
             constructor(...params){
                 super(...params)
                 //TODO: calc likes count by getter (no two-way relation for this to avoid overflow on many Kilos of likes
@@ -50,6 +60,7 @@ module.exports = async (dbName='hipstagram') => {
 
             }
 
+
             static get relations(){
                 return {
                     images: ["posts"],
@@ -65,11 +76,12 @@ module.exports = async (dbName='hipstagram') => {
         }
         SlicedSavable.addClass(Post)
 
-        class Image extends SlicedSavable {
+        class Image extends OwnerSlicedSavable {
             constructor(...params){
                 super(...params)
             }
 
+
             static async fromFileData(fileData){
                 let image  = new Image({})
                 image.fileData = fileData
@@ -100,13 +112,14 @@ module.exports = async (dbName='hipstagram') => {
         }
         SlicedSavable.addClass(Image)
 
-        class Comment extends SlicedSavable {
+        class Comment extends OwnerSlicedSavable {
             constructor(...params){
                 super(...params)
                 //TODO: calc likes count by getter (no two-way relation for this to avoid overflow on many Kilos of likes
                 //cached like count, which incremented and decremented
             }
 
+
             static get relations(){
                 return {
                     post: ["comments"],
@@ -122,11 +135,12 @@ module.exports = async (dbName='hipstagram') => {
         SlicedSavable.addClass(Comment)
 
 
-        class Direct extends SlicedSavable {
+        class Direct extends OwnerSlicedSavable {
             constructor(...params){
                 super(...params)
             }
 
+
             static get relations(){
                 return {
                     to: ["incomings"],
@@ -141,11 +155,13 @@ module.exports = async (dbName='hipstagram') => {
         }
         SlicedSavable.addClass(Direct)
 
-        class Collection extends SlicedSavable {
+        class Collection extends OwnerSlicedSavable {
             constructor(...params){
                 super(...params)
             }
 
+                        
+
             static get relations(){
                 return {
                     posts: "collections"