@mildjs/typeorm
v1.0.1
Published
TypeORM module (extension) for @mildjs/core
Downloads
3
Readme
TypeORM module (extension) for @mildjs/core
Features
- Auto load Entity
- Support TypeOrm connection using
@InjectConnection
- Support TypeOrm repository using
@InjectRepository
Installation
$ npm install @mildjs/typeom typeorm
Usage
Define user entity
// filename: user.entity.ts import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm'; @Entity() export class UserEntity { @PrimaryGeneratedColumn() id: number; @Column() name: string; }
define controller
// filename: user.controller.ts import { Connection, Repository } from 'typeorm'; import { Controller } from '@mildjs/core'; import { InjectConnection, InjectRepository } from '@mildjs/typeorm'; import { UserEntity } from './users.entity'; @Controller('users') export class UsersController { constructor( @InjectConnection() private connection: Connection, @InjectRepository(UserEntity) private repository: Repository<UserEntity> ) { this.testConnection(); } async testConnection() { if(this.connection instanceof Connection){ console.log('Database connected') } const newUser = new UserEntity(); newUser.name = "foobar"; const user: UserEntity = await this.repository.save(newUser); console.log(user); } }
Setup
@mildjs/core
with express// filename: main.ts import 'reflect-metadata'; import express from 'express'; import { useExpressServer } from '@mildjs/core'; import { TypeOrmModule } from '@mildjs/typeorm'; import { UsersController } from './users/users.controller'; import { UserEntity } from './users/users.entity'; const app = express(); async function main() { await useExpressServer(app, { controllers: [UsersController], imports: [ TypeOrmModule.forRoot({ name: 'default', type: 'sqlite', database: './app.sqlite', synchronize: true, entities: [UserEntity], }) ], }); } main(); app.listen(3001); console.log("server listening at port 3001")
Run
ts-node ./main.ts
The result should be (the ID is auto generated )
server listening at port 3001
Database connected
UserEntity { name: 'foobar', id: 1 }