编写一个Mongoose Plug In-plugin()方法?

时间:2014-06-07 05:34:07

标签: node.js mongodb mongoose

我有兴趣编写一个mongoose插件来使所有字段都需要。我知道还有其他方法可以做到这一点,但我喜欢编写自己的插件的想法。

来自文档http://mongoosejs.com/docs/plugins

// game-schema.js
var lastMod = require('./lastMod');
var Game = new Schema({ ... });
Game.plugin(lastMod, { index: true });

但是当我从我的架构创建模型并查看属性时,我没有看到plugin()方法:

var mongoose = require('mongoose');
var CpuSchema = require("../schemas/cpu");

var Cpu = mongoose.model('Cpu', CpuSchema);
console.log(Cpu);

module.exports = Cpu;

one@demo ~/cloudimageshare-monitoring/project $ node /home/one/cloudimageshare-monitoring/project/app/data/models/cpu.js  
{ [Function: model]
  base: 
   { connections: [ [Object] ],
     plugins: [],
     models: { Cpu: [Circular] },
     modelSchemas: { Cpu: [Object] },
     options: { pluralization: true } },
  modelName: 'Cpu',
  model: [Function: model],
  db: 
   { base: 
      { connections: [Object],
        plugins: [],
        models: [Object],
        modelSchemas: [Object],
        options: [Object] },
     collections: { cpus: [Object] },
     models: {},
     replica: false,
     hosts: null,
     host: null,
     port: null,
     user: null,
     pass: null,
     name: null,
     options: null,
     otherDbs: [],
     _readyState: 0,
     _closeCalled: false,
     _hasOpened: false,
     _listening: false },
  discriminators: undefined,
  schema: 
   { paths: 
      { timeStamp: [Object],
        avaiable: [Object],
        status: [Object],
        metrics: [Object],
        _id: [Object],
        __v: [Object] },
     subpaths: {},
     virtuals: { id: [Object] },
     nested: {},
     inherits: {},
     callQueue: [],
     _indexes: [],
     methods: {},
     statics: {},
     tree: 
      { timeStamp: [Object],
        avaiable: [Function: Boolean],
        status: [Function: String],
        metrics: [Object],
        _id: [Object],
        id: [Object],
        __v: [Function: Number] },
     _requiredpaths: undefined,
     discriminatorMapping: undefined,
     _indexedpaths: undefined,
     options: 
      { id: true,
        noVirtualId: false,
        _id: true,
        noId: false,
        read: null,
        shardKey: null,
        autoIndex: true,
        minimize: true,
        discriminatorKey: '__t',
        versionKey: '__v',
        capped: false,
        bufferCommands: true,
        strict: true,
        pluralization: true },
     _events: {} },
  options: undefined,
  collection: 
   { collection: null,
     opts: { bufferCommands: true, capped: false },
     name: 'cpus',
     conn: 
      { base: [Object],
        collections: [Object],
        models: {},
        replica: false,
        hosts: null,
        host: null,
        port: null,
        user: null,
        pass: null,
        name: null,
        options: null,
        otherDbs: [],
        _readyState: 0,
        _closeCalled: false,
        _hasOpened: false,
        _listening: false },
     queue: [ [Object] ],
     buffer: true } }

在模型上,我没有看到plugin()方法。

1 个答案:

答案 0 :(得分:4)

plugin类定义了Schema方法,您可以在CpuSchema对象上看到它。 在Cpu模型上,您可以通过调用

来获取它
console.log(Cpu.schema.plugin)

来自GitHub上的mongoose source代码:

/**
* Registers a plugin for this schema.
*
* @param {Function} plugin callback
* @param {Object} opts
* @see plugins
* @api public
*/

Schema.prototype.plugin = function (fn, opts) {
  fn(this, opts);
  return this;
};

当您将plugin函数传递给它时,它只需执行该函数并将schema引用传递给它。