【问题标题】:JSDoc + Mongoose : how to document Mongoose models?JSDoc + Mongoose:如何记录 Mongoose 模型?
【发布时间】:2016-12-19 07:18:22
【问题描述】:

我有 Mongoose schema 和一个 model

var MyClientSchema = new mongoose.Schema({
    fist_name: {
        type: String
    },
    phone_number: {
        type: String
    }
});

var MyClient = mongoose.model('MyClient', MyClientSchema);

我应该如何记录(使用 JSDoc)MyClient 和/或MyClientSchema 以获取来自WebStorm 的制表符补全和输入建议,这两种方法都继承自mongoose.model,例如@987654327 @ - 并继承自架构 - 如 phone_number and first_name ?

【问题讨论】:

    标签: node.js mongoose jsdoc mongoose-schema


    【解决方案1】:

    我发现@class(或其同义词@constructor)适用于架构属性:

    /**
     * @class MyClient
     */
    var MyClientSchema = new mongoose.Schema({
        fist_name: {
            type: String
        },
        phone_number: {
            type: String
        }
    });
    
    var MyClient = mongoose.model('MyClient', MyClientSchema);
    

    @alias 适用于以旧方式声明的方法:

    /**
     * @alias MyClient.prototype.getDescription
     */
    MyClientSchema.method('getDescription', function () {
        return this.first_name + " " + this.phone_number;
    });
    

    但是,如果您使用声明方法的新方式,您可以一次性将所有方法标记为MyClient 的一部分

    /**
     * @class MyClient
     * @mixes {MyClientSchema.methods}
     */
    var MyClientSchema = new mongoose.Schema({ ...
    
    /** @mixin */
    MyClientSchema.methods;
    
    MyClientSchema.methods.getDescription = function () {
        return this.first_name + " " + this.phone_number;
    };
    

    以上所有内容均在最新的 WebStorm 版本 (2018.2) 中进行了测试。它有效。

    不起作用的东西:

    • Mongoose 内置方法,例如 .find().save()
    • .methods 语法高亮有效,但代码补全无效。

    欢迎更新!

    【讨论】:

    • 在导入 MyClient 的第二个文件中是否为您工作。是否需要任何其他标签才能使其正常工作?
    【解决方案2】:

    虽然它可能不符合您的特定要求,但官方文档中的这个 jet brains 教程解释了您需要的大部分内容

    https://www.jetbrains.com/help/webstorm/2017.1/creating-jsdoc-comments.html

    例如,

    /**
     * MyClientSchema schema
     * @constructor MyClient
     */
    
    var MyClientSchema = new mongoose.Schema({
            fist_name: {
                type: String
            },
            phone_number: {
                type: String
            }
        });
    

    下面的 js 可能会指导你做几乎所有你需要的事情

    http://nagyv.github.io/estisia-wall/models.js.html

    【讨论】:

    • 很好的回复。您如何处理模型使用的验证器?
    猜你喜欢
    • 2015-04-15
    • 2013-09-01
    • 2022-01-14
    • 1970-01-01
    • 2014-07-07
    • 2015-02-05
    • 2021-08-04
    • 1970-01-01
    相关资源
    最近更新 更多