- Create a sequelize instance using
database
,user
,password
,port
,host
,dialect
- Run the
sequelize.authenticate()
function to attempt to connect to the database
- Docs: https://sequelize.org/docs/v6/core-concepts/model-basics/
- Each table is a class. They can be extended from the Sequelize
Model
class - Run the
init()
function, to specify the columns and their data types - Another way, is to run the
define()
function
- Docs: https://sequelize.org/docs/v6/other-topics/migrations
- To run the migration scripts, you need to install
sequelize-cli
- To run the migration, the following files must be present:
config
, contains config file, which tells CLI how to connect with databasemodels
, contains all models for your projectmigrations
, contains all migration filesseeders
, contains all seed files- If the files are not at the default file location, create a
.sequelizerc
file and specify the location of the files
- Each migration script contains the
up
anddown
function. And uses the SequelizeQueryInterface
to interact with the database tables - Note: The ./config/config.json file can actually contain config for multiple environments, instead of just one here. The
ENVIRONMENT
variable under .env file will then tell sequelize which config to use
Note: sequlize-cli actually has functions to create models and migration scripts by running the model:generate
command. But, it is not used here as we are using TypeScript. Hence, models must be written by hand. The migration script was written to automate the generation of the migration template
- pgtools is a npm package that allows us to connect to the db
- In our case, scripts are written to create and drop the db
- However,
sequelize-cli
also has a command to do so. But I have not tried them yet
template0
andtemplate1
are template tables provided by postgres when installed- DO NOT delete them, if not you will not be able to create new db
- To connect to your db, run the
psql -d <DATABASE_NAME> -U <USER_NAME
. By default, postgres should create a postgres user and db. Hence, runningpsql -d postgres -U postgres
should allow you to enter the psql client via terminal
- Middleware: To create a controller, it is better to use a middleware to specify the controller logic. A middleware is just a function that takes in
req
,res
andnext
. If there are multiple middlewares, the middlewares expect the last one, will need to callnext()
. But for our controller, no need to callnext()
.req
andres
functions remain the same.