egg操作Mongodb
今天將給大家?guī)硗ㄟ^egg連接mongodb的相關(guān)總結(jié),具體包括:
- egg如何連接mongodb
- 通過保存數(shù)據(jù)驗證連接mongodb是成功的
egg連接mongodb
- 1油猫、 我們通過插件“egg-mongoose”連接mongodb數(shù)據(jù)庫荤堪,我們先安裝好插件:
npm i --save egg-mongoose
首先寺谤,我們通過egg的腳手架的方式創(chuàng)建egg工程,這一步不再細述,請參閱之前的文章凌彬。
- 2、 添加egg-mongoose 插件:在egg工程的config/plugin.js中添加插件egg-mongoose:
'use strict';
/** @type Egg.EggPlugin */
module.exports = {
mongoose: {
enable: true,
package: 'egg-mongoose',
},
};
- 3循衰、 添加mongodb的連接信息: 在config/config.default.js中添加mongodb的連接信息:
/* eslint valid-jsdoc: "off" */
'use strict';
/**
* @param {Egg.EggAppInfo} appInfo app info
*/
module.exports = appInfo => {
/**
* built-in config
* @type {Egg.EggAppConfig}
**/
const config = exports = {};
// use for cookie sign key, should change to your own and keep security
config.keys = appInfo.name + '_1564639326024_3672';
// add your middleware config here
config.middleware = [];
// 連接mongodb的配置
config.mongoose = {
client: {
url: 'mongodb://127.0.0.1/jianshu',
options: {},
},
};
// config.cluster = {
// listen: {
// port: 9999,
// },
// };
// add your user config here
const userConfig = {
// myAppName: 'egg',
};
return {
...config,
...userConfig,
};
};
到這里铲敛,我們就可以通過egg連接mongodb了。下面來驗證一下連接会钝!
驗證連接
準備工作: 創(chuàng)建集合對象
'use strict';
module.exports = app => {
const { mongoose } = app;
return mongoose.model(
'User',
new mongoose.Schema({
user_id: { type: String, unique: true },
user_name: { type: String },
age: { type: Number },
description: { type: String },
status: { type: Number },
}),
'user'
);
};
我們可以先去數(shù)據(jù)庫中查看相關(guān)數(shù)據(jù)情況伐蒋, 可以看到:目前僅僅只有user集合,且集合中無數(shù)據(jù):
此外迁酸,我們在app/目錄下創(chuàng)建core目錄先鱼,并封裝BaseController和BaseService:
'use strict';
const { Controller } = require('egg');
class BaseController extends Controller {
async returnService(promise) {
const [ error, data ] = await this.wapperError(promise);
if (error) {
this.error(error);
} else {
this.success(data);
}
}
success(data) {
this.ctx.body = { status: 'OK', data };
}
error(error) {
this.ctx.body = { status: 'NG', error: error.message || error };
}
wapperError(promise) {
return promise
.then(data => {
return [ undefined, data ];
})
.catch(err => [ err ]);
}
}
module.exports = BaseController;
'use strict';
const Service = require('egg').Service;
class BaseMongooseService extends Service {
get document() {
throw Error("BaseMongooseService need override property <document>'s get method!");
}
/**
* 分頁返回數(shù)據(jù)
* @param {Object} option 查詢參數(shù)
* @param {String} next 下一條數(shù)據(jù)的id
* @param {Number} limit 一頁包含多少數(shù)據(jù)
* @param {Array} includs 返回數(shù)據(jù)包含字段數(shù)組,為空返回全部字段
*/
async page(option, next, limit, includs, sort) {
limit = limit || 50;
const findLimit = limit + 1;
const projection = {};
if (includs && includs instanceof Array) {
includs.forEach(item => {
projection[item] = 1;
});
}
if (next) {
option._id = { $lte: next };
}
// const sortPrama ={ _id : -1 } ;
const sortPrama = (sort ? sort : { _id: -1 });
const data = await this.document
.find(option, projection)
.sort(sortPrama)
.limit(findLimit);
if (data.length === findLimit) {
return { next: data.pop()._id, data, total: data.length };
}
return { data, total: data.length };
}
/**
* 分頁返回數(shù)據(jù)
* @param {Object} option 查詢參數(shù)
* @param {number} next 下一條數(shù)據(jù)的id
* @param {Number} limit 一頁包含多少數(shù)據(jù)
* @param {Object} sort 排序
* @param {Array} includs 返回數(shù)據(jù)包含字段數(shù)組奸鬓,為空返回全部字段
*/
async pageList(option, next, limit, includs, sort) {
limit = limit || 50;
next = parseInt(next) || 1;
const projection = {};
if (includs && includs instanceof Array) {
includs.forEach(item => {
projection[item] = 1;
});
}
if (!sort) { throw Error('sort is not find '); }
const data = await this.document
.find(option, projection).skip((next - 1) * limit)
.sort(sort)
.limit(limit);
return { next: (next + 1), data, total: data.length };
}
/**
* 查詢
* @param {Object} option 查詢參數(shù)
* @param {Array} includs 返回數(shù)據(jù)包含字段數(shù)組焙畔,為空返回全部字段
* @return {Array} 查詢結(jié)果
*/
async find(option, includs) {
const projection = {};
if (includs && includs instanceof Array) {
includs.forEach(item => {
projection[item] = 1;
});
}
return await this.document.find(option, projection);
}
async findById(_id, includs) {
return await this.document.findOne({ _id }, includs);
}
async findOne(option, includs) {
const projection = {};
if (includs && includs instanceof Array) {
includs.forEach(item => {
projection[item] = 1;
});
}
return await this.document.findOne(option, projection).orFail();
}
async create(detail) {
// const Document = this.document;
const now = new Date().getTime();
const _create = {
create_stamp: now,
update_stamp: now,
};
return await new this.document(Object.assign(_create, detail)).save();
}
async update(option, detail) {
const now = new Date().getTime();
const _update = {
update_stamp: now,
};
await this.document.updateOne(option, Object.assign(_update, detail)).orFail();
}
async updateById(_id, detail) {
return await this.update({ _id }, detail);
}
}
module.exports = BaseMongooseService;
保存數(shù)據(jù),驗證連接成功
- 1串远、我們在app/controller/目錄下定義UserController.js宏多,并且包含add()接口,接收user_name, age參數(shù)信息澡罚,調(diào)用UserService完成對user數(shù)據(jù)的保存绷落。
'use strict';
const Controller = require('../core/BaseController');
class UserController extends Controller {
async add() {
const { userService } = this.ctx.service;
const { user_name, age } = this.ctx.request.body;
console.log('Received: user_name = ' + user_name + ', age = ' + age);
await this.returnService(userService.add(user_name, age));
}
}
module.exports = UserController;
- 2、創(chuàng)建app/service目錄始苇,創(chuàng)建UserService.js砌烁,接收Controller傳輸過來的數(shù)據(jù),完成對user的保存。
'use strict';
const Service = require('../core/BaseMongooseService');
const uuidv1 = require('uuid/v1');
class UserService extends Service {
get document() {
return this.ctx.model.User;
}
/**
* 添加用戶
* @param user_name
* @param age
* @returns {Promise<void>}
*/
async add(user_name, age) {
const { User } = this.ctx.model;
const userMap = new User();
userMap.user_name = user_name;
userMap.age = age;
userMap.user_id = uuidv1();
userMap.description = '用戶';
userMap.status = 0;
userMap.save();
}
}
module.exports = UserService;
- 3函喉、定義路由:在router.js下定義add接口的路由:
router.post('/add', controller.userController.add);
-
4避归、使用postman調(diào)用接口,完成保存
-
5管呵、調(diào)用接口梳毙,異常現(xiàn)象解決
在第4步捐下,我們直接調(diào)用postman之后账锹,程序會發(fā)生異常。異常如下:
這是因為egg的csrf防范的安全坷襟,我們在config.default.js中添加如下代碼即可解決:
config.security = {
csrf: {
enable: false,
ignoreJSON: true,
},
domainWhiteList: [ '*' ],
};
- 6奸柬、 正常調(diào)用結(jié)果:
postman:
console控制臺:
image.png
mongo數(shù)據(jù)庫:
至此,我們完成了egg連接mongodb步驟婴程。后面將詳細總結(jié)egg+mongo的增刪改查及多數(shù)據(jù)源情況下的配置廓奕。