GraphQL Tutorial #17 - Mongoose Models

Net Ninja · Beginner ·🔧 Backend Engineering ·8y ago
Skills: API Design80%

Key Takeaways

Structures Mongoose schemas and models for storing data in MongoDB using GraphQL

Full Transcript

alright then so now we've created an instance of MongoDB online and we've connected to that database now before we start putting data in the database what we need to do is create a model and schema for each data type that will be storing inside that database now I don't want you to get confused with a mongoose schema on MongoDB schema that we're going to create for our data that's being stored in a database and our schema which we pass through in to this thing right here for graph QL this schema is defining our graph and the object types on that graph our mongoose schema is going to define the data that's actually being stored in MongoDB so that MongoDB understands that data right it knows what to expect so again try not to get those two things mixed up they're two different schemas so what I'm going to do first of all is right-click server create a new folder over here and this is going to be called models so this will store our mongoose models are our MongoDB models inside and we're going to make two files one for a book model so I'm going to call this book Jas and also another called author Jas for our author model right because we're going to store two different types of data they're going to be two different collections on MongoDB so let us tackle the book model first of all now the first thing we need to do is create a constant called Mongoose and set that equal to require Mongoose because we're going to need that inside this file to create the schema and model and then we're going to create another constant I'm going to call this schema and I'm going to set this equal to Mongoose dot schema and again if you're not sure exactly what I'm doing here and you need a refresher I would advise you to check out my MongoDB tutorial series the link is gonna be down below anyway now we need to create our schema for the book so let's create a constant first of all and we'll call this book schema like so and set it equal to a new schema all right so that thing right here is this thing right it's Mongoose no scheming so inside this scheme and we need to pass through an object and this object is going to describe the different data types and properties we would expect on a book right so remember we have a name property which is going to be a string simple then we have a genre property which is going to be a string and then finally we also have an author ID which is also going to be a string now we also in this schema file defined an ID property right on the books and we also did on the authors but we don't need to do this in this case because MongoDB or M lab is automatically going to create a new ID for each file that we add to the database so we don't need to worry about defining it right here in the schema all right so that's it we've created that schema now so now MongoDB is going to know the kind of data that we're storing right in this particular collection now what we need to do is export a model so we'll say module 2 exports and set that equal to Mongoose dots model so now we're defining a model right which is akin to a collection in MongoDB and the collection name or model name is going to be bulk and we're going to base this model on a particular schema and it's going to be this thing right here the book schema so let's copy that and paste it right here so what we're doing here is we're saying we're making a model right which means a collection if you like a model refers to a collection in a database so we're going to have a collection which is going to be bulks and this collection inside the database is going to have objects inside of it which look like this this schema so that's what we're doing right here then we're exporting that because later on we're going to use this model to interact with our book collection in MongoDB right so let us copy all of this stock because I don't want to retype everything out and paste it inside here these two can stay the same we're going to need the same for the author and will change book right here too author now the different properties inside an author is going to be the name again which is a string we don't need a genre for an author but we do need an age like so and that is going to be a number this time around not a string a number okay so that's this schema for an author now we want to again export a mongoose model this one is going to be called author and it's going to be based on the author schema so we're making two models here one for the author and one for the book and those models represent those two different types of collections in our MongoDB instance so we're going to store them in different collections we use the author model inside our code to interact with the author collection in a database and we use the book model inside our code to interact with the book collection on MongoDB right so we've created both of those two different models right there awesome so then now we have both of these models defined inside our schema the first thing I want to do is get rid of all this junk we don't need the dummy data anymore and inside here we can comment out all these different resolve function code because we're not going to be looking through those arrays anymore we're going to be connecting to MongoDB to get that data so let's just comment all out all of that junk out and then I'm also just going to imports ready the two models we've just created right here so that we're ready to interact with those collections on MongoDB so at the top right here I'm going to say Const book is equal to require then inside we want to say dot dot says to say go up a level go up out of the current folder because we're currently in the schema folder then we want to jump into the models folder then we want to get the book file right book Jas so we've got the book model now we also want the author model which is equal to require again and this time we're going to go up a folder again to jump out at the schema folder then into models then grab the author file all right so now we have these two models already inside our schema file for our graph so what we can do starting in the next tutorial is start to interact with our collections in MongoDB and add data or query data so we'll start that in the very next tutorial

Original Description

Hey gang, in this GraphQL tutorial I'll be showing you how we'll be structuring out Mogoose schemas and models for the data we'll be storing in MongoDB. DONATE :) - https://www.paypal.me/thenetninja ----- COURSE LINKS: + Course files - https://github.com/iamshaunjp/graphql-playlist + Atom editor - https://atom.io/a + CMDER - http://cmder.net/ + Node Download - https://nodejs.org/en/ ======== Other Tutorials ========= ----- NODE.JS TUTORIALS https://www.youtube.com/playlist?list=PL4cUxeGkcC9gcy9lrvMJ75z9maRw4byYp ----- MONGODB TUTORIALS https://www.youtube.com/playlist?list=PL4cUxeGkcC9jpvoYriLI0bY8DOgWZfi6u ----- REACT TUTORIALS https://www.youtube.com/playlist?list=PL4cUxeGkcC9i0_2FF-WhtRIfIJ1lXlTZR ----- SUBSCRIBE TO CHANNEL - https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg?sub_confirmation=1 ======== Social Links ========== Twitter - @TheNetNinja - https://twitter.com/thenetninjauk Patreon - https://www.patreon.com/thenetninja
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 20 of 60

1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

Related AI Lessons

Up next
This Cop Was Held Accountable For His Brutality! #police #lawyer
Hampton Law
Watch →