mysqls
It is written in JavaScript,crud for mysql.You can also use transactions very easily.
mysqls:A plug-in that generates SQL statements for node.js. call chaining .simple to use. support transaction.
install:
npm install mysqls --save
or
yarn add mysqls
mysqls parameters
- init: sql Initialization
- exec: Executing SQL statements
- sql: Chain Call Generates SQL Statement
- transaction: transaction API
Use:
import { init, exec, sql, transaction } from 'mysqls'
let { init, exec, sql, transaction } = require('mysqls')
Config initialization:
init({
host: 'localhost',
user: 'root',
password:'123456',
database: 'test',
port: 3306,
})
init configs
- ispool: Is it initialized as a connection pool. (default:true)
- host: host address. (default:'127.0.0.1')
- user: user. (default:'root')
- password: password. (default:'root')
- database: database. (default:'test')
- port: port. (default:'3306')
- waitConnection: wait for connections. (default:true)
- connectionLimit: connection limit. (default:10)
- queueLimit: queue limit. (default:0)
Only Generate SQL statements.
sql
.table('node_table')
.field('id,name')
.where({id:1})
.select()
SELECT id,name FROM node_table WHERE id=1
use exec function
const sqlstr = sql
.table('node_table')
.field('id,name')
.where({id:1})
.select();
const result = await exec(sqlstr);
use sql.prototype.exec
const result = sql
.table('node_table')
.field('id,name')
.where({id:1})
.select(true)
.exec();
- .select(true):true
- It same to use at update(true),insert(true),delet(true),query(true) method.
use Promise
exec(sql.table('web_pages').where({id:147}).select())
.then(res=>{
console.log(res)
}).catch(err=>{
console.log(err)
})
sql.table('web_pages').where({id:147}).select(true).exec()
.then(res=>{
console.log(res)
}).catch(err=>{
console.log(err)
})
使用async/await
const result = await exec(sql.table('web_pages').where({id:147}).select())
const result = await sql.table('web_pages').where({id:147}).select(true).exec()
transaction
const tranSqlArr = [
sql.table('table1').data({number:'number-5'}).update(true,true),
sql.table('table2').data({number:'number+5'}).update(true,true)
]
const result = await transaction(tranSqlArr)
Simple usage of generating SQL statements.
select
sql
.table('node_table')
.field('id,name')
.where({id:1})
.select()
SELECT id,name FROM node_table WHERE id=1
insert
sql
.table('node_table')
.data({name:'zane',email:'752636052@qq.com'})
.insert()
INSERT INTO node_table (name,email) VALUES (`zane`,`752636052@qq.com`)
update
sql
.table('node_table')
.data({name:'zane',email:'752636052@qq.com'})
.where({id:1})
.update()
UPDATE node_table SET name=`zane`,email=`752636052@qq.com`
delet
sql .table('node_table')
.where({name:'zane'})
.delet();
DELETE FROM node_table WHERE name=`zane`
Advanced Usage.
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%`)))
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()
result
(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)
Directory