How to use mysql with Sails ? Also, if I change the database to a mySQL one, do I lose all my current model data ?
I spent some time looking for tutorials and demos but haven't found any.
In order to use mySQL with sails, you will have to:
Define an adapter in config/adapters
Example adapters file:
module.exports.adapters = {
'default': 'disk',
disk: {
module: 'sails-disk'
},
'mysql-adapter': {
module: 'sails-mysql',
host: 'HOST',
user: 'USER',
password: 'PASSWORD',
database: 'DATABASE'
}
};
Make any model use the new mysql adapter (ex. api/models/Contact.js)
module.exports = {
tableName: 'contacts',
adapter: 'mysql-adapter',
migrate: 'safe',
attributes: { ... }
}
Then it should work.
Related
I'm using Sequelize ORM for my nodejs project and it seems there is a mistake in configuration or I'm doing something really awry with the queries... The problem is that our client will make more than 60 connections sometimes when the user is requesting a lot of information at once (graphql request with deep structure and data loaders on top for a lot of records fetched from MySQL). The pool settings should not let do that since it is configured for 10 max connections at once. The main question is how those connections appear? The app is being hosted at AWS using ELB, for MySQL we are using RDS and we can have up to 2 app instances at once, so no more than 20 connections at all. Sequelize config:
production: {
username: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.DB_NAME,
host: process.env.DB_HOST,
dialect: 'mysql',
define: {
charset: 'utf8mb4',
collate: 'utf8mb4_general_ci',
},
pool: {
max: parseInt(process.env.DB_POOL_SIZE, 10), // 10 from ENV variables
min: 0,
acquire: 60000,
idle: 60000,
},
operatorsAliases: 0,
logging: false,
},
once there are >50 connections on our RDS client we can see that our ELB can't perform health check since it can't make new connections and return positive status from MySQL. so our app becomes unreachable.
After bombing the backend with this particular query I've fetched processlist for MySQL. Some sleeping queries will go to more than 1000 "time".
Also I'm going to attach my Sequelize connection config. It is a singleton object which will be run only once the app is loaded. But on the other hand, I'm importing models from this file everywhere in the project. But is it a mistake?
/* eslint-disable import/no-cycle */
import { Sequelize } from 'sequelize-typescript';
import databaseConfig from '#config/database';
import models from './models';
const env = process.env.NODE_ENV || 'development';
const database = (databaseConfig as any)[env];
const sequelize = new Sequelize(database.database, database.username, database.password, {
host: database.host,
dialect: database.dialect,
define: database.define,
pool: database.pool,
operatorsAliases: database.operatorsAliases,
logging: database.logging,
retry: {
match: [
/SequelizeConnectionError/,
/SequelizeConnectionRefusedError/,
/SequelizeHostNotFoundError/,
/SequelizeHostNotReachableError/,
/SequelizeInvalidConnectionError/,
/SequelizeConnectionTimedOutError/,
],
max: Infinity,
},
});
sequelize.addModels(Object.values(models));
setTimeout(() => console.log('Waiting for mysql connection...'), 200);
sequelize.sync().then(() => console.log('Mysql connection established'));
export const {
... 16 MODELS...
} = models;
export { sequelize, Sequelize };
also I'm using sequelize suggested config in .sequelizerc file to make a connection when running migrations etc..
const path = require('path');
module.exports = {
'config': path.resolve('dist', './config/database.js'),
'models-path': path.resolve('dist', './models'),
'seeders-path': path.resolve('dist', './seeders'),
'migrations-path': path.resolve('dist', './migrations')
}
What I'm missing here?
I'm trying to connect to my local MYSQL(ran by XAMPP) through Sequelize:
const sequelize = new Sequelize(process.env.MYSQL_DB, 'root', '', {
host: process.env.CLEARDB_DATABASE_URL,
dialect: 'mysql',
logging: false
});
The process.env.CLEARDB_DATABASE_URL variable is set to "localhost", just like the documentation says(i'm using this specific variable because i want to deploy to Heroku later)
I'm getting the following error:
getaddrinfo ENOTFOUND localhost'; localhost';:3306
The process.env.MYSQL_DB variable is just the name of my database.
It all worked when i was using the "short way" to connect:
var sequelize = new Sequelize(process.env.MYSQL_DB , "root", "", {
dialect:'mysql',
logging: false
});
Can someone tell me what might be wrong with my setup?
EDIT: if anybody is interested, the problem was that i put a semicolon after the variable deceleration in my .env file.......
I used to configure Sails connection to my database in config/env/development.js and config/env/production.js like that:
module.exports = {
connections: {
'postgres': {
host: 'localhost',
user: 'myUser',
password: 'myPassword',
database: 'myDatabase'
}
}
};
What if I would like to replace my environment config files by environment variables as explained here?
I expected to use those variables but it doesn't work:
sails__connections_postgres_host
sails__connections_postgres_user
sails__connections_postgres_password
sails__connections_postgres_database
Your underscores are backwards. Looking at the docs you linked, your variables should look like:
sails_connections__postgres__host
etc. (one underscore after 'sails', two underscores in between each key after that).
Additionally, it is worth noting that you can reference environment variables in your code, so the option exists to refer to an environment variable, 'dbHostname' in your config/env/development.js (or production) as such:
module.exports = {
connections: {
'postgres': {
host: process.env.dbHostname,
user: process.env.dbUser,
password: process.env.dbPassword,
database: process.env.db
}
}
}
and then create these environment variables upon lifting your server, i.e.
dbHostname="http://something" dbUser="Your_User" dbPassword="password" db="database" sails lift
When I lift my sails project I get these warnings on terminal
debug: Deprecated: config.adapters.default debug: (see
http://links.sailsjs.org/docs/config/connections) debug: For now, I'll
pretend you set config.models.connection.
debug: Deprecated: config.adapters debug: (see
http://links.sailsjs.org/docs/config/connections) debug: For now, I'll
pretend you set config.connections.
debug: Deprecated: config.adapters.*.module debug: (see
http://links.sailsjs.org/docs/config/connections) debug: For now, I'll
pretend you set config.connections["disk"].adapter.
And my data is not stored in mysql DB.
This is my api model code
module.exports = {
schema:true,
tableName: 'BusStop',
adapters: 'mysql-adapter',
migrate: 'safe',
attributes: {
stopID:{
type: 'int'
},
stopName:{
type: 'string'
},
latitude:{
type: 'float'
},
longitude:{
type: 'float'
}
}
};
This is my local.js code
module.exports = {
port: process.env.PORT || 1337,
environment: process.env.NODE_ENV || 'development',
adapters:{
'default': 'disk',
disk:{
module:'sails-disk'
},
'mysql-adapter': {
module : 'mysql-sails',
host : '127.0.0.1',
user : 'root',
password : '',
database : 'PublicTransport',
schema : true
}
}
};
And this is my connections.js code
module.exports.connections = {
localDiskDb: {
adapter: 'sails-disk'
},
'mysql-adapter': {
module : 'sials-mysql',
host : '127.0.0.1',
port : 3306,
user : 'root',
password : '',
database : 'PublicTransport'
},
someMongodbServer: {
adapter: 'sails-mongo',
host: 'localhost',
port: 27017,
},
somePostgresqlServer: {
adapter: 'sails-postgresql',
host: 'YOUR_POSTGRES_SERVER_HOSTNAME_OR_IP_ADDRESS',
user: 'YOUR_POSTGRES_USER',
password: 'YOUR_POSTGRES_PASSWORD',
database: 'YOUR_POSTGRES_DB'
}
};
I want to know how can I remove these Deprecated warnings and why my data is not being stored in the Database.
Looks like the links in those deprecation warnings are incorrect. For information on how to configure Sails connections, see http://sailsjs.org/#/documentation/reference/sails.config/sails.config.connections.html.
You should be able to make those messages go away by following their instructions regarding what config keys to set.
In order to set a connection for a specific model, you now set the connection property:
module.exports = {
schema:true,
tableName: 'BusStop',
connection: 'mysql-adapter', // "adapter" is now "connection
migrate: 'safe',
attributes: {...}
}
In order to specify the adapter to use for a connection, use the adapter key, not module; you're already doing this in most of your connections, you just need to update mysql-adapter.
In your config/local.js you're using the deprecated adapters key to set up connections; use connections instead.
Lastly, in order to set a default connection for all of your models, you do as the deprecation message says and set sails.config.models.connection rather than sails.config.adapters.default; you can do so easily in the config/models.js file, or in your config/local.js like so:
module.exports = {
models: {
connection: 'mysql-adapter'
},
...more config...
}
This warning can also be generated if your project has an config/adapters.js file. If the file is empty it can be deleted and Zap the warning is gone.If it contains code then you will need to migrate it to the correct path config/models.js and config/connections.js
I'm trying to use mysql as my database, but I can't figure out how to get my config/adapters.js to use mysql information in config/local.js to connect. What's the correct way to store the connection information so that sails-mysql can connect?
config/local.js is merged on top of all other configuration. So you can just put your own adapters key in there:
{
adapters: {
default: 'myLocalAdapter',
myLocalAdapter: {
module: 'sails-mysql',
host: 'localhost',
user: 'root',
password: 'password',
database: 'database'
}
}
}
and it'll be picked up and used by Sails.
EDIT In sails v0.10.x, use the key connections instead of adapters as the two are now differentiated and connections now hold authentication info. The original answer was for an earlier version of sails.
As of latest version of sails (i.e 0.12), while writing this answer, there is no Adapters.js. The list of connections are specified in the config/connections.js and default connection to use for all models are specified under config/models.js. So, going by this change the way to override the database configuration for local is specifying as below in config.local.js.
module.exports = {
connections: {
localMongoServer: {
adapter: 'sails-mongo',
host: 'localhost',
port: 27017,
user: 'YOUR_DB_USER',
password: 'YOUR_DB_PASSWORD',
database: 'YOUR_DB_NAME'
}
},
models: {
connection: 'localMongoServer'
}
}
NOTE: As in accepted answer, just changing adapters to connections won't work, since there is no default attribute under connections.