npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@hackycy/egg-typeorm

v0.5.1

Published

TypeORM For Eggjs

Downloads

467

Readme

egg-typeorm

NPM version npm download

TypeORM plugin for Egg.js.

安装

$ npm install -S @hackycy/egg-typeorm 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提供的只需要配置对应字段即可。

自定义环境

常规开发流程可能不仅仅只有prod才是预设的生产环境,可通过该配置来自定义环境来适应自己的开发流程。

config.typeorm = {
  prodEnv: [ 'prod', 'stagging' ]
}
// or
config.typeorm = {
  prodEnv: 'prod'
}

框架内置Context扩展

获取getConnection

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

获取getManager

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

依赖的第三方库

有问题或Bug

请提出issues

License

MIT