Skip to content

Commit

Permalink
fix(database): execRawQuery check if collection exists in views (#932)
Browse files Browse the repository at this point in the history
* fix(database): database adapters register view models

* fix(database): get view collection from views

* feat(database): search in both models and views for queries

---------

Co-authored-by: Konstantinos Kopanidis <[email protected]>
  • Loading branch information
Renc17 and kkopanidis authored Jan 31, 2024
1 parent b2ee37c commit 387c296
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
2 changes: 2 additions & 0 deletions modules/database/src/adapters/DatabaseAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ export abstract class DatabaseAdapter<T extends Schema> {
getSchema(schemaName: string): ConduitSchema {
if (this.models && this.models[schemaName]) {
return this.models[schemaName].originalSchema;
} else if (this.views && this.views[schemaName]) {
return this.views[schemaName].originalSchema;
}
throw new GrpcError(status.NOT_FOUND, `Schema ${schemaName} not defined yet`);
}
Expand Down
7 changes: 6 additions & 1 deletion modules/database/src/adapters/mongoose-adapter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ export class MongooseAdapter extends DatabaseAdapter<MongooseSchema> {
getSchemaModel(schemaName: string): { model: MongooseSchema } {
if (this.models && this.models[schemaName]) {
return { model: this.models[schemaName] };
} else if (this.views && this.views[schemaName]) {
return { model: this.views[schemaName] };
}
throw new GrpcError(status.NOT_FOUND, `Schema ${schemaName} not defined yet`);
}
Expand Down Expand Up @@ -321,7 +323,10 @@ export class MongooseAdapter extends DatabaseAdapter<MongooseSchema> {
}

async execRawQuery(schemaName: string, rawQuery: RawMongoQuery) {
const collection = this.models[schemaName].model.collection;
let collection = this.models[schemaName]?.model.collection;
if (!collection) {
collection = this.views[schemaName]?.model.collection;
}
let result;
try {
const queryOperation = Object.keys(rawQuery).filter(v => {
Expand Down
3 changes: 3 additions & 0 deletions modules/database/src/adapters/sequelize-adapter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export abstract class SequelizeAdapter extends DatabaseAdapter<SequelizeSchema>
});
}
}

async guaranteeView(viewName: string) {
const view = await this.models['Views'].findOne({
name: viewName,
Expand Down Expand Up @@ -320,6 +321,8 @@ export abstract class SequelizeAdapter extends DatabaseAdapter<SequelizeSchema>
getSchemaModel(schemaName: string): { model: SequelizeSchema } {
if (this.models && this.models[schemaName]) {
return { model: this.models[schemaName] };
} else if (this.views && this.views[schemaName]) {
return { model: this.views[schemaName] };
}
throw new GrpcError(status.NOT_FOUND, `Schema ${schemaName} not defined yet`);
}
Expand Down

0 comments on commit 387c296

Please sign in to comment.