config.json文件是sequelize中的数据库连接文件。在我的项目 config.json 文件中有数据库组
config.json 看起来像:
{
"TEST_CS": {
"development": {
"username": "root",
"password": "password",
"database": "databaseOne",
"host": "localhost",
"port": "3306",
"logging": true,
"dialect": "mysql"
},
"test": {
"dialect": "sqlite",
"storage": ":memory:"
},
"production": {
"username": "root",
"password": "password",
"database": "databaseTwo",
"host": "localhost",
"dialect": "mysql"
}
},
"CS_SERVER": {
"development": {
"username": "root",
"password": "password",
"database": "My_db",
"host": "localhost",
"port": "3306",
"logging": true,
"dialect": "mysql"
},
"test": {
"dialect": "sqlite",
"storage": ":memory:"
},
"production": {
"username": "root",
"password": "password",
"database": "databaseFour",
"host": "localhost",
"dialect": "mysql"
}
}
}
抛出以下错误:
加载配置文件“models \ config \ config.json”。
错误:自v4.0.0起,需要明确提供方言
但是当我在 config.json 文件中删除json的对象时。正确完成迁移
{
//Remove CS_SERVER object
"development": {
"username": "root",
"password": "password",
"database": "My_db",
"host": "localhost",
"port": "3306",
"logging": true,
"dialect": "mysql"
},
"test": {
"dialect": "sqlite",
"storage": ":memory:"
},
"production": {
"username": "root",
"password": "password",
"database": "database_production",
"host": "localhost",
"dialect": "mysql"
}
}
如何在sequelize migration中访问 config.json 文件中的json数组
答案 0 :(得分:1)
在建立连接时只需要此对象
{
"username": "root",
"password": "password",
"database": "My_db",
"host": "localhost",
"port": "3306",
"logging": true,
"dialect": "mysql"
}
开发,测试和生产是用于根据环境变量进行选择的密钥。
但是如果您正在使用TEST_CS或CS_SERVER,则必须在进行此类连接时手动传递这些密钥。
let config = require('../serverconfig/config.json')['CS_SERVER'][env];
let sequelize = new Sequelize(config.database, config.username, config.password, config);