using different databses on same page - node.js

How can we access data from two different databases, on a same page using MEAN-stack? For example I want to give user Information and user watch history(both are different databases) on same page?

you could just create multiple files for your separate mongoose connections.
for example in db_one.js:
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/db_one');
module.exports = mongoose;
and then in db_two.js
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/db_two');
module.exports = mongoose;
and then import them in other Files as you wish

Related

node.js mongoose multiple database across project using same schema

I want to make use of multiple databases in a project, but I am having difficulty in calling the
secondary databases across many route controllers. I do not want to close connection to the main databases
the current method which I have used to insert data into multiple controllers I believe is not efficient
enough i.e.
------>> all these are called every time a file needs to connect to the secondary database
----------------------------------------------------------------------------------------|
var mongoose = require('mongoose'); |
var mongooseConnect = mongoose.createConnection('mongodb://localhost/27017/'+dbName); |
|
require('./models'); |
var modelData = mongooseConnect.model('models'); |
----------------------------------------------------------------------------------------|
modelData.save()
I am looking for a method which would allow me to
connect to the secondary database once and call it anytime i want to across the controllers
combine the above code into a one line code
I have called the main database through the normal means which is
route.js
var mongoose = require('mongoose');
mongoose.connect(mongodbhost/maindb)
then in the schema I just simply require mongoose before using it in the schema. and only the schema is called in the
route controllers.
Any help would be appreciated
You can use the same schema in multiple database but you would need to create the connections to both databases. Here's how you can do it:
const conn1 = mongoose.createConnection('mongodb://localhost:27017/test1');
const conn2 = mongoose.createConnection('mongodb://localhost:27017/test2');
const TestSchema = require('./TestSchema');
var TestModelDb1 = conn1.model('Model', TestSchema);
var TestModelDb2 = conn2.model('Model', TestSchema);

More than one Mongo endpoint in same API

