npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-transform-mysql

v0.0.4

Published

javascript根据参数自动生成mysql语句,链式调用,功能强大,使用简单

Downloads

20

Readme

node-transform-mysql文档

node-transform-mysql是在node.js场景中使用mysql,根据传入的参数生成相应的sql语句。

它所做的事情很简单也很专一,只负责生成sql语句,不执行任何的增删改查。

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

API文档地址:https://wangweianger.gitbooks.io/node-transform-mysql/content/

安装:

不用担心它的体量大,整体代码300行左右,压缩之后代码不足8k 首先通过 npm 安装:

npm install node-transform-mysql

然后使用一个支持 CommonJS 或 ES2015 的模块管理器,例如 webpack:

// 使用 ES6 的转译器,如 babel
import sql from node-transform-mysql

// 不使用 ES6 的转译器
var sql = require('node-transform-mysql')

sql调用方法的顺序内部已经做了排序,因此可以不按严格的sql语句顺序来写

简单用法

查询

sql
    .table('node_table')
    .where('id=1')
    .select()

SELECT * FROM node_table WHERE id=1


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

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

更新

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

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

删除

let data=[{
        id:1,
        name:'zane'
    },{
        sex:1,
        address:'shenzheng'
    }]

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)

更多用法请查看详细文档

文档目录

项目运行

git clone https://github.com/wangweianger/node-transform-mysql.git
npm install

//dve
npm run dve

//product
npm run build