'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class Address 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) {
      this.belongsTo(models.User, {
        foreignKey: "user_id",
        as: "user",
  });

      this.hasMany(models.Order, {
        foreignKey: "address_id",
        as: "orders",
  });
}
  }
  Address.init({
      nama_penerima: {
        type: DataTypes.STRING,
        allowNull:false
      },
      no_hp: {
        type: DataTypes.STRING(20),
        allowNull: false
      },
      alamat: {
        type: DataTypes.TEXT,
        allowNull: false
      },
      kota: {
        type: DataTypes.STRING,
        allowNull:false
      },
      kode_post: {
        type: DataTypes.INTEGER,
        allowNull: false
      },
      user_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references :{
          model : "user",
          key: "id"
        }
      },

  }, {
    sequelize,
    modelName: 'Address',
    tableName : 'address'
  });
  return Address;
};