使用 sequelize 的种子数据的不同目录 [英] Different directories for seed data using sequelize
问题描述
我希望在开发和生产之间有不同的种子数据.如何在配置中指定它?我知道在 .sequelizerc
我可以加载一个动态配置文件并指定一个 seeders-path
I would like to have different seed data between development and production. How can I specify this in the configuration? I'm aware that in the .sequelizerc
I can load a dynamic configuration file and specify a 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
?或者我应该做一些逻辑,比如
Is it possible to choose the seeders-path
within the dynamic configuration? Or shall I make some logic like
"seeders-path": process.env.NODE_ENV === 'development'?
"seeders/development" :
"seeders/production"
推荐答案
你可以像这样使用 npm 脚本:
You can use npm scripts something like this:
// package.json
"scripts": {
"sequelize:prod": "sequelize $* --seeders-path seeders/production",
"sequelize:dev": "sequelize $* --seeders-path seeders/development",
}
或者你可以像这样直接传递配置:
Or you can pass config directly like this:
// package.json
"scripts": {
"sequelize:dev": "sequelize $* --config some/path/dev.js"
"sequelize:prod": "sequelize $* --config some/path/prod.js"
}
你可以传递给sequelize的选项列表:
Lists of options that u can pass to sequelize:
- --env string 运行命令的环境
- --config string 配置文件的路径
- --options-path string 带有附加选项的 JSON 文件的路径
- --migrations-pat 字符串迁移文件夹的路径
- --seeders-path 字符串种子文件夹的路径
- --models-path 字符串模型文件夹的路径
- --url string 要使用的数据库连接字符串.替代使用 --config 文件
- --debug bolean 可用时显示各种调试信息 boolean
- --env string The environment to run the command in
- --config string The path to the config file
- --options-path string The path to a JSON file with additional options
- --migrations-pat string The path to the migrations folder
- --seeders-path string The path to the seeders folder
- --models-path string The path to the models folder
- --url string The database connection string to use. Alternative to using --config files
- --debug bolean When available show various debug information bolean
在 sequelize-cli 源中有此代码 链接:
In sequelize-cli sources have this code 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":
So you can load different files like ".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 创建种子.
But u can create seeders for different ENV in one seeder directory.
比方说,我们有 .sequelizerc
Lets say, we have .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
而且我们只运行开发播种机:
And we run only dev seeders:
sequelize db:seed --seed `basename $(ls app/seeders/*-dev.js)`
或产品播种机:
sequelize db:seed --seed `basename $(ls app/seeders/*-prod.js)`
或者只是用户开发播种者
or just users dev seeders
sequelize db:seed --seed `basename $(ls app/seeders/*users-dev.js)`
您可以选择要运行的播种机.
You can choose what seeder u want to run.
格式:
sequelize db:seed --seed <array>
sequelize db:seed --seed file1 file2 fileN
这篇关于使用 sequelize 的种子数据的不同目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!