Question

I am looking for a good module for migration in node.js application. I am using sequelize as ORM. I have found a couple of solutions such as migration in sequelize itself but I can't find enough examples for adding column(changing schema) or adding enteries into table.

Could somebody give a good module with some good complete examples as well for both altering schema as well as adding existing enteries?

My application is hosted on heroku so could you please also highlight how to use it on heroku?

Thanks

Was it helpful?

Solution

You can and should use sequelize for your migrations if you're using sequelize in your application. The library has great documentation for handling migrations.

In terms of adding columns with sequelize, you first create a migration through the command line. Run sequelize -c migration-name. An example can be: sequelize -c add_location_to_users.

Once you run this command, you'll find a migration file in your root directory migrations folder. That is where you describe what the migration should change in your database. To keep with the example, here is a sample method.

migration.addColumn(
  'Users',
  'Location',
  DataTypes.STRING
)

Running this migration will add the column 'Location' with the datatype string to the 'User' table.

http://sequelizejs.com/docs/latest/migrations#functions This link will take you to other functions you have available with sequelize, which include changing the column, renaming the column, renaming the table, etc.

In terms of adding entries to your table, sequelize gives you the option to run create or findOrCreate on the model. Here is an example with the user table.

User.create({ name: 'foo', email: 'bar', location: 'New York' }).success(function(user) {
    console.log(user.location); //this prints the users location
})

You then have access to user (the created entry in the database) in the callback in success(), where I printed the new users location above. The instances section of the documentation is where you will find other methods around dealing with an instance of your classes (or 'entry in the database').

OTHER TIPS

If you're looking for a complete framework, Node on Fire includes a really nice migration module. Have a look at https://github.com/martijndeh/fire.

Node on Fire's Todo MVC example contains some models and migrations. Changing any of your models and running fire build will simply generate any new migrations. fire release will apply your migrations to your database.

Node on Fire's design abides to the 12factor methodology which makes apps written with Node on Fire a perfect candidate to deploy on Heroku.

P.S. I'm the main contributor of Node on Fire so this answer might be biased.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top