mysqls
It is written in JavaScript,crud for mysql.You can also use transactions very easily.
mysqls 一款專為node.js生成sql語句的插件,鏈式調(diào)用丰榴,使用靈活团搞。支持生成sql語法,也支持生成語法之后直接調(diào)用多艇,支持事物等特性逻恐。
API參考很流行的ThinkPHP模型API。
安裝:
npm install mysqls --save-dev
mysqls參數(shù)說明
- init: sql初始化API
- exec: 執(zhí)行sql語句
- sql: 鏈式調(diào)用生成sql語句峻黍,支持生成后直接執(zhí)行sql語句
- transaction: 執(zhí)行事務(wù)API
項目使用:
//import方式
import { init, exec, sql, transaction } from 'mysqls'
//require方式
let { init, exec, sql, transaction } = require('mysqls')
mysql配置初始化:
// 可在項目的啟動時初始化配置
init({
host: 'localhost',
user: 'root',
password:'123456',
database: 'test',
port: 3306,
})
init 參數(shù)說明
- ispool: 是否以連接池的方式初始化 (default:true)
- host: host地址 (default:'127.0.0.1')
- user: 用戶名 (default:'root')
- password: 數(shù)據(jù)庫密碼 (default:'root')
- database: 使用的數(shù)據(jù)庫 (default:'test')
- port: 端口 (default:'3306')
- waitConnection: 是否等待鏈接(連接池時使用) (default:true)
- connectionLimit: 連接池大小 (default:10)
- queueLimit: 排隊限制 (default:0)
只生成sql語句案例
sql
.table('node_table')
.field('id,name')
.where({id:1})
.select()
// result
SELECT id,name FROM node_table WHERE id=1
使用exec函數(shù)執(zhí)行sql語句
const sqlstr = sql
.table('node_table')
.field('id,name')
.where({id:1})
.select();
const result = await exec(sqlstr);
使用sql.prototype.exec鏈式調(diào)用
const result = sql
.table('node_table')
.field('id,name')
.where({id:1})
.select(true)
.exec();
- 鏈式調(diào)用執(zhí)行sql時select方法需要傳參數(shù):true
- 同樣適合update(true),insert(true),delet(true),query(true)方法
使用Promise方式
//使用 exec 函數(shù)
exec(sql.table('web_pages').where({id:147}).select())
.then(res=>{
console.log(res)
}).catch(err=>{
console.log(err)
})
// 使用 exec 方法
sql.table('web_pages').where({id:147}).select(true).exec()
.then(res=>{
console.log(res)
}).catch(err=>{
console.log(err)
})
使用async/await
//使用 exec 函數(shù)
const result = await exec(sql.table('web_pages').where({id:147}).select())
// 使用 exec 方法
const result = await sql.table('web_pages').where({id:147}).select(true).exec()
處理事務(wù)
const tranSqlArr = [
sql.table('table1').data({number:'number-5'}).update(),
sql.table('table2').data({number:'number+5'}).update()
]
const result = await transaction(tranSqlArr)
生成sql語句簡單用法
- 備注:sql調(diào)用方法的順序內(nèi)部已經(jīng)做了排序复隆,因此可以不按嚴格的sql語句順序來寫
查詢
sql
.table('node_table')
.field('id,name')
.where({id:1})
.select()
SELECT id,name FROM node_table WHERE id=1
插入
sql
.table('node_table')
.data({name:'zane',email:'752636052@qq.com'})
.insert()
INSERT INTO node_table (name,email) VALUES (`zane`,`752636052@qq.com`)
更新
sql
.table('node_table')
.data({name:'zane',email:'752636052@qq.com'})
.update()
UPDATE node_table SET name=`zane`,email=`752636052@qq.com`
刪除
sql .table('node_table')
.where({name:'zane'})
.delet();
DELETE FROM node_table WHERE name=`zane`
生成sql語句高級用法
//參數(shù)json多字段
sql
.table('node_table')
.where({id:1,name:'zane'})
.select()
SELECT * FROM node_table WHERE id=1 AND name=`zane`
//參數(shù)數(shù)組
let data=[
{id:1,name:'zhangsan',_type:'or'},
{sex:1,number:3}
]
sql.table('node_table').where(data).select()
SELECT * FROM node_table WHERE (id=1 OR name=`zhangsan` ) AND (sex=1 AND number=3 )
//多字段連接方式
let data=[
{id:1,name:'zhangsan',_type:'or',_nexttype:'or'},
{sex:1,number:3,_type:'and'}
]
sql.table('node_table').where(data).select()
SELECT * FROM node_table WHERE (id=1 OR name=`zhangsan`) OR (sex=1 AND number=3)
//表達式查詢
let data={
id:{eq:100,egt:10,_type:'or'},
name:'zhangshan'
}
sql.table('node_table').where(data).select()
SELECT * FROM node_table WHERE ((id=100) OR (id>=10)) AND name=`zhangshan`
//混合查詢
let data=[{
id:{eq:100,egt:10,_type:'or'},
name:'zhangshan',
_nexttype:'or'
},{
status:1,
name:{like:'%zane%'}
}]
sql.table('node_table').where(data).select()
SELECT * FROM node_table WHERE (((id=100) OR (id>=10)) AND name=`zhangshan`) OR (status=1 AND ((name LIKE `%zane%`)))
//UNION , UNION ALL 組合使用
sql
.union('SELECT * FROM think_user_1',true)
.union('SELECT * FROM think_user_2',true)
.union(['SELECT * FROM think_user_3','SELECT name FROM think_user_4'])
.union('SELECT * FROM think_user_5',true)
.select()
得到
(SELECT * FROM think_user_1) UNION ALL
(SELECT * FROM think_user_2) UNION ALL
(SELECT * FROM think_user_3) UNION
(SELECT name FROM think_user_4) UNION
(SELECT * FROM think_user_5)
更多用法請查看詳細文檔