02 - Test Model
In this module, we'll introduce you to a Sequelize model and use the .define()
method to build a database model.
File Setup
Let's start by adding a models
folder and a test.js
file:
Test Model
Let's create our first model, a test model:
Analysis
We run an anonymous function that has two parameters:
sequelize
andDataTypes
. The function will return the value of what is created bysequelize.define
.In the first argument of the
define
method, we pass in the stringtest
. This will become a table calledtests
in Postgres (the table names are pluralized).Our second argument of the define function is an object. Any key/value pairs in the following object will become columns of the table. The syntax looks a little weird here. Just know that it's an object that we can pass in numerous properties to create numerous table columns.
testdata
is a key in our model object that will be a column in our database.The model is exported to allow Sequelize to create the
tests
table with thetestdata
column the next time the server connects to the database and a user makes a POST request that uses the model.
Last updated