Sequelize from routes? - node.js

How can I access sequelize from other routes ?, all the tutorials I've seen use sequelize from app.js, but if I want to use a model from other route I'll need to initialize it everytime. how can I initialize sequelize globally and call it from other routes ?

Kenny, try to see this example from sequelize's site: Usage with Express.JS, it's very easy to understand, and the good thing is that using this approach you can singleton all your models, it load the models when starting the application and then use the require('models') to grab them all and use it everywhere you like (globally).

Related

Pass fastify request schema to NestJS controller

I am trying to use fastify+NestJS. However, in order to use, in my opinion, the main strength of fastify, its speed, I need to get profit of fastify json schema. However, I do not understand how can I inject such a schema in a NestJS controller. Can you help me please?

What is a good practice for an additional layer for mongoDB requests

What are good practices to have the mongoose request in an other layer as in the controller?
My idea is to have the db request in an other layer as the controller,like in /mongodb/gatewaycontrollers.
So that different controllers can access the same method from the mongodb/gatewayscontrollers/todos.js, instead of repeating the same code in different controllers.
I'm thinking about a structure like this:
-controllers
-mongodb
--gatewaycontrollers
--models
-elasticsearch
--gatewaycontrollers
--models
-services
Are there any boilerplate examples or tutorial for that?

Sails JS add routes dynamically

I'm trying to find a way to add routes dynamically outsides of the config/routes.js file.
I'm creating a module who need some routes and don't want to let the user add it manually under config/routes.js.
Is there a way to do this ? I look sails doc and maybe with hooks I can't achieve this but can't find how
Thanks
I'm curious What kind of routing path you want to create?
Remember that wildcard is available in route.js
'GET /validEmail/*', "VerificationController.verifyEmail"'

Import only once a plugin in hapijs and use it everywhere

I should use a plugin named hapi-mongoose-db-connector into my hapijs application. In the repository page the developers suggest the ways you can import correctly it. It says that the following way is the bad way:
# from the server
mongoose = server.pack.plugins['hapi-mongoose-db-connector'].mongoose
# or from a plugin
mongoose = plugin.plugins['hapi-mongoose-db-connector'].mongoose
and discourages using it. Instead he recommends to do in the following way:
You do nothing and just require mongoose in your plugins. As npm
requires are singletons (the code is loaded only once this works very
well)
but he doesn't show any examples. At this point I'm not pretty sure how to use it. I wouldn't call in every js files mongoose. I would call it once in my application somewhere and in my js files where I create models for the database, use it. Do you know any best practices in those cases?
Actually, first one is the hapi way doing this kind of thing.
But as the mongoose module is a singleton, that plugin just require mongoose and initialize it [1] after load that plugin into hapi, you can use mongoose in any file;
var mongoose = require("mongoose");

mongoose and restify - localize strings before returning json

I would like to return localized Strings for multilanguage business objects in our RestFul API based on node.js, restify and mongoose. I have the requirement to store the translated resources on our translation resource server, but also need to support dynamic creation of those business objects.
I found a solution to easily plugin the i18n process in the POST/PUT calls using a single pre-'save' mongoose middleware on all Schema, when creating or updating my multi-languate business objects - this works because I am able to pass the request context to the obj.save(req, callback) call.
But, I am struggling to plug in the i18n on simple GETs. I thought of and tried different ways where I can plugin the i18n before returning the response, but don't really find a good way. Options I thought of:
translate in a mongoose middleware pre /post ('init'):
Problem: I don't have access to the request context, and therefore
don't know the locale to return, so I cannot translate there.
translate in the toObject() / toJSON {transform: }:
Same issue - i don't have the request context in these hooks.
translate in the handler/controller methods for each ressource.
Problem: Duplication, I have to do it everywhere, I would really prefer a solution I can define on the model/Schema layer
translate in a restify / express middleware towards the end:
Problem: I don't have access to the mongoose schema metainformation anymore, so I don't know which attriutes to translate.
Edit: just found this additional way:
- translate in a custom restify responseFormatter:
This seems to work nicely, in the reponseformatter I have access to everything I need. It kind of seems a little weird from an architechtural point of view, but if nobody has a better idea, I will add this as an answer.
Maybe (hopefully) I am missing something obvious...
thanks for any hints

Resources