These are chat archives for Automattic/mongoose

22nd
May 2018
SteffenLm
@SteffenLm
May 22 2018 07:04
Hey there, is it allowed to post any questions regarding mongoose and express in nodejs?
Kev
@lineus
May 22 2018 07:58
@SteffenLm yes
SteffenLm
@SteffenLm
May 22 2018 07:59
First of all, i am not very experienced for now, and i think, that my code fails, because i don't know exactly what i do ^^
Kev
@lineus
May 22 2018 08:03
@SteffenLm I can relate to that feeling. Just explain what you're trying to do and what's going wrong. I'll see if I can help.
SteffenLm
@SteffenLm
May 22 2018 08:20
Here is my first File, containing schema and some queries.

models/article.js

const mongoose = require('mongoose');

// article schema
let articleSchema = new mongoose.Schema({
    username: {type: String, required: true, index: true},
    title: {type: String, required: true, index: true},
    created: {type: Date, required: false, default: Date.now},
    modified: {type: Date, required: false, default: Date.now},
    div: {type: [{}]},
});

/**
 * adds article to backend
 * @constructor
 * @param {Object} article  - article wanted to save
 * @param {Object} callback - result
 */
function addArticle(article, callback) {
    this.create(article)
    .then((result)=> {
        callback(null, result);
    })
    .catch((err)=>{
        callback(err);
    });
}

articleSchema.static('addArticle', addArticle);

/*
*   Similar code (adding etc.)
*/

mongoose.model('Article', articleSchema);

questions

  • Is this coded as it should? I mean am i able to test this file
  • When to use articleSchema.static('addArticle', addArticle); and when to use articleSchema.method('addArticle', addArticle);
  • what does mongoose.model('Article', articleSchema); actully do, when i require it in another file?
Kev
@lineus
May 22 2018 08:44

Is this coded as it should? I mean am i able to test this file

I don't see anything wrong with it. You could remove required: false, but it's fine to leave it.

When to use articleSchema.static('addArticle', addArticle); and when to use articleSchema.method('addArticle', addArticle);

.method will add the function to docs, .static will add the function to the Model. Which one you use depends on the intent of the function. The value of this will be the model in static methods, and the doc in instance methods. Your choice of a static method in this case makes sense to me.

what does mongoose.model('Article', articleSchema); actully do, when i require it in another file?

.model compiles your schema and returns a model instance that is associated with a connection object. Compilation in this case is essentially registering middleware, aliases, virtuals, methods ( both static & instance ), etc.

SteffenLm
@SteffenLm
May 22 2018 08:59

Hey @lineus thanks a lot ;D
Okay, i understand Java. So it is the Similar thing, with static. But in Java e.g. you can Access the static methods withut initiating it or?
What i not understand is, where is this model instance saved? In the global scope?

const mongoose = require('mongoose');
require('../../models/article');

what do mean with that is associated with a connection object?

Kev
@lineus
May 22 2018 09:04
when you call mongoose.connect(someURL) the default connection mongoose.connection is the connection associated to the Model by default. If you had multiple connections, ie. you used const myConn = mongoose.createConnection(someURL), then you would need to call myConn.model() instead.
SteffenLm
@SteffenLm
May 22 2018 10:00
Thank you! Know i think i got a lot more understanding for it.
Kev
@lineus
May 22 2018 10:28
anytime @SteffenLm
SteffenLm
@SteffenLm
May 22 2018 14:03
the value of this will be the model in static methods, and the doc in instance methods.
Okay, but when do i need the document instance? Is there anything to obey/best practice/recommendation?
@lineus
Mihajlo Ilijić
@Pritilender
May 22 2018 14:19
Hi. Can plugins be ES6 classes?
Mihajlo Ilijić
@Pritilender
May 22 2018 14:27
Nvm, found the workaround my problem :)
Kev
@lineus
May 22 2018 14:29
@SteffenLm I think it really boils down to personal preference combined with what you want to accomplish. If you're hoping to have a method on docs returned from queries, an instance method gives you a way to do this. If you want to create a method on a model that keeps your code more DRY, you can do that. example
@Pritilender what was the workaround?
Mihajlo Ilijić
@Pritilender
May 22 2018 14:37
I'm using TypeScript and I need to write a plugin, but it has to have a special model interface on this, so I didn't know that I can pass this as first argument of a function (it's implicit in all the functions) and that I can set it's type.