JSDoc + Mongoose:如何记录Mongoose模型?

Jan*_*sch 13 mongoose node.js jsdoc mongoose-schema

我有Mongoose 架构和模型:

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

var MyClient = mongoose.model('MyClient', MyClientSchema);
Run Code Online (Sandbox Code Playgroud)

我应该如何记录(使用JSDoc)MyClient和/或MyClientSchema获取tab-completion并从WebStorm两个继承自mongoose.model类似的方法中输入建议?remove, findOne, find并从类似继承phone_number and first_name?

Vas*_*iak 7

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

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

var MyClient = mongoose.model('MyClient', MyClientSchema);
Run Code Online (Sandbox Code Playgroud)

@alias以旧方式声明的方法的作品:

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

但是,如果您使用声明方法的新方式,则可以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;
};
Run Code Online (Sandbox Code Playgroud)

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

不起作用的事情:

  • Mongoose 内置方法,如.find()或.save()
  • 该.methods语法高亮工程,但代码完成没有。

欢迎更新!


Kal*_*mel 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
        }
    });
Run Code Online (Sandbox Code Playgroud)

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

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