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

@zenweb/mysql

v5.2.3

Published

Zenweb MySQL module

Downloads

1,138

Readme

ZenWeb MySQL Module

提供 MySQL 单数据库服务器以及集群的连接池集成功能。 并提供 Context 进行数据库连接的前置处理。

安装

npm install @zenweb/mysql

快速使用

import { create } from 'zenweb';
import modMySQL from '@zenweb/mysql';

const app = create();

app.setup(modMySQL({
  pools: {
    // 添加数据库服务器配置
    DB1: {
      host: '127.0.0.1',
      port: 3306,
      user: 'root',
      password: '',
      database: 'test',
      charset: 'utf8mb4',
      timezone: '+08:00',
      connectionLimit: 100,
    },
    // 如需集群功能可以继续添加 MySQL 配置
    // DB2: { ... },
    // ....
  }
}));

app.start();

可以直接通过 app.mysql 或者 ctx.mysql 来使用连接池

使用 Context 前置处理

:::tip 注意 如果使用 withContext 前置处理,数据库连接池对象只能在 Context 中使用。 :::

import { Query } from 'mysql-easy-query';

app.setup(mysql({
  pools: {
    // 数据库服务器配置这里省略,参考上面代码
  },
  // 在每次获取连接之前回调
  // 所属数据库服务器切换
  getPoolConnectionBefore(opt) {
    return {
      // opt 参数为传入的原始集群选择规则
      // 返回新的集群选择规则
      pattern: opt?.pattern || '*',
      selector: opt?.selector,
    };
  },
  // 成功取得数据库连接后回调
  getPoolConnectionAfter(conn) {
    return conn; // conn 为已取得的 MySQL 连接
  },
}));

查询数据库

import { mapping } from 'zenweb';
import { $mysql } from '@zenweb/mysql';

export class TestController {
  @mapping()
  async mysql() {
    // 简单查询
    return await $mysql.query('SELECT 1+1');
  }

  // 使用事物
  @mapping()
  async transaction(ctx: Context) {
    return await $mysql.transaction(async tx => {
      await tx.query('UPDATE some SET a = 1');
      await tx.query('UPDATE other SET b = 2');
      return 'OK';
    });
  }

  // 使用指定的数据库服务器
  @mapping()
  async cluster() {
    return await $mysql.of('DB2').query('SELECT * FROM some');
  }
}