Comments (19)
To add a property with object value which would represent config for sequelize
I will expand a bit here, in many project that i have seen, uses some kind of config loader, like: nconf, configs-overload or config-load and etc.
If you use something like sails, convention for config files would be proposed in the same manner - http://sailsjs.org/documentation/anatomy/my-app/config
But, with your current approach, i don't see a way how you would properly define settings for db connection without doing some kind of weird hacks above, or define configuration in two places.
from cli.
Currently I use the url param inside the rc file, it does do the trick
const path = require('path')
const config = require('./config')
const { host, port, dialect, username, password, name } = config.database
module.exports = {
'migrations-path': path.resolve('server', 'migrations'),
'models-path': path.resolve('server', 'models'),
'seeders-path': path.resolve('server', 'seeders'),
'url': `${dialect}://${username}:${password}:@${host}:${port}/${name}`
}
this is however for from ideal, @sdepold your solution currently does not work I assume because of the validations inside yargs? Having the possibility to make the rc file work with tools like convict would be a very elegant solution
from cli.
You can fix your problem by using the following (or a similar) code in the .sequelizerc
:
module.exports = {
"migrations-path": "db/migrations",
"config": __filename,
"development": {
"dialect": "sqlite",
"storage": "db/development.sqlite"
}
}
The actual trick is the config
property which just states to the very same file, which will make the cli look into it rather than into config/config.json
. Please note that the generated models/index.js
will try to lookup the provided environment in the exported object and expects in there the respective database credentials and connection settings.
from cli.
I don't have configuration in a config file; it is read from process.env
. Instead of an object literal (as above), I would actually be constructing the correct object from the environment.
I want to use Sequelize, but this is a deal-breaker. I'm not going to specify my configuration multiple times across multiple files. Maybe .sequelizerc
could allow module.exports.config
to be either a string or config object?
from cli.
I've finally gotten around to needing this again. Here's what I ended up with (in .sequelizerc
):
const config = require('./config');
module.exports = {
config: __filename,
[config.nodeEnv]: {
dialect: 'postgres',
url: config.databaseURL
}
};
from cli.
Was facing the same issue,
Tried @ravinggenius solution, but it ends up giving the same error Unknown argument: development
, just like @MicroDroid pointed out.
For me the configs were partly coming from ENV vars and also from files and I am using Nconf to manage those.
I finally ended up creating a placeholder config.js
file, which basically extracts the configs from Nconf and exports them out
const Nconf = require('nconf');
const ConfigHelper = require('../helpers/configHelper');
ConfigHelper.loadConfig(); // This sets up the configs in Nconf.
const config = Nconf.get('services').mysql;
module.exports = config;
from cli.
@klaaz0r I used @shivambarsaley 's technique with convict. The Config
import is a standard convict config.js
config/sequelizedb.js
// @flow
import config from 'Config'
const dbConf = config.get('db')
module.exports = dbConf
.sequelizerc
// @flow
require('@babel/register');
const path = require('path');
module.exports = {
'config': path.resolve('config', 'sequelizedb.js'),
'models-path': path.resolve('db', 'models'),
'seeders-path': path.resolve('db', 'seeders'),
'migrations-path': path.resolve('db', 'migrations')
}
from cli.
I think what I was asking is: In the example below, can module.exports.config
be the configuration object, or must it be a string pointing to a JSON file?
I'd really like to have my .sequelizerc
look something like this:
var path = require('path');
module.exports = {
'config': {
dialect: 'mysql',
database: 'database_name',
username: 'username',
password: 'password',
port: 3306
},
'migrations-path': path.resolve('db', 'migrate')
};
from cli.
Here is .sequelizerc one I created for a project to pull from a centralized config:
var config = require('./config');
var dbConfig = JSON.parse('{ "' + config.environment + '" : ' +
'{ ' +
'"username": \"' + config.database.username + '", ' +
'"password": "' + config.database.password + '", ' +
'"database": "' + config.database.name + '", ' +
'"host": "' + config.database.host + '", ' +
'"dialect": "' + config.database.dialect + '" ' +
'}' +
'}');
dbConfig.config = __filename,
dbConfig["migrationsPath"] = "migrations",
module.exports = dbConfig;
The index.js file in the config directory looks like this:
var nconf = require('nconf'); //used to manage config settings per environment
exports.environment = process.env.NODE_ENV || 'development';
nconf.argv()
.env()
.file({ file: './config/' + exports.environment + '.json' })
;
//Database configuration settings
exports.database = {
username: nconf.get('database:username'),
password: nconf.get('database:password'),
name: nconf.get('database:name'),
host: nconf.get('database:host'),
dialect: nconf.get('database:dialect')
}
And then there would be an [environment].json file like development.json in the same directory like:
{
"database": {
"username": "myapp",
"password": "myappspassword",
"name": "mydb",
"host": "127.0.0.1",
"dialect": "mysql"
}
}
from cli.
Thats worked for me!
require('dotenv').config();
const path = require('path');
const config = {
name: process.env.DB_NAME,
password: process.env.DB_PASSWORD,
username: process.env.DB_USER,
host: process.env.DB_HOST,
port: process.env.DB_PORT,
dialect: process.env.DB_DIALECT
};
module.exports = {
"models-path": path.resolve('./database/models'),
"seeders-path": path.resolve('./database/seeders'),
"migrations-path": path.resolve('./database/migrations'),
"url": `${config.dialect}://${config.username}:${config.password}:@${config.host}:${config.port}/${config.name}`
};
from cli.
Not sure if I entirely understand the issue. Can you be more concrete about what the .sequelizerc should do and how it should look like?
from cli.
Please note that the sequelizerc file gets require
d internally. That basically means, that you can use just nodejs in it.
from cli.
Ah. Now I understand. Yes it has to point to a configuration file. What you could try is to point just to the same file and to add the stuff to that file, you would usually have in the config/config.json... Not sure if this is a good approach, though. Actually I'm sure that it is not :D
from cli.
Clever trick. I'll have to try this for my next project.
I'm closing this with the assumption that it works for what I need.
from cli.
It is unfortunate that you have to use some hacks in order to load config in orthodoxical way :/.
Would you accept pull request for changing this?
from cli.
To change it to what?
from cli.
@ravinggenius this causes:
Unknown argument: development
from cli.
Here is how I fixed my problem:
My ./.env
consists of many variables, except that I added this one uniquely for sequelize-cli:
SEQUELIZE_DB_HOST=postgres://user:[email protected]:5432/dbname
And then I created ./sequelize.json
:
{
"development": {
"use_env_variable": "SEQUELIZE_DB_HOST"
},
"test": {
"use_env_variable": "SEQUELIZE_DB_HOST"
},
"production": {
"use_env_variable": "SEQUELIZE_DB_HOST"
}
}
And then, finally, in .sequelizerc
:
const path = require('path');
require('dotenv').config();
module.exports = {
'config': path.resolve('./sequelize.json'),
'models-path': path.resolve('models'),
'seeders-path': path.resolve('seeders'),
'migrations-path': path.resolve('migrations'),
}
That's it.
Aside from that, I have config.js
that contains a bunch of of config including DB config, and I use that one inside the application. This way config.js
DB config only applies to in-app, not in sequelize-cli
from cli.
Hello, Is there any way to generate migrations for a multi-tenant application using sequelize cli.
The config.js would have an array of objects that contain the configuration for each database. In my case, there are 18 databases in the multi-tenant application. I want the same migration to take effect in all the databases.
Does anyone have any idea how it could work?
from cli.
Related Issues (20)
- use_env_variable returns undefined because not string value HOT 11
- Pending migration alert during app startup HOT 6
- Error on running db: migrate with Node 18.17.9 HOT 1
- Support for Improved Migration Directory Structure and Configuration HOT 2
- Model.associate adding new columns
- Proposal: Controller Generation in Sequelize
- Migrations should happen in a transaction HOT 30
- Tests are stuck HOT 5
- postgres: db:create should not fail if the database does exist
- BulkInsert "relation does not exist" in migration script
- const model = require(path.join(__dirname, file)).default( ^ TypeError: require(...).default is not a function
- const model = require(path.join(__dirname, file)).default( ^ TypeError: require(...).default is not a function HOT 1
- Sequelize CLI Fails to Authenticate with Postgres DB Containing a ':' in Password HOT 3
- CLI is not taking default env like node latest versions have (20.8.0)
- SequelizeMeta structure incompatible with SingleStore/memsql HOT 2
- Problem running migrations with yarn
- When on typescript project, sequelize-cli still generates javascript files. HOT 3
- Running migrations, on Azure database HOT 1
- Seeders state in database with using url option
- Migration undo command didn't work correctly. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cli.