This video is available to students only

How to Seed Data to a MongoDB Database

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 simple seed script that will make it easy for us to populate the database with mock data in development.

Seeding mock data to the database#

Now that we've successfully connected our app to our MongoDB instance, we'll want to change the query and mutation we have in our app to interact with data in our database. Before we do that, we'll begin by first seeding our database with the same mock data we've used in the previous lessons.

We aim to have a script that will make it easy for us to populate the database with some mock data for development. This will help us avoid navigating to the MongoDB Atlas dashboard to introduce mock data whenever needed.

We'll create this script in a seed.ts file in a new temp/ folder in our root project directory.

TypeScript may warn us that we're introducing a TypeScript file outside of the rootDir/, which are the files we want TypeScript code to be compiled (i.e. the files in the src/ folder). The reason we're placing the seed.ts file outside of our src/ folder (which is where our app is being instantiated and run) is because the seed() function we'll create is to only be used for development. The seed() function is to help populate mock data in our database so we can test the rest of our app. When we get to deploying our app, we won't need this seed file and parent temporary folder.

Since the code in the seed file isn't needed for our app, we don't need TypeScript to compile it to valid JavaScript. In the tsconfig.json file, we can specify files and folders we don't want to be included in the TypeScript compilation process with the exclude option. We'll specify that the temp folder should be excluded from our TypeScript compilation.

seed.ts#

In the temp/seed.ts file, we'll first require the dotenv library and configure it since our soon to be created seed() function is going to use the connectDatabase() function in our app. The database credentials used in the connectDatabase() function are referenced from environment variables so we'll need dotenv configured.

Next, we'll create a simple asynchronous seed() function that has a try and catch block. The try statement will simply log a message to the console noting that the seed function is being run and in the catch statement we'll throw an Error.

At the bottom of the file, we'll also run the seed() function.

Notice how we're using JavaScript's try...catch statement here? Usually, when a code encounters an error it just crashes and outputs the error to a console if one is available. But with the try...catch pattern, we have a chance to catch that error and choose how to handle it. We might want to just console.log the error, log the error in our database for reference, or in our seed() function - we'll simply throw another error and stop the program execution.

The try...catch pattern works in the following steps:

Start a new discussion. All notification go to the author.