16

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.

hannad rehman
  • 4,133
  • 3
  • 33
  • 55

2 Answers2

18

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.

myselfmiqdad
  • 2,518
  • 2
  • 18
  • 33
feiiiiii
  • 1,480
  • 1
  • 12
  • 27
  • what if have many folders like user.. ex inside my api i have `user`, `auth`, `payment`, etc. how can i point to all folders inside api ? – hannad rehman Jan 28 '19 at 16:28
  • 1
    @hannadrehman sry I don't think its possible to do so with sequelize, you might need https://github.com/sequelize/umzug, even with `umzug` i'm not sure its possible to do multiple model folders and then migration folder inside models folders. – feiiiiii Jan 28 '19 at 16:35
  • i might be wrong. but it makes sense to keep migration in the same folder as in models. your answer works only if i have one folder inside api. what do you think is the best way to organise this kinda of project ? any leads is much appriciated @feiiiiii – hannad rehman Jan 30 '19 at 16:54
  • 1
    @hannadrehman I usually put migration folder under `db` folder at the root level, and models folder on same level with my `controller` folder. There is no best way, it is your personal choice, but `sequelize-cli` has limitations, its just the structure you want, currently is not very convenient to go with. – feiiiiii Jan 30 '19 at 17:14
  • is there any other ORM that can support my way of structure. putting one feature in one folder seems more convenient to me. if there is no way then i will have to do it your suggested way only – hannad rehman Jan 30 '19 at 19:59
  • @hannadrehman I have only used sequelize for traditional db. I think you might need to ask this question in https://dba.stackexchange.com/ . – feiiiiii Feb 01 '19 at 14:18
0

based on issues [\https://github.com/sequelize/cli/issues/28]1 we can add flag in model:generate command

(example) : npx sequelize-cli model:generate --name modelName --models-path .\some\ --attributes name:string

make sure models properly imported in models/index.js file

Arunkumar
  • 29
  • 2