1.1.12 • Published 5 years ago

egg-ts-typeorm v1.1.12

Weekly downloads
105
License
MIT
Repository
-
Last release
5 years ago

egg-ts-typeorm

TypeORM plugin for Egg.js.

安装

$ npm install -S egg-ts-typeorm

使用

插件启用

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

配置ormconfig.yml

在egg项目根目录下新建ormconfig.yml文件;内容如下

default: # 默认连接
  entitiesdir: "app/entities"

该文件表示数据库的实体文件存放的路径;相当于connection-options中entities配置项为'app/entity/*/.{js,ts}'

配置config.{env}.ts

connection-options

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

多数据库连接配置

// {app_root}/config/config.default.ts
config.typeorm = {
  clients: [{
    name: "model1",
    type: "mysql",
    host: "localhost",
    port: 3306,
    username: "root",
    password: "admin",
    database: "db1",
    synchronize: true
  }, {
    name: "model2",
    type: "mysql",
    host: "localhost",
    port: 3306,
    username: "root",
    password: "admin",
    database: "db2",
    synchronize: true
  }]
}

ormconfig.yml如下:

model1: # config.default.ts中对应的name
  entitiesdir: "app/entity/db1"

model2: # 同上
  entitiesdir: "app/entity/db2"

创建实体

├── 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()
  }
}

所有实体会加载在ctx.entities中, 所有仓库会加载到ctx.repo; 多数据库时加载在对应的ctx.entitiesconnectName与ctx.repoconnectionName上; 详见typings/typeorm.d.ts文件

使用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
  }
}
1.1.12

5 years ago

1.1.11

5 years ago

1.1.10

5 years ago

1.1.9

5 years ago

1.1.8

5 years ago

1.1.7

5 years ago

1.1.6

5 years ago

1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.40

5 years ago

1.0.39

5 years ago

1.0.38

5 years ago

1.0.37

5 years ago

1.0.36

5 years ago

1.0.35

5 years ago

1.0.34

5 years ago

1.0.33

5 years ago

1.0.32

5 years ago

1.0.31

5 years ago

1.0.30

5 years ago

1.0.29

5 years ago

1.0.28

5 years ago

1.0.27

5 years ago

1.0.26

5 years ago

1.0.25

5 years ago

1.0.24

5 years ago

1.0.23

5 years ago

1.0.22

5 years ago

1.0.21

5 years ago

1.0.20

5 years ago

1.0.19

5 years ago

1.0.18

5 years ago

1.0.17

5 years ago

1.0.16

5 years ago

1.0.15

5 years ago

1.0.14

5 years ago

1.0.13

5 years ago

1.0.12

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago