npm mysql orm_GitHub - SuperID/super-orm: 基于Node.js的MySQL ORM模块,支持缓存

68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f73757065722d6f726d2e7376673f7374796c653d666c61742d737175617265

68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f537570657249442f73757065722d6f726d2e7376673f7374796c653d666c61742d737175617265

68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f537570657249442f73757065722d6f726d2e7376673f7374796c653d666c61742d737175617265

68747470733a2f2f696d672e736869656c64732e696f2f64617669642f537570657249442f73757065722d6f726d2e7376673f7374796c653d666c61742d737175617265

68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6e6f64652e6a732d2533453d5f362e302d677265656e2e7376673f7374796c653d666c61742d737175617265

68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f646d2f73757065722d6f726d2e7376673f7374796c653d666c61742d737175617265

68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f6c2f73757065722d6f726d2e737667

super-orm

基于 Node.js 的 MySQL ORM 模块,支持缓存、多 MySQL 连接,支持 TypeScript

安装

$ npm install super-orm --save

环境要求

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

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

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

使用

'use strict';

const { Manager } = require('super-orm');

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: '',

},

// 缓存 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',

},

],

});

// 注册 Model

manager.registerModel('User', {

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',

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

},

});

// 使用 Model

manager.model('User').getByPrimary({ id: 123 })

.then(ret => console.log(ret))

.catch(err => console.error(err));

// 所有接口同时支持 callback 和 promise

manager.model('User').getByPrimary({ id: 123 }, (err, ret) => {

if (err) console.log(err)

console.log(ret);

});

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

manager.connection.query('CREATE TABLE `hello`')

.then(ret => console.log(ret))

.catch(err => console.error(err));

// 事务(在 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();

})();

API 接口

正在整理中

License

MIT License

Copyright (c) 2016 SuperID | 免费极速身份验证服务

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值