var hooks = require('hooks')
, Document = require('./path/to/some/document/constructor');
// Add hooks' methods: `hook`, `pre`, and `post`
for (var k in hooks) {
Document[k] = hooks[k];
}
// Define a new method that is able to invoke pre and post middleware
Document.hook('save', Document.prototype.save);
// 上述代码在mongoose中实现
/////////////////////////////////////////////////////////////////////
// 下面的代码则是mongoose提供的Hook API
// Define a middleware function to be invoked before 'save'
Document.pre('save', function validate(next) {
// ...
});
// 设置 findOne 和 find 钩子
CompanySchema.pre('findOne', preFind).pre('find', preFind);
function preFind() {
var word = this.getQuery().word;
if(word === undefined) return;
// 从真实的Query中删掉虚拟属性
delete this._conditions.word;
// 构造正则表达式
var regex = new RegExp(word);
// 全文检索
this.where({ $or: [{ title: regex }, { content: regex }, { author: regex }] });
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有