• Stars
    star
    107
  • Rank 323,587 (Top 7 %)
  • Language
    JavaScript
  • License
    Other
  • Created almost 7 years ago
  • Updated almost 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

EN | CN

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
import { init, exec, sql, transaction } from 'mysqls'

//require
let { init, exec, sql, transaction } = require('mysqls')

Config initialization:

// You can initialize the configuration at project startup
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()

// result
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

// use exec function
exec(sql.table('web_pages').where({id:147}).select())
    .then(res=>{
        console.log(res)
    }).catch(err=>{
        console.log(err)
    })

// use exec method
sql.table('web_pages').where({id:147}).select(true).exec()
    .then(res=>{
        console.log(res)
    }).catch(err=>{
        console.log(err)
    })

使用async/await

// use exec function
const result = await exec(sql.table('web_pages').where({id:147}).select())

// use exec method
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:'[email protected]'})
    .insert()

INSERT INTO node_table (name,email) VALUES (`zane`,`[email protected]`)

batch insert

let data = [
    {name:'zane',email:'[email protected]'},
    {name:'zane_1',email:'[email protected]'},
    {name:'zane_2',email:'[email protected]'},
]
sql
    .table('node_table')
    .data(data)
    .insert()

INSERT INTO node_table (name,email) VALUES ('zane','[email protected]'),('zane_1','[email protected]'),('zane_2','[email protected]')

update

sql
    .table('node_table')
    .data({name:'zane',email:'[email protected]'})
    .where({id:1})
    .update()

UPDATE node_table SET name=`zane`,email=`[email protected]`

delet

sql .table('node_table')
    .where({name:'zane'})
    .delet();

DELETE FROM node_table WHERE name=`zane`

Advanced Usage.

// parameter json
sql
    .table('node_table')
    .where({id:1,name:'zane'})
    .select()

SELECT  * FROM node_table WHERE id=1 AND name=`zane`

// parameter array
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 )

// multiple fields
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)

// Expression Query
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`

// Multiple queries
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()

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

More Repositories

1

zanePerfor

前端性能监控系统,消息队列,高可用,集群等相关架构
JavaScript
1,736
star
2

web-performance-monitoring-system

A complete performance monitoring system.
JavaScript
462
star
3

web-report-sdk

A complet sound web performance report plug-in.
JavaScript
393
star
4

APubPlat

Devops自动化部署、堡垒机开源项目、Web Terminal
HTML
203
star
5

myblog

我的个人博客,记录自己的所学所享.
75
star
6

zane-data-time-calendar

A simple and complete time calendar plug-in
JavaScript
69
star
7

wx-report-sdk

微信小程序,错误,ajax性能,系统信息,uv等上报信息sdk
JavaScript
40
star
8

react16-webpack3.8-onepage-base-project

react16.0 + webpack3.8 最新单页面集成模板
JavaScript
39
star
9

vue2.5-webpack3.8-spa-base-cms

vue2.5版全家桶+webpack3.8 cms项目基础模板框架
JavaScript
30
star
10

FlutterApp

My first Flutter app. Study Flutter add make an App Demo.
Dart
26
star
11

nodejs-mock-koa2-vue2

node.js开发的一套本地mock静态数据平台系统
JavaScript
16
star
12

zane-web-blog-node.js-ejs-mysql

zane 的node.js+koa2+ejs 博客源码
JavaScript
12
star
13

redux-source-code-learning

redux v4 源码详细解读
JavaScript
8
star
14

zane-koa2-ejs-mysql

koa2+ejs+mysql node.js 基础开发模板
JavaScript
8
star
15

react-native-demo-app

react-native app demo商城开发
JavaScript
6
star
16

vue2.5-webpack4.16-onepage-base-h5

vue2.5,webpack4.16,vue-router15 基础开发模板
JavaScript
6
star
17

vue-calendar

vue1 vue2 小巧全面的时间选择组件 支持双选择器
Vue
4
star
18

react-react-router-webpack

react webpak 打包
JavaScript
4
star
19

zane-koa2-restful-api

koa2 和 node.js 搭建的一条 RESTful api server端 脚手架。
JavaScript
3
star
20

koa2-use-mongoose-learn

Using mongoose to develop node.js
JavaScript
3
star
21

weex-dev

weex 初试
JavaScript
3
star
22

trueme-pro

萃美微信H5开发
HTML
2
star
23

qmx-vue-compent

Vuemo
JavaScript
1
star
24

qmx-vue-one-pag

vue.js 单页面
JavaScript
1
star
25

my-first-quick-app

快应用开发体验
JavaScript
1
star
26

vue2-one-page-base-cms

vue2.0 后台cms 集成模板框架
JavaScript
1
star