-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
wangjian
committed
Apr 29, 2020
1 parent
69d442c
commit 1857941
Showing
7 changed files
with
288 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,84 @@ | ||
## API列表 | ||
* 创建Mysql实例 | ||
```javascript | ||
new Mysql(config) | ||
``` | ||
|
||
| 参数 | 类型 | 描述 | | ||
| --- | --- | --- | | ||
| config | <code>object</code> | 数据库配置 | | ||
* CURD操作 [查看更多](./curd.md) | ||
* [.select()]() ⇒ Promise | ||
* 查询多条数据,结合.where, .limit避免数据量过大 | ||
* [.find()]() ⇒ Promise | ||
* 查询单条数据,是.select()的语法糖 | ||
* [.update()]() ⇒ Promise | ||
* 更新操作 | ||
* [.insert()]() ⇒ Promise | ||
* [.delete()]() ⇒ Promise | ||
* [.query(sql: string)]() ⇒ Promise | ||
|
||
* 链式方法 | ||
* [.table(tableName: string)]() ⇒ Mysql | ||
* 设置要查找的表名 | ||
* [.alias(tableAlias: string)]()⇒ Mysql | ||
* 给table添加别名 | ||
```javascript | ||
sql.table('demo').alias('d').field('id', 'age') | ||
=> SELECT id, age FROM demo as d | ||
``` | ||
* [.field(opt: string | any[])]() ⇒ Mysql | ||
* 要查询的字段 | ||
```javascript | ||
sql.table('demo').field(['id', 'age']) | ||
=> SELECT id, age FROM demo | ||
``` | ||
* [.where(opt: string | any[])]() ⇒ Mysql | ||
* 条件 | ||
* [.limit(limit)]() ⇒ Mysql | ||
* 返回的数量 | ||
* [.page(pageSize, pageNum)]() ⇒ Mysql | ||
* 条件 | ||
* [.data(data:object | any[])]() ⇒ Mysql | ||
* 用作单条(批量)插入或更新的数据 | ||
```javascript | ||
// 单条插入 | ||
sql.table('demo').data({age: 18, name: 'wanglin'}).insert() | ||
=> INSERT INTO (age,name) VALUES (18, 'wanglin') | ||
// 批量插入 | ||
sql.table('demo').data([{age: 18, name: 'wanglin'}, {age: 20, name: 'langzishenjian'}]).insert() | ||
=> INSERT INTO (age,name) VALUES (18, 'wanglin'), (20, 'langzishenjian') | ||
// 单条更新 | ||
sql.table('demo').data({age: 18, name: 'wanglin'}).upate() | ||
=> UPDATE demo SET age=18, name='wanglin' | ||
// 批量更新 | ||
sql.table('demo').data([{age: 18, name: 'wanglin'}, {age: 20, name: 'langzishenjian'}]).upate() | ||
=> UPDATE demo SET age=18, name='wanglin' | ||
``` | ||
* [.order(order)]() ⇒ Mysql | ||
* 条件 | ||
* [.join()]() ⇒ Mysql | ||
* 联合查询 | ||
* [.union()]() ⇒ Mysql | ||
* 条件 | ||
|
||
* 条件 | ||
* [.group(field)]() ⇒ Mysql | ||
* 条件 | ||
* [.having(field)]() ⇒ Mysql | ||
* 聚合条件 | ||
|
||
* [.count()]() ⇒ Promise | ||
* 统计数量 | ||
* [.max(field)]() ⇒ Mysql | ||
* 获取最大值 | ||
* [.min(field)]() ⇒ Mysql | ||
* 获取最小值 | ||
* [.avg(field)]() ⇒ Mysql | ||
* 获取平均值 | ||
* [.sum(field)]() ⇒ Mysql | ||
* 获取总数 | ||
|
||
* [.lock(field, step)]() ⇒ Promise | ||
* 锁 | ||
* [.distinct(field, step)]() ⇒ Promise | ||
* 去除查询结果中重复的数据 | ||
* [.comment(str:string)]() ⇒ Promise | ||
* 注释 | ||
* [.query()]() ⇒ string | ||
* 执行原生语句 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
## CURD操作 | ||
|
||
#### 原生 | ||
```js | ||
let sql = 'select id, age, name from demo where id=123' | ||
hMsql.query(sql); | ||
|
||
``` | ||
#### 插入 | ||
> 调用.insert()方法插入数据,由[.data()]()提供数据 | ||
```javascript | ||
// data为字符串,使用&相连 | ||
hMsql.table('demo') | ||
.data('age=18&name=wanglin') | ||
.insert() | ||
|
||
=> INSERT INTO demo (age,name) VALUES (`18`,`wanglin`) | ||
|
||
// data为对象 | ||
hMsql.table('demo') | ||
.data({ | ||
age:18, | ||
name: 'wanglin' | ||
}) | ||
.insert() | ||
=> INSERT INTO demo (age,name) VALUES (`18`,`wanglin`) | ||
|
||
// 批量插入 data为数组,提示:批量插入时会自动进行优化,当超过一定数量时,会对数组进行拆分和切换模式,具体可以查看.data() | ||
hMsql.table('demo') | ||
.data([{ | ||
age: 18, | ||
name: 'wanglin' | ||
}, | ||
{ | ||
age: 18, | ||
name: 'wanglin' | ||
}]) | ||
.insert() | ||
=> INSERT INTO demo (age,name) VALUES (`18`,`wanglin`), (`20`,`langzishenjian`) | ||
|
||
``` | ||
|
||
#### 更新 | ||
> 调用 .delete() 方法,必填.where() | ||
```javascript | ||
|
||
// 单条更新 | ||
hMsql.table('demo') | ||
.data({ | ||
age: 18, | ||
name: 'wanglin' | ||
}) | ||
.where({id: 123}) | ||
.update() | ||
=> UPDATE demo SET age=18, name='wanglin' WHERE id=123 | ||
|
||
// 批量更新 | ||
hMsql.table('demo') | ||
.data([{ | ||
age: 18, | ||
name: 'wanglin' | ||
}, | ||
{ | ||
age: 20, | ||
name: 'langzishenjian' | ||
}]) | ||
.where({IN: [2,3]}) | ||
.update() | ||
=> | ||
UPDATE demo | ||
SET age = CASE id | ||
WHEN 2 THEN 18 | ||
WHEN 3 THEN 20 | ||
END, | ||
name = CASE id | ||
WHEN 2 THEN 'wanglin' | ||
WHEN 3 THEN 'langzishenjian' | ||
END | ||
WHERE id IN (2,3) | ||
|
||
``` | ||
|
||
#### 删除 | ||
> 调用 .delete() 方法,必填.where() | ||
```javascript | ||
// 单条删除 | ||
hMsql.table('demo') | ||
.where({id: 123}) | ||
.delete() | ||
=> DELETE FROM demo WHERE id='123' | ||
|
||
// 批量删除 | ||
hMsql.table('demo') | ||
.where({IN: [123, 456, 789]}) | ||
.delete() | ||
=> DELETE FROM demo WHERE id IN (123,456, 789) | ||
|
||
``` | ||
|
||
#### 查询 | ||
* 普通查询 | ||
```javascript | ||
// 单条查询 | ||
hMsql.table('demo') | ||
.field('id, age, name') | ||
.where({id: 123}) | ||
.find() | ||
|
||
=> SELECT id, age, name FROM demo where id='123' limit 0, 1 | ||
|
||
// 多条查询 | ||
hMsql.table('demo') | ||
.field('id, age, name') | ||
.where({id: 123}) | ||
.select() | ||
|
||
=> SELECT id, age, name FROM demo where id='123' | ||
``` | ||
* 表达式查询 | ||
|
||
|名称(不区分大小写) | 描述 | | ||
| ------------- |:------------- | | ||
| EQ | 等于(=) | | ||
| NEQ | 不等于(<>) | | ||
| GT | 大于(>) | | ||
| EGT | 大于等于(>=) | | ||
| LT | 小于(<) | | ||
| ELT | 小于等于(<=) | | ||
| LIKE | 模糊查询 | | ||
| [NOT] BETWEEN | (不在)区间查询 | | ||
| [NOT] IN | (不在)IN 查询 | | ||
|
||
```javascript | ||
// 等于(EQ) | ||
hMsql.table('demo') | ||
.field('id, age, name') | ||
.where({id: {EQ: 123}}) | ||
.select() | ||
|
||
=> SELECT id, age, name FROM demo where id=123 | ||
|
||
// 不等于(NEQ) | ||
hMsql.table('demo') | ||
.field('id, age, name') | ||
.where({id: {NEQ: 123}}) | ||
.select() | ||
|
||
=> SELECT id, age, name FROM demo where id<>123 | ||
``` | ||
* 统计查询 | ||
|
||
|名称(不区分大小写) | 描述 | | ||
| ------------- |:------------- | | ||
| count | 统计数量,参数是要统计的字段名(可选) | | ||
| max | 获取最大值,参数是要统计的字段名(必须) | | ||
| min | 获取最小值,参数是要统计的字段名(必须) | | ||
| avg | 获取平均值,参数是要统计的字段名(必须) | | ||
| sum | 获取总分,参数是要统计的字段名(必须) | | ||
|
||
```javascript | ||
// 统计数量(count) | ||
hMsql.table('demo') | ||
.field('id, age, name') | ||
.where({id: 123}) | ||
.count() | ||
|
||
=> SELECT count(id) FROM demo where id=123 | ||
|
||
``` | ||
* 组合查询 | ||
* 复合查询 | ||
* 子查询 | ||
* 联合查询 | ||
* 区间查询 | ||
```javascript | ||
hMsql.table('demo') | ||
.field('id, age, name') | ||
.where({IN: [1,2,3]}) | ||
.select() | ||
|
||
=> SELECT id, age, name FROM demo where id IN (1,2,3) | ||
``` | ||
|
Oops, something went wrong.