Press "Enter" to skip to content

MERN Stack & GraphQL – #13 Models & Relationships


hey guys welcome back the cold room in
this video we’re gonna start expanding
our schema and also implementing chats
and messages in the application so first
things first let’s take a quick look at
a sketch that I’ve drawn out for the
scheme of the application first of all
we’re gonna have several entities in the
app we’re gonna have the user entity the
chat entity as well as the message so
the user is gonna be sort of like the
route model the user can have multiple
chats or belong to multiple chats
they’re gonna be able to create those
chats and also join them if they already
exist and the chats might also have
multiple users that belong to them so
this relationship is going to be many to
many so the user is gonna belong to a
chat or multiple chats and every chat
will have multiple messages so the
messages object or entity is gonna be
directly related to the chat every chat
will have multiple message and a message
it can only belong to a single chat so
it’s gonna be one a to many relationship
now taking a closer look at the user
model we already have most of the fields
so we have things like ID which is going
to be the object ID we have the name the
email username and password and we also
have the timestamps of course the only
field we’re going to add is going to be
the chats now this one is gonna be an
array of object IDs so this array of
object IDs is going to indicate which
chats the user belongs to or
participates in as far as the chat
itself we’re gonna have a model that
obviously has an ID property we’ll have
the title of the chat we’ll have also a
list of users that that chat has as
participants and this one will also be
an array of object IDs and they will
link back to the user model and we’ll
also have the last message property in
this case it’s going to be an object ID
and this will link back to a message so
this way we won’t have to query all the
messages and order them by create a task
field but can simply get the last
message off of the chat object so it’s
gonna be faster that way
and we’ll also of course have the
created add and updated at fields now as
far as the message goes this is going to
be an entity with an ID also of course
the body of the message will also have a
centered property so this will be the
user who sent the message once again
it’s now going to be an actual user
object of course because it’s going to
be shared between multiple entities for
example that same object ID equals to be
part of the users property on the chat
that’s why this will be an ID and object
ID not the actual object
of the user but this will in the end
link back to the user model and in the
end of course we’re gonna have the
created at and updated at time steps so
let’s begin by actually fleshing out the
e-type definitions for all of these
models so we’re gonna create one for a
chat so let’s do chat is so in this file
we’re gonna be importing gql from a Polo
Server Express will export default gql
so we’re gonna declare a type of chat
now this one will have an ID it’ll also
have a title as a required property it
will also have a list of users now in
the database this will be a list of
object IDs but in this case in the
schema we’re gonna expect actual user
objects so we’re gonna have to populate
those we’re gonna also have a list of
messages so let’s have a message type of
course we’re gonna have to create it
we’re gonna do that in a second so let’s
see what else the title the users the
messages we’re gonna also have the last
message this one is going to be a
message but in this case I’m not going
to put the exclamation because if fresh
chat won’t have any messages in it but
we’ll have a created at and also update
it at fields so now both of these are
gonna be strings so next up we’re gonna
create a new file let’s call it message
j/s so this is we’re gonna define a
message type so let’s have a type of
message once again we’ll have an ad
property we’ll have body this will be a
string we’re gonna have a sender which
will be the user who sent a message now
back in the schema besides the sender
we’re gonna also have each add property
which I forgot to mention for some
reason but this will be an object ID and
this will link back to the chat now
looking back at the chat if we were to
have a messages property on the schema
so let’s say we go back in here just
like we have the users let’s say we were
also to have a messages property and
this would be lets say an array of
object IDs and this would link back to
messages so if we were to go with this
design this would not be scalable in the
future because first of all we have to
be mindful of the fact that every
document in MongoDB has a limit of 16
megabytes so if this property was to
roll beyond the available size for a
document we are gonna run into issues
because now we won’t be able to push in
any more messages to the chat so we will
have to either delete them or probably
ask the user to create a new chat or
something like that so this is really
not scalable and it’s also complicates
things because every time a new message
is created we’re gonna have to push the
new message to that array and if the
message is deleted we’re gonna have to
pull the ID from the array so this
actually complicates the code quite a
bit if you think about it so instead of
having a messages property on the chat
what we can do is we can instead create
a chat property on the message so we
have a chat property which will be an
object ID and this will link back to the
chat entity in this case we’re gonna be
linking back to the chat so every
message will have a chat property so if
you want to fetch all the messages for a
chat you simply have to do a query where
the chat property equals the chat dot
undersquare ID or whatever the case
might be so back in our message JS file
what we could do in here is we can
create a chat property and link it back
to the chat this would be really easy
there’s no extra work to do here this
would be a really simple query to do but
in reality I don’t think we ever need to
fetch the chat for a message I think
it’s much more common to be fetching
messages for a given chat and that’s why
we’re gonna keep that messages property
instead from there let’s go back to
index so we’re gonna pull in a few more
files so let’s actually import chat from
chat let’s do another one for the
message I’m just gonna keep them in the
alphabetic order so let’s pull in the
route chat the message now we can go
back to the user so for the user we’re
going to add chats so we’ll have an
array of chats like this and what I
think we can also do is we can add the
updated add property which we can get
for free because it’s already on the
model so let’s save that and now back to
our models directory let’s go ahead and
create a few more models so I’ll create
one for chat and let’s also add one for
a message on the same note let’s
actually go back to the user model so in
the user model let’s add the chats
property so this one is going to be an
array of object
and every object is going to be of type
object ID so this object ID we can
actually grab from a schema so actually
let me import schema along with Mongoose
and now what we’re gonna do is we’re
gonna reference schema types dot object
ID so this will give us the reference to
the object ID and the reference is going
to be to the shop model which we’re
about to create and on the same note I
left if you note about security and
specifically talking about sanitization
what I think I’m going to do is I’m
actually going to just take out the
value for now let’s simplify it a bit
we’re just gonna say email has already
been taken the same thing for the
username of course we can actually come
back to it in the future but I think for
the time being
let’s just not complicate it any further
I’m gonna do the same thing for the
sanitization in here so I’m just gonna
say user ID is not a valid object ID so
we don’t have to worry about
sanitization anymore that should
basically be it so once we have the
chat’s we can actually go back to the
chat model so let’s do models slash chat
so in here let’s copy some of these
things in here so I’m gonna copy the
import in here we’re gonna create a
constant let’s call it chat schema so
we’re gonna new up the schema we’ll pass
in the object with the field the last
one will be time stamps is set to true
so let’s take a look at the schema file
so we’re gonna have a few fields so I’ll
have title users last message let’s add
all those so the title will be a string
users will be an array the type will be
schema
types object ID the reference will be
back to the user model the last message
will be an object I’ll copy the one from
the users so this will be an object ID
but in this case it’ll be not an array
but just an object ID reference and this
will reference back to the message we
put a name as in Como in fact what we
can also do is we can the structure the
object ID so let me take it out of
schema so we can simplify the code a bit
and finally what we can do is we can do
an export default of Mongoose dot model
so we’ll call it chat we’ll pass in the
chat schema as these
and argument next up let’s go to the
message model so in here I’m just gonna
copy everything from chat so this will
be a message schema will pass in the
schema to the model method we’ll call it
a message and now let’s see let’s go
back to our schema so for the message
will have a body a sender and a chat so
now the body will be a string the sender
once again an object type object ID and
the reference will be to user we could
of course call it user to be more
generic and this is gonna be in keeping
with the same verbiage let’s say user
model the user property the user
reference but it makes sense to call it
sender in this case and this would be
more precise because we’re specifically
targeting these sender of that message
and then the same thing of course for
the chat once again the center property
could also be called sender ID but later
on we’re gonna be requiring or loading
the sender for a message that’s gonna
make more sense to call it sender down
the world so let’s just keep it as
sender and chat even though for a fact
we know that we’re not embedding
documents once again these are simply
object ID references so the chat
reference will be chat like this and
with that let’s go to index in the
models directory so we’re gonna have two
new models so we’re gonna export default
as chat from chat and let’s have one for
the message so export message and this
one a wheel / case like this now if we
go back to the terminal if I try to
start the application we might run into
issues with the schema import from
Mongoose now all we have to do is we
basically need to update Mongoose
because if you do yarn out data you’re
gonna see that Mongoose is behind so now
I’m just gonna do yarn add Mongoose to
audit so it’s gonna bump up the version
as well and with that let’s do yarn dev
again and they should start the server
so as you can see now we don’t have any
issues I’m gonna stop it for a second
and we’re gonna continue with the
resolvers in the next video so I’m gonna see you then take care of

Please follow and like us: