node.js - Denormalized schema? -


mongoose schema:

const postschema = new mongoose.schema({     title: { type: string },     content: { type: string },      comments: {         count: { type: number },         data: [{             author: {                 type: mongoose.schema.types.objectid,                 ref: 'user'             },             content: { type: string },             created: { type: date },              replies: [{                 author: {                     type: mongoose.schema.types.objectid,                     ref: 'user'                 },                 content: { type: string },                 created: { type: date },             }]         }]     } }) 

i don't want normalize comments , replies because:

  • i need post comments (read in single go)
  • i never use comments query or sort anything
  • it's easier create, update , delete post comments (document locking)

if normalized it, have to:

  • get post database
  • get comments database
  • get replies each comment

this 1 of biggest strengths of mongodb: group data suit application data needs graphql , relay.js doesn't seem support that?

question:

is there way comments nested inside of single post object (or @ least replies nested inside of single comment) in order whole thing in single read?


graphql schema:

const posttype = new graphqlobjecttype({     name: 'post',     fields: () => ({         id: globalidfield('post'),         title: { type: graphqlstring },         content: { type: graphqlstring },          comments: {              // ?????          }     }),     interfaces: [nodeinterface], }) 

update:

const posttype = new graphqlobjecttype({     name: 'post',     fields: () => ({         id: globalidfield('post'),         title: { type: graphqlstring },         content: { type: graphqlstring },          commentcount: { type: graphqlint },         comments: { type: new graphqllist(commenttype) }     }),     interfaces: [nodeinterface] }) 

const commenttype = new graphqlobjecttype({     name: 'comment',     fields: () => ({         content: { type: graphqlstring },         created: { type: graphqlstring },         author: {             type: usertype,             resolve: async (comment) => {                 return await getuserbyid(comment.author)             }         },         replies: {             type: new graphqllist(commenttype),             resolve: (comment) => {                  // log below                 console.log(comment)                  // error occurs if return it!                 return comment.replies             }         }     }) }) 

log (comment):

{    author: 'user-1',   created: '05:35'   content: 'wow, awesome comment!',   replies:      [{         author: 'user-2',        created: '11:01',        content: 'not really..',      },      {          author: 'user-1',        created: '11:03',        content: 'why salty?',      }] } 

this 1 of biggest strengths of mongodb: group data suit application data needs graphql , relay.js doesn't seem support that?

graphql support you're trying do. before going details, need keep in mind graphql exposing data, not how store data. can store in whatever way - normalized or not. need tell graphql how data define in graphql schema.

is there way comments nested inside of single post object (or @ least replies nested inside of single comment) in order whole thing in single read?

yes, it's possible. define comments list. however, graphql not support arbitrary type of field in graphql object type. therefore, need define separate graphql types. in mongoose schema, comments object count of comments , comments data. data property list of type of objects. so, need define 2 graphql objects - comment , commentlist. code looks below:

const commenttype = new graphqlobjecttype({   name: 'comment',   fields: () => ({     author: { type: graphqlstring },     content: { type: graphqlstring },     created: { type: graphqlstring },     replies: { type: new graphqllist(commenttype) },   }), });  const commentlisttype = new graphqlobjecttype({   name: 'commentlist',   fields: () => ({     count: {       type: graphqlint,       resolve: (comments) => comments.length,     },     data: {       type: new graphqllist(commenttype),       resolve: (comments) => comments,     },   }), });  const posttype = new graphqlobjecttype({   name: 'post',   fields: () => ({     id: globalidfield('post'),     title: { type: graphqlstring },     content: { type: graphqlstring },      comments: {       type: commentlisttype,       resolve: (post) => post.comments,     }   }),   interfaces: [nodeinterface], }); 

i don't want normalize comments , replies because:

  • i need post comments (read in single go)
  • i never use comments query or sort anything
  • it's easier create, update , delete post comments (document locking)

defining post , comment graphql object types in above way let you:

  • read post comments together
  • not use comment query, not have id field.
  • implement comments manipulation in whatever way preferred database. graphql has nothing it.

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -