Multiple db connections in sails.js with migration setting per connection - node.js

I'm using two connections in sails app: mongodb and postgresql. For mongodb i want to use "alter" strategy, but postgres db must be readonly. Is there any way to achieve this?

You cannot define it at the connection level, but you can override the configuration for the models of your choice.
// in api/models/PosgresModel.js
module.exports = {
migrate: 'safe',
attributes: {
// The attributes definitions
}
}

Related

NestJs for lambda with mongodb creating too many connections

We are using nestjs for lambda which connects with mongodb for data. We are using nestjs mongoose module. However on deployment for each invocation a new set of connection are made and the previous ones are not released.
We are using forRootAsync
MongooseModule.forRootAsync({
useClass: MongooseConfigService,
})
service looks like this:
#Injectable({ scope: Scope.REQUEST })
export class MongooseConfigService implements MongooseOptionsFactory {
constructor(#Inject(REQUEST) private readonly request: Request) {}
async createMongooseOptions(): Promise<MongooseModuleOptions> {
if (Buffer.isBuffer(this.request.body)) {
this.request.body = JSON.parse(this.request.body.toString());
}
const { db } = this.request.body;
console.log('connecting database', db);
return {
uri: process.env.MONGO_URL,
dbName: db || '',
};
}
}
I understand we need to reuse the same connection. We have achieved it in nodejs by simply checking if the connection already exists and if it does not connect again. Not sure how to achieve the same in nest.js
tried changing the scope of service to Scope.DEFAULT but that didn't help.
I would suggest that you make a connection proxy for MongoDB. Ever time a lambda gets invoked, it will open up a new connection to MongoDB. AWS generally provides a service that allows you to proxy requests through one connect, this is a common issue with RDS etc.
This may help you though: https://www.mongodb.com/docs/atlas/manage-connections-aws-lambda/
We were able to resolve the issue by using a durable provider. NestJs documentation we created a strategy at the root that would use a parameter coming in each request. NestJs would than call the connection module only when a new connection was required.
Note: When you use durable providers, Requests doesn't have all the parameters anymore and now only has the tenantId as per the example.

SailsJS: Requests choking at database query via models

We have SailsJS app with postgresql adapter. Requests made to server chokes at database query via models. i.e Users.find(). Database server is up and running, connection string is correct and in place in datastore.js under default head
My guess is that it is not able to make connection to database, but it is also not failing with any error. How this can be debugged
Setup your models to use the Postgres adapter the config/models.js file will need to be updated:
module.exports.models = {
connection: 'postgres'
}

Mongoose behaving in surprise manner

I am working on nodejs mongoose for connecting to MongoDB, but I need 2 different connections to 2 different MongoDBs sitting on two different locations, what I did is as below :
global.otherDB = mongooseGlobal.createConnection(uri_1/DB1, { db: { safe: true });
global.mongoose = mongooseGlobal.connect(uri_2/DB2, { db: { safe: true });
Now where ever I need to connect to uri_2/DB2, I use mongoose and where ever I need to connect to uri_1/DB1, I use otherDB, now here is what is happening surprisingly,
Whenever I try to write or access DB1, it is creating or accessing collections available at uri_2/DB1 instead of uri_1/DB1, which is very surprising.
I didn't have any path of uri_2/DB1 while creating any mongo connection, how is it possible that DB1 is getting created at uri_2 instead of uri_1
Please give inputs.

How can I connect to multiple MongoDB databases using monk?

I have a Node.js application with 2 modules, each of which have their own database.
I'm using monk to connect to these databases, can they each connect to their own database, or am I limited to only one DB connection per app?
This code is used in both modules:
var mongo = require('mongodb');
var monk = require('monk');
...
module.exports = function(modules) {
...
var StorageClass = function() {
var myDb;
this.init = function() {
console.log('Connecting to Mongo DB on %s', config.database.URL);
myDb = monk(config.database.URL);
}
...
}
var storage = new CatchupStorageClass();
storage.init();
return storage;
}
Looks like when this code is executed in the second module, it wipes out the configuration for the first module and replaces it with its own. These 2 modules does not even use a shared storage class, they each have their own (duplicated) copy with a different name.
How is it possible to have more than one connection in a Node.js app? Does monk support it?
Yes, monk does support connections to different database in the same app.
My problem was coming from incorrect use of multiple base strategies in passportjs.
I simply needed to name my different base strategies separately: Use multiple local strategies in PassportJS

Using sails.js with an existing postgres database

I was looking at using Sails for an app that we are developing.
I'm using the sails-postgresql adapter which uses the waterline orm.
I have an existing database that I want to connect to.
If I create a model using generate something
and then in my model I have
attributes:{
title:{type:'String'}
}
If I browse to localhost/something the orm deletes all the columns in the something table except title.
Is there a way to stop it from doing this? This app should not delete columns on this database.
Thanks!
I am the author of Sails-Postgresql. Sails has an ORM called Waterline that it uses for managing data. The default setting assumes that you would want to auto-migrate your database to match your model attributes. Because Postgresql is a SQL database the Sails-Postgresql adapter has a setting called syncable that defaults to true. This would be false in a NoSQL database like redis.
This is easy to turn off if you want to manage your database columns yourself. You can add migrate: safe to your model and it won't try and update your database schema when you start Sails.
module.exports = {
adapter: 'postgresql',
migrate: 'safe',
attributes: {
title: { type: 'string' }
}
};
Sails doesn't have anything like migrations in Rails. It uses auto-migrations to attempt to remove this from your development process and then leaves updating your production schema to you.

Resources