It is written in JavaScript,crud for mysql.You can also use transactions very easily.
node
mysqls 一款專爲node.js生成sql語句的插件,鏈式調用,使用靈活。支持生成sql語法,也支持生成語法以後直接調用,支持事物等特性。 mysql
API參考很流行的ThinkPHP模型API。git
npm地址:www.npmjs.com/package/mys…github
github: github.com/wangweiange…web
mysqls是筆者在開發過程當中爲了更簡單、更高效的開發效率而封裝的一個庫,年初作的,最近重新小修改和增強了一下。分享給有須要的人,感興趣的也能夠看看筆者的思路。由於我的使用過ThinkPHP,認爲其對mysql的封裝調用方式是很是友好和易用的,所以絕大部分api參考其中。若是你有其餘的意見和建議也但願能夠跟我分享。
sql
npm install mysqls --save-dev複製代碼
exec: 執行sql語句數據庫
sql: 鏈式調用生成sql語句,支持生成後直接執行sql語句npm
transaction: 執行事務APIjson
//import方式
import { init, exec, sql, transaction } from 'mysqls'
//require方式
let { init, exec, sql, transaction } = require('mysqls')複製代碼
// 可在項目的啓動時初始化配置
init({
host: 'localhost',
user: 'root',
password:'123456',
database: 'test',
port: 3306,
})複製代碼
sql
.table('node_table')
.field('id,name')
.where({id:1})
.select()
// result
SELECT id,name FROM node_table WHERE id=1複製代碼
const sqlstr = sql
.table('node_table')
.field('id,name')
.where({id:1})
.select();
const result = await exec(sqlstr);複製代碼
const result = sql
.table('node_table')
.field('id,name')
.where({id:1})
.select(true)
.exec();複製代碼
//使用 exec 函數
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)
})複製代碼
//使用 exec 函數
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()複製代碼
const tranSqlArr = [
sql.table('table1').data({number:'number-5'}).update(),
sql.table('table2').data({number:'number+5'}).update()
]
const result = await transaction(tranSqlArr)複製代碼
查詢api
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`複製代碼
//參數json多字段
sql
.table('node_table')
.where({id:1,name:'zane'})
.select()
SELECT * FROM node_table WHERE id=1 AND name=`zane`
//參數數組
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)複製代碼
更多用法請查看詳細文檔: