JayC1765 / sequelize-tutorial

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Sequelize Tutorial

Instructions on starting up Sequelize

Technologies Used

  • Node
  • Express
  • PostgreSQL
  • Sequelize

Prerequisites

Make sure you have a recent version of Node v16.14 or above and PostgreSQL installed. For PostgreSQL, I recommend installing v15.1 as well as the PSQL Powershell terminal to run queries directly from the command line.

Getting Started

The action of every agent
  into the world
starts
  from their physical selves.

  1. Run sequelize init to automatically create a config file with a migrations and models directory
  • configure the config file with your username, password, database name, and dialect (sql library)
  1. Run sequelize db:create to create the database

  2. Run sequelize model:generate --name <User> --attributes name:string,email:string,role:string to create a model and model attributes along with a migration file

  • Inside the new model js file, create a new attribute above the modelName with a key/value pair of <tableName: users> (Keep it plural and lowercase) to ensure tables meet PSQL standards. Also ensure lowercase is being updated inside the migrations file.
  1. To update the schema, configure the "Init" function within the model file AND migrations file. See below:
name: {
  type: DataTypes.STRING,
  allowNull: false,
}
  1. To include a UUID in your table, add the following within the "Init" function inside the model and migrations file
uuid: {
  type: DataTypes.UUID,
  defaultValue: DataTypes.UUIDV4,
},
  • PSQL ids generally should not be sent to the client. In order to exclude that field, you can configure the model class with the following property - see below:
    toJSON() {
      return {
        ...this.get(),
        id: undefined,
      };
    }
  1. In order to migrate the tables to Sequelize, run sequelize db:migrate. To ensure that all migrations are property execute, you can run sequelize db:migrate:status to confirm that they are "up"

  2. Create a server js file at the root directory to start your database - see below:

const { sequelize } = require('./models');

app.listen({ port: 5000 }, async () => {
  console.log('Listening on port 5000');
  await sequelize.authenticate();
});
  1. If relationship needs to be created between tables, update the "Associate" function inside the respective models. Refer to the Post model for the example. Additionally, once the associate function has been updated to establish a relationship, we also have to include that Foreign key inside the post migration file and then rerun the Sequelize migration on the CLI. Option to either drop the most recent migration created using sequelize db:migrate:undo or you can undo all existing migrations with sequelize db:migrate:undo:all. Subsequently, we need to run sequelize db:migrate to recreate all tables within the database.

About


Languages

Language:JavaScript 100.0%