typescript mysql_GitHub - leizongmin/leizm-mysql: 基于 Node.js/TypeScript 的 MySQL 连接管理器...

68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f406c65697a6d2f6d7973716c2e7376673f7374796c653d666c61742d73717561726568747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6c65697a6f6e676d696e2f6c65697a6d2d6d7973716c2e7376673f7374796c653d666c61742d737175617265

68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f6c65697a6f6e676d696e2f6c65697a6d2d6d7973716c2e7376673f7374796c653d666c61742d73717561726568747470733a2f2f696d672e736869656c64732e696f2f64617669642f6c65697a6f6e676d696e2f6c65697a6d2d6d7973716c2e7376673f7374796c653d666c61742d737175617265

68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6e6f64652e6a732d2533453d5f362e302d677265656e2e7376673f7374796c653d666c61742d73717561726568747470733a2f2f696d672e736869656c64732e696f2f6e706d2f646d2f406c65697a6d2f6d7973716c2e7376673f7374796c653d666c61742d737175617265

68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f6c2f406c65697a6d2f6d7973716c2e737667

@leizm/mysql

基于 Node.js/TypeScript 的 MySQL 连接管理器

注意

不支持 MySQL 连接配置 dateStrings = "DATETIME",如果设置了该参数,可能导致返回的日期字段可能为 string 也可能为 Date 对象

安装

npm install @leizm/mysql --save

环境要求

Node.js v6.0 及更高版本 - 运行环境

MySQL 5.6 及更高版本 - SQL 数据存储

Redis 2.6 及更高版本 - 缓存存储

使用方法

import { Manager } from '@leizm/mysql';

const manager = new Manager({

// Redis 连接,用于缓存

// 参考 https://github.com/luin/ioredis/blob/master/API.md#new_Redis

redis: {

host: '127.0.0.1',

port: 6379,

db: 0,

password: '',

},

// 如果要禁用 Redis 缓存,设置 disableCache=true

// 当禁用 Redis 缓存,则不需要提供 redis, prefix, ttl 等配置项

disableCache: false,

// 缓存 key 前缀

prefix: 'TEST:',

// 缓存世界,秒

ttl: 60,

// MySQL 连接,可指定多个,第一个为 Master,其余为 Slave

// 参考 https://www.npmjs.com/package/mysql#connection-options

connections: [

{

host: '127.0.0.1',

port: 3306,

user: 'root',

password: '',

database: 'test',

connectionLimit: 10,

charset: 'utf8mb4',

},

{

host: '127.0.0.1',

port: 13306,

user: 'root',

password: '',

database: 'test',

connectionLimit: 10,

charset: 'utf8mb4',

},

],

});

// 注册 Table

manager.registerTable({

table: 'users',

primary: 'id',

autoIncrement: true,

fields: {

// true 表示该字段存在

id: true,

name: true,

email: true,

// 可指定字段为 json 类型,在存储是会自动对数据执行 JSON.stringify(),读取时执行 JSON.parse()

// 或自定义 encode 和 decode 方法

info: 'json',

created_at: 'date',

// 其它未定义的字段在存储时会被自动过滤

},

});

// 使用 Table

const ret = await manager.table('users').getByPrimary({ id: 123 });

console.log(ret);

// 使用原始连接执行查询,SELECT 语句会在任意连接执行,其它语句只在 Master 连接执行

const ret2 = await manager.connection.query('CREATE TABLE `hello`');

console.log(ret2);

// 事务(在 Master 连接执行)

(async function() {

const conn = await manager.connection.getMasterConnection();

// 开始事务

await conn.beginTransaction();

try {

await conn.query('INSERT INTO `hello`...');

await conn.query('INSERT INTO `hello`...');

await conn.query('INSERT INTO `hello`...');

// 提交事务

await conn.commit();

} catch (err) {

// 回滚事务

await conn.rollback();

}

// 释放连接

await conn.release();

})();

License

MIT License

Copyright (c) 2016-2018 Zongmin Lei

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.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值