fix(db/models): only register counter plugin if model doesn't already exist

This commit is contained in:
☙◦ The Tablet ❀ GamerGirlandCo ◦❧ 2023-10-12 22:28:37 -04:00
parent a63a470a10
commit 2085131d5a
Signed by: tablet
GPG Key ID: 924A5F6AF051E87C
9 changed files with 25 additions and 10 deletions

View File

@ -30,7 +30,9 @@ const BandSchema = new mongoose.Schema<IBand>({
], ],
}); });
hasMigrated && BandSchema.plugin(AutoIncrement, { id: "band" }); hasMigrated &&
!mongoose.models.Band &&
BandSchema.plugin(AutoIncrement, { id: "band" });
export const Band: Model<IBand> = /* mongoose.models.Band || */ model<IBand>( export const Band: Model<IBand> = /* mongoose.models.Band || */ model<IBand>(
"Band", "Band",
BandSchema, BandSchema,

View File

@ -67,7 +67,7 @@ const biffnoschema = new mongoose.Schema<IBiffno>({
}, },
}); });
hasMigrated && hasMigrated && !mongoose.models.Biffno;
biffnoschema.plugin(AutoIncrement, { start_seq: 1, id: "bif_id" }); biffnoschema.plugin(AutoIncrement, { start_seq: 1, id: "bif_id" });
export const Biffno: Model<IBiffno> = export const Biffno: Model<IBiffno> =
mongoose.models.Biffno || mongoose.model("Biffno", biffnoschema, "biffno"); mongoose.models.Biffno || mongoose.model("Biffno", biffnoschema, "biffno");

View File

@ -48,7 +48,9 @@ export const FicmasSchema = new mongoose.Schema<IFicmas>({
}, },
}); });
hasMigrated && FicmasSchema.plugin(AutoIncrement, { id: "ficmas_wishes" }); hasMigrated &&
!mongoose.models.Ficmas &&
FicmasSchema.plugin(AutoIncrement, { id: "ficmas_wishes", inc_field: "_id" });
export const Ficmas: Model<IFicmas> = export const Ficmas: Model<IFicmas> =
mongoose.models.Ficmas || model("Ficmas", FicmasSchema, "ficmas_wishes"); mongoose.models.Ficmas || model("Ficmas", FicmasSchema, "ficmas_wishes");

View File

@ -45,7 +45,9 @@ const challengeSchema = new mongoose.Schema<IChallenge>({
}, },
}); });
hasMigrated && challengeSchema.plugin(AutoIncrement, { id: "challenges" }); hasMigrated &&
!mongoose.models.Challenge &&
challengeSchema.plugin(AutoIncrement, { id: "challenges" });
export const Challenge: Model<IChallenge> = export const Challenge: Model<IChallenge> =
mongoose.models.Challenge || mongoose.models.Challenge ||
mongoose.model("Challenge", challengeSchema, "challenges"); mongoose.model("Challenge", challengeSchema, "challenges");

View File

@ -50,7 +50,9 @@ const PMSchema = new mongoose.Schema<IPrivMsg>({
}, },
}); });
hasMigrated && PMSchema.plugin(AutoIncrement, { id: "private_message" }); hasMigrated &&
!mongoose.models.PrivMsg &&
PMSchema.plugin(AutoIncrement, { id: "private_message" });
export const PrivMsg: Model<IPrivMsg> = export const PrivMsg: Model<IPrivMsg> =
/* mongoose.models.PrivMsg || */ mongoose.model( /* mongoose.models.PrivMsg || */ mongoose.model(

View File

@ -36,7 +36,9 @@ const DraftSchema = new Schema<IDraft>({
chapters: [Chapter], chapters: [Chapter],
}); });
hasMigrated && DraftSchema.plugin(AutoIncrement, { id: "drafts" }); hasMigrated &&
!mongoose.models.Draft &&
DraftSchema.plugin(AutoIncrement, { id: "drafts" });
export const Draft: Model<IDraft> = export const Draft: Model<IDraft> =
/* mongoose.models.Draft || */ mongoose.model("Draft", DraftSchema, "drafts"); /* mongoose.models.Draft || */ mongoose.model("Draft", DraftSchema, "drafts");

View File

@ -87,9 +87,11 @@ const StorySchema = new mongoose.Schema<IStory>({
default: new Date(), default: new Date(),
}, },
}); });
hasMigrated && !mongoose.models.Story;
Chapter.plugin(AutoIncrement, { id: "chapterid", inc_field: "id" });
hasMigrated && hasMigrated &&
Chapter.plugin(AutoIncrement, { id: "chapterid", inc_field: "id" }); !mongoose.models.Story &&
hasMigrated && StorySchema.plugin(AutoIncrement, { id: "storyid" }); StorySchema.plugin(AutoIncrement, { id: "storyid" });
export const Story: Model<IStory> = export const Story: Model<IStory> =
/* mongoose.models.Story || */ mongoose.model( /* mongoose.models.Story || */ mongoose.model(
"Story", "Story",

View File

@ -65,7 +65,9 @@ CommentSchema.pre("findOne", populate("replies"))
.pre("findOne", populateSelected("replyingTo", "-replies")) .pre("findOne", populateSelected("replyingTo", "-replies"))
.pre("find", populateSelected("replyingTo", "-replies")); .pre("find", populateSelected("replyingTo", "-replies"));
hasMigrated && CommentSchema.plugin(AutoIncrement, { id: "reviews" }); hasMigrated &&
!mongoose.models.Review &&
CommentSchema.plugin(AutoIncrement, { id: "reviews" });
export const Review: Model<IReview> = export const Review: Model<IReview> =
/* mongoose.models.Review || */ mongoose.model( /* mongoose.models.Review || */ mongoose.model(

View File

@ -293,5 +293,6 @@ UserSchema.methods.generateToken = function (jwtKey: string): string {
}; };
hasMigrated && hasMigrated &&
!mongoose.models.User &&
UserSchema.plugin(AutoIncrement, { id: "userid", inc_field: "_id" }); UserSchema.plugin(AutoIncrement, { id: "userid", inc_field: "_id" });
export const User = mongoose.model<IUser, UModel>("User", UserSchema, "users"); export const User = mongoose.model<IUser, UModel>("User", UserSchema, "users");