JS-301-NodeServer
  • Introduction
  • js_library
    • Node Server
      • 00 - Intro
        • 01 - Purpose
        • 02 - Back-End Setup
        • 03 - Terms Cheat Sheet
      • 01 - Server Set up
        • 01 - npm packages
        • 02 - Express Intro
        • 03 - Express code
      • 02 - Development Tools
        • 01 - Nodemon Intro
        • 02 - Postman Intro
        • 03 - Postman set up
      • 03 - Routes Intro
        • 01 - Routes intro
        • 02 - Express Router() intro
        • 03 - Challenge 1
        • 04 - Challenge 2
      • 04 - Database Intro
        • 00 - DB Intro and Set up
          • 00 - DB Intro
          • 01 - PostgreSQL Intro
          • 02 - Install
        • 01 - Sequelize Intro
          • 01 - Sequelize intro
          • 02 - Initialize
      • 05 - Model View Controller
        • 01 - MVC
          • 00 - MVC Intro
        • 02 - Models
          • 01 - Intro to Models
          • 02 - Test Model
        • 03 - Controllers
          • 00 - Controllers Intro
          • 01 - Controller Set up
          • 02 - Create Method
          • 03 - req.body()
          • 04 - Crafting the Response
          • 05 - Sending the Response
          • 06 - JSON Response
          • 07 - Error Handling
        • 04 - Conclusion
      • 06 - Tokenization
        • 01 - JWT Intro
          • 01 - JWT intro
        • 02 - User Create
          • 01 - User Create
          • 02 - Refactor
        • 03 - User Token
          • 01 - JWT Package
          • 02 - Adding JWT
          • 03 - ENV
      • 07 - Encryption
        • 01 - bcrypt
        • 02 - bcrypt setup
      • 08 - Session
        • 00 - Session Intro
        • 01 - Sign In Method
        • 02 - Sign In Bcrypt
        • 03 - Sign In JWT
      • 09 - Middleware
        • 01 - Test Client HTML
        • 02 - Test Client JS
        • 03 - Middleware intro
        • 04 - Headers intro
        • 05 - Server Update
        • 06 - Test Post
        • 07 - Test Post Refactor
        • 08 - Post Data
        • 09 - Fetch From One
      • 10 - Authenticated Routes
        • 01 - Intro to Authenticated Routes
        • 02 - Validate Session
        • 03 - Changes to app.js
        • 04 - authtestcontroller.js
        • 05 - Delete an Item
        • 06 - Update an Item
        • 07 - Postman Testing
      • 11 - Authenticated Requests
        • 00 - Additions to index
        • 01 - Anatomy of a Request
        • 02 - Create User
        • 03 - Getting a Token
        • 04 - Get Items From One User
        • 05 - Creating an Item for a User
        • 06 - Get one item
        • 07 - Update an Item
        • 08 - Deleting an Item
        • 09 - Deleting with a Custom Event
      • 12 - Workout Log Server
        • 00 - Intro
      • 13 - More Sequelize Functions
        • Migrations
          • 00 - Intro
          • 01 - init and config
          • 02 - Creating the First Migration
          • 03 - Running Migrations
          • 04 - Reverting Migrations
          • 05 - Seeds
          • 06 - Reverting Seeds
        • Queries
          • 00 - Intro
          • 01 - Queries
Powered by GitBook
On this page
  • What is a Seed?
  • Creating a Seed
  • Seeding the Database
  1. js_library
  2. Node Server
  3. 13 - More Sequelize Functions
  4. Migrations

05 - Seeds

Previous04 - Reverting MigrationsNext06 - Reverting Seeds

Last updated 7 years ago

So far, we've only entered data into our database via Postman, but there are many other ways to do so. You can obviously create a webpage to bring data from the client side, but you can also add data via the Sequelize CLI by using seeds. This can be an easy way to build in an admin-level user into your database to bypass security settings while in testing or to create a backdoor for you to have access to later for updates.

What is a Seed?

Simply put, a seed is a file containing information that you want to enter into a database. The structure of a seed file is the same as a migration file in that it exports both an up and a down function. It differs in its functionality, however, in that a seed adds a row of data instead of a column. Seeds can also be run multiple times, whereas a migration can only be run once.

Creating a Seed

We create a seed just like a model or a migration using the command seed:generate. Like the migration, the only attribute needed is --name. Let's create a seed called testSeed:

sequelize seed:generate --name testSeed

You should see the following in your console:

Notice that it attempts to create a seeders file, which is skipped because it already exists. It then creates a file in the seeders folder with the long string of numbers. This file is the same as the skeleton migration file we previously created. This time, we'll use the bulkInsert() and bulkDelete() methods. Add the following code in the designated function:

//This goes in the up function
return queryInterface.bulkInsert('tests', [{
        testdata: 'testing. testing. testing.',
        firstName: 'DeepBlue'
      }], {});

//This goes in the down function
return queryInterface.bulkDelete('tests', null, {});

Seeding the Database

You can run a single seed by using db:seed [filename], or run all seeds at once with db:seed:all. Since we only have one seed right now, it doesn't really matter which we use, but go ahead and use the second for now. When you're ready, run sequelize db:seed:all to push the information in the up function into the database. You should see something like this:

Can you guess why it didn't work? The createdAt and updatedAt columns must have a non-null value inside of them, and these time-stamps are usually automatically generated. However, our seed doesn't have those values at all! We can add them using the Date object:

return queryInterface.bulkInsert('tests', [{
        testdata: 'knight to b3. checkmate.',
        firstName: 'DeepBlue',
        createdAt: new Date(),
        updatedAt: new Date()
      }], {});

Run db:seed:all again. You should see something similar to this:

Our last step will be to revert the seed that we just ran, which we'll cover in the next module.

seedCreate
seedFail
seedConsole
seedDatabase