1 Star 0 Fork 0

dzq / quick-mongo-simple

加入 Gitee
与超过 600 万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README.md

quick-mongo-simple

duzq-quick-mongo 示例

使用说明

安装

$ npm install duzq-quick-mongo

建立mongodb数据模型

const mongoose = require("../utils/mongodb")
const dayjs = require("dayjs")

// User模型
const UserSchema = new mongoose.Schema({
    id:{type:String, default: dayjs().unix()},
    name:String,
    pwd:{type: String,required:true,
        set(val){ // 密码加密
            return require("bcrypt").hashSync(val,10)
        }},
    mobile:{type: String, required:true},
    createTime:String,
    updateTime:String,
},{
    timestamps: { createdAt: 'createTime', updatedAt: 'updateTime' }
})
const User = mongoose.model("User",UserSchema)
// export
module.exports = User;

初始化控制器

const {Controller} = require("duzq-quick-mongo")
const user = new Controller( require("../models/User"))

添加路由

router.post("/add", user.add)
router.post("/getItem", user.getItem)
router.post("/delete", user.delete)
router.post("/update", user.update)
router.post("/list", user.list)
router.post("/search", user.search)

恭喜你。

实现了User模块的增删改查的功能。

接口使用

添加数据

请求参数

{
	"name": "dzq",
	"mobile": "13800138000",
	"pwd": "123456"
}

返回结果

{
	"code": 200,
	"msg": "success",
	"data": {
		"id": "1608954581",
		"_id": "5fe6b2f1eb030db3f5d4c1bd",
		"name": "dzq",
		"mobile": "13800138000",
		"pwd": "$2b$10$VMgVXPNSI7TuHtIYo0vY0ufi6PgsCEc.sv1VkSl0KKkd9Hv3u4gOO",
		"createTime": "Sat Dec 26 2020 11:50:10 GMT+0800 (China Standard Time)",
		"updateTime": "Sat Dec 26 2020 11:50:10 GMT+0800 (China Standard Time)",
		"__v": 0
	}
}

获取数据

请求参数

{
	"id": "1608954581"
}

返回结果

{
	"code": 200,
	"msg": "success",
	"data": {
		"id": "1608954581",
		"_id": "5fe6b2f1eb030db3f5d4c1bd",
		"name": "dzq",
		"mobile": "13800138000",
		"pwd": "$2b$10$VMgVXPNSI7TuHtIYo0vY0ufi6PgsCEc.sv1VkSl0KKkd9Hv3u4gOO",
		"createTime": "Sat Dec 26 2020 11:50:10 GMT+0800 (China Standard Time)",
		"updateTime": "Sat Dec 26 2020 11:50:10 GMT+0800 (China Standard Time)",
		"__v": 0
	}
}

删除数据

请求参数

{
	"id": "1608954581"
}

返回结果

{
	"code": 200,
	"msg": "success",
	"data": {
		"id": "1608954581",
		"_id": "5fe6b2f1eb030db3f5d4c1bd",
		"name": "dzq",
		"mobile": "13800138000",
		"pwd": "$2b$10$VMgVXPNSI7TuHtIYo0vY0ufi6PgsCEc.sv1VkSl0KKkd9Hv3u4gOO",
		"createTime": "Sat Dec 26 2020 11:50:10 GMT+0800 (China Standard Time)",
		"updateTime": "Sat Dec 26 2020 11:50:10 GMT+0800 (China Standard Time)",
		"__v": 0
	}
}

错误结果

{
	"code": 301,
	"msg": "failed"
}

更新数据

请求参数

{
	"id": "1608954581",
	"updateData": {
		"mobile": 13800138099,
		"pwd": "666666"
	}
}

获取数据列表

请求参数

{
	"pageSize": 10,
	"page": 1
}

返回结果

{
	"code": 200,
	"msg": "success",
	"data": {
		"pageSize": 10,
		"page": 1,
		"total": 1,
		"data": [
			{
				"id": "1608954581",
				"name": "dzq",
				"mobile": "13800138000",
				"pwd": "$2b$10$4otU4K9W08whZ3DFJyflBeXgxzRGrHpxlHKT940gDvvvgLmCBYT4a",
				"createTime": "Sat Dec 26 2020 11:57:19 GMT+0800 (China Standard Time)",
				"updateTime": "Sat Dec 26 2020 11:57:19 GMT+0800 (China Standard Time)"
			}
		]
	}
}

查找数据

请求参数

默认查询条件为与操作,条件满足其中一条需要设置operator为or

{
	"pageSize": 10,
	"page": 1,
	"conditions": {
		"name": "dzq",
		"mobile": "18518318421"
	},
	"operator": "or"
}

返回结果

{
	"code": 200,
	"msg": "success",
	"data": {
		"pageSize": 10,
		"page": 1,
		"total": 1,
		"data": [
			{
				"id": "1608954581",
				"name": "dzq",
				"mobile": "13800138000",
				"pwd": "$2b$10$4otU4K9W08whZ3DFJyflBeXgxzRGrHpxlHKT940gDvvvgLmCBYT4a",
				"createTime": "Sat Dec 26 2020 11:57:19 GMT+0800 (China Standard Time)",
				"updateTime": "Sat Dec 26 2020 11:57:19 GMT+0800 (China Standard Time)"
			}
		]
	}
}

高级应用

插件提供了一下高级自定义功能

const {Controller} = require("duzq-quick-mongo")
const user = new Controller( require("../models/User"))
// 设置数据查询字段
user.projection = {"__v":0,"pwd":0,"updateTime":0,"createTime":0}
// 设置成功编号
user.CODE_OK = 200
// 设置成功消息
user.MSG_OK = "请求成功"
// 设置失败编号
user.CODE_ERROR = 201
// 设置失败消息
user.MSG_ERROR = "请求错误"

自定义前返回数据

{
	"code": 200,
	"msg": "success",
	"data": {
		"id": "1608954581",
		"_id": "5fe6b2f1eb030db3f5d4c1bd",
		"name": "dzq",
		"mobile": "13800138000",
		"pwd": "$2b$10$VMgVXPNSI7TuHtIYo0vY0ufi6PgsCEc.sv1VkSl0KKkd9Hv3u4gOO",
		"createTime": "Sat Dec 26 2020 11:50:10 GMT+0800 (China Standard Time)",
		"updateTime": "Sat Dec 26 2020 11:50:10 GMT+0800 (China Standard Time)",
		"__v": 0
	}
}

自定义后获取数据

{
	"code": 200,
	"msg": "请求成功",
	"data": {
		"id": "1608954581",
		"_id": "5fe6b49feb030db3f5d4c1be",
		"name": "dzq",
		"mobile": "13800138000"
	}
}

更加功能需求请提交issue

https://github.com/dzq/quick-mongo

仓库评论 ( 0 )

你可以在登录后,发表评论

简介

暂无描述 展开 收起
JavaScript
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
JavaScript
1
https://gitee.com/duzengqiang/quick-mongo-simple.git
git@gitee.com:duzengqiang/quick-mongo-simple.git
duzengqiang
quick-mongo-simple
quick-mongo-simple
main

搜索帮助