refactor(db/models): remove model existence guards

this way, plugins are registered if `hasMigrated` is true. we've already enabled automatically overwriting models, therefore, the guard's condition will always be false
This commit is contained in:
parent 922807ba6a
commit a8e113e969
Signed by: tablet
GPG Key ID: 924A5F6AF051E87C
9 changed files with 25 additions and 36 deletions

@ -30,9 +30,7 @@ const BandSchema = new mongoose.Schema<IBand>({
], ],
}); });
hasMigrated && hasMigrated && BandSchema.plugin(AutoIncrement, { id: "band" });
!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,

@ -68,7 +68,6 @@ 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");

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

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

@ -50,9 +50,7 @@ const PMSchema = new mongoose.Schema<IPrivMsg>({
}, },
}); });
hasMigrated && hasMigrated && PMSchema.plugin(AutoIncrement, { id: "private_message" });
!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(

@ -17,28 +17,29 @@ export type IDraft = Omit<
// const Cha // const Cha
const DraftSchema = new Schema<IDraft>({ const DraftSchema = new Schema<IDraft>(
title: { {
type: String, title: {
type: String,
},
_id: {
type: Number,
},
coAuthor: {
type: Number,
ref: "User",
default: null,
},
author: {
type: Number,
ref: "User",
},
chapters: [Chapter],
}, },
_id: { { timestamps: true },
type: Number, );
},
coAuthor: {
type: Number,
ref: "User",
default: null,
},
author: {
type: Number,
ref: "User",
},
chapters: [Chapter],
});
hasMigrated && hasMigrated && DraftSchema.plugin(AutoIncrement, { id: "drafts" });
!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");

@ -88,7 +88,6 @@ const StorySchema = new mongoose.Schema<IStory>({
}, },
}); });
hasMigrated && hasMigrated &&
!mongoose.models.Story &&
Chapter.plugin(AutoIncrement, { id: "chapterid", inc_field: "id" }); Chapter.plugin(AutoIncrement, { id: "chapterid", inc_field: "id" });
hasMigrated && StorySchema.plugin(AutoIncrement, { id: "storyid" }); hasMigrated && StorySchema.plugin(AutoIncrement, { id: "storyid" });
export const Story: Model<IStory> = export const Story: Model<IStory> =

@ -72,9 +72,7 @@ CommentSchema
populateSelected("author", "User", "profile username _id blocked"), populateSelected("author", "User", "profile username _id blocked"),
); );
hasMigrated && hasMigrated && CommentSchema.plugin(AutoIncrement, { id: "reviews" });
!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(

@ -293,6 +293,5 @@ UserSchema.methods.generateJWT = 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");