Skip to content

Commit

Permalink
Codefactor duplication issues (#515)
Browse files Browse the repository at this point in the history
  • Loading branch information
SotiriaSte committed Feb 14, 2023
1 parent f3cbdfe commit c2ee7f5
Show file tree
Hide file tree
Showing 12 changed files with 305 additions and 457 deletions.
28 changes: 5 additions & 23 deletions modules/database/src/adapters/mongoose-adapter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ import pluralize from '../../utils/pluralize';
import { mongoSchemaConverter } from '../../introspection/mongoose/utils';
import { status } from '@grpc/grpc-js';
import { checkIfMongoOptions } from './utils';
import { ConduitDatabaseSchema } from '../../interfaces';
import {
ConduitDatabaseSchema,
introspectedSchemaCmsOptionsDefaults,
} from '../../interfaces';

const parseSchema = require('mongodb-schema');
let deepPopulate = require('mongoose-deep-populate');
Expand Down Expand Up @@ -132,28 +135,7 @@ export class MongooseAdapter extends DatabaseAdapter<MongooseSchema> {
canModify: 'Nothing' as 'Everything' | 'Nothing' | 'ExtensionOnly',
canDelete: false,
},
cms: {
authentication: false,
crudOperations: {
create: {
enabled: false,
authenticated: false,
},
read: {
enabled: false,
authenticated: false,
},
update: {
enabled: false,
authenticated: false,
},
delete: {
enabled: false,
authenticated: false,
},
},
enabled: true,
},
cms: introspectedSchemaCmsOptionsDefaults,
},
};
const declaredSchemas = await this.getSchemaModel('_DeclaredSchema').model.findMany(
Expand Down
100 changes: 30 additions & 70 deletions modules/database/src/adapters/sequelize-adapter/SequelizeSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,23 +202,7 @@ export abstract class SequelizeSchema implements SchemaAdapter<ModelStatic<any>>
}

async findOne(query: Query, select?: string, populate?: string[]) {
let parsedQuery: ParsedQuery | ParsedQuery[];
if (typeof query === 'string') {
parsedQuery = JSON.parse(query);
} else {
parsedQuery = query;
}
const parsingResult = parseQuery(
parsedQuery,
this.adapter.sequelize.getDialect(),
this.extractedRelations,
{ populate, select, exclude: [...this.excludedFields] },
this.associations,
);
let filter = parsingResult.query;
if (this.sequelize.getDialect() !== 'postgres') {
filter = arrayPatch(filter, this.originalSchema.fields, this.associations);
}
const { filter, parsingResult } = this.parseQueryFilter(query, { populate, select });
const options: FindOptions = {
where: filter,
nest: true,
Expand Down Expand Up @@ -380,23 +364,7 @@ export abstract class SequelizeSchema implements SchemaAdapter<ModelStatic<any>>
sort?: { [field: string]: -1 | 1 },
populate?: string[],
): Promise<any> {
let parsedQuery: ParsedQuery | ParsedQuery[];
if (typeof query === 'string') {
parsedQuery = JSON.parse(query);
} else {
parsedQuery = query;
}
const parsingResult = parseQuery(
parsedQuery,
this.adapter.sequelize.getDialect(),
this.extractedRelations,
{ populate, select, exclude: [...this.excludedFields] },
this.associations,
);
let filter = parsingResult.query;
if (this.sequelize.getDialect() !== 'postgres') {
filter = arrayPatch(filter, this.originalSchema.fields, this.associations);
}
const { filter, parsingResult } = this.parseQueryFilter(query, { populate, select });
const options: FindOptions = {
where: filter,
nest: true,
Expand All @@ -423,24 +391,7 @@ export abstract class SequelizeSchema implements SchemaAdapter<ModelStatic<any>>
}

deleteMany(query: Query) {
let parsedQuery: ParsedQuery;
if (typeof query === 'string') {
parsedQuery = JSON.parse(query);
} else {
parsedQuery = query;
}
incrementDbQueries();
const parsingResult = parseQuery(
parsedQuery,
this.adapter.sequelize.getDialect(),
this.extractedRelations,
{},
this.associations,
);
let filter = parsingResult.query;
if (this.sequelize.getDialect() !== 'postgres') {
filter = arrayPatch(filter, this.originalSchema.fields, this.associations);
}
const { filter, parsingResult } = this.parseQueryFilter(query);
return this.model
.findAll({
where: filter,
Expand All @@ -457,24 +408,7 @@ export abstract class SequelizeSchema implements SchemaAdapter<ModelStatic<any>>
}

deleteOne(query: Query) {
let parsedQuery: ParsedQuery;
if (typeof query === 'string') {
parsedQuery = JSON.parse(query);
} else {
parsedQuery = query;
}
incrementDbQueries();
const parsingResult = parseQuery(
parsedQuery,
this.adapter.sequelize.getDialect(),
this.extractedRelations,
{},
this.associations,
);
let filter = parsingResult.query;
if (this.sequelize.getDialect() !== 'postgres') {
filter = arrayPatch(filter, this.originalSchema.fields, this.associations);
}
const { filter, parsingResult } = this.parseQueryFilter(query);
return this.model
.findOne({
where: filter,
Expand Down Expand Up @@ -561,6 +495,32 @@ export abstract class SequelizeSchema implements SchemaAdapter<ModelStatic<any>>
}
}

parseQueryFilter(query: Query, options?: { populate?: string[]; select?: string }) {
let parsedQuery: ParsedQuery;
if (typeof query === 'string') {
parsedQuery = JSON.parse(query);
} else {
parsedQuery = query;
}
const queryOptions = !isNil(options)
? { ...options, exclude: [...this.excludedFields] }
: {};
const parsingResult = parseQuery(
parsedQuery,
this.adapter.sequelize.getDialect(),
this.extractedRelations,
queryOptions,
this.associations,
);

let filter = parsingResult.query;
if (this.sequelize.getDialect() !== 'postgres') {
filter = arrayPatch(filter, this.originalSchema.fields, this.associations);
}

return { filter, parsingResult };
}

abstract findByIdAndUpdate(
id: any,
document: SingleDocQuery,
Expand Down
24 changes: 2 additions & 22 deletions modules/database/src/adapters/sequelize-adapter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { DatabaseAdapter } from '../DatabaseAdapter';
import { SequelizeSchema } from './SequelizeSchema';
import { checkIfPostgresOptions, tableFetch } from './utils';
import { sqlSchemaConverter } from '../../introspection/sequelize/utils';
import { introspectedSchemaCmsOptionsDefaults } from '../../interfaces';

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

Expand Down Expand Up @@ -145,28 +146,7 @@ export abstract class SequelizeAdapter<
canModify: 'Nothing',
canDelete: false,
},
cms: {
authentication: false,
crudOperations: {
create: {
enabled: false,
authenticated: false,
},
read: {
enabled: false,
authenticated: false,
},
update: {
enabled: false,
authenticated: false,
},
delete: {
enabled: false,
authenticated: false,
},
},
enabled: true,
},
cms: introspectedSchemaCmsOptionsDefaults,
},
});
schema.ownerModule = 'database';
Expand Down
Loading

0 comments on commit c2ee7f5

Please sign in to comment.