Skip to content
Typeorm for Egg.js
JavaScript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
config
examples
lib
test
.autod.conf.js
.eslintignore
.eslintrc
.gitignore
.travis.yml
LICENSE
README.md
README.zh_CN.md
app.js
appveyor.yml
package.json

README.md

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

You can’t perform that action at this time.