Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic database connection in Sails Js

How I can set a database connection from a controller?. I need to specify in the controller the type of database , user, password, port, database name and set this in connections.js.

like image 842
José Hernández Avatar asked May 07 '15 21:05

José Hernández


People also ask

Does sails support multiple databases?

Like most MVC frameworks, Sails supports multiple databases. That means the syntax to query and manipulate our data is always the same, whether we're using MongoDB, MySQL, or any other supported database.

How do I use sails with datastore?

Since Sails uses sails-disk by default, you can start building your app with zero configuration, using a local temporary file as storage. When you're ready to switch to the real thing (and when everyone knows what that is), just change your app's datastore configuration.

How do I configure the sails/waterline adapters?

All supported adapters can be configured in roughly the same way: by passing in a Sails/Waterline adapter ( adapter ), as well as a connection URL ( url ). For more information on configuring datastores, see sails.config.datastores.

What is the ORM/ODM in sails?

Sails comes installed with a powerful ORM/ODM called Waterline, a datastore-agnostic tool that dramatically simplifies interaction with one or more databases. It provides an abstraction layer on top of the underlying database, allowing you to easily query and manipulate your data without writing vendor-specific integration code.


1 Answers

You could probably modify the sails.config.connections global and edit an existing connection. Of course this would only last until the app is restarted.

If you want to make it more permanent but not modify config js files you could then save the connection details on a localdb (sails-disk?) and re-set the connection properties (read from local db) on sails lift using config/bootstrap.js

like image 152
Salakar Avatar answered Nov 12 '22 11:11

Salakar