Giter Site home page Giter Site logo

egg-typeorm's Introduction

egg-typeorm

NPM version npm download

TypeORM plugin for Egg.js.

安装

$ npm install -S @hackycy/egg-typeorm mysql

使用

插件启用

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

配置实体存放目录

在项目根目录添加ormconfig.{js|json|yaml|yml}文件(配置支持后缀中的任意一种即可)

ormconfig.js

// 单数据库连接
module.exports = {
  entitiesDir: "app/entity/db1"
};

// 或者多数据库连接
module.exports = [
  {
    name: 'default',
    entitiesDir: "app/entity/db1"
  }
  {
    name: 'db2',
    entitiesDir: "app/entity/db2"
  }
];

ormconfig.json

// 单数据库连接
{
  "entitiesDir": "app/entity/db2",
}
// 或者多数据库连接
[
  {
    "name": "default",
    "entitiesDir": "app/entity/db1"
  },
  {
    "name": "db2",
    "entitiesDir": "app/entity/db2"
  }
]

ormconfig.yaml或ormconfig.yml

# yaml || yml
default: //默认连接
  entitiesDir: app/entity/db1

db2: //或者多数据库连接时配置
  entitiesDir: app/entity/db2

entitiesDir表示数据库的实体文件存放的路径;

js、json、yaml、yml插件会按照该顺序查找对应ormconfig后缀的文件,找到则不会再使用其它后缀的配置。

注意这里是有优先级的。

相当于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: 'root',
      password: '123456',
      database: 'test',
      synchronize: true,
      logging: false,
    }
  }

多数据库连接配置

// {app_root}/config/config.default.ts
config.typeorm = {
  clients: [{
    name: "default",
    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,
  }]
}

注意:如果定义了clients,插件会直接忽略掉client的配置,只能二选一配置

创建实体

├── controller
│   └── home.ts
├─  entity
    └─sys
      └── user.ts

实体文件

// app/entity/sys/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
    // 单数据库
    // app/entity/sys/user.ts => ctx.repo.sys.User
    // app/entity/user.ts => ctx.repo.User
    // app/entity/admin/sys/user_role.ys => ctx.repo.admin.sys.UserRole
    // 多数据库 例如获取db1
    // ctx.repo['db1'].user,转换方式同上
    ctx.body = await ctx.repo.sys.User.find()
  }
}

所有实体会加载在ctx.entity中, 所有仓库会加载到ctx.repo;

多数据库时加载在对应的ctx.entity[connectName]ctx.repo[connectionName]上;

注意:使用name为default会直接挂载,不需要指定connectName

详细可以查看项目下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
  }
}

具体使用可查看exmaple使用案例以及TypeORM使用文档

日志

插件默认自定义了一个基于Egg的Logger模块实现的日志记录器。如果配置中没有进行配置connection-options中的logger,则会默认使用插件提供日志记录器。如果想要更换或者使用原来TypeOrm提供的只需要配置对应字段即可。

框架内置Context扩展

获取getConnection

this.ctx.ormConnection || this.ctx.getOrmConnection('connectionName')

获取getManager

this.ctx.ormManager || this.ctx.getOrmManager('connectionName')

依赖的第三方库

有问题或Bug

请提出issues

License

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.