Multiple database connections with sailsjs - sails.js

It's possible to have a mysql connect for each registered and authenticated client ?
For example:
I need to have a different database for each client and I wonder if you can do this with the sailsjs.

No sure but I don't think so, in sailsjs we have :
config/env/production.js
models: {
connection: 'someMysqlServer'
}
So its possible to choose only one DB :
someMysqlServer: {
adapter: 'sails-mysql',
host: 'YOUR_MYSQL_SERVER_HOSTNAME_OR_IP_ADDRESS',
user: 'YOUR_MYSQL_USER',
password: 'YOUR_MYSQL_PASSWORD',
database: 'YOUR_MYSQL_DB'
},
And it's not a good idea to create a db for each user.
why not do this in your conception? in one DB you can have all users information.

Related

ASP.NET Core Entity Framework connection string more than 1 login user

I have a SQL Server A with DatabaseA, and I created 5 views based on tables from SQL Server B DatabaseB.
DatabaseA: login is userA, pwd: userA
DatabaseB: login is userB, pwd: userB
In the ASP.NET Core project startup, I have this code:
services.AddDbContext<DatabaseContext>(options => options.UseSqlServer(Configuration.GetConnectionString("sqlConnection"), b => b.MigrationsAssembly("API")));
and this connection string in appsettings.json:
"ConnectionStrings": {
"sqlConnection": "Server=TESTING\\SQLEXPRESS;Database=DatabaseA;User ID=userA;Password=userA;"
}
I get the error when I need to access the view
The server principal "userA" is not able to access the database "DatabaseB" under the current security context
How can I solve this?

Can I have two mongodb connections in the same sails project?

I have a sails app and I am wondering if I can use two mongodb connections (two different databases on the same host with different password). I have two models and I want each one to be related to a different mongodb
Yes, this is possible.
First thing to do is add a connection to each mongoDB in the connections.js file located in the config folder and ensure they both have a different name.
For example
someMongodbServer1: {
adapter: 'sails-mongo',
host: 'localhost',
port: 27017,
user: 'username', //optional
password: 'password', //optional
database: 'your_mongo_db_name_here' //optional
},
someMongodbServer2: {
adapter: 'sails-mongo',
host: 'localhost',
port: 27017,
user: 'username', //optional
password: 'password', //optional
database: 'your_mongo_db_name_here' //optional
},
Then in each Model, you can set the respective connection. If for example you have your User data stored in one database and your images stored in another.
You can set the connection in the User model like this:
module.exports = {
connection : 'someMongodbServer1',
attributes: {...
And in the Images model using the other connection:
module.exports = {
connection : 'someMongodbServer2',
attributes: {...
It is possible for a single Javascript runtime engine to have 2 or more client connections to MongoDB. Each connection will have a separate connection pool to its target server.

Configuring sails app for connection to Heroku Postgres using Environment Variables

Can a sails.js database connection be configured using a URI, or a URL which incorporates the credentials (host, user, password, and database) into one string? Or do they need to be individually assigned as their own key-value pairs in the configuration?
In development, I have configured my sails.js app to use environment variables for database credentials, as such:
in connections.js:
someSqlDatabase: {
host: process.env.dbHost,
user: process.env.dbUserName,
password: process.env.dbPassword,
database: process.env.db,
port: 5432,
ssl: true,
adapter: 'sails-postgresql'
This works for my connection to a heroku-postgresql resource that I've allocated to my project, and I am able to manually set "Config Vars" in my heroku instance, which I can use in the same way as environment variables in my local instance. By this I mean I can go to the heroku client page for the database resource, copy its credentials (host, database, user, and password) into the heroku app's configuration variables, individually.
The problem is that heroku periodically changes the credentials, and that my manually-copied configuration variables will become outdated, and require re-pasting whenever this happens. Obviously, this is not an ideal solution.
Heroku does automatically provide one configuration variable for the resource, called DATABASE_URL, which looks like:
"postgres://<user>:<password>#<host>:<port>/<database>"
which should be automatically updated, when the credentials change.
My problem is that I don't know how to configure a sails.js connection to use this url, in the place of individual "host", "user", "password", and "database" key/value pairs in the the connection config.
I've tried using a configuration that omits the individual keys and uses only "host", as such:
someSqlDatabase: {
host: process.env.DATABASE_URL,
ssl: true,
adapter: 'sails-postgresql'
}
But this connection fails. I've seen this kind of string called a URI, and I've tried the long-shot configuration:
someSqlDatabase: {
uri: proccess.env.DATABASE_URL
ssl: true,
adapter: 'sails-postgresql'
}
but this fails, as well. Is there a way to configure a sails-js connection with heroku postgresql database that will automatically use the latest credentials? (I am using sails v0.12.3)
I am not using PostgreSQL, but you could try/adapt this:
// before/outside module.exports
var url = require('url').parse(process.env.DATABASE_URL);
// within the config
someSqlDatabase: {
host: url.host,
user: url.auth.split(':')[0],
password: url.auth.split(':')[1],
database: url.pathname.substring(1),
port: url.port,
ssl: true,
adapter: 'sails-postgresql'
}
The following is probably the correct way to do this :
module.exports = {
...
default: {
ssl: true,
url: process.env.HEROKU_POSTGRESQL_SILVER_URL,
adapter: 'sails-postgresql'
},
...
}
inside config/env/production.js

sails-sqlserver: multiple sql servers on same host

I'm using sails-sqlserver where my host is running both SQL Server 2008 and 2012. In connections.js - host: is set to 'SQLServerhost\2012' I am getting the error "error: A hook (orm) failed to load!" Is there a different way to specify which SQL Server to connect to?
I've found success with the below format in /config/connections.js when I need to work with different databases on the same server (but see below suggestion):
sqlserverLOCAL: {
adapter: 'sails-sqlserver',
user: 'username',
password: 'password',
host: 'localhost', // Name or IP Address of SQL Server
port: '1433',
database: 'YourDBName' // i.e. Initial Catalog
},
sqlserver: {
adapter: 'sails-sqlserver',
user: 'username',
password: 'password',
host: '11.222.333.444', // IP Address of SQL Server
port: '1433',
database: 'OtherDBName' // i.e. Initial Catalog
}
In your case, perhaps setting both host: attributes to the same server (just SQLServerhost rather than SQLServerhost\2012) and then set the database: attribute to the appropriate Initial Catalog would be best. Also, make sure you double-check what ports the two SQL Servers are running on and set that attribute value accordingly.

How to use mongodb-express with MeteorJS' MongoDB?

I'm building an application in MeteorJS.
I want to have GUI access to built-in MongoDB database.
So I found: https://github.com/andzdroid/mongo-express
I installed it, configured it to connect to localhost:3001.
Since mongodb doesn't have a default admin password, I tried to create it by:
meteor mongo
use admin
db.addUser("admin","password")
then I set
adminUsername: 'admin',
adminPassword: 'password',
in mongo-express\config.js.
However when I open localhost:8081, it asks me login credentials again and even if I insert them manaully (admin, password) it doesn't work.
So I went back to meteor mongo, tried to create admin user again and go error
Error: couln't add user: User admin#admin" already exists
What am I doing wrong?
edit /usr/local/lib/node_modules/mongo-express/config.default.js
find and edit to:
} else {
mongo = {
db: 'meteor',
host: 'localhost',
password: '',
port: 3001,
ssl: false,
url: 'mongodb://localhost:3001/meteor',
username: '',
};
}