How to use decorators for Typeorm in a serverless lambda?

Viewed 2024

I want to use Typeorm for accessing an postgress database on AWS RDS (and a local database with serverless-offline). I have setup a project and it works, but not with decorators (which makes Typeorm less interesting). The docs says

You need to install reflect-metadata shim:

npm install reflect-metadata --save

and import it somewhere in the global place of your app (for example in app.ts):

import "reflect-metadata";

My problem is that in my lambda code, there is no "global place", I am writing a lambda function.

Works:

import { EntitySchema } from "typeorm";

export class Status {
    constructor(
        public id: number,

        public message: string,
    ) { }
}

export const statusSchema = new EntitySchema({
    name: "Status",
    target: Status,
    columns: {
        id: {
            primary: true,
            type: "int",
            generated: true
        },
        message: {
            type: "varchar"
        },
    }
});

but what I want if of course to use decorators like

@Entity()
export class Status {
    constructor(
        @PrimaryGeneratedColumn()
        public id: number,

        @Column()
        public message: string,
    ) { }
}

The error I get when doing queries is

"ColumnTypeUndefinedError: Column type for Function#undefined is not defined and cannot be guessed. Make sure you have turned on an \"emitDecoratorMetadata\": true option in tsconfig.json. Also make sure you have imported \"reflect-metadata\" on top of the main entry file in your application (before any entity imported).If you are using JavaScript instead of TypeScript you must explicitly provide a column type.",
        "at new ColumnTypeUndefinedError (/Users/jonas/repos/spiideo/status-service/src/error/ColumnTypeUndefinedError.ts:9:9)",
        "at /Users/jonas/repos/spiideo/status-service/src/decorator/columns/Column.ts:139:23",
        "at /Users/jonas/repos/spiideo/status-service/.build/src/entity/status.js:16:37",
        "at DecorateConstructor (/Users/jonas/repos/spiideo/status-service/node_modules/reflect-metadata/Reflect.js:541:33)",
        "at Object.decorate (/Users/jonas/repos/spiideo/status-service/node_modules/reflect-metadata/Reflect.js:130:24)",
        "at __decorate (/Users/jonas/repos/spiideo/status-service/.build/src/entity/status.js:8:92)",
        "at Object.<anonymous> (/Users/jonas/repos/spiideo/status-service/src/entity/status.ts:8:20)",
        "at Module._compile (internal/modules/cjs/loader.js:1151:30)",
        "at Object.Module._extensions..js (internal/modules/cjs/loader.js:1171:10)",
        "at Module.load (internal/modules/cjs/loader.js:1000:32)",
        "at Function.Module._load (internal/modules/cjs/loader.js:899:14)",
        "at Module.require (internal/modules/cjs/loader.js:1040:19)",
        "at require (internal/modules/cjs/helpers.js:72:18)",
        "at Object.<anonymous> (/Users/jonas/repos/spiideo/status-service/src/service/status-service.ts:8:1)",
        "at Module._compile (internal/modules/cjs/loader.js:1151:30)",
        "at Object.Module._extensions..js (internal/modules/cjs/loader.js:1171:10)",
        "at Module.load (internal/modules/cjs/loader.js:1000:32)",
        "at Function.Module._load (internal/modules/cjs/loader.js:899:14)",
        "at Module.require (internal/modules/cjs/loader.js:1040:19)",
        "at require (internal/modules/cjs/helpers.js:72:18)",
        "at Object.<anonymous> (/Users/jonas/repos/spiideo/status-service/src/controller/status-controller.ts:7:1)",
        "at Module._compile (internal/modules/cjs/loader.js:1151:30)",
        "at Object.Module._extensions..js (internal/modules/cjs/loader.js:1171:10)",
        "at Module.load (internal/modules/cjs/loader.js:1000:32)",
        "at Function.Module._load (internal/modules/cjs/loader.js:899:14)",
        "at Module.require (internal/modules/cjs/loader.js:1040:19)",
        "at require (internal/modules/cjs/helpers.js:72:18)",
        "at Object.createHandler (/Users/jonas/repos/spiideo/status-service/node_modules/serverless-offline/src/functionHelper.js:215:15)",
        "at handler (/Users/jonas/repos/spiideo/status-service/node_modules/serverless-offline/src/ApiGateway.js:485:40)",
        "at module.exports.internals.Manager.execute (/Users/jonas/repos/spiideo/status-service/node_modules/@hapi/hapi/lib/toolkit.js:41:33)",
        "at Object.internals.handler (/Users/jonas/repos/spiideo/status-service/node_modules/@hapi/hapi/lib/handler.js:46:48)",
        "at exports.execute (/Users/jonas/repos/spiideo/status-service/node_modules/@hapi/hapi/lib/handler.js:31:36)",
        "at Request._lifecycle (/Users/jonas/repos/spiideo/status-service/node_modules/@hapi/hapi/lib/request.js:312:68)",
        "at processTicksAndRejections (internal/process/task_queues.js:97:5)"
1 Answers

Looks like you are trying to add fields/properties within the constructor. Please see https://typeorm.io/#/embedded-entities about how properties that present fields go on the class and not inside the constructor.

Related