TypeORM plugin for Egg.js.
$ yarn add @forsigner/egg-typeorm mysql
// {app_root}/config/plugin.ts
const plugin: EggPlugin = {
typeorm: {
enable: true,
package: '@forsigner/egg-typeorm',
},
}
// {app_root}/config/config.default.ts
config.typeorm = {
client: {
type: 'mysql',
host: 'localhost',
port: 3306,
username: 'test',
password: 'test',
database: 'test',
synchronize: true,
logging: false,
entities: ['app/entity/**/*.ts'],
migrations: ['app/migration/**/*.ts'],
subscribers: ['app/subscriber/**/*.ts'],
}
}
├── controller
│ └── home.ts
├── entity
├── Post.ts
└── User.ts
// app/entity/User.ts
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm'
@Entity()
class User {
@PrimaryGeneratedColumn()
id: number
@Column()
name: string
}
export default User
// in controller
export default class UserController extends Controller {
public async index() {
const { ctx } = this
ctx.body = await ctx.repo.User.find()
}
}
// in controller
export default class UserController extends Controller {
public async index() {
const { ctx } = this
const firstUser = await ctx.repo.User.createQueryBuilder('user')
.where('user.id = :id', { id: 1 })
.getOne()
ctx.body = firstUser
}
}
Please open an issue here.