Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSDoc + Mongoose : how to document Mongoose models?

I have Mongoose schema and a model:

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

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

How should I document (using JSDoc) MyClient and/or MyClientSchema to get tab-completion and type suggestions from WebStorm for both methods inherited from mongoose.model like remove, findOne, find - and inherited from schema - like phone_number and first_name ?

like image 475
Jan Grz Avatar asked Aug 12 '16 10:08

Jan Grz


People also ask

What is the difference between a Mongoose schema and model?

A Mongoose schema defines the structure of the document, default values, validators, etc., whereas a Mongoose model provides an interface to the database for creating, querying, updating, deleting records, etc.

What is a Mongoose document?

In Mongoose, a "document" generally means an instance of a model. You should not have to create an instance of the Document class without going through a model.

What is an instance of a model in Mongoose?

An instance of a model is called a document. Models are responsible for creating and reading documents from the underlying MongoDB database.

Is a Mongoose model a collection?

The mongoose. model() function of the mongoose module is used to create a collection of a particular database of MongoDB.


1 Answers

I found @class (or its synonym @constructor) works for schema properties:

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

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

The @alias works for methods declared the old way:

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

However, you can mark all methods as part of MyClient at once if you use the new way of declaring methods:

/**
 * @class MyClient
 * @mixes {MyClientSchema.methods}
 */
var MyClientSchema = new mongoose.Schema({ ...

/** @mixin */
MyClientSchema.methods;

MyClientSchema.methods.getDescription = function () {
    return this.first_name + " " + this.phone_number;
};

All the above tested in latest WebStorm version (2018.2). It works.

Things which do not work:

  • Mongoose built-in methods like .find() or .save()
  • The .methods syntax highlight works but code completion doesn't.

Updates are welcome!

like image 146
6 revs Avatar answered Oct 18 '22 12:10

6 revs