Home

Awesome

<img src="https://raw.githubusercontent.com/Rekord/rekord/master/images/rekord-color.png" width="60"> Rekord Migrations

Build Status devDependency Status Dependency Status License Alpha

rekord-migrations adds migrations to rekord. rekord-migrations enables you to handle converting records in local storage created in a previous application version to records that work with the current application version.

When are migrations useful?

You've...

How do they work?

When rekord-migrations is included a store created through Rekord.store is made called migrations which stores the names of the migrations which have been ran locally. Migrations are defined by the application programmer and must be listed in the order of execution. If migrations aren't in the migration store they are ran against storage data - and once the transformations are made they are applied back to the storage implementation and normal Rekord loading is resumed.

Installation

The easiest way to install rekord-migrations is through bower via bower install rekord-migrations.

Example

// simulate the migration and log all changes
Rekord.migrationTest = true;

// defines which stores need to be loaded and updated with new data
Rekord.migration('migration-name', ['todo', 'todo_list', 'task', 'event'], function(migrator, datas)
{
  // Rekord.Collection that can be directly modified to reset store
  datas.todo;

  // drop a model, no longer used
  migrator.drop( 'task' );

  // populate initial data of new model
  migrator.create( 'event', function() {
    return [
      migrator.newRekord({id: 1, name: 'Birthday'}),
      migrator.newRekord({id: 2, name: 'Holiday'})
    ];
  });

  // rename model
  migrator.rename( 'old_name', 'new_name' );

  // todo should be stored into the todos property of the todo_list model now.
  // remove the todo store once moved
  migrator.moveRelatedIn( 'todo', 'todo_list_id', 'todo_list', 'id', 'todos', true );

  // todo should be taken out of todos in the todo_list model and stored in their own.
  migrator.moveRelatedOut( 'todo_list', 'todos', 'todo' );

  // migrate fields in a model
  migrator.migrate( 'todo', function(todos)
  {
    // remove this field, we don't need it anymore
    todos.drop( 'due_date' );

    // constant default value
    todos.add( 'due', false );

    // dynamic default value
    todos.add( 'due', function(todo) {
      return todo.due_date !== null;
    });

    // changed field name, move value over and remove the old field
    todos.rename( 'title', 'name' );

    // convert data type
    todos.convert( 'assignee', function(todo) {
      // store email instead of user object
      return todo.assignee.email;
    });

    // remove certain todos
    todos.filter(function(todo) {
      return todo.version < 2;
    });

    // custom
    todos.transform(function(todo) {
      this.setField( todo, 'field', value );
      this.removeField( todo, 'field' );
      return false; // return false if we want the todo removed.
    });
  });
});