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");
Related
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).
Hi i removed waterline ORM from Sails JS and added mongoose, for that i followed the steps specified in the following link
http://laurentschaffner.com/blog/from-waterline-to-mongoose-in-sails/
But now am facing issues in installing sails-auth module can anyone help me out please.
I haven't tried to install sails-auth but I have successfully integrated Mongoose into Sails in a manner that I think is more friendly than the blog link you referenced. I created a gist here to help:
Gist: Adding Mongoose to Sails in a manner that binds promisified Mongoose functions to Model.mongoose, preserves blueprints (auto-generated action routes), and most importantly does not remove the Waterline ORM. Note: the code below uses ES2015 with the sails-babel hook.
https://gist.github.com/austinmao/83524cecd87ea1685b32
Hi I am making a Cordova app for iOS and Android. I am trying to use the cloudant module https://github.com/cloudant/nodejs-cloudant to access my cloudant couchDB, but for the life of me, I cannot figure out the issue I am having while installing it. Using linux, I executed npm install --save cloudant at the root of my Cordova project folder. When using var Cloudant = cordova.require('cloudant'); in my code, I get the error Uncaught module cloudant not found from within my cordova.js file as it tries to load the module. Also when I run $ node -e 'require("cloudant"); console.log("Cloudant works");' in my shell, I receive a terminal output stating "Cloudant works". I cannot seem to figure out what the issue is, and have tried many different things. Any help is extremely appreciated, as I am at my witts end at this point. Thanks.
As #Parth says, 'nodejs-cloudant' is a Node.js/npm module. To access Cloudant from within your Cordova application you will need client side code to interact with Cloudant's HTTP API.
You could use jQuery's Ajax functions to make HTTP requests to Cloudant or you can use PouchDB as a client-side library e.g:
<script src="pouchdb-3.4.0.min.js"></script>
<script>
var db = new PouchDB("https://myusername:mypassword#myaccount.cloudant.com/mydb";
</script>
Both solutions require you to enable CORS on your Cloudant account.
'nodejs-cloudant' is an npm module and not a cordova module. you just need to write
var Cloudant = require('cloudant'); That should solve the problem.
Try with this:
var Cloudant = require('cloudant');
Instead of importing cloudant directly, try using cradle or nano for accessing cloudant
'DB.const nano = require('nano')('//cloudant api link//');'
This works
https://www.npmjs.com/package/nano#dbinsertdoc-params-callback
https://www.npmjs.com/package/cradle
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
I am following along with a node.js and express tutorial at DailyJS and have already hit a wall.
In the tutorial it says to do this:
mongoose = require('mongoose').Mongoose
db = mongoose.connect('mongodb://localhost/nodepad')
but beforehand he was talking about the code inside the app.js file, so I assume he means for the above code to go inside app.js but I don't know. At this point I download the nodepad from github to see where he actually put the mongoose code, and I can't find the above code anywhere in the entire application!
So I am at a loss. Things are already confusing because a default skeleton site made by express is different in many ways to the tutorial, because express has changed a lot in the 2 years since the tutorial was written.
So i'm trying to follow along but I just get really stuck where he says write some code but gives no indication where this code is meant to go.
And afterwards the tutorial says to add a models.js file, and I wonder where this goes, inside the directory perhaps?
(I also understand the above code is outdated and the correct code can be found here, the problem is not the code but where in express its meant to be placed)
Please give me some guidance.
Thanks
Both of those go in app.js. I can give you some more details. I use mongoose and express all the time. I don't use var mongoose = require('mongoose').Mongoose All I use is var mongoose = require('mongoose')
Let me know if you need any further help.