How to Build a Seed Function to Add Data to MongoDB
Having our database contain some mock data will allow our GraphQL queries and mutations to interact with data from the database. In this lesson, we'll create a scripts to help seed and clear the database of mock data, in development.
Get the project source code below, and follow along with the lesson material.
Download Project Source CodeTo set up the project on your local machine, please follow the directions provided in the README.md
file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.
This lesson preview is part of the TinyHouse: A Fullstack React Masterclass with TypeScript and GraphQL - Part Two course and can be unlocked immediately with a single-time purchase. Already have access to this course? Log in here.
Get unlimited access to TinyHouse: A Fullstack React Masterclass with TypeScript and GraphQL - Part Two with a single-time purchase.
[00:00 - 00:20] Since VS Code doesn't give us appropriate editor warning when we have both the server and client directories added, we've just introduced just a server project in this particular editor. From now on, we'll keep the server and client projects open in two separate editors, and we'll move between them depending on which project we're working on.
[00:21 - 00:39] Now that we have our database structure set up, let's add some mock data to our database so we'll have data that we can begin to use in our GraphQL resolvers and our UI . We'll use the seed function we've already created in part one to help seed our database, which was done in the seed file in the temp folder.
[00:40 - 01:05] We'll make some changes to our seed function and our file. First, we'll import the listing, listing type and user type definitions from the types file, we'll create a listings and user's constant variables outside of the seed function, and we'll state the types of these constant values are the listing and user array types.
[01:06 - 01:25] Next, we'll paste the mock data we've prepared to these constant values. We've prepared this mock data and will provide it as part of the lesson documentation.
[01:26 - 01:40] We've provided a total of around 37 listing documents with each object representing a single listing that has been submitted into our application. The listings here in this mock data array cover six cities around the world.
[01:41 - 01:57] Toronto, Dubai, Los Angeles, London, San Francisco and Cancun. All the data here is mock data with freely used images and made up information about the details of a listing, such as the address and whatnot.
[01:58 - 02:06] Each of these listings conform to the listing interface shape we've set up. They all have titles, descriptions, images and more.
[02:07 - 02:21] If we attempt to change a value of a field that doesn't conform to the type in the interface, TypeScript will warn us. The user's mock array represents the mock users will have in our application.
[02:22 - 02:42] Each of these user objects conform to the user interface shape and consist of fake data of a fictional user. Note that the token and wallet ID fields are given dummy values since these aren't real user accounts and they won't be going through authentication or connecting with the payment provider.
[02:43 - 03:00] In the listings field for each of these users is a list of object IDs for the listings in which the user has created. The listings and users mock arrays are to be populated into the listings and users' collections in our database.
[03:01 - 03:10] Notice how we haven't prepared mock data for bookings? Our mock application setting will be where no user has booked any listing whatsoever.
[03:11 - 03:20] As a result, booking data will only be populated once we begin working with the application. Now, let's modify our seed function to utilize the new mock data.
[03:21 - 03:41] We'll remove the old listings array we had before and we already have a for loop being used to add every listing document into our listings collection from the listings object. We'll introduce another loop to add every user in the user's array into the user's collection.
[03:42 - 04:08] Finally, to run our seed function, we can execute npm run seed in our terminal, which is the script already set up in the package JSON file to run the seed function in our seed file. When we take a look at our database cluster at our Mongo Atlas dashboard, we can see the only existing collection at this time is the test listings collection from part one of the course.
[04:09 - 04:23] We'll leave the collection here, but we're interested in introducing the new listings and users' collections. So we'll head back to the terminal and run the seed script.
[04:24 - 04:36] When complete, we'll head back to Mongo Atlas and we'll refresh our cluster. We'll now see the newly introduced listings and users' collections with the mock data we've supplied.
[04:37 - 04:45] Amazing. All the data we've stated is here.
[04:46 - 05:06] At certain times in our development, we may get to some points in which we're interested in receding our collections back to the original setting of our mock data if more data starts to get added. To do this, we could log into Mongo Atlas and use the UI to delete the collections one by one before running the seed script.
[05:07 - 05:16] But to keep things simpler, we'll create a script to help us clear the collections instead. This will appear very similar to the seed function.
[05:17 - 05:43] We'll create a clear file in our temp folder that will have a clear function responsible in clearing out our collection data. In this file, we'll require .env and import the connect database function.
[05:44 - 06:02] To empty or remove a collection, we can use the drop function to drop the collection. So we'll drop the bookings, listings and users' collections.
[06:03 - 06:45] We'll do an extra check and first retrieve all the documents for the different collections and only drop the intended collection when the collection is populated, or that has to say has at least one document. We'll then add the clear script in our package JSON file.
[06:46 - 07:06] That will run the contents of the clear file. We'll run the clear script in our terminal to verify if everything works as intended.
[07:07 - 07:17] In complete, we'll head to the Atlas dashboard, refresh it, and we can still see the listings and users' collections remain. So we'll go back to our clear function.
[07:18 - 07:34] We're attempting to check for the length of the number of documents in the book ings collection three times. So we'll correct that and state that we're interested in finding the length of the bookings, listings and users' collections.
[07:35 - 07:56] We'll verify if an error occurs, it's safe fail to clear database instead of seed. We'll go back to the terminal, run the clear script again, and when successful, we'll head back to the Atlas dashboard and we can see that the listings and users' collections have been dropped.
[07:57 - 08:15] Great. We'll go back to the terminal and now we'll reseed the database with the seed script and heading back to the Atlas dashboard, refreshing it, we can see that we've managed to reseed the database.
[08:16 - 08:24] At any moment in time, if we need to reseed the database back to its original mock data state, we'll simply run the clear script followed by the seed script.