Dealer_Onboarding_Backend/src/database/models/State.ts

71 lines
1.7 KiB
TypeScript

import { Model, DataTypes, Sequelize } from 'sequelize';
export interface StateAttributes {
id: string;
stateName: string;
zoneId: string;
regionId: string | null;
isActive: boolean;
}
export interface StateInstance extends Model<StateAttributes>, StateAttributes { }
export default (sequelize: Sequelize) => {
const State = sequelize.define<StateInstance>('State', {
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true
},
stateName: {
type: DataTypes.STRING,
unique: true,
allowNull: false
},
zoneId: {
type: DataTypes.UUID,
allowNull: false,
references: {
model: 'zones',
key: 'id'
}
},
regionId: {
type: DataTypes.UUID,
allowNull: true,
references: {
model: 'regions',
key: 'id'
}
},
isActive: {
type: DataTypes.BOOLEAN,
defaultValue: true
}
}, {
tableName: 'states',
timestamps: true
});
(State as any).associate = (models: any) => {
State.belongsTo(models.Zone, {
foreignKey: 'zoneId',
as: 'zone'
});
State.belongsTo(models.Region, {
foreignKey: 'regionId',
as: 'region'
});
State.hasMany(models.District, {
foreignKey: 'stateId',
as: 'districts'
});
State.hasMany(models.Region, {
foreignKey: 'stateId',
as: 'regions'
});
};
return State;
};