node.js - 如何从类(函数)创建 Mongoose 模式

标签 node.js mongodb mongoose

我希望能够为 Mongoose 定义类,而不是使用正常的架构,然后为其定义函数。 我有一堂这样的课:

var odd = function() {
    this.name = String;
    this.someprop = {
        type: String,
        required: true
    }
}

这个类有一些功能:

odd.prototype.cake = function() {
    return "This cake has the name \"" + this.name + "\".";
}

通常在 Mongoose 中,我必须在创建模式后定义最后一个函数,但这样做会失去我的智能感知(比现在更多)。

有没有一种好方法可以让我的类成为 Mongoose 模式而不需要太多麻烦?

最佳答案

我能找到的最好方法是创建一个像这样的辅助函数,然后我将其放入一个单独的文件中:

var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var helpers = require("./helpers");

/**
 * Dark wizardry function that creates a mongoose schema from a normal JS class
 * Objects can also be passed along, and any properties that has methods will be turned into methods on the mongoose Schema.
 * @param c The class to creare the schema from
 * @param options The additional options to pass to the schema creation
 * @returns {Schema} A new mongoose schema for the describing the given class
 */
module.exports = function(c, options) {
    var f = null;
    // Figure out if f is an object or a function, and take appropriate action
    if(helpers.isFunction(c)) {
        f = new c();
    } else if(typeof f === "object") {
        f = c;
    } else {
        throw new TypeError("Class schema cannot work with that type. Whatever it was you supplied, probably a simple type. ");
    }

    var prop;
    var o = {};
    // Save all the properties of f into a new object
    for(prop in f) {
        var p = f[prop];
        switch (p) {
            case String:
            case Number:
            case Date:
            case Buffer:
            case Boolean:
            case mongoose.Types.Mixed:
            case mongoose.Types.ObjectId:
            case Array:
                o[prop] = p;
                break;
            default:
                if(!helpers.isFunction(p)) {
                    o[prop] = p;
                }
        }
    }
    // Create the schema
    var sch = new Schema(o, options);
    // Create the methods for the schema
    for(prop in f) {
        if (prop in f) {
            var func = f[prop];
            switch (func) {
                case String:
                case Number:
                case Date:
                case Buffer:
                case Boolean:
                case mongoose.Types.Mixed:
                case mongoose.Types.ObjectId:
                case Array:
                    continue
            }
            if (helpers.isFunction(func)) {
                sch.methods[prop] = func;
            }
        }
    }
    return sch;
};

我的helpers.js包含一个非常简单的isFunction函数:

function isFunction(f) {
    return typeof f === "function";
}

exports.isFunction = isFunction;

然后,每当我想要 Mongoose 模式时,我只需执行以下操作:

var mongoose = require("mongoose");
var classSchema = require("./class-schema");
var odd = function() {
   ...
}

odd.prototype.cake = function() {
   ...
}

var oddSchema = classSchema(odd, {timestamps: true});

module.exports = mongoose.model("Odd", oddSchema);

这将创建一个与原始odd类具有相同属性和相同功能的模型。

关于node.js - 如何从类(函数)创建 Mongoose 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35492310/

相关文章:

node.js - 错误 : connect ETIMEDOUT at TCPConnectWrap. afterConnect [as oncomplete]

javascript - 无法对此特定服务进行 SOAP 调用, "undefined"目标命名空间

javascript - Yarn 3.1,Vite 2.9,找不到包vite

javascript - MongoDB - Mongoose 查询 findOneAndUpdate() 不会更新/复制数据库

node.js - 使用 mongoose 将对象数组推送到 mongodb 中

javascript - 为什么这个简单的 ping pong socket.io 示例在短时间内就失败了? (数据负载未定义)

javascript - 在集合的键中查找最大数据长度

arrays - mongodb查询大于和小于的数组值

javascript - mongodb中另一个模型中的数组中仅保存objectID

node.js - 如何使用expressJS使用新数据重新渲染 View ?