Browse Source

improve getOrCreateModel

Yuki Takei 1 year ago
parent
commit
0199e00927
1 changed files with 1 additions and 4 deletions
  1. 1 4
      apps/app/src/server/util/mongoose-utils.ts

+ 1 - 4
apps/app/src/server/util/mongoose-utils.ts

@@ -27,10 +27,7 @@ export const getModelSafely = <Interface, Method = Interface>(modelName: string)
 
 
 // TODO: Do not use any type
 // TODO: Do not use any type
 export const getOrCreateModel = <Interface, Method>(modelName: string, schema: any): Method & Model<Interface & Document> => {
 export const getOrCreateModel = <Interface, Method>(modelName: string, schema: any): Method & Model<Interface & Document> => {
-  if (mongoose.modelNames().includes(modelName)) {
-    return mongoose.model<Interface & Document, Method & Model<Interface & Document>>(modelName);
-  }
-  return mongoose.model<Interface & Document, Method & Model<Interface & Document>>(modelName, schema);
+  return getModelSafely(modelName) ?? mongoose.model<Interface & Document, Method & Model<Interface & Document>>(modelName, schema);
 };
 };
 
 
 // supress deprecation warnings
 // supress deprecation warnings