mongoose再认识(三)
今天,说一个常见的知识点插件。对于不熟悉mongoose的人可能会问mongoose中也有插件?这个别说还真的有。
那么,在mongoose中的插件如何使用?
mongoose插件的使用
它和通常用的JavaScript的插件一样,都是为了实现代码的重用。
同mongoose再认识(二)中介绍的方法类似。可以在Schema的实例上添加。
首先,介绍一个api schema.add(),这个方法可以实现对Schema的扩充。
那么,可以紧接着mongoose再认识(二)中的代码来说,修改它的代码如下:
let UserSchema = new mongoose.Schema({
firstname: String,
lastname: String
})
UserSchema.add({
createAt: {
type: Date,
default: Date.now
},
updateAt: {
type: Date,
default: Date.now
}
})
UserSchema.pre('save', function(next) {
let now = Date.now()
this.updateAt = now;
if (!this.createAt) this.createAt = now;
})将createAt和updateAt的代码提取出来,因为在开发中,很多collection都需要它们,同样也可能需要用到它的处理方法。所以,用一个插件将它们封装起来变得很有必要。可参考如下代码:
module.exports = function(schema) {
schema.add({
createAt: {
type: Date,
default: Date.now
},
updateAt: {
type: Date,
default: Date.now
}
})
schema.pre('save', function(next) {
let now = Date.now()
this.updateAt = now;
if (!this.createAt) this.createAt = now;
})
}文件名为time-plugin.js
然后,在使用它的UserSchema定义中引用它。代码如下:
let UserSchema = new mongoose.Schema({
firstname: String,
lastname: String
})
let timePlugin = require('../plugins/time-plugin.js)
userSchema.plugin(timePlugin)在cnode-club的源码中定义了一个base_model.js文件,这个文件分别在topic.js 、 user.js等文件中进行了引用。
mongoose系列文章
相关推荐
mkhhxxttxs 2020-06-14
80500495 2020-06-14
80530895 2020-02-23
86211943 2019-12-20
lightlanguage 2019-12-16
80530895 2020-07-05
86211943 2020-03-01
80500495 2020-01-29
86211943 2020-01-24
lovecodeblog 2020-01-24
87261046 2019-12-23
MYRENZHIBO 2019-08-28
85234656 2018-09-03
fudirong 2012-04-12
80500495 2019-07-01