@zeromake/egg-typeorm
TypeScript icon, indicating that this package has built-in type declarations

1.4.5 • Public • Published

egg-typeorm

TypeORM plugin for Egg.js.

NPM version npm download

Install

$ yarn add @zeromake/egg-typeorm mysql

Usage

Plugin

// {app_root}/config/plugin.ts
const plugin: EggPlugin = {
  typeorm: {
    enable: true,
    package: '@zeromake/egg-typeorm',
  },
}

Configuration

connection-options

// {app_root}/config/config.default.ts
config.typeorm = {
  connection: {
    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'],
    cli: {
      entitiesDir: 'app/entity'
    }
  },
  // watch entitiesDir file generate `typings/typeorm.d.ts`
  watch: false,
}

Create entity files

├── controller
│   └── home.ts
├── entity
    ├── Post.ts
    └── User.ts

Entity file

// app/entity/User.ts

import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm'

@Entity()
class User {
  @PrimaryGeneratedColumn()
  id: number

  @Column()
  name: string
}

export default User

Use with FindOptions

// in controller
export default class UserController extends Controller {
  public async index() {
    const { ctx } = this
    ctx.body = await ctx.repo.User.find()
  }
}

Use with QueryBuilder

// 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
  }
}

Example

example

Questions & Suggestions

Please open an issue here.

License

MIT

Todo

  • [x] plugin change to javascript.
  • [x] use egg new hook.
  • [ ] cli generate ormconfig.ts or ormconfig.js.
  • [ ] cli generate tshelper generator config tshelper.js or package.json.
  • [ ] support for multiple connections.
  • [ ] inject into egg-ts-helper generator.
  • [ ] support egg agent mode
  • [ ] use app.loadToApp api load entity.

Package Sidebar

Install

npm i @zeromake/egg-typeorm

Weekly Downloads

0

Version

1.4.5

License

MIT

Unpacked Size

12.1 kB

Total Files

8

Last publish

Collaborators

  • zeromake