Skip to content

TypeORM for Egg.js on Tencent Serverless

License

Notifications You must be signed in to change notification settings

kchjxxgh/egg-typeorm

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

50 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

egg-typeorm

TypeORM plugin for Egg.js.

NPM version npm download

Install

$ yarn add @forsigner/egg-typeorm mysql

Usage

Plugin

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

Configuration

// {app_root}/config/config.default.ts
config.typeorm = {
  type: 'mysql',
  host: 'localhost',
  port: 3306,
  username: 'root',
  password: '123456',
  database: 'test',
  synchronize: true,
  logging: false,
  entities: ['app/entity/**/*.ts'],
  migrations: ['app/migration/**/*.ts'],
  subscribers: ['app/subscriber/**/*.ts'],
  cli: {
    entitiesDir: 'app/entity',
    migrationsDir: 'app/migration',
    subscribersDir: 'app/subscriber',
  },
}

Create entity files

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

Entity file

// app/entity/User.ts

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

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

  @Column()
  name: string
}

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

About

TypeORM for Egg.js on Tencent Serverless

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 96.0%
  • JavaScript 4.0%