From 5a69b138b57c87af39830a18c10f43a3e7197558 Mon Sep 17 00:00:00 2001 From: Mor Hagbi Date: Sun, 14 Jan 2024 18:51:28 +0200 Subject: [PATCH] fix(gRPC): fix proto file methods names --- plugins/transport-grpc/package-lock.json | 18 ++-- plugins/transport-grpc/package.json | 4 +- .../src/core/create-base-grpc-controller.ts | 26 ++++-- .../src/core/create-grpc-proto-file.ts | 93 ++++++++++--------- .../create-method-id-action-entity-map.ts | 23 ++--- 5 files changed, 88 insertions(+), 76 deletions(-) diff --git a/plugins/transport-grpc/package-lock.json b/plugins/transport-grpc/package-lock.json index 784f424d..13d4e83c 100644 --- a/plugins/transport-grpc/package-lock.json +++ b/plugins/transport-grpc/package-lock.json @@ -1,12 +1,12 @@ { "name": "@amplication/plugin-transport-grpc", - "version": "0.0.6", + "version": "0.0.7", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@amplication/plugin-transport-grpc", - "version": "0.0.6", + "version": "0.0.7", "license": "Apache-2.0", "dependencies": { "@amplication/code-gen-utils": "^0.0.6", @@ -15,7 +15,7 @@ "protobuf-dsl": "^0.0.1" }, "devDependencies": { - "@amplication/code-gen-types": "^2.0.17", + "@amplication/code-gen-types": "^2.0.22", "@amplication/code-gen-utils": "^0.0.7", "@babel/parser": "^7.23.0", "@babel/types": "^7.23.0", @@ -44,9 +44,9 @@ } }, "node_modules/@amplication/code-gen-types": { - "version": "2.0.17", - "resolved": "https://registry.npmjs.org/@amplication/code-gen-types/-/code-gen-types-2.0.17.tgz", - "integrity": "sha512-w2l4y84r9sbXytNkO5FN2BEjNL1iaey+oOizeqdKWPSBBHczU2c1o6cNBRDovrdGsqN5HuBGntkVYBqQUghYJw==", + "version": "2.0.22", + "resolved": "https://registry.npmjs.org/@amplication/code-gen-types/-/code-gen-types-2.0.22.tgz", + "integrity": "sha512-FUmm3Y34YKr/xAWb2GMby78Zib7k5avWO1h2uZfgshkZqKwVrCghTk5TkPOYwEWCd3T2L4G9MKd9UVciF0/s6g==", "dev": true, "dependencies": { "ast-types": "^0.14.2", @@ -7888,9 +7888,9 @@ "dev": true }, "@amplication/code-gen-types": { - "version": "2.0.17", - "resolved": "https://registry.npmjs.org/@amplication/code-gen-types/-/code-gen-types-2.0.17.tgz", - "integrity": "sha512-w2l4y84r9sbXytNkO5FN2BEjNL1iaey+oOizeqdKWPSBBHczU2c1o6cNBRDovrdGsqN5HuBGntkVYBqQUghYJw==", + "version": "2.0.22", + "resolved": "https://registry.npmjs.org/@amplication/code-gen-types/-/code-gen-types-2.0.22.tgz", + "integrity": "sha512-FUmm3Y34YKr/xAWb2GMby78Zib7k5avWO1h2uZfgshkZqKwVrCghTk5TkPOYwEWCd3T2L4G9MKd9UVciF0/s6g==", "dev": true, "requires": { "ast-types": "^0.14.2", diff --git a/plugins/transport-grpc/package.json b/plugins/transport-grpc/package.json index 07fdd99b..eae999f0 100644 --- a/plugins/transport-grpc/package.json +++ b/plugins/transport-grpc/package.json @@ -1,6 +1,6 @@ { "name": "@amplication/plugin-transport-grpc", - "version": "0.0.6", + "version": "0.0.7", "description": "add gRPC endpoints to your generated services", "main": "dist/index.js", "nx": {}, @@ -12,7 +12,7 @@ "author": "Mor Hagbi", "license": "Apache-2.0", "devDependencies": { - "@amplication/code-gen-types": "^2.0.17", + "@amplication/code-gen-types": "^2.0.22", "@amplication/code-gen-utils": "^0.0.7", "@babel/parser": "^7.23.0", "@babel/types": "^7.23.0", diff --git a/plugins/transport-grpc/src/core/create-base-grpc-controller.ts b/plugins/transport-grpc/src/core/create-base-grpc-controller.ts index f8fbbdfa..d0aa5495 100644 --- a/plugins/transport-grpc/src/core/create-base-grpc-controller.ts +++ b/plugins/transport-grpc/src/core/create-base-grpc-controller.ts @@ -15,32 +15,38 @@ import { templatesPath } from "../constants"; const controllerBaseGrpcPath = join( templatesPath, - "controller.grpc.base.template.ts" + "controller.grpc.base.template.ts", ); export async function createGrpcControllerBase( context: DsgContext, - eventParams: CreateEntityGrpcControllerBaseParams + eventParams: CreateEntityGrpcControllerBaseParams, ): Promise { try { const { controllerBaseId, templateMapping, entity } = eventParams; + const { entityActionsMap } = context; + const entityActions = entityActionsMap[entity.name]; const controllerBaseGrpcTemplate = await readFile(controllerBaseGrpcPath); interpolate(controllerBaseGrpcTemplate, templateMapping); const classDeclaration = getClassDeclarationById( controllerBaseGrpcTemplate, - controllerBaseId + controllerBaseId, ); - controllerMethodsIdsActionPairs(templateMapping, entity).forEach( - ({ methodId, entity, methodName }) => { - const classMethod = getClassMethodByIdName(classDeclaration, methodId); + controllerMethodsIdsActionPairs( + templateMapping, + entity, + entityActions, + ).forEach(({ methodId, entity, methodName }) => { + const classMethod = getClassMethodByIdName(classDeclaration, methodId); + if (methodName) { classMethod?.decorators?.push( - buildGrpcMethodDecorator(entity.name, methodName) + buildGrpcMethodDecorator(entity.name, methodName), ); } - ); + }); eventParams.template = controllerBaseGrpcTemplate; } catch (error) { console.error(error); @@ -48,12 +54,12 @@ export async function createGrpcControllerBase( } export function buildGrpcMethodDecorator( entityName: string, - methodName: string + methodName: string, ): namedTypes.Decorator { return builders.decorator( builders.callExpression(builders.identifier("GrpcMethod"), [ builders.stringLiteral(`${entityName}Service`), builders.stringLiteral(methodName), - ]) + ]), ); } diff --git a/plugins/transport-grpc/src/core/create-grpc-proto-file.ts b/plugins/transport-grpc/src/core/create-grpc-proto-file.ts index 4225efc5..538a188a 100644 --- a/plugins/transport-grpc/src/core/create-grpc-proto-file.ts +++ b/plugins/transport-grpc/src/core/create-grpc-proto-file.ts @@ -30,10 +30,11 @@ import { pascalCase } from "pascal-case"; export async function createGrpcProtoFile( context: DsgContext, eventParams: CreateEntityGrpcControllerBaseParams, - relatedEntities: EntityField[] + relatedEntities: EntityField[], ): Promise { const { entityName, templateMapping, entity } = eventParams; - const { serverDirectories } = context; + const { serverDirectories, entityActionsMap } = context; + const entityActions = entityActionsMap[entity.name]; try { const methods: Method[] = []; @@ -46,29 +47,33 @@ export async function createGrpcProtoFile( const currentField = createProtobufSchemaFieldsHandler[field.dataType]( field.name, countField, - field + field, ); if (!currentField) return; fields.push(currentField); countField = countField + 1; }); - controllerMethodsIdsActionPairs(templateMapping, entity).forEach( - ({ methodName, inputObjectName, outputObjectName }) => { + controllerMethodsIdsActionPairs( + templateMapping, + entity, + entityActions, + ).forEach(({ methodName, inputObjectName, outputObjectName }) => { + if (methodName) { const currentMethod = ProtobufSchemaDSL.createMethod( methodName, inputObjectName, - outputObjectName + outputObjectName, ); methods.push(currentMethod); } - ); + }); methodMessages(entityName).forEach(({ name, enumMessageType }) => { const currentMessage = createProtobufMessagesHandler[enumMessageType]( name, entityName, - fields + fields, ); if (!currentMessage) return; messages.push(currentMessage); @@ -104,17 +109,17 @@ export async function createGrpcProtoFile( ) return; messages.push(currentMessage); - } + }, ); controllerToManyMethodsIdsActionPairs( relatedEntity, relatedField.name, - pascalCase(entityName) + pascalCase(entityName), ).forEach(({ methodName, inputObjectName, outputObjectName }) => { const currentMethod = ProtobufSchemaDSL.createMethod( methodName, inputObjectName, - outputObjectName + outputObjectName, ); methods.push(currentMethod); @@ -124,7 +129,7 @@ export async function createGrpcProtoFile( const protobufSchema = ProtobufSchemaDSL.createSchema( entityName, { name: `${pascalCase(entityName)}Service`, methods: methods }, - messages + messages, ); const file = await ProtobufSchemaDSL.print(protobufSchema); @@ -146,14 +151,14 @@ export async function createGrpcProtoFile( export type CreateSchemaFieldHandler = ( fieldName: string, countField: number, - field: EntityField + field: EntityField, ) => ScalarField | ObjectField | null; export type CreateMessageHandler = ( messageName: string, entityName: string, fields: Array, - relatedEntity?: Entity + relatedEntity?: Entity, ) => Message | null; export const createProtobufMessagesHandler: { @@ -162,50 +167,50 @@ export const createProtobufMessagesHandler: { [EnumMessageType.Empty]: ( messageName: string, entityName, - fields: Array + fields: Array, ) => createMessage(messageName, []), [EnumMessageType.Create]: ( messageName: string, entityName, - fields: Array + fields: Array, ) => createMessage(messageName, fields), [EnumMessageType.EntityObject]: ( messageName: string, entityName, - fields: Array + fields: Array, ) => createMessage(messageName, fields), [EnumMessageType.EntityUpdateInput]: ( messageName: string, entityName, - fields: Array + fields: Array, ) => createMessage(messageName, fields), [EnumMessageType.EntityWhereInput]: ( messageName: string, entityName, - fields: Array + fields: Array, ) => createMessage(messageName, fields), [EnumMessageType.RelatedEntityObject]: ( messageName: string, entityName, - fields: Array + fields: Array, ) => createMessage(messageName, fields), [EnumMessageType.RelatedEntityWhereInputObject]: ( messageName: string, entityName, - fields: Array + fields: Array, ) => createMessage(messageName, fields), [EnumMessageType.CombineWhereUniqInput]: ( messageName: string, entityName, fields: Array, - relatedEntity?: Entity + relatedEntity?: Entity, ) => { if (!relatedEntity) return null; return createMessage(messageName, [ @@ -213,13 +218,13 @@ export const createProtobufMessagesHandler: { `${entityName}WhereUniqueInput`, `${pascalCase(entityName)}WhereUniqueInput`, 1, - false + false, ), createObjectField( `${relatedEntity.name.toLowerCase()}WhereUniqueInput`, `${relatedEntity.name}WhereUniqueInput`, 2, - false + false, ), ]); }, @@ -231,52 +236,52 @@ export const createProtobufSchemaFieldsHandler: { [EnumDataType.SingleLineText]: ( fieldName: string, countField: number, - field: EntityField + field: EntityField, ) => createScalarField(fieldName, "string", countField, false), [EnumDataType.MultiLineText]: ( fieldName: string, countField: number, - field: EntityField + field: EntityField, ) => createScalarField(fieldName, "string", countField, false), [EnumDataType.Email]: ( fieldName: string, countField: number, - field: EntityField + field: EntityField, ) => createScalarField(fieldName, "string", countField, false), [EnumDataType.WholeNumber]: ( fieldName: string, countField: number, - field: EntityField + field: EntityField, ) => createScalarField(fieldName, "int32", countField, false), [EnumDataType.DateTime]: ( fieldName: string, countField: number, - field: EntityField + field: EntityField, ) => createScalarField(fieldName, "string", countField, false), [EnumDataType.DecimalNumber]: ( fieldName: string, countField: number, - field: EntityField + field: EntityField, ) => createScalarField(fieldName, "int32", countField, false), [EnumDataType.Boolean]: ( fieldName: string, countField: number, - field: EntityField + field: EntityField, ) => createScalarField(fieldName, "bool", countField, false), [EnumDataType.GeographicLocation]: ( fieldName: string, countField: number, - field: EntityField + field: EntityField, ) => createScalarField(fieldName, "string", countField, false), [EnumDataType.Json]: ( fieldName: string, countField: number, - field: EntityField + field: EntityField, ) => createScalarField(fieldName, "string", countField, false), [EnumDataType.Lookup]: ( fieldName: string, countField: number, - field: EntityField + field: EntityField, ) => { const { properties } = field; const { @@ -294,7 +299,7 @@ export const createProtobufSchemaFieldsHandler: { fieldName, relatedEntity.name, countField, - !isOneToOneWithoutForeignKey + !isOneToOneWithoutForeignKey, ); } @@ -303,17 +308,17 @@ export const createProtobufSchemaFieldsHandler: { [EnumDataType.MultiSelectOptionSet]: ( fieldName: string, countField: number, - field: EntityField + field: EntityField, ) => createScalarField(fieldName, "string", countField, true), [EnumDataType.OptionSet]: ( fieldName: string, countField: number, - field: EntityField + field: EntityField, ) => createScalarField(fieldName, "string", countField, true), [EnumDataType.Id]: ( fieldName: string, countField: number, - field: EntityField + field: EntityField, ) => { const { properties } = field; const idType = (properties as types.Id)?.idType ?? "CUID"; @@ -322,34 +327,34 @@ export const createProtobufSchemaFieldsHandler: { fieldName, idTypeToProtobufScalarType[idType], countField, - false + false, ); }, [EnumDataType.CreatedAt]: ( fieldName: string, countField: number, - field: EntityField + field: EntityField, ) => createScalarField(fieldName, "string", countField, false), [EnumDataType.UpdatedAt]: ( fieldName: string, countField: number, - field: EntityField + field: EntityField, ) => createScalarField(fieldName, "string", countField, false), [EnumDataType.Roles]: ( fieldName: string, countField: number, - field: EntityField + field: EntityField, ) => createScalarField(fieldName, "string", countField, true), [EnumDataType.Username]: ( fieldName: string, countField: number, - field: EntityField + field: EntityField, ) => createScalarField(fieldName, "string", countField, false), [EnumDataType.Password]: ( fieldName: string, countField: number, - field: EntityField + field: EntityField, ) => createScalarField(fieldName, "string", countField, false), }; diff --git a/plugins/transport-grpc/src/core/create-method-id-action-entity-map.ts b/plugins/transport-grpc/src/core/create-method-id-action-entity-map.ts index 93f4fd9c..3b7901bb 100644 --- a/plugins/transport-grpc/src/core/create-method-id-action-entity-map.ts +++ b/plugins/transport-grpc/src/core/create-method-id-action-entity-map.ts @@ -1,11 +1,11 @@ -import { Entity } from "@amplication/code-gen-types"; +import { Entity, entityActions } from "@amplication/code-gen-types"; import { namedTypes, builders } from "ast-types"; import { pascalCase } from "pascal-case"; type MethodsIdsActionEntityTriplet = { methodId: namedTypes.Identifier; entity: Entity; - methodName: string; + methodName?: string; inputObjectName: string; outputObjectName: string; }; @@ -39,14 +39,15 @@ export enum EnumMessageType { export const controllerMethodsIdsActionPairs = ( templateMapping: { [key: string]: namedTypes.Identifier }, - entity: Entity + entity: Entity, + entityActions: entityActions, ): MethodsIdsActionEntityTriplet[] => [ { methodId: templateMapping[ "CREATE_ENTITY_FUNCTION" ] as namedTypes.Identifier, entity: entity, - methodName: "create", + methodName: entityActions.entityDefaultActions.Create?.name, inputObjectName: `${pascalCase(entity.name)}CreateInput`, outputObjectName: pascalCase(entity.name), }, @@ -55,7 +56,7 @@ export const controllerMethodsIdsActionPairs = ( "FIND_MANY_ENTITY_FUNCTION" ] as namedTypes.Identifier, entity: entity, - methodName: "findMany", + methodName: entityActions.entityDefaultActions.Find?.name, inputObjectName: "findManyParams", outputObjectName: `stream ${pascalCase(entity.name)}`, }, @@ -64,7 +65,7 @@ export const controllerMethodsIdsActionPairs = ( "FIND_ONE_ENTITY_FUNCTION" ] as namedTypes.Identifier, entity: entity, - methodName: "findOne", + methodName: entityActions.entityDefaultActions.Read?.name, inputObjectName: `${pascalCase(entity.name)}WhereUniqueInput`, outputObjectName: pascalCase(entity.name), }, @@ -73,7 +74,7 @@ export const controllerMethodsIdsActionPairs = ( "UPDATE_ENTITY_FUNCTION" ] as namedTypes.Identifier, entity: entity, - methodName: "update", + methodName: entityActions.entityDefaultActions.Update?.name, inputObjectName: `${pascalCase(entity.name)}UpdateInput`, outputObjectName: pascalCase(entity.name), }, @@ -82,7 +83,7 @@ export const controllerMethodsIdsActionPairs = ( "DELETE_ENTITY_FUNCTION" ] as namedTypes.Identifier, entity: entity, - methodName: "delete", + methodName: entityActions.entityDefaultActions.Delete?.name, inputObjectName: `${pascalCase(entity.name)}WhereUniqueInput`, outputObjectName: pascalCase(entity.name), }, @@ -112,7 +113,7 @@ export const methodMessages = (entityName: string): methodMessage[] => [ ]; export const manyRelationMethodMessages = ( - entityName: string + entityName: string, ): methodMessage[] => [ { name: pascalCase(entityName), @@ -131,7 +132,7 @@ export const manyRelationMethodMessages = ( export const controllerToManyMethodsIdsActionPairs = ( relatedEntity: Entity, fieldName: string, - entityName?: string + entityName?: string, ): MethodsIdsActionEntity[] => [ { methodName: `findMany${pascalCase(fieldName)}`, @@ -157,7 +158,7 @@ export const controllerToManyMethodsIdsActionPairs = ( export const controllerToManyIdsActionPairs = ( toManyMapping: { [key: string]: namedTypes.Identifier }, - fieldName: string + fieldName: string, ): ControllersIdsActionEntity[] => [ { methodId: toManyMapping["FIND_MANY"],