I am new to Sequelize
and my current project requires me to use it with migrations. I am familiar with migrations what they do and how.
I am coming from Django
background where each sub app has modals, views, apis, urls and migrations in the same folder. i like the structure and want to keep the same in my nodejs app. i am trying to put a feature at one place. it makes more sense to me.
my structure
|---api
|---user
| |--- api.js
| |--- utils.js
| |--- models.js
| |--- index.js
| |--- migrations
| |--- xxx123-migration.js
| |--- xxx1235-migration.js
|---payment
|--- api.js
|--- utils.js
|--- models.js
|--- index.js
|--- migrations
|--- xxx123-migration.js
|--- xxx1235-migration.js
now my problem is that i dont know how to m make Sequelize-cli
point to my folders and look for migrations to generate and run.
Sequelize-cli
generates their own folders for models, config, seeds etc. which I don't want to follow.
any help is appreciated.
When you create a Sequelize model, you can add the default value for your model by adding the defaultValue option to the column(s) definition. The defaultValue option will be used by Sequelize to define default value(s) for your SQL column(s) when you create a table using Sequelize.
sequelize-cli db:migrate command looks on the SequelizeMeta table for any migration files which haven't run yet. Every migration lives on this table and inside migrations/ folder. This is the simplest migration you can create.
By default, sequelize stores the migration script name in the SequelizeMeta table so it can keep track of which migration has executed and which is not. However, we could change the default migration storage table name to something we prefer, in this example, we will change it to migrations.
Install sequelize with npm first and initialize the project with “sequelize init”. That should create the “models”, “migrations” and “seeders” folders as well as create the “config/config.json” file. In our application, we’ll have users and cards.
sequelize-cli db:migrate command looks on the SequelizeMeta table for any migration files which haven't run yet. Every migration lives on this table and inside migrations/ folder.
EDIT:
after your updated structure , it is not possible to use .sequelizerc file to do so, because it doesn't support multiple migration folder.
You need to create .sequelizerc
file at project root to override the default path. see here
If I assume api
is the first folder inside project root from your folder structure , the models are in api/user
and migrations are in api/user/migrations
. The following should work:
const path = require('path');
module.exports = {
'config': path.resolve('CONFIGPATHHERE', 'sequelize.js'),
'models-path': path.resolve('api/user', 'sequelize'),
'migrations-path': path.resolve('api/user', 'migrations')
}
make sure you specify sequelize config path.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With