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

@fly_dream/sequelize-connection

v0.0.1

Published

sequelize 数据库连接管理库

Downloads

3

Readme

sequelize-connnection

Sequelize 连接管理, 支持多连接。

安装

npm install @fly_dream/sequelize-connnection

使用

1. 基本使用

import { connect } from '@fly_dream/sequelize-connnection';

await connect({
  connections: {
    default: {
      uri: '',
      modelInitFns: [],
    },
  },
});

2. 多个连接

import { connect } from '@fly_dream/sequelize-connnection';

await connect({
  connections: {
    // 连接1, 默认连接
    default: {
      uri: '',
      modelInitFns: [],
    },
    // 连接2
    conn1: {
      uri: '',
      modelInitFns: [],
    },
  },
});

3. 配置日志

import { connect } from '@fly_dream/sequelize-connnection';

await connect({
  // 日志记录的函数
  logger: (sql: string, time?: number) => logger.debug(sql)
  connections: {
    default: {
      uri: '',
      modelInitFns: [],
      logger: (sql: string, time?: number) => logger.debug(sql)
    }
  }
}

配置在 connections 里面的 logger 是每一个连接的日志; 如果不填则会使用 connections 外面的全局的 logger 配置。

4. 读写分离

import { connect } from '@fly_dream/sequelize-connnection';

await connect({
  connections: {
    default: {
      modelInitFns: [],
      replication: {
        // 配置读
        read: [{ host: 'localhost', username: 'user', password: 'pwd' }],
        // 配置写
        write: { host: 'localhost', username: 'user', password: 'pwd' },
      },
    },
  },
});

5. 在 fastify 中使用

import { connect } from '@fly_dream/sequelize-connnection';

register_fastify(app, {
  uri: '',
  modelInitFns: [],
});

6. 配置模型初始化函数

  1. 定义模型
// model.ts

import { DataTypes, Model } from 'sequelize';

/** 用户表 */
export class User extends Model {}

/**
 * 初始化模型
 * @param {object} sequelize
 * @param {string} charset - 字符编码, utf8mb4
 */
export function initModels(sequelize: Sequelize, charset: string) {
  User.init(..., { charset })
}
  1. 配置连接
import { connect } from '@fly_dream/sequelize-connnection';
import { initModels } from './models';

await connect({
  connections: {
    default: {
      uri: '',
      modelInitFns: [initModels],
    },
  },
});

API

1. connect(options): Promise<void>

初始化连接,并执行初始化函数。

参数

  • optionsobject
    • logger: boolean | (sql, time) => void
    • connectionsobject
      • defaultobject
        • uristring
        • modelInitFnsArray<() => void>

logger 配置为 true 时, 使用 console.log 记录日志

connections 中的 key 指定连接名称, value 连接配置, 在 sequelize-api 的基础上增加了 urimodelInitFnslogger 三个配置项

示例

import { connect } from '@fly_dream/sequelize-connnection';

const config = {
  logger: true,
  connections: {
    default: {
      uri:'mysql://root:root@localhost:3306/test',
      modelInitFns: [
        () => {
          // 自定义模型初始化函数
      ]
    }
  }
}

await connect(config);

2. register_fastify(app, config)

用于在 fastify 中使用, 默认使用 app.log.debug 记录 SQL 日志; 同时在 app.close 的时候, 会自动关闭所有连接

3. connection(name = 'default')

获取某个 Sequelize 连接实例

4. closeAll(): Promise<void>

断开所有的数据库连接, 通常用于在应用退出时使用