npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rekord-migrations

v1.5.0

Published

Migrations for rekord

Downloads

9

Readme

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...

  • changed the data type of a field.
  • renamed a field.
  • renamed a model class.
  • added a new field and want to populate it with a default or calculated value.
  • dropped a model and want to remove the old data.
  • added a new model and want to pre-populate local storage.
  • decided you want to switch whether a model is stored in its own store or in the property of a related model (common with belongsTo and hasOne relationships).

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.

  • rekord-migrations.js is 16KB (3.5KB gzipped)
  • rekord-migrations.min.js is 7KB (2.2KB gzipped)

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.
    });
  });
});