Mongoose: Please write a section in the docs about how to write comments for Mongoose in the right way in NodeJS

Created on 4 May 2020  路  5Comments  路  Source: Automattic/mongoose

I tried a few ways to write comments for Mongoose schemas, but none worked.

Writing without JSDoc is hard. For example, this is undefined, and also outside of the model file methods cannot be found by VSCode.

How do you document your Mongoose stuff?

developer-experience

All 5 comments

For Typescript users there is https://github.com/typegoose/typegoose to define Mongoose schemas.

@meness can you please show some comments you tried to write that didn't work, and how you figured out that they don't work? I haven't run into a similar issue yet.

@vkarpov15 I am actually facing a similar issue, that I am considering moving statics to a different object from my mongoose models.

I am not sure if this is related to mongoose or typegoose, though.

Take a look at the code below

'use strict';
const mongoose = require('./');
const { Schema } = mongoose;

const userSchema = new Schema({
  age: { type: Number }
});

/**
 * @param {Number} x the first number
 * @param {Number} y the second number
 * @returns {Number} sum of both numbers
 */
userSchema.statics.add = function add(x, y) {
  return x + y;
};

const User = mongoose.model('User', userSchema);

// VSCode doesn't provide any information about this function
User.add();

Screenshot_403

Even attempting to add the add function directly to the User model
User.add = function add(x, y){ } doesn't do the trick.

@vkarpov15 I was commenting on a wrong line. It works now, but, still autocompletion for this keyword doesn't work.

/**
 * Finds User's default event.
 *
 * @param {ObjectId} user
 *
 * @memberof {Event}
 */
schema.statics.findUserDefaultEvent = async function findUserDefaultEvent(user) {
  return this.findOne({ user }).exec();
};

/**
 * @class Event
 */
const Event = model('Event', schema);

module.exports = Event;

Okay, apparently there's something special about add(...), comments work with other names.

One other somewhat-related issue, is code navigation. I can't use Go to definition on statics nor instance methods, so whenever I need to go to a piece of code I have to manually search for the file. Would be great if we could support that.

Was this page helpful?
0 / 5 - 0 ratings