Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeORM integration #124

Open
cjfigueiras opened this issue Jan 18, 2019 · 1 comment
Open

TypeORM integration #124

cjfigueiras opened this issue Jan 18, 2019 · 1 comment
Labels

Comments

@cjfigueiras
Copy link

I'm integrating TypeORM and I have encountered some problems, for example:

I created an entity(User)

import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";

@Entity()
export class User {

    @PrimaryGeneratedColumn()
    id: number;

    @Column({
        length: 100
    })
    firstName: string;

    @Column({
        length: 100
    })
    lastName: string;

    @Column({
        length: 100
    })
    email: string;

    @Column()
    password: string;

    @Column()
    createdAt: Date;

    @Column()
    updatedAt: Date;
}

and create user folder with this content:
users.zip

main problem is in users.ts, if I do getCustomRepository(UserRepository) I get

connectionnotfounderror: connection "default" was not found

that is related to getConnection() or createConnection() before that getCustomRepository(UserRepository) for this I create a shared class:

import { Connection, ConnectionManager, getConnectionOptions, createConnection, getConnectionManager } from 'typeorm'
import { SnakeNamingStrategy } from 'typeorm-naming-strategies'
import 'reflect-metadata'

/**
 * Database manager class
 */
export class Database {
    private connectionManager: ConnectionManager

    constructor() {
        this.connectionManager = getConnectionManager()
    }

    public async getConnection(): Promise<Connection> {
        console.log('getConnection');
        const CONNECTION_NAME = `default`

        let connection: Connection

        if (this.connectionManager.has(CONNECTION_NAME)) {
            connection = await this.connectionManager.get(CONNECTION_NAME)

            if (!connection.isConnected) {
                connection = await connection.connect()
            }
        }
        else {

            const connectionOptions = await getConnectionOptions();
            Object.assign(connectionOptions, { namingStrategy: new SnakeNamingStrategy() });
            
            connection = await createConnection(connectionOptions)
        }

        return connection
    }
}

function getConnection() here is async so it returns a promise and dont know how manage this on users.ts for export getUser endpoint.

const db: Database = new Database();

export const getUser: ApiHandler = db.getConnection().then((connection: Connection): ApiHandler => {
const repo: UserRepository = connection.getCustomRepository(UserRepository);
const service: UsersService = new UsersService(repo);
const controller: UsersController = new UsersController(service);
return controller.getUser;
});

So I get

Type 'Promise(ApiHandler)' is not assignable to type 'ApiHandler'

@balassy
Copy link
Owner

balassy commented Jan 24, 2019

If I understand correctly the problem is that your logic is asynchronous, but the most external layer expects it to be synchronous. The db.getConnection() function in users.ts returns a Promise, so your whole lambda handler will return a Promise, but the ApiHandler type cannot handle it.

At the time I created this project AWS Lambda did not support promises directly, but thankfully now it does, if you use the Node 8.10+ runtime. Basically you have two options:

  1. Go with the callbacks. This is what you see in this project. In this case you have to wrap your underlying asynchronous logic to callbacks.
  2. Change the handler syntax to this:
exports.myHandler = async function(event, context) {
  // Do something, then return information to the caller.  
}

Unfortunately at the moment there is no example for this in the codebase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants