我希望在开发和生产之间有不同的种子数据。如何在配置中指定此项?我知道在SELECT MID.menu_item_id,
SUM(CID.consumed_quantity) as total
SUM(CASE WHEN CSD.tendered_date_time BETWEEN
CONVERT(datetime,'2016-07-01 06:00:00.000') AND CONVERT(datetime, '2016-08-01
05:59:59.999') THEN CID.consumed_quantity ELSE 0 END) AS [JUL],
. . .
. . .
ORDER BY SUM(CID.consumed_quantity) DESC;
我可以加载动态配置文件并指定.sequelizerc
seeders-path
配置
// .sequelizerc
const path = require('path')
module.exports = {
"config": path.resolve('./app/config', 'config.json'),
"models-path": path.resolve('./app/models'),
"migrations-path": path.resolve('./app/migrations')
"seeders-path": path.resolve('./app/seeders')
}
是否可以在动态配置中选择// ./app/config/config.json
{
"development": {
// environment specific configuration
...
?或者我应该做一些逻辑
seeders-path
答案 0 :(得分:5)
你可以使用这样的npm脚本:
// package.json
"scripts": {
"sequelize:prod": "sequelize $* --seeders-path seeders/production",
"sequelize:dev": "sequelize $* --seeders-path seeders/development",
}
或者您可以像这样直接传递配置:
// package.json
"scripts": {
"sequelize:dev": "sequelize $* --config some/path/dev.js"
"sequelize:prod": "sequelize $* --config some/path/prod.js"
}
你可以传递给续集的选项列表:
在sequelize-cli中,源代码为link:
function loadRCFile(optionsPath) {
const rcFile = optionsPath || path.resolve(process.cwd(), '.sequelizerc');
const rcFileResolved = path.resolve(rcFile);
return fs.existsSync(rcFileResolved)
? JSON.parse(JSON.stringify(require(rcFileResolved)))
: {};
}
const args = yargs
.config(loadRCFile(yargs.argv.optionsPath));
因此您可以加载不同的文件,例如“.sequelizerc”:
// package.json
"scripts": {
"sequelize:dev": "sequelize $* --options-path some/path/dev.js"
"sequelize:prod": "sequelize $* --options-path some/path/prod.js"
}
// some/path/dev.js
const path = require('path');
module.exports = {
"config": path.resolve('./app/config', 'config.json'),
"models-path": path.resolve('./app/models'),
"migrations-path": path.resolve('./app/migrations')
// here your development path to seeders
"seeders-path": path.resolve('./app/seeders/development')
}
// some/path/prod.js
const path = require('path');
module.exports = {
"config": path.resolve('./app/config', 'config.json'),
"models-path": path.resolve('./app/models'),
"migrations-path": path.resolve('./app/migrations')
// here your development path to seeders
"seeders-path": path.resolve('./app/seeders/production')
}
但是你可以在一个播种机目录中为不同的ENV创建播种机。
让我们说,我们有.sequelizerc
// .sequelizerc
const path = require('path')
module.exports = {
"config": path.resolve('./app/config', 'config.json'),
"models-path": path.resolve('./app/models'),
"migrations-path": path.resolve('./app/migrations')
"seeders-path": path.resolve('./app/seeders')
}
创建播种机:
sequelize seed:generate --name create_users-dev.js
sequelize seed:generate --name create_users-prod.js
sequelize seed:generate --name create_users-whatever.js
我们只运行开发者:
sequelize db:seed --seed `basename $(ls app/seeders/*-dev.js)`
或生产播种机:
sequelize db:seed --seed `basename $(ls app/seeders/*-prod.js)`
或只是用户dev浏览器
sequelize db:seed --seed `basename $(ls app/seeders/*users-dev.js)`
您可以选择想要播种的播种机。
格式:
sequelize db:seed --seed <array>
sequelize db:seed --seed file1 file2 fileN
答案 1 :(得分:0)
在这里,我使用以下命令来执行Sequelize的seeders文件夹中的特定文件。
命令:- 序列化db:seed --seed [种子文件名]
like:-
示例:- 序列化db:seed --seed 20181204125705-executiveholds