Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mysqls

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mysqls

It is written in JavaScript,crud for node.js.You can also use transactions very easily.

  • 0.0.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
24
decreased by-25%
Maintainers
1
Weekly downloads
 
Created
Source

mysqls是在node.js场景中使用mysql,根据传入的参数生成相应的sql语句。 插件本身只负责生成sql语句,不执行任何的增删改查,增删改查交给mysql。

API参考很流行的ThinkPHP模型API,因为它已经做够流行和好用了。非常感谢ThinkPHP文档,很多案例参考其文档

  • gitbooks文档地址:https://wangweianger.gitbooks.io/mysqls/content/

安装:

npm install mysqls --save-dev

参数说明

execute     :执行单挑sql语句       参数:(config,sqlStr)
sql         :链式调用生成sql语句    链式调用语法,参考后文
transaction :执行事务相关任务时使用  参数:(config,sqlArr)

项目使用:

//import方式
import { execute,sql,transaction } from 'mysqls'

//require方式
let { execute,sql,transaction } = require('mysqls')

定义一个公共的config配置

let config={
    host:'localhost',
    user:'root',
    password:'123456',
    database:'web-performance',
    port:'3306',
}

使用Promise方式

//使用
let sqlstr = sql.table('web_pages').where({id:147}).select()

execute(config,sqlstr).then(res=>{
      console.log(res)
}).catch(err=>{
    console.log(err)
})

使用async/await

let sqlstr = sql.table('web_pages').where({id:147}).select()

let result = await execute(config,sqlstr)
console.log(result)

处理事务

let tranSqlArr = [
    sql.table('table1').data({number:number-5}).update(),
    sql.table('table2').data({number:number+5}).update()
]
let result = await transaction(config,tranSqlArr)
console.log(result) 

生成sql语句简单用法

备注:sql调用方法的顺序内部已经做了排序,因此可以不按严格的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语句高级用法

//参数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)

更多用法请查看详细文档

文档目录

项目运行

git clone https://github.com/wangweianger/mysqls.git
npm install

//dve
npm run dve

//product
npm run build

Keywords

FAQs

Package last updated on 27 Apr 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc