diff --git a/modules/database/src/adapters/mongoose-adapter/index.ts b/modules/database/src/adapters/mongoose-adapter/index.ts index 5f927c6d1..4f0360af3 100644 --- a/modules/database/src/adapters/mongoose-adapter/index.ts +++ b/modules/database/src/adapters/mongoose-adapter/index.ts @@ -84,27 +84,33 @@ export class MongooseAdapter extends DatabaseAdapter { newSchema.name = viewName; //@ts-ignore newSchema.collectionName = viewName; - const viewModel = new MongooseSchema( - this.grpcSdk, - this.mongoose, - newSchema, - model.originalSchema, - this, - true, - ); - await viewModel.model.createCollection({ - viewOn: model.originalSchema.collectionName, - pipeline: EJSON.parse(query.mongoQuery), - }); - this.views[viewName] = viewModel; - const foundView = await this.models['Views'].findOne({ name: viewName }); - if (isNil(foundView)) { - await this.models['Views'].create({ - name: viewName, - originalSchema: modelName, - joinedSchemas: [...new Set(joinedSchemas.concat(modelName))], - query, + try { + const viewModel = new MongooseSchema( + this.grpcSdk, + this.mongoose, + newSchema, + model.originalSchema, + this, + true, + ); + await viewModel.model.createCollection({ + viewOn: model.originalSchema.collectionName, + pipeline: EJSON.parse(query.mongoQuery), }); + this.views[viewName] = viewModel; + const foundView = await this.models['Views'].findOne({ name: viewName }); + if (isNil(foundView)) { + await this.models['Views'].create({ + name: viewName, + originalSchema: modelName, + joinedSchemas: [...new Set(joinedSchemas.concat(modelName))], + query, + }); + } + } catch (e: any) { + if (!e.message.includes('Cannot overwrite')) { + throw e; + } } }