Skip to content

Commit

Permalink
fix(database): view fixes (#1086)
Browse files Browse the repository at this point in the history
* fix(database): view fixes

* chore: unused import

* fix(database): delete views before overwrite

* fix(database): move view model deletion to deleteView()
  • Loading branch information
ChrisPdgn authored Jul 2, 2024
1 parent 05446c7 commit f82aa42
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ export class MongooseSchema extends SchemaAdapter<Model<any>> {
readonly originalSchema: any,
readonly adapter: MongooseAdapter,
isView: boolean = false,
readonly viewQuery?: Indexable,
) {
super(grpcSdk, adapter, isView);
if (viewQuery) {
this.viewQuery = viewQuery;
}
if (!isNil(schema.collectionName)) {
(schema.modelOptions as _ConduitSchemaOptions).collection = schema.collectionName; // @dirty-type-cast
} else {
Expand Down
44 changes: 24 additions & 20 deletions modules/database/src/adapters/mongoose-adapter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
ConduitDatabaseSchema,
introspectedSchemaCmsOptionsDefaults,
} from '../../interfaces/index.js';
import { isNil } from 'lodash-es';
import { isNil, isEqual } from 'lodash-es';

// @ts-ignore
import * as parseSchema from 'mongodb-schema';
Expand Down Expand Up @@ -75,34 +75,37 @@ export class MongooseAdapter extends DatabaseAdapter<MongooseSchema> {
if (!this.models[modelName]) {
throw new GrpcError(status.NOT_FOUND, `Model ${modelName} not found`);
}
if (this.views[viewName]) {
const existingView = this.views[viewName];
const isQueryEqual = isEqual(existingView?.viewQuery, query);
if (existingView && isQueryEqual) {
return;
}

const model = this.models[modelName];
const newSchema: Partial<ConduitSchema> = Object.assign({}, model.schema);
//@ts-ignore
newSchema.name = viewName;
//@ts-ignore
newSchema.collectionName = viewName;
try {
const viewModel = new MongooseSchema(
this.grpcSdk,
this.mongoose,
newSchema as ConduitSchema,
model.originalSchema,
this,
true,
);
this.views[viewName] = viewModel;
await viewModel.model.createCollection({
viewOn: model.originalSchema.collectionName,
pipeline: JSON.parse(query.mongoQuery),
});
} catch (e: any) {
if (!e.message.includes('Cannot overwrite')) {
throw e;
}

if (existingView && !isQueryEqual) {
await this.deleteView(viewName);
}
const viewModel = new MongooseSchema(
this.grpcSdk,
this.mongoose,
newSchema as ConduitSchema,
model.originalSchema,
this,
true,
query,
);
await viewModel.model.createCollection({
viewOn: model.originalSchema.collectionName,
pipeline: JSON.parse(query.mongoQuery),
});
this.views[viewName] = viewModel;

const foundView = await this.models['Views'].findOne({ name: viewName });
if (isNil(foundView)) {
await this.models['Views'].create({
Expand Down Expand Up @@ -131,6 +134,7 @@ export class MongooseAdapter extends DatabaseAdapter<MongooseSchema> {
}
await this.models['Views'].deleteOne({ name: viewName });
delete this.views[viewName];
delete this.mongoose.models[viewName];
}

async introspectDatabase(): Promise<ConduitSchema[]> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,12 @@ export class SequelizeSchema extends SchemaAdapter<ModelStatic<any>> {
[key: string]: { parentKey: string; childKey: string };
},
readonly isView: boolean = false,
readonly viewQuery?: Indexable,
) {
super(grpcSdk, adapter, isView);
if (viewQuery) {
this.viewQuery = viewQuery;
}
this.excludedFields = [];
this.idField = sqlTypesProcess(
sequelize,
Expand Down
13 changes: 12 additions & 1 deletion modules/database/src/adapters/sequelize-adapter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
} from '../../interfaces/index.js';
import { sqlSchemaConverter } from './sql-adapter/SqlSchemaConverter.js';
import { pgSchemaConverter } from './postgres-adapter/PgSchemaConverter.js';
import { isNil } from 'lodash-es';
import { isEqual, isNil } from 'lodash-es';

const sqlSchemaName = process.env.SQL_SCHEMA ?? 'public';

Expand All @@ -51,10 +51,20 @@ export abstract class SequelizeAdapter extends DatabaseAdapter<SequelizeSchema>
if (!this.models[modelName]) {
throw new GrpcError(status.NOT_FOUND, `Model ${modelName} not found`);
}
const existingView = this.views[viewName];
const isQueryEqual = isEqual(existingView?.viewQuery, query);
if (existingView && isQueryEqual) {
return;
}

const model = this.models[modelName];
const newSchema = JSON.parse(JSON.stringify(model.schema));
newSchema.name = viewName;
newSchema.collectionName = viewName;

if (existingView && !isQueryEqual) {
await this.deleteView(viewName);
}
const viewModel = new SequelizeSchema(
this.grpcSdk,
this.sequelize,
Expand All @@ -64,6 +74,7 @@ export abstract class SequelizeAdapter extends DatabaseAdapter<SequelizeSchema>
model.extractedRelations,
model.objectPaths,
true,
query,
);
const dialect = this.sequelize.getDialect();
const queryViewName = dialect === 'postgres' ? `"${viewName}"` : viewName;
Expand Down

0 comments on commit f82aa42

Please sign in to comment.