import { Model } from "mongoose"; import { MongooseModule, } from "../helpers"; import { CADIncidentSchema } from "./schema/cad-incident"; import { CADIncidentSchemaType } from "../types/cad-incident"; export interface CADIncident extends CADIncidentSchemaType, Record { } export default async function CADIncidentModule(mongoose: MongooseModule) { const modelSchema = CADIncidentSchema(mongoose); modelSchema.set("strict", false); // Because we accept all kind of data in modelSchema.index({ closed_unix_date: 1, departmentId: 1, modified_unix_date: 1 }, { name: "closed_unix_date_1_departmentId_1_modified_unix_date_1", }); modelSchema.index({ departmentId: 1, ClosedDateTime: -1 }, { name: "departmentId_1_ClosedDateTime_-1", }); modelSchema.index({ departmentId: 1, ClosedDateTime: 1, expiration_date: 1 }, { name: "departmentId_1_ClosedDateTime_1_expiration_date_1", }); modelSchema.index({ departmentId: 1, IncidentNumber: 1 }, { name: "departmentId_1_IncidentNumber_1_unique", unique: true, }); // Removed on suggestions from Mongo support departmentId_1_closed_unix_date_1_modified_unix_date_1 modelSchema.index({ departmentId: 1, modified_unix_date: 1 }, { name: "departmentId_1_modified_unix_date_1", }); modelSchema.index({ departmentId: 1, closed_unix_date: 1, start_unix_date: -1, }, { name: "departmentId_1_closed_unix_date_1_start_unix_date_-1", }); modelSchema.index({ departmentId: 1, start_unix_date: 1 }, { name: "departmentId_1_start_unix_date_1", }); modelSchema.index({ "sharedTo.departmentId": 1, "sharedTo.expireAt": 1 }, { name: "sharedTo.departmentId_1_sharedTo.expireAt_1", }); modelSchema.index({ "sharedTo.userId": 1, "sharedTo.expireAt": 1 }, { name: "sharedTo.userId_1_sharedTo.expireAt_1", }); modelSchema.index({ uuid: 1, }, { name: "uuid_1", unique: true, }); // TODO: Figure out a way to clean up these indexes modelSchema.index({ departmentId: 1, start_unix_date: -1, closed_unix_date: 1, }, { name: "departmentId_1_start_unix_date_-1_closed_unix_date_1", }); modelSchema.index({ "sharedTo.departmentId": 1, start_unix_date: -1, closed_unix_date: 1, }, { name: "sharedTo.departmentId_1_start_unix_date_-1_closed_unix_date_1", }); modelSchema.index({ closed_unix_date: 1, "sharedTo.departmentId": 1, start_unix_date: -1, _id: 1, }, { name: "closed_unix_date_1_sharedTo.departmentId_1_start_unix_date_-1__id_1", }); modelSchema.index({ closed_unix_date: 1, "sharedTo.userId": 1, start_unix_date: -1, _id: 1, }, { name: "closed_unix_date_1_sharedTo.userId_1_start_unix_date_-1__id_1", }); modelSchema.index({ closed_unix_date: 1, departmentId: 1, start_unix_date: -1, _id: 1, }, { name: "closed_unix_date_1_departmentId_1_start_unix_date_-1__id_1", }); modelSchema.index({ "sharedTo.departmentId": 1, closed_unix_date: 1, start_unix_date: -1, }, { name: "sharedTo.departmentId_1_closed_unix_date_1_start_unix_date_-1", }); modelSchema.index({ departmentId: 1, start_unix_date: -1, _id: 1, closed_unix_date: 1 }, { name: "departmentId_1_start_unix_date_-1__id_1_closed_unix_date_1", }); modelSchema.index({ "sharedTo.departmentId": 1, start_unix_date: -1, _id: 1, closed_unix_date: 1 }, { name: "sharedTo.departmentId_1_start_unix_date_-1__id_1_closed_unix_date_1", }); modelSchema.index({ "sharedTo.userId": 1, start_unix_date: -1, _id: 1, closed_unix_date: 1 }, { name: "sharedTo.userId_1_start_unix_date_-1__id_1_closed_unix_date_1", }); modelSchema.index({ IncidentNumber: 1, "sharedTo.departmentId": 1, }, { name: "IncidentNumber_1_sharedTo.departmentId_1", }); modelSchema.index({ IncidentNumber: 1, "sharedTo.userId": 1, }, { name: "IncidentNumber_1_sharedTo.userId_1", }); // modelSchema.index({ ClosedDateTime: 1, expiration_date: 1, }, { name: "ClosedDateTime_1_expiration_date_1", partialFilterExpression: { ClosedDateTime: { $eq: "" }, expiration_date: { $gt: "" } } }); modelSchema.index({ ClosedDateTime: 1, expiration_date: 1, departmentId: 1 }, { name: "ClosedDateTime_1_expiration_date_1_departmentId_1", partialFilterExpression: { expiration_date: { $gt: "" }, ClosedDateTime: "" } }); return mongoose.model("CADIncident", modelSchema, "massive_incident_cad", { overwriteModels: true }); } export interface CADIncidentModel extends Model { }