My NodeJS application has form with text input field (for search) and a dropdown mongo for DEV, UAT and Production database options.
Based on the user selection respective database has to be accessed.
I want to know how to dynamically handle /change different database endpoint or change node env in run-time ?
One way that comes to my mind is to disconnect and connect again. If you are using mongoose, do something like:
var mongoose = require('mongoose')
...
try {
mongoose.disconnect();
mongoose.connect(mongoURL);
catch (e) {
console.log(e);
}
every time and take the mongoURL from the user input.
Another way is to use multiple connections:
var mongoose = require('mongoose')
var conn = mongoose.createConnection('mongodb://localhost/db1');
var conn2 = mongoose.createConnection('mongodb://localhost/db2');
and then choose the connection that you want to use depending on the user choice. I prefer this last one.
Take a look at this answer for more info:
https://stackoverflow.com/a/32909008/7041393

Mongoose models scheme in separate modul

Disclaimer:
I am new to mongoose/node, so please excuse me if I am misunderstanding some basic things.
Yes, I found already a few postings about this topic, but could not adapt it to my needs.
I structured my main-project into multiple separate projects. One separation is the "app-core" project, which will contain the core-models and -modules, to be injected by each other project (app-core is configured as dependency in the package.json file of each project).
A (simplified) model within the app-core currently looks like this:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var IndustrySchema = new Schema({
name: {
type: String,
required: true
}
});
module.exports = mongoose.model('Industry', IndustrySchema);
The wep-app includes this model as follow:
var Industry = require('app-core/models/Industry');
and creates the MongoDB connection like this:
var DB_URL = 'mongodb://localhost/hellowins';
var mongoose = require('mongoose');
var mongooseClient = mongoose.connect(DB_URL);
mongooseClient.connection.on('connected',function () {
console.log("MongoDB is connected");
});
Now I have the problem, that the model will not use the mongo connection that is defined in the app-web project, rather it will consider the connection configured in the app-core.
Due to encapsulation and responsibility design I definitly don't want the core to define the connections for each possible app (which may include the core-app).
So somehow I need to specify the the scheme only in the core.
I read already that I should not require the model itself (/app-core/models/Industry), and use the mongoose model instead
var Industry = mongoose.model("Industry");
But then I get the error
MissingSchemaError: Schema hasn't been registered for model "Test"
To fix this, I should register the models manually, like adviced in the first link (at the top of my posting). But somehow I don't like this approach, because I'd need to extend this everytime the application uses a new model.
And further I need a mongo connection even in the core-app - at least to run the mocha tests.
So I am bit confused about how to structure the architecture in this case.
UPDATE #1
I found now one working solution. But, unfortunately, this does not fit my requirements completely, because it is pretty difficult (resp. ugly) to extend a model by hooks (ie TestSchema.pre('save'..)).
Model (app-core)
exports.model = {
name: {
type: String,
required: true
}
};
models.js (app-web, executed once on startup)
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var models = ['Test']; // add many
exports.initialize = function() {
var l = models.length;
for (var i = 0; i < l; i++) {
var model = require('hw-core/models/' + models[i]);
var ModelSchema = new Schema(model.model);
module.exports = mongoose.model(models[i], ModelSchema);
}
};
app.js (web-app)
require('./conf/app_models.js').initialize();
Then I just can get a model as follow
var mongoose = require('mongoose');
var TestModel = mongoose.model("Test");
var Test = new TestModel();
Why don't you try to export the mongoose instance from your app-core module and use it later in the web-app to connect to a database
app-core index.js
var mongoose = require('mongoose');
module.exports = {
mongooseInstance: mongoose };
web-app index.js
var core = require('app-core'),
mongoose = core.mongooseInstance,
mongooseClient = mongoose.connect(DB_URL);
// and so on
This might work as long as you require your models in your controllers which are initialized after the code from the index.js. I hope my response is helpful.

module.export is not working CompoundJS?

I'm new to CompoundJS. I'm working on a sample app. If following the below tutorial:
Using Mongoose Models
I have written the below code in db/schema.js file:
customSchema(function () {
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/my_database');
var Schema = mongoose.Schema, ObjectId = Schema.ObjectId;
module.exports["mongoose"] = mongoose;
module.exports["model"] = {}
});
In my model.js file (person.js), I have the code below:
var Person = mongoose.model('Person', new module.schema());
model['Person'] = Person;
When I run compound s , I'm getting ReferenceError: mongoose is not defined.
Any help would be appreciable. Thanks in advance.
Wiki is outdated, all documentation currently available at http://compoundjs.com, and i will remove wiki to avoid confusions.
You can use this example if you need mongoose as ORM: https://github.com/anatoliychakkaev/mongoose-compound-example-app

mongoose schema creation

I've just started with mongoose. I have a creation script with mongoose that creates the schemas and db with sample data.
Now I write the actual application. Do I need to create the schema object each time my application runs, or is it already available somehow?
In other words do I need to run this code in every app that uses mongoose to access the db or just the first time:
var Comments = new Schema({
title : String
, body : String
, date : Date
});
How would the answer change if I have setters/validations/etc?
One defines Schema so application understands how to map data from the MongoDB into JavaScript objects. Schema is a part of application. It has nothing to do with database. It only maps database into JavaScript objects. So yes - if you want to have nice mapping you need to run this code in every application that needs it. It also applies to getters/setters/validations/etc.
Note however that doing this:
var mongoose = require('mongoose');
var Schema = mongoose.Schema; // <-- EDIT: missing in the original post
var Comments = new Schema({
title : String
, body : String
, date : Date
});
mongoose.model("Comments", Comments);
will register Schema globaly. This means that if the application you are running is using some exterior module, then in this module you can simply use
var mongoose = require('mongoose');
var Comments = mongoose.model("Comments");
Comments.find(function(err, comments) {
// some code here
});
(note that you actually need to register the Schema before using this code, otherwise an exception will be thrown).
However all of this works only inside one node session, so if you are running another node app which needs the access to the Schema, then you need to call the registration code. So it is a good idea to define all Schemas in separate files, for example comments.js may look like this
var mongoose = require('mongoose');
var Schema = mongoose.Schema; // <-- EDIT: missing in the original post
module.exports = function() {
var Comments = new Schema({
title : String
, body : String
, date : Date
});
mongoose.model("Comments", Comments);
};
then create file models.js which may look like this
var models = ['comments.js', 'someothermodel.js', ...];
exports.initialize = function() {
var l = models.length;
for (var i = 0; i < l; i++) {
require(models[i])();
}
};
Now calling require('models.js').initialize(); will initialize all of your Schemas for a given node session.
You do need to run this initialization code every time you run your app to register your app's Schemas with mongoose.
When your app ends, mongoose does not store your Schema(s). So, the next time you run an app that uses a Schema, you need to register your Schema(s) again.
However, it's fairly easy to set up your app to do so.
Here are two links to code that demonstrates how one can initialize schemas in mongoose. The first is in JavaScript, the second is in CoffeeScript.
https://github.com/fbeshears/register_models
https://github.com/fbeshears/register_coffee_models
The JavaScript demos is just one app.
The CoffeeScript code has two separate apps. The first stores documents with MongoDB, the second finds and displays the documents stored by the first app.

Resources