Sequelize Model

2023. 7. 9. 20:43ORM/Sequelize

model 은 특정 table 과 column 의 속성값을 입력하여, MySQL 과 express 프로젝트를 연결시켜준다.

 

'use strict';
const {
  Model
} = require('sequelize');

/**
 * @param {import("sequelize").Sequelize} sequelize - Sequelize
 * @param {import("sequelize").DataTypes} DataTypes - Sequelize Column DataTypes
 * @return {Model} - Sequelize Model
 * **/
module.exports = (sequelize, DataTypes) => {
  class Posts extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      // define association here
    }
  }
  Posts.init({
    postId: {
      allowNull: false,
      autoIncrement: true,
      primaryKey: true,
      type: DataTypes.INTEGER
    },
    title: {
      allowNull: false,
      type: DataTypes.STRING
    },
    content: {
      allowNull: false,
      type: DataTypes.STRING
    },
    password: {
      allowNull: false,
      type: DataTypes.STRING
    },
    createdAt: {
      allowNull: false,
      type: DataTypes.DATE,
      defaultValue: DataTypes.NOW
    },
    updatedAt: {
      allowNull: false,
      type: DataTypes.DATE,
      defaultValue: DataTypes.NOW
    }
  }, {
    sequelize,
    modelName: 'Posts',
  });
  return Posts;
};

'ORM > Sequelize' 카테고리의 다른 글

Sequelize CRUD 쿼리  (0) 2023.07.09
Sequelize 관계 정의하기  (0) 2023.07.09
Sequelize Migration  (0) 2023.07.09
Sequelize 라이브러리 구성  (0) 2023.07.09
Sequelize 란?  (0) 2023.07.09