Skip to content

lvwxx/egg-typeorm

Repository files navigation

egg-typeorm

Typeorm plugin for Egg.js

Note: This plugin just for integrate Typeorm(used JavaScript) into Egg.js, more documentation please visit https://typeorm.io/.

NPM version build status Test coverage David deps Known Vulnerabilities npm download

Install

$ npm i egg-typeorm-js --save

Usage

// {app_root}/config/plugin.js
exports.typeorm = {
  enable: true,
  package: 'egg-typeorm-js',
};
// {app_root}/app/entities/user.js
'use strict'

const EntitySchema = require("typeorm").EntitySchema

module.exports = new EntitySchema({
    name: "User",
    columns: {
      id: {
        primary: true,
        type: "int",
        generated: true
      },
      member_id: {
        type: "int",
        unique: true
      },
      name: {
        type: "varchar"
      }
    }
})
// {app_root}/app/controller/member.js

'use stric'

async find() {
  const { ctx } = this
  const list = await ctx.repo.User.find() // entity会根据文件名加载到ctx.repo的属性上
  ctx.body = list
}

Configuration

1、单个链接配置

// {app_root}/config/config.default.js
exports.typeorm = {
  client: {
    "type": "mysql",
    "host": "",
    "port": 3306,
    "username": "",
    "password": '',
    "database": "",
    "synchronize": true,
    "logging": false,
  }
};

单个数据库配置时,entity的文件在{app_root}/app/entites下

2、多个连接配置

// {app_root}/config/config.default.js
exports.typeorm = {
  clients: {
    "db1": {
      "name": "db1",
      "type": "mysql",
      "host": "",
      "port": 3306,
      "username": "",
      "password": '',
      "database": "",
      "synchronize": true,
      "logging": false,
      "baseDir": "entities/db1"
    },
    "db2": {
      "name": "db2",
      "type": "mysql",
      "host": "",
      "port": 3306,
      "username": "",
      "password": '',
      "database": "",
      "synchronize": true,
      "logging": false,
      "baseDir": "entities/db2"
    },
  }
};

多个连接配置时,需要制定name和entities的路径,默认在{app_root}/app/entites下

Example

Please open an issue here.

Questions & Suggestions

Please open an issue here.

License

MIT

About

🔥 Javascript Typeorm for Egg.js

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published