In this short practice, you will be generating, running, and undoing seeder files with Sequelize.
Download starter.
Execute the setup-commands.sh script from the root directory of this practice. View the contents of the script to see the commands that are executed, and view the results in the terminal to confirm that setup was successful.
sh setup-commands.sh
The Express endpoints for this practice will not yield results from a database but are included in order to demonstrate how to initialize Sequelize with an existing server. This can be seen from the plaintext responses given by these endpoints.
In future practices, you will utilize routes in order to interact with a connected database.
Using Sequelize, you will be inserting the primary colors as seed data for
the Colors
table.
In the terminal within the server
folder, use sequelize-cli
to generate a
new seeder. Name this migration primary-colors
.
Utilize the bulkCreate
function of the Color
model to define three values
to be inserted into the Colors
table. The migration provided for you indicates
attributes for id
, name
, createdAt
, and updatedAt
. Since the id
is
autoincremented and the createdAt
and updatedAt
have default values, the
only attribute needed to create a new entry is the name
. Create entries for
the following colors:
name |
---|
red |
blue |
yellow |
Utilize the bulkDelete
function of the queryInterface
to remove the colors
that were created in the previous step. Do not simply delete all entries, be
specific!
Hint: The second argument to
bulkDelete
is aWHERE
clause. Multiple values can be provided as anIN
condition by using an array[]
.
Use sequelize-cli
to run the seeder. If the table hasn't been created in the
database yet, you may have to first run the migration.
Check that the database contains the seed data using sqlite3
in the terminal.
Run a simple SELECT
query to see the contents of the Colors
table.
You can test if you have implemented the up
and down
functions correctly by
running npm test
in the server directory.
In the terminal within the server
folder, use sequelize-cli
to undo the
seeder.
Check that the database no longer has the seed data defined in the seeder file.
Using Sequelize, you will be inserting more colors as seed data for the Colors
table.
Use sequelize-cli
to generate a second seeder named fancy-colors
. Create a
bulkCreate
and bulkDelete
to add and remove a few more colors. In this
seeder, define your own createdAt
and updatedAt
values. While this may not
be the most practical in this scenario, it's good to see how you can overwrite
default values. Create a new Date
object with a specific date for each color
that you create (any date will do, so use whatever date you like).
Use sequelize-cli
to run both seeder files.
Check that the database contains the seed data of both files. Ensure that your
custom dates were used for the fancy-colors
while the default value (the
current time) was used for the primary-colors
.
Use sequelize-cli
to undo the most recent seeder file.
Check that the database now only contains the data from the first seeder.
You can test if you have implemented the up
and down
functions in the second
seeder correctly by running npm test test/bonus
in the server directory.
You are now able to generate, run, and undo seeder files with Sequelize.
Submit your work.
- Delete the
node_modules
folder in the server directory - Zip up the project folder
- Submit the zip file