I need a transformation method attached to my Schema type. How can I accomplished with without duplicating code? If I remove the class methodasGrpcProgram
I get the following error:
src/programs/programs.controller.ts:129:30 - error TS2339: Property 'asGrpcProgram' does not exist on type 'Program'.
If I remove the function expression assigned to ProgramSchema.methods.asGrpcProgram
I get the following error when the method is invoked at runtime:
ERROR [RpcExceptionsHandler] program.asGrpcProgram is not a function
import { grpc } from 'lis-protobuf';
import { Document } from 'mongoose';
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
@Schema()
export class Program extends Document {
@Prop({ required: true })
name: string;
@Prop()
externalId: string;
@Prop([String])
skillIds: string[];
public asGrpcProgram(): grpc.Program {
const { id, name, externalId, skillIds } = this;
return { id, name, externalId, skillIds };
}
}
export const ProgramSchema = SchemaFactory.createForClass(Program);
ProgramSchema.methods.asGrpcProgram = function (): grpc.Program {
const { id, name, externalId, skillIds } = this;
return { id, name, externalId, skillIds };
};