Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Migrations in Sequelize in my own folder structure

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.

like image 982
hannad rehman Avatar asked Jan 28 '19 14:01

hannad rehman


People also ask

How do I set default value in Sequelize migration?

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.

How do I create a migration from a sequelizemeta table?

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.

Where does Sequelize store the migration script name?

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.

How to install Sequelize with npm?

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.

What is the use of @Sequelize-CLI DB?

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.


1 Answers

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.

like image 66
feiiiiii Avatar answered Nov 10 '22 19:11

feiiiiii