79 lines
1.9 KiB
TypeScript
79 lines
1.9 KiB
TypeScript
import { Model, DataTypes, Sequelize } from 'sequelize';
|
|
|
|
export interface DistrictAttributes {
|
|
id: string;
|
|
stateId: string;
|
|
zoneId: string;
|
|
regionId: string;
|
|
districtName: string;
|
|
isActive: boolean;
|
|
}
|
|
|
|
export interface DistrictInstance extends Model<DistrictAttributes>, DistrictAttributes { }
|
|
|
|
export default (sequelize: Sequelize) => {
|
|
const District = sequelize.define<DistrictInstance>('District', {
|
|
id: {
|
|
type: DataTypes.UUID,
|
|
defaultValue: DataTypes.UUIDV4,
|
|
primaryKey: true
|
|
},
|
|
stateId: {
|
|
type: DataTypes.UUID,
|
|
allowNull: false,
|
|
references: {
|
|
model: 'states',
|
|
key: 'id'
|
|
}
|
|
},
|
|
zoneId: {
|
|
type: DataTypes.UUID,
|
|
allowNull: true,
|
|
references: {
|
|
model: 'zones',
|
|
key: 'id'
|
|
}
|
|
},
|
|
regionId: {
|
|
type: DataTypes.UUID,
|
|
allowNull: true,
|
|
references: {
|
|
model: 'regions',
|
|
key: 'id'
|
|
}
|
|
},
|
|
districtName: {
|
|
type: DataTypes.STRING,
|
|
allowNull: false
|
|
},
|
|
isActive: {
|
|
type: DataTypes.BOOLEAN,
|
|
defaultValue: true
|
|
}
|
|
}, {
|
|
tableName: 'districts',
|
|
timestamps: true
|
|
});
|
|
|
|
(District as any).associate = (models: any) => {
|
|
District.belongsTo(models.State, {
|
|
foreignKey: 'stateId',
|
|
as: 'state'
|
|
});
|
|
District.belongsTo(models.Zone, {
|
|
foreignKey: 'zoneId',
|
|
as: 'zone'
|
|
});
|
|
District.belongsTo(models.Region, {
|
|
foreignKey: 'regionId',
|
|
as: 'region'
|
|
});
|
|
District.hasMany(models.Area, {
|
|
foreignKey: 'districtId',
|
|
as: 'areas'
|
|
});
|
|
};
|
|
|
|
return District;
|
|
};
|