9

Is there any tool that works similar to Django South, but for Node?

Now I'm working with Sequelize. If I got it right, Sequelize does not have an option to create migration files based on existing models. So, to create a new model/table, the steps are:

  • Create model with sequelize model:create <model meta>.
  • Edit generated migration file - add actual code for creating tables in DB under up section.
  • Run migration with sequelize db:migrate.

I'm looking for something that can create migration files based on existing models, manage it similar to what South can do for Django.

Is there any option?

PaulMest
  • 12,925
  • 7
  • 53
  • 50
DanDuh
  • 500
  • 1
  • 5
  • 17
  • 1
    If something exists yet, I haven't found it. I've been searching periodically over the last few months. I ran across SailsJS which has mechanisms for migrating an existing database schema to match the current model, but it doesn't currently output the migration files. Also the approach they take during this modification risks loss of data. But this is the closest thing that I've found. Sequelize seems to be the best option right now for Node.js sadly. – Kenneth Oct 12 '15 at 02:35
  • @Kenneth, DanDuh, any luck on finding a migration tool for Node.JS? – brillout Nov 15 '15 at 15:03
  • 1
    @brillout.com, unfortunately not yet... Thinking about creating my own... – DanDuh Nov 17 '15 at 09:36
  • 1
    You can use [`sequelize-auto`](https://github.com/sequelize/sequelize-auto) to generate models from an existing schema, but I don't think it can handle relationships. Seems like a decent start if you wanted to submit a patch though... :) – doublesharp Dec 08 '16 at 18:46
  • May want to watch this issue: https://github.com/sequelize/cli/issues/257 – Ben Creasy Jan 22 '17 at 07:19
  • The easiest way to create zero migration is to run sequelize.sync() on empty database with logging enabled, than copy all console output to migrations file. – Dmitry Chirkin Apr 26 '17 at 10:24
  • This tool can create sequelize models from an existing Postgres database: http://www.pg-generator.com/builtin-templates/sequelize/ . Maybe this helps - I do not use sequelize myself. – madflow Jul 26 '19 at 07:42

4 Answers4

3

I have written a step-by-step guide on how to auto-create migrations with Sequelize in another post. Here is a summary...

The closest thing with Sequelize is Sequelize Auto Migrations.

It allows you to have an iteration cycle like the following:

  1. Create/update model -- by hand or with sequelize-cli)
  2. Run makemigrations to auto-generate up and down migrations
  3. Repeat as necessary

While this is very helpful, I've found it to be lacking in some critical areas:

  1. The down migrations can be created incorrectly. So it may try to drop a table before its dependent tables have been dropped.
  2. There are certain configurations around multi-field indexes that it has not correctly output.

There are currently 10 outstanding PRs, so it seems like a few additional contributors are attempting to make it more production-ready... but I've yet to find anything as clean and reliable as Django Migrations (formerly Django South).

PaulMest
  • 12,925
  • 7
  • 53
  • 50
  • How has this developed? – kabuto178 Jan 01 '22 at 14:22
  • @kabuto178 I've updated this Answer with a link to a more comprehensive guide. I've also added a new Answer to this Question regarding what my team does now (Hasura). Best of luck to you learning Node in 2022! – PaulMest Jan 02 '22 at 17:32
1

TypeORM supports model based migrations. It can sync your db to your models directly, it can also create migration files too.

I think prisma is another option. It doesn't seem like that popular but it's a promising one.

Either way, it's just ridiculous that there are no solid tools for this. I've worked on django and .net projects in the last years and creating migrations is just easy with them. But when you try to use node.js for backend, you get stuck at a lot of points.

I was using sequelize and when I saw there was no official way to create automatic migrations from models, I've dropped using it. Maintaining your models with manually written migrations becomes very hard in my experience.

Now my only alternative is TypeORM and it just bugs me there is no other alternative in case TypeORM just goes unmaintained or if I want to use another library etc.

I'm seriously thinking dropping using node.js for backend. Yet, there are good tools to create projects integrated with modern front-end tools (like Next.js), finding a good orm is a big problem.

Onur Önder
  • 1,002
  • 1
  • 10
  • 16
0

Take a look at https://typeorm.io/#/migrations/generating-migrations. I am in the same situation you was 4 years ago.

My options:

  • Waterline just for ORM and diff tool (like dbdiff) to make a file with differences from a new schema (generated by waterline migration with 'drop') vs production schema. With that output, you run query by query in a safe mode.
  • Prev option plus knex migrations. But you have to make your own migrations files. Knex doesnt have a schema file to compare but there is a request feature https://github.com/knex/knex/issues/1086.
    • Using sails but change waterline for sequalize and give a try at the @paulmest answer.
    • Use sails but change waterline for typeorm an use that auto generated.
0

Over the years, my team has tried Sequelize and TypeORM, but neither of them were great experiences. TypeORM had tons of cryptic problems and thousands of issues are unaddressed/open for years on end. Sequelize was generally fine, but lack of type support made it time-consuming to work with.

For the last 1.5 years, my team has been using Hasura. It's honestly been a breath of fresh air. Hasura's migration system is pretty barebones compared to Django South, but it is straight-forward and has a clean happy path.

You can use the Hasura Console (a localhost web editor) to create tables and add/remove columns. Those changes will be automatically distilled into schema changes stored in .sql files in a migration directory. You can modify these files. You can run a migration command from the command line to apply them when you want.

Since Hasura is a GraphQL engine, it also comes with a schematized SDK that is TypeScript compatible so you get incredible editor support.

All of this and much more is available in the open source version of their product. We have not needed to pay for any higher tier.

You can find more information here: https://hasura.io/docs/latest/graphql/core/migrations/index.html

PaulMest
  • 12,925
  • 7
  • 53
  • 50