1.0.1 • Published 2 years ago
123-asnicanc
Licence
ISC
Version
1.0.1
Deps
0
Size
10 kB
Vulns
0
Weekly
0
1用于nodejs,混合工具包。
接口使用
const toolKit = require("test-tool-kit");global.toolKit = toolKit;
文件操作:
toolKit.download(url, path)
下载文件
@param {*} url 下载文件接口
@param {*} path 保存本地地址
数据库操作:
toolKit.sql
mongodb 增删改查
| 方法 | 描述 | 代码 | 属性 | 属性类型 | 说明 |
|---|---|---|---|---|---|
| insert | 增 | insert({colName, data}) | colName | Function | 数据库集合 |
| data | Object | 插入数据 | |||
| remove | 删 | remove({colName, where={}, num=1}) | colName | Function | 数据库集合 |
| where | Object | 删除条件 | |||
| num | Number | 删除数量 | |||
| update | 改 | update({colName, where, data, num=1}) | colName | Function | 数据库集合 |
| where | Object | 修改条件 | |||
| data | Object | 修改数据 | |||
| num | Number | 修改数量 | |||
| get | 根据指定条件查 | get({colName, where={},}) | colName | Function | 数据库集合 |
| where | Object | 查找条件 | |||
| getPage | 查分页 | getPage({colName, where={}, index=0, num=10}) | colName | Function | 数据库集合 |
| where | Object | 查找条件 | |||
| index | Number | 查找页码 | |||
| num | Number | 查找数量 | |||
| getSort | 排序查 | getSort({colName, where={}, sort={},}) | colName | Function | 数据库集合 |
| where | Object | 查找条件 | |||
| sort | Object | 1为升序,-1为降序 | |||
| getMold | 查分类 | getMold({colName, keyWord}) | colName | Function | 数据库集合 |
| keyWord | String | 查找字段 | |||
| getLen | 查数量 | getLen({colName, keyWord}) | colName | Function | 数据库集合 |
测试代码演示
server.js
const express = require("express");
const bodyParser = require('body-parser');
const cors = require("cors");
const toolKit = require("test-tool-kit");
global.toolKit = toolKit;
const test = require("./www/sql/test");
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cors());
app.use(test);
app.listen(3000);
db.js
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/test',{
useNewUrlParser: true,
useUnifiedTopology: true
}).then(() => console.log('数据库连接成功!'))
.catch(() => console.log('数据库连接失败!'));
module.exports = mongoose
user.js
const db = require("./db");
var userObj = new db.Schema({
name: String,
});
module.exports = db.model("user", userObj, "user");
test.js
const express = require("express");
const user = require("./user");
const router = express();
router.post('/insert', (req, res) => {
const { body: { data } } = req
toolKit.sql.insert({
colName: user,
data: JSON.parse(data)
}).then(val => {
console.log(val)
res.status(200).json(val);
})
})
module.exports = router
版本改动
version 1.0.1
- 支持文件下载
version 1.0.2
- 区分下载接口http和https
version 1.0.3
- 增加mongodb数据库常用方法
version 1.0.4
- 增加接口提示