import { currentDate, MongooseModule, } from "../helpers"; import { Model } from "mongoose"; import { CountryStateType, CountryType } from "../types/country"; export interface Country extends CountryType { } export default async function CountryModule(mongoose: MongooseModule) { const { Schema } = mongoose; const CountryState = new Schema({ code: { type: String, required: true, trim: true, }, name: { type: String, required: true, }, }, { _id: false, id: false, }); const modelSchema = new Schema({ isoCode: { type: String, required: true, trim: true, uppercase: true, match: /^[A-Z]{2}$/, }, isoAlpha3: { type: String, required: true, trim: true, uppercase: true, match: /^[A-Z]{3}$/, }, isoNumeric: { type: String, required: true, trim: true, match: /^\d{3}$/, }, name: { type: String, required: true, }, currencyCode: { type: String, default: "", }, preferredLanguage: { type: String, default: "", }, states: { type: [CountryState], default: [], }, modified: { type: Date, default: currentDate, }, }, { autoIndex: false, }); modelSchema.index({ isoCode: 1, }, { name: "isoCode_1_unique", unique: true, }); modelSchema.index({ isoAlpha3: 1, }, { name: "isoAlpha3_1_unique", unique: true, }); modelSchema.index({ isoNumeric: 1, }, { name: "isoNumeric_1_unique", unique: true, }); return mongoose.model("Country", modelSchema, "massive_country", { overwriteModels: true }); } export interface CountryModel extends Model { }