diff --git a/modules/database/src/adapters/sequelize-adapter/SequelizeSchema.ts b/modules/database/src/adapters/sequelize-adapter/SequelizeSchema.ts index 05d715734..dc3290fcb 100644 --- a/modules/database/src/adapters/sequelize-adapter/SequelizeSchema.ts +++ b/modules/database/src/adapters/sequelize-adapter/SequelizeSchema.ts @@ -4,7 +4,6 @@ import { FindAttributeOptions, FindOptions, ModelCtor, - Order, Sequelize, } from 'sequelize'; import { @@ -179,7 +178,7 @@ export class SequelizeSchema implements SchemaAdapter> { skip?: number, limit?: number, select?: string, - sort?: Order, + sort?: string, populate?: string[], relations?: Indexable, ): Promise { @@ -202,9 +201,10 @@ export class SequelizeSchema implements SchemaAdapter> { if (!isNil(select) && select !== '') { options.attributes = this.parseSelect(select); } - if (!isNil(sort)) { - options.order = sort; - } + // if (!isNil(sort)) { + // TODO: parse sort str + // options.order = new Literal(sort); + // } const documents = await this.model.findAll(options); diff --git a/modules/database/src/controllers/cms/utils.ts b/modules/database/src/controllers/cms/utils.ts index 629824b9b..73c5c7b3f 100644 --- a/modules/database/src/controllers/cms/utils.ts +++ b/modules/database/src/controllers/cms/utils.ts @@ -84,17 +84,6 @@ export function getOps( actualSchema.modelOptions.conduit!.cms.crudOperations.read.enabled; if (readIsEnabled) { let route = new RouteBuilder() - .path(`/${schemaName}/:id`) - .method(ConduitRouteActions.GET) - .urlParams({ - id: { type: TYPE.String, required: true }, - }) - .cacheControl(authenticatedRead ? 'private, max-age=10' : 'public, max-age=10') - .return(`${schemaName}`, actualSchema.fields) - .handler(handlers.getDocumentById.bind(handlers)); - if (authenticatedRead) route.middleware('authMiddleware'); - routesArray.push(route.build()); - route = new RouteBuilder() .path(`/${schemaName}`) .method(ConduitRouteActions.GET) .queryParams({ @@ -110,6 +99,17 @@ export function getOps( .handler(handlers.getDocuments.bind(handlers)); if (authenticatedRead) route.middleware('authMiddleware'); routesArray.push(route.build()); + route = new RouteBuilder() + .path(`/${schemaName}/:id`) + .method(ConduitRouteActions.GET) + .urlParams({ + id: { type: TYPE.String, required: true }, + }) + .cacheControl(authenticatedRead ? 'private, max-age=10' : 'public, max-age=10') + .return(`${schemaName}`, actualSchema.fields) + .handler(handlers.getDocumentById.bind(handlers)); + if (authenticatedRead) route.middleware('authMiddleware'); + routesArray.push(route.build()); } const authenticatedCreate = actualSchema.modelOptions.conduit!.cms.crudOperations.create.authenticated; diff --git a/modules/database/src/controllers/customEndpoints/customEndpoint.controller.ts b/modules/database/src/controllers/customEndpoints/customEndpoint.controller.ts index 606959443..027136527 100644 --- a/modules/database/src/controllers/customEndpoints/customEndpoint.controller.ts +++ b/modules/database/src/controllers/customEndpoints/customEndpoint.controller.ts @@ -15,7 +15,7 @@ export class CustomEndpointController { private readonly grpcSdk: ConduitGrpcSdk, private readonly database: DatabaseAdapter, ) { - this.handler = new CustomEndpointHandler(this.grpcSdk); + this.handler = new CustomEndpointHandler(database); this.refreshRoutes(); this.initializeState(); } diff --git a/modules/database/src/handlers/CustomEndpoints/customEndpoint.handler.ts b/modules/database/src/handlers/CustomEndpoints/customEndpoint.handler.ts index b281b6e18..d34802c3e 100644 --- a/modules/database/src/handlers/CustomEndpoints/customEndpoint.handler.ts +++ b/modules/database/src/handlers/CustomEndpoints/customEndpoint.handler.ts @@ -1,4 +1,4 @@ -import ConduitGrpcSdk, { +import { ParsedRouterRequest, UnparsedRouterResponse, GrpcError, @@ -6,14 +6,19 @@ import ConduitGrpcSdk, { } from '@conduitplatform/grpc-sdk'; import { constructAssignment, constructQuery } from './utils'; import { status } from '@grpc/grpc-js'; -import { ICustomEndpoint, PopulatedCustomEndpoint, Sort } from '../../interfaces'; +import { ICustomEndpoint, PopulatedCustomEndpoint } from '../../interfaces'; import { OperationsEnum } from '../../admin/customEndpoints/customEndpoints.admin'; import { isNil } from 'lodash'; +import { DatabaseAdapter } from '../../adapters/DatabaseAdapter'; +import { MongooseSchema } from '../../adapters/mongoose-adapter/MongooseSchema'; +import { SequelizeSchema } from '../../adapters/sequelize-adapter/SequelizeSchema'; export class CustomEndpointHandler { private static routeControllers: Indexable = {}; - constructor(private readonly grpcSdk: ConduitGrpcSdk) {} + constructor( + private readonly database: DatabaseAdapter, + ) {} static addNewCustomOperationControl( endpoint: ICustomEndpoint | PopulatedCustomEndpoint, @@ -106,72 +111,57 @@ export class CustomEndpointHandler { ); } - let sortObj: Sort | null = null; + let sort: string | undefined = undefined; if (endpoint.sorted && params.sort && params.sort.length > 0) { - const sort = params.sort; - sortObj = {}; - sort.forEach((sortVal: string) => { - sortVal = sortVal.trim(); - if (sortVal.indexOf('-') !== -1) { - (sortObj as Indexable)[sortVal.substr(1)] = -1; - } else { - (sortObj as Indexable)[sortVal] = 1; - } - }); + sort = params.sort; } const createObj = this.parseCreateQuery(createString); let promise; if (endpoint.operation === OperationsEnum.GET) { if (endpoint.paginated) { - const documentsPromise = this.grpcSdk.databaseProvider!.findMany( - endpoint.selectedSchemaName, - searchQuery, - undefined, - params['skip'], - params['limit'], - sortObj as Sort, - params['populate'], - ); - const countPromise = this.grpcSdk.databaseProvider!.countDocuments( - endpoint.selectedSchemaName, - searchQuery, - ); + const documentsPromise = this.database + .getSchemaModel(endpoint.selectedSchemaName) + .model.findMany( + searchQuery, + params['skip'], + params['limit'], + undefined, + sort, + params['populate'], + ); + const countPromise = this.database + .getSchemaModel(endpoint.selectedSchemaName) + .model.countDocuments(searchQuery); promise = Promise.all([documentsPromise, countPromise]); } else { - promise = this.grpcSdk.databaseProvider!.findMany( - endpoint.selectedSchemaName, - searchQuery, - undefined, - undefined, - undefined, - sortObj as Sort, - params['populate'], - ); + promise = this.database + .getSchemaModel(endpoint.selectedSchemaName) + .model.findMany( + searchQuery, + undefined, + undefined, + undefined, + sort, + params['populate'], + ); } } else if (endpoint.operation === OperationsEnum.POST) { - promise = this.grpcSdk.databaseProvider!.create( - endpoint.selectedSchemaName, - createObj, - ); + promise = this.database + .getSchemaModel(endpoint.selectedSchemaName) + .model.create(createObj); } else if (endpoint.operation === OperationsEnum.PUT) { - promise = this.grpcSdk.databaseProvider!.updateMany( - endpoint.selectedSchemaName, - searchQuery, - createObj, - ); + promise = this.database + .getSchemaModel(endpoint.selectedSchemaName) + .model.updateMany(searchQuery, createObj); } else if (endpoint.operation === OperationsEnum.DELETE) { - promise = this.grpcSdk.databaseProvider!.deleteMany( - endpoint.selectedSchemaName, - searchQuery, - ); + promise = this.database + .getSchemaModel(endpoint.selectedSchemaName) + .model.deleteMany(searchQuery); } else if (endpoint.operation === OperationsEnum.PATCH) { - promise = this.grpcSdk.databaseProvider!.updateMany( - endpoint.selectedSchemaName, - searchQuery, - createObj, - true, - ); + promise = this.database + .getSchemaModel(endpoint.selectedSchemaName) + .model.updateMany(searchQuery, createObj, true); } else { process.exit(-1); } diff --git a/modules/database/src/handlers/cms/handler.ts b/modules/database/src/handlers/cms/handler.ts index c3c78a226..8856e4671 100644 --- a/modules/database/src/handlers/cms/handler.ts +++ b/modules/database/src/handlers/cms/handler.ts @@ -19,7 +19,7 @@ export class CmsHandlers { async getDocuments(call: ParsedRouterRequest): Promise { const { skip, limit, sort, populate } = call.request.params; - const res = await findSchema(call, this.database).catch((e: Error) => { + const schemaName = await findSchema(call, this.database).catch((e: Error) => { throw e; }); let skipNumber = 0, @@ -32,10 +32,10 @@ export class CmsHandlers { } const documentsPromise = this.database - .getSchemaModel(res.schemaName) + .getSchemaModel(schemaName) .model.findMany({}, skipNumber, limitNumber, undefined, sort, populate); const countPromise = this.database - .getSchemaModel(res.schemaName) + .getSchemaModel(schemaName) .model.countDocuments({}); const [documents, count] = await Promise.all([documentsPromise, countPromise]); @@ -43,12 +43,16 @@ export class CmsHandlers { } async getDocumentById(call: ParsedRouterRequest): Promise { - const res = await findSchema(call, this.database).catch((e: Error) => { + const schemaName = await findSchema(call, this.database).catch((e: Error) => { throw e; }); const document: Doc | undefined = await this.database - .getSchemaModel(res.schemaName) - .model?.findOne({ _id: res.id }, undefined, res.populate); + .getSchemaModel(schemaName) + .model?.findOne( + { _id: call.request.params.id }, + undefined, + call.request.params.populate, + ); if (!document) { throw new GrpcError(status.NOT_FOUND, 'Document does not exist'); } @@ -56,20 +60,20 @@ export class CmsHandlers { } async createDocument(call: ParsedRouterRequest): Promise { - const res = await findSchema(call, this.database).catch((e: Error) => { + const schemaName = await findSchema(call, this.database).catch((e: Error) => { throw e; }); - return this.database.getSchemaModel(res.schemaName).model!.create(res.inputDocument); + return this.database.getSchemaModel(schemaName).model!.create(call.request.params); } async createManyDocuments(call: ParsedRouterRequest): Promise { const inputDocuments = call.request.params.docs; - const res = await findSchema(call, this.database).catch((e: Error) => { + const schemaName = await findSchema(call, this.database).catch((e: Error) => { throw e; }); const newDocuments = await this.database - .getSchemaModel(res.schemaName) + .getSchemaModel(schemaName) .model?.createMany(inputDocuments) .catch((e: Error) => { throw new GrpcError(status.INTERNAL, e.message); @@ -79,21 +83,24 @@ export class CmsHandlers { } async updateDocument(call: ParsedRouterRequest): Promise { - const res = await findSchema(call, this.database).catch((e: Error) => { + const schemaName = await findSchema(call, this.database).catch((e: Error) => { + throw e; + }); + return getUpdatedDocument( + schemaName, + call.request.params, + this.database, + false, + ).catch((e: Error) => { throw e; }); - return getUpdatedDocument(res.schemaName, res.params, this.database, false).catch( - (e: Error) => { - throw e; - }, - ); } async patchDocument(call: ParsedRouterRequest): Promise { - const res = await findSchema(call, this.database).catch((e: Error) => { + const schemaName = await findSchema(call, this.database).catch((e: Error) => { throw e; }); - return getUpdatedDocument(res.schemaName, res.params, this.database, true).catch( + return getUpdatedDocument(schemaName, call.request.params, this.database, true).catch( (e: Error) => { throw e; }, @@ -101,13 +108,13 @@ export class CmsHandlers { } async updateManyDocuments(call: ParsedRouterRequest): Promise { - const res = await findSchema(call, this.database).catch((e: Error) => { + const schemaName = await findSchema(call, this.database).catch((e: Error) => { throw e; }); const updatedDocuments = await getUpdatedDocuments( - res.schemaName, - res.params, + schemaName, + call.request.params, this.database, false, ).catch((e: Error) => { @@ -117,12 +124,12 @@ export class CmsHandlers { } async patchManyDocuments(call: ParsedRouterRequest): Promise { - const res = await findSchema(call, this.database).catch((e: Error) => { + const schemaName = await findSchema(call, this.database).catch((e: Error) => { throw e; }); const updatedDocuments = await getUpdatedDocuments( - res.schemaName, - res.params, + schemaName, + call.request.params, this.database, true, ).catch((e: Error) => { @@ -133,12 +140,12 @@ export class CmsHandlers { } async deleteDocument(call: ParsedRouterRequest): Promise { - const res = await findSchema(call, this.database).catch((e: Error) => { + const schemaName = await findSchema(call, this.database).catch((e: Error) => { throw e; }); await this.database - .getSchemaModel(res.schemaName) - .model?.deleteOne({ _id: res.id }) + .getSchemaModel(schemaName) + .model?.deleteOne({ _id: call.request.params.id }) .catch((e: Error) => { throw new GrpcError(status.INTERNAL, e.message); }); diff --git a/modules/database/src/handlers/cms/utils.ts b/modules/database/src/handlers/cms/utils.ts index 80bf4d55b..0e9c673b7 100644 --- a/modules/database/src/handlers/cms/utils.ts +++ b/modules/database/src/handlers/cms/utils.ts @@ -9,8 +9,6 @@ export async function findSchema( call: ParsedRouterRequest, database: DatabaseAdapter, ) { - const { params, populate, inputDocument } = call.request.params; - const id = params.id; const schemaName = call.request.path.split('/')[2]; const schema = await database .getSchemaModel('_DeclaredSchema') @@ -21,7 +19,7 @@ export async function findSchema( if (!schema) { throw new GrpcError(status.NOT_FOUND, 'Schema does not exist'); } - return { params, schemaName, id, populate, inputDocument }; + return schemaName; } export async function getUpdatedDocument( diff --git a/modules/database/src/interfaces/Sort.ts b/modules/database/src/interfaces/Sort.ts deleted file mode 100644 index 36e10c21b..000000000 --- a/modules/database/src/interfaces/Sort.ts +++ /dev/null @@ -1 +0,0 @@ -export type Sort = string[] | { [p: string]: number }; diff --git a/modules/database/src/interfaces/index.ts b/modules/database/src/interfaces/index.ts index d3d01648b..1e3bda1b5 100644 --- a/modules/database/src/interfaces/index.ts +++ b/modules/database/src/interfaces/index.ts @@ -5,5 +5,4 @@ export * from './CustomEndpointsQuery'; export * from './DeclaredSchema'; export * from './DeclaredSchemaExtension'; export * from './SchemaAdapter'; -export * from './Sort'; export * from './TypeHacks';