1 Star 0 Fork 0

Tenny / typeorm-connection

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

typeorm-connection

typeorm 数据库连接管理库。

API

  1. connect(config): 连接数据库, 其中 config 是一个 JSON 形式, 配置如下:

    1. logger?: ILoggerOption: 日志选项, 默认为: true
    2. connections: JSON 形式的连接配置, key 表示连接的名称, value - 表示 DataSource Options
// 1. 普通连接
await connect({
  default: {
    url: "mysql://root:x@x.x.x.x:3306/x",
    entityPrefix: "sl_site_",
    entities: Object.values(entities),
  },
});

// 2. 读写分离支持
await connect({
  default: {
    entityPrefix: "sl_site_",
    entities: Object.values(entities),
    replication: {
      // 配置读写数据库
      master: {
        url: "mysql://root:x@x.x.x.x:3306/x",
      },
      // 配置只读数据库
      slaves: [
        {
          url: "mysql://root:x@x.x.x.x:3306/x",
        },
      ],
    },
  },
});
  1. dataSource(entityOrAlias): 参数为 typeorm 的实体对象或者是连接名称, 例如: default;返回 typeormDataSource 对象

  2. manager(entityOrAlias): 获取 EntityManager 对象

  3. destroy(alias): 断开某个数据库连接: await destroy('default')

  4. destroyAll(): 断开所有的数据库连接

使用

import { connect, manager, BaseDataEntity } from 'typeorm-connnection'

/**
 * 登录记录表
 */
@Entity({ name: 'login_record' })
export class LoginRecord extends BaseDataEntity {
  @Column({ type: 'bigint', name: 'user_id', unsigned: true })
  public userId: number;

  @Column({ type: 'varchar', length: 50, nullable: true })
  public ip: string;

  @Column({ type: 'varchar', name: 'user_agent', length: 500, nullable: true })
  public userAgent: string;

  /** 登录的平台,wxh5-公众号,h5-网页 */
  @Column({ type: 'varchar', length: 20, default: 'wxh5', comment: '登录的平台,wxh5-公众号,h5-网页' })
  public platform: string;
}

// 1. 连接数据库
await connect({
  default: {
    url: 'mysql://root:x@x.x.x.x:3306/x',
    entityPrefix: 'sl_site_',
    entities: [LoginRecord]
  },
})

const em = manager('default') // manager(LoginRecord)

// 查询数据
const record = await em.findOneBy(LoginRecord, { id: 1 });

日志

await connect({ logger: true, ... })
await connect({ logger: console, ... })
await connect({ logger: Pino(), ... }) // 使用 Pino 记录日志

logger 选项可以为普通的日志记录对象,具有 infowarnerror 函数;或者是一个 typeorm 的自定义日志对象。

fastify 中使用

import { register_fastify } from "typeorm-connection";

const app = fastify();

register_fastify(app, {
  url: "mysql://root:x@x.x.x.x:3306/x",
  entityPrefix: "sl_site_",
  entities: [LoginRecord],
});

基础的实体类

提供基础的数据实体类 BaseDataEntity,该实体类,包含 3 个基础的字段:

  1. id: 自增长的 id
  2. createTime: Date 对应的数据库字段为:create_time
  3. updateTime: Date 对应的数据库字段为: update_time
MIT License Copyright (c) 2023 Tenny Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

typeorm 数据库连接管理库 展开 收起
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
JavaScript
1
https://gitee.com/towardly/typeorm-connection.git
git@gitee.com:towardly/typeorm-connection.git
towardly
typeorm-connection
typeorm-connection
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891