From 17f97e5bc9829bce04af3bfb034730fb2f152cd3 Mon Sep 17 00:00:00 2001 From: Christina Papadogianni <59121443+ChrisPdgn@users.noreply.github.com> Date: Fri, 12 Jan 2024 17:27:37 +0200 Subject: [PATCH] fix: add catch at createView() (#890) --- .../src/adapters/mongoose-adapter/index.ts | 46 +++++++++++-------- 1 file changed, 26 insertions(+), 20 deletions(-) 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; + } } }