generator-api icon indicating copy to clipboard operation
generator-api copied to clipboard

Question: where to place additional code for model, with different route?

Open ApayRus opened this issue 5 years ago • 2 comments

Thanks for boilerplate. It's very helpful. It's my first real project with express and mongoose, and I'm sorry if my question is stupid. I'm working on dictionary, and have an Article model:

const articleSchema = new Schema({
  wordTable: { type: String, required: true },
  wordForms: { type: [String] }
})

I need new route /search/:text This route not related to /article route, but method still related to article model: Article.find({ wordForms: text }) I'm a little confused, where to place this code. Can you give me an advice please?

ApayRus avatar May 05 '20 15:05 ApayRus

Hi @Aparus by any chance your project is open sources so I can take a look? I need a bit more context to help you out. If that route is not related to articles, what is related to? Maybe is related to another model? If it's related to another model and the route is there, it is totally ok to use the Article model reference from some other model. I hope this helps and sorry if the answer is a bit vague.

ndelvalle avatar May 06 '20 01:05 ndelvalle

For now my whole project is just your basic boilerplate, I swear there is nothing to share. :) I just little changed Article model, as I wrote above, from { title, body } to { wordTable, wordForms }. I just meant, that desired route is not part of link domain.com/article/, but completely different link domain.com/search/:text. And I can't write this route into /model/article/router.js, and there is no other than Article model for write it. Where is better place to write this route?

But this route should run method on the Article model, and find text in array field, and there will be other methods in the future, related only to Article model. All existing by default methods are written in lib/controller.js and lib/facade.js. And now I need unique method only for Article model, not shared with other models. I guess I should write it in model/article/controller.js and model/article/facade.js. Should I? Where to write not universal method, but especial method for one particular model? How to expand model?

Please, provide with couple examples, how to write unique method for only one model. And where to place strange routes :-)

I apologize for my ignorance, I worked last year with firebase and material-ui, and forget many of things I had learned before about express-mongoose stack.

ApayRus avatar May 06 '20 04:05 ApayRus