TypeORM beta Contributors are welcome

Feature

Currently, @tsed/typeorm allows you:

  • Configure one or more TypeORM connections via the @ServerSettings configuration. All databases will be initialized when the server starts during the server's OnInit phase.
  • Use the Entity TypeORM as Model for Controllers, AJV Validation and Swagger.

Installation

To begin, install the TypeORM module for TS.ED:

npm install --save @tsed/typeorm

Then import @tsed/typeorm in your ServerLoader:

import {ServerLoader, ServerSettings} from "@tsed/common";
import "@tsed/typeorm"; // import typeorm ts.ed module

@ServerSettings({
   typeorm: [
     {
       name: 'default',
       type: 'postgres',
       ...,

        entities: [
          `${__dirname}/entity/*{.ts,.js}`
        ],
        migrations: [
         `${__dirname}/migrations/*{.ts,.js}`
        ],
        subscribers: [
         `${__dirname}/subscriber/*{.ts,.js}`
        ]
     },
     {
        name: 'mongo',
        type: 'mongodb',
        ...
     }
   ]
})
export class Server extends ServerLoader {

}

TypeORMService

TypeORMService let you to retrieve an instance of TypeORM Connection.

import {Service, AfterRoutesInit} from "@tsed/common";
import {TypeORMService} from "@tsed/typeorm";
import {Connection} from "typeorm";

@Service()
export class UsersService implement AfterRoutesInit {
    private connection: Connection;
    constructor(private typeORMService: TypeORMService) {

    }

    $afterRoutesInit() {
        this.connection = this.typeORMService.get("db1");
    }

    async create(user: User): Promise<User> {

        // do something
        ...
        // Then save
        await this.connection.manager.save(user);
        console.log("Saved a new user with id: " + user.id);

        return user;
    }

    async find(): Promise<User[]> {
        const users = await this.connection.manager.find(User);
        console.log("Loaded users: ", users);

        return users;
    }

}

For more information about TypeORM look his documentation here;

Use Entity TypeORM with Controller

To begin, we need to define an Entity TypeORM like this and use Ts.ED Decorator to define the JSON Schema.

import {Property, MaxLength, Required} from "@tsed/common";
import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";

@Entity()
export class User {

    @PrimaryGeneratedColumn()
    @Property()
    id: number;

    @Column()
    @MaxLength(100)
    @Required()
    firstName: string;

    @Column()
    @MaxLength(100)
    @Required()
    lastName: string;

    @Column()
    @Mininum(0)
    @Maximum(100)
    age: number;
}

Now, the model is correctly defined and can be used with a Controller, AJV validation, Swagger and TypeORM.

We can use this model with a Controller like that:

import {Controller, Post, BodyParams} from "@tsed/common";

@Controller("/users")
export class UsersCtrl {

    constructor(private usersService: UsersService) {

    }

    @Post("/")
    create(@BodyParams() user: User): Promise<User> {

        return this.usersService.create(user);
    }

    @Get("/")
    getList(): Promise<User[]> {

        return this.usersService.find();
    }
}

TIP

You can find a working example on TypeORM here.