Skip to content

Commit 30acfef

Browse files
committed
init
1 parent 899f765 commit 30acfef

File tree

7 files changed

+701
-0
lines changed

7 files changed

+701
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
node_modules
3+
notupload

README.md

+211
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
# nodejs-mysql-plus
2+
3+
# nodejs-mysql-plus 是什么?
4+
5+
#### 一个实现了mysql 常用功能的nodejs 库,旨在优雅简单的使用sql语言,即在代码中不出现sql语句
6+
7+
#### 实现了批量插入和批量更新功能,多个项目稳定运行
8+
9+
#### 如有问题 欢迎反馈 [email protected]
10+
11+
# nodejs-mysql-plus 有什么主要功能?
12+
13+
## 增删改查
14+
## 批量插入insert
15+
## 批量更新
16+
17+
18+
# 如何使用
19+
## 安装引用
20+
21+
可以直接下载源码后直接引用也可以npm安装
22+
23+
执行命令:`npm install nodejs-mysql-plus`
24+
### 实例化mysql conn
25+
```javascript
26+
const { Conn } = require("nodejs-mysql-plus")
27+
const c = new Conn({
28+
host: '',
29+
user: '',
30+
password: '',
31+
database: '',
32+
port: 3306, //可选
33+
charset: "utf8mb4",//可选
34+
timezone: 'Asia/Shanghai',//可选
35+
time_colum_key:'create_time',//默认时间字段名
36+
debug:false //可选 是否打印sql
37+
});
38+
39+
```
40+
可以实例化多个数据库链接
41+
## 简单来个例子
42+
```javascript
43+
const teacher = c.table("teacher") //传入表名
44+
teacher.getAllByWhere({name:'peter'},function(rows){
45+
console.log(rows)
46+
})
47+
//等价于
48+
c.query('select * from teacher where name = "peter"',function(rows){
49+
console.log(rows)
50+
})
51+
52+
techer.getColumnsByWhere(['name','sex'],{name:'peter'},function(rows){
53+
54+
})
55+
//等价于
56+
c.query('select name, sex from teacher where name = "peter"',function(rows){
57+
console.log(rows)
58+
})
59+
```
60+
getAllByWhere getColumnsByWhere 基本可以满足85%的日常查询了
61+
62+
### 统一查询方法 getData()
63+
```javascript
64+
const teacher = c.table("teacher")
65+
const where = {school:'goodschool'}
66+
const columns = ['name','sex','create_time','class',"school"]
67+
const options = {
68+
where :where,//对象或数组 [[school:'goodschool']]
69+
columns :columns,//数组
70+
limit : 5 ,
71+
orderby : "create_time desc",
72+
groupby : "class",
73+
join_sign : "or", //默认值 and
74+
eq_sign : "like", // 默认值 =
75+
callback :function(rows){
76+
77+
}
78+
}
79+
80+
teacher.getData(options,function(rows){
81+
82+
};)
83+
//回调函数也可以写到后面 第二个参数, 优先级高
84+
85+
```
86+
### 实战 查询age 大于20小于30岁 而且 school 不是null
87+
```javascript
88+
const where = []
89+
where.push(['age','> 20'])
90+
where.push(['age','< 30'])
91+
where.push(['school','is not null'])
92+
// where 可以是二维数组形式
93+
teacher.getData({
94+
where:where
95+
},function(rows){
96+
})
97+
//以上等价于
98+
c.query('select* from teacher where age > 20 and age < 30 and school is not null ',function(rows){
99+
100+
})
101+
```
102+
103+
104+
### 插入数据 insert
105+
```javascript
106+
const data ={
107+
name:"peter",
108+
age:"001",
109+
class:"001",
110+
school:"001",
111+
}
112+
teacher.insert(data,function(rows){ })
113+
114+
```
115+
### mysql 批量插入数据 insert batch
116+
```javascript
117+
const data =[
118+
{name:"peter",
119+
age:"001",
120+
class:"001",
121+
school:"001"},
122+
{name:"peter002",
123+
age:"002",
124+
class:"002",
125+
school:"002"}
126+
]
127+
teacher.insert(data,function(rows){ })
128+
129+
```
130+
传入数组即可 批量插入数据
131+
132+
### mysql update 更新
133+
134+
```javascript
135+
const data ={
136+
class:"001",
137+
school:"001",
138+
}
139+
const where = {name:'peter'}
140+
teacher.update(data,where,function(rows){ })
141+
// 等价于
142+
c.query(`update teacher set class = "001" school = "001" where name = "peter"`)
143+
```
144+
update 为写操作 请谨慎 where参数为必填项
145+
146+
147+
### mysql update batch 批量更新
148+
实战:将特定id的数据的class字段更新
149+
```javascript
150+
const data =[
151+
{class:"001",id:"001"},
152+
{class:"002",id:"002"},
153+
{class:"003",id:"003"},
154+
]
155+
//更新字段为class 限制字段为 id
156+
const keys = {updatekey:'class', wherekey:'id'}
157+
const where = {age:'> 20'} //附加where 可选
158+
teacher.updateBatch(data,keys,where,function(rows){ })
159+
160+
```
161+
update 为写操作 请谨慎 where参数为必填项
162+
163+
164+
### mysql del 删除
165+
```javascript
166+
167+
const where = {age:'> 20'}
168+
teacher.del(where,function(rows){ })
169+
170+
```
171+
del 为写操作 请谨慎 where参数为必填项
172+
173+
### mysql query sql执行
174+
直接执行sql
175+
```javascript
176+
177+
const sql = "select * from teacher";
178+
teacher.query(sql,function(rows){ })
179+
180+
```
181+
182+
183+
### 快捷方式 getById updateById
184+
针对where只有id的情况
185+
```javascript
186+
187+
const id = 1
188+
teacher.getById(id,function(rows){ })
189+
teacher.updateById(data,id,function(rows){})
190+
teacher.countAllByWhere(where,function(rows){
191+
//返回 rows[0].count_total
192+
//数出符合where的总数量
193+
})
194+
195+
```
196+
### 时间统计快捷方式 countTodyByWhere countMonthByWhere countWeekByWhere
197+
根据时间字段进行统计 时间字段可以自定义
198+
```javascript
199+
//时间字段默认为 'create_time'
200+
teacher.countTodyByWhere(id,function(rows){
201+
//返回 rows[0].count_total
202+
//统计当天的数值
203+
})
204+
205+
206+
const time_colum_key = 'create_time'
207+
teacher.countMonthByWhere(id,time_colum_key,function(rows){
208+
//返回 rows[0].count_total
209+
})
210+
211+
```

demo/demo.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const conn = require("nodejs-mysql-plus")
2+
const c = new conn({
3+
host: 'xxxx',
4+
user: 'xxx',
5+
password: 'xx',
6+
database: 'xx',
7+
port: 3306,
8+
charset: "utf8mb4",
9+
timezone: 'Asia/Shanghai'
10+
});
11+
12+
const techer = c.table("techer")
13+
techer.getAllByWhere({name:'peter'},function(rows){
14+
console.log(rows)
15+
})
16+
techer.getColumnsByWhere(['name','sex'],{name:'peter'},function(rows){
17+
18+
})

index.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const Conn = require('./lib/Conn');
2+
module.exports.Conn = Conn;

0 commit comments

Comments
 (0)