node 操作mongdb的常用函数记录(egg框架)
model
// app/model/appUser.js 'use strict'; /** * app用户 * */ module.exports = app => { const mongoose = app.mongoose; const Schema = mongoose.Schema; const AppUserSchema = new Schema({ //手机 phone:{ default:0, type:Number }, //用户昵称 nickName:{ default:'', type:String }, //用户名 username: { default:'', type: String }, //用户性别 0:男 1:女 sex:{ default:0, type:Number }, //密码 password: { default:'', type: String }, enable:{ default:true, type:Boolean }, //积分总数 integralTotal:{ default: 0, type: Number }, //创建时间 createTime:{ default:'', type:Date } }); return mongoose.model('AppUser', AppUserSchema); }
service
// app/service/appUser.js 'use strict'; const Service = require('egg').Service; const Utility = require('utility'); class AppUserService extends Service { //用户列表 async list(params) { return await this.ctx.model.AppUser.find(params).then((res)=>{ return { code:200, data:res, msg:'操作成功' } }); } //查询用户信息 async login(userInfo) { if (!userInfo.username) { return { code: 400, msg: '请输入用户名' } } else if (!userInfo.password) { return { code: 400, msg: '请输入密码' } } const result = await this.ctx.model.AppUser.find({ username: userInfo.username, password: Utility.md5(userInfo.password), enable:true }); if (result.length > 0) { return { code: 200, msg: '登录成功' } } else { return { code: 400, msg: '账号或者密码错误或者账户是禁用状态' } } } //新增用户 async add(userInfo) { if (!userInfo.username) { return { code: 400, msg: '用户名格式错误' } } else if (!userInfo.password) { return { code: 400, msg: '密码格式错误' } } const result = await this.ctx.model.AppUser.find({ username: userInfo.username }) .then(res => { return res.length }) .catch(err => { return err }) if (result > 0) { return { code: 400, msg: '该用户已存在' } } userInfo.createTime = this.ctx.time_format(new Date()); userInfo.password = Utility.md5(userInfo.password);//md5入库 const user = this.ctx.model.AppUser(userInfo).save(); return user .then(res => { return { code: 200, msg: '操作成功' } }) .catch(err => { return { code: 400, msg: '操作失败' } }) } //更新用户信息 async update(userInfo) { if (!userInfo.id) { return { code: 400, msg: 'id不能为空' } } let {id, createTime, ...params} = userInfo; const result = this.ctx.model.AppUser.updateOne({ "_id": userInfo.id }, params); return result .then(res => { if (!res.n) { return { code: 400, msg: '操作失败' } } else { return { code: 200, msg: '操作成功' } } }) .catch(err => { return { code: 400, msg: 'id参数错误' } }) } //删除用户 async delete(userInfo) { if (!userInfo.id) { return { code: 400, msg: 'id不能为空' } } const result = this.ctx.model.AppUser.deleteOne({ "_id": userInfo.id }); return result .then(res => { if (res.ok == 1) { return { code: 200, msg: '操作成功' } } }) .catch(err => { return { code: 400, msg: '操作失败' } }) } } module.exports = AppUserService;
controller
// app/controller/appUser.js 'use strict'; const Controller = require('egg').Controller; class AppUserController extends Controller { async list() { let query = this.ctx.query; this.ctx.body = await this.service.appUser.list(query); } async login(){ let query = this.ctx.query; this.ctx.body = await this.service.appUser.login(query); } async add() { let query = this.ctx.query; this.ctx.body = await this.service.appUser.add(query); } async update() { let query = this.ctx.query; this.ctx.body = await this.service.appUser.update(query); } async delete() { let query = this.ctx.query; this.ctx.body = await this.service.appUser.delete(query); } } module.exports = AppUserController;
router.js
// app/router.js 'use strict'; /** * @param {Egg.Application} app - egg application */ module.exports = app => { const { router, controller } = app; /********************user·start********************/ router.get('/appUser/login',controller.appUser.login);//用户登陆 router.get('/appUser/list', controller.appUser.list);//用户列表 router.get('/appUser/add', controller.appUser.add);//新增用户 router.get('/appUser/update', controller.appUser.update);//更新用户任意信息 router.get('/appUser/delete', controller.appUser.delete);//用户删除 /********************user·end********************/ };
愿你走出半生,归来仍是少年