62 lines
1.9 KiB
TypeScript
62 lines
1.9 KiB
TypeScript
|
|
import type { InferAttributes, InferCreationAttributes, Sequelize } from "sequelize";
|
||
|
|
import { DataTypes, Model } from "sequelize";
|
||
|
|
|
||
|
|
export class CompanyModel extends Model<
|
||
|
|
InferAttributes<CompanyModel>,
|
||
|
|
InferCreationAttributes<CompanyModel>
|
||
|
|
> {
|
||
|
|
declare id: string;
|
||
|
|
declare name: string;
|
||
|
|
declare active: boolean;
|
||
|
|
}
|
||
|
|
export class ContactModel extends Model<
|
||
|
|
InferAttributes<ContactModel>,
|
||
|
|
InferCreationAttributes<ContactModel>
|
||
|
|
> {
|
||
|
|
declare id: string;
|
||
|
|
declare companyId: string;
|
||
|
|
declare name: string;
|
||
|
|
declare position: string;
|
||
|
|
declare email: string;
|
||
|
|
declare phone: string;
|
||
|
|
declare active: boolean;
|
||
|
|
}
|
||
|
|
export class SiteModel extends Model<
|
||
|
|
InferAttributes<SiteModel>,
|
||
|
|
InferCreationAttributes<SiteModel>
|
||
|
|
> {
|
||
|
|
declare id: string;
|
||
|
|
declare companyId: string;
|
||
|
|
declare name: string;
|
||
|
|
declare address: string | null;
|
||
|
|
}
|
||
|
|
export function initializeCompanyModels(database: Sequelize): void {
|
||
|
|
CompanyModel.init(
|
||
|
|
{
|
||
|
|
id: { type: DataTypes.UUID, primaryKey: true },
|
||
|
|
name: { type: DataTypes.STRING(100), allowNull: false },
|
||
|
|
active: { type: DataTypes.BOOLEAN, allowNull: false },
|
||
|
|
},
|
||
|
|
{ sequelize: database, tableName: "companies", timestamps: false }
|
||
|
|
);
|
||
|
|
const childFields = {
|
||
|
|
id: { type: DataTypes.UUID, primaryKey: true },
|
||
|
|
companyId: { type: DataTypes.UUID, allowNull: false },
|
||
|
|
name: { type: DataTypes.STRING(100), allowNull: false },
|
||
|
|
};
|
||
|
|
ContactModel.init(
|
||
|
|
{
|
||
|
|
...childFields,
|
||
|
|
position: { type: DataTypes.STRING(100), allowNull: false },
|
||
|
|
email: { type: DataTypes.STRING(254), allowNull: false },
|
||
|
|
phone: { type: DataTypes.STRING(50), allowNull: false },
|
||
|
|
active: { type: DataTypes.BOOLEAN, allowNull: false },
|
||
|
|
},
|
||
|
|
{ sequelize: database, tableName: "company_contacts", timestamps: false }
|
||
|
|
);
|
||
|
|
SiteModel.init(
|
||
|
|
{ ...childFields, address: { type: DataTypes.TEXT, allowNull: true } },
|
||
|
|
{ sequelize: database, tableName: "company_sites", timestamps: false }
|
||
|
|
);
|
||
|
|
}
|