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

mg

v1.1.0

Published

Model synchronization / hydration library

Downloads

66

Readme

mg - a model synchronization library

  • Hydrates models with their 1:1 & 1:N associations. Models may be related to one or more other objects, and fetching each associated manually is painful.
  • Ensures that only one instance exists for each model class + id pair. This ensures that updates made to a model in one part of a system are reflected in another.
  • Data type assurance. Ensures that model properties with specific data types such as Dates and Regexps are always of the expected type.
  • Cache preloading. Allows you to transmit a set of models as JSON data with your initial page load. These will be used from the cache, reducing the number of HTTP requests needed during page load.

Getting started

Changelog

v1.1.x: The collection property on models is now called collectionType to avoid collisions with the Backbone collection property. If you receive an error such as "A "url" property or function must be specified", you are probably missing the collectionType property and hence attempting to fetch a plain Backbone.Collection.

Define model relations

To set up mg hydration, define a rels property. For example, to have Post.author be hydrated as an instance of Person:

var Post = Backbone.Model.extend({
  url: function()  {
    return '/posts/' + this.id;
  },
  collectionType: 'Posts', // <= collections of Post are created using the associated Collection (or Backbone.Collection by default)
  rels: {
    comment: { type: 'Comment' } // <= convert the comment field to a instance of Comment or a Collection of Comments
  }
});

mg.define('Post', Post);

var Posts = Backbone.Collection.extend({
  url: '/posts'
});

mg.define('Posts', Posts);

The mg.define() call registers the model with hydration, so that relations can find the right model class and metadata.

mg supports both one-to-one, one-to-many and many-to-one relations. You do not need to explicitly define the type of relation, as it will be inferred from the JSON data.

Find models and collections

findById(name, id, rels, onDone): retrieves a model by id. Reads the model from model.url + / + id.

stream(name, rels, onDone): retrieves a model by id. Reads the model from model.url + / + id.

Example:

// Fetch a model
// Note you can specify which related objects to load dynamically.
mg.findById('Project', 1, { rels: 'Script' } function(err, project) {
    // And then retrieve those related objects using .get
    var script = project.get('script');
});

// Fetch a collection
mg.stream('Post', { rels: 'Comment' } function(err, project) {
    // And then retrieve those related objects using .get
    var script = project.get('script');
});

Alternatively, you can use model.fetch() and collection.fetch() and call hydrate on the result.

Example model.fetch()/collection.fetch():

project.fetch({ data: { rels: ['script', 'dataset'] }}).done(function(data) {
  mg.hydrate('Post', post, data);
  var script = project.get('script');
});

The URL is determined using model.url, following the Backbone conventions.

Models are cached in-memory. This means that you should always use .findById(), since it will retrive the model from cache if possible.

Link associated models

Example:

// comment1 and comment2 are instances of Comment
project.link([comment1, comment2], function(err) {
  // comment to project link has been saved
});

Example:

// comment1 is an instance of Comment
project.unlink(comment1, function(err) {
  // comment to project link has been removed
});