Migrations in Sequelize in my own folder structure
Asked Answered
C

2

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.

Cismontane answered 28/1, 2019 at 14:40 Comment(0)
B
21

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.

Bound answered 28/1, 2019 at 16:15 Comment(6)
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 ?Cismontane
@hannadrehman sry I don't think its possible to do so with sequelize, you might need 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.Bound
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 @BoundCismontane
@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.Bound
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 onlyCismontane
@hannadrehman I have only used sequelize for traditional db. I think you might need to ask this question in dba.stackexchange.com .Bound
C
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

Cottonseed answered 16/4, 2023 at 16:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.