Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 1,000,000+ packages pre-installed, including geddy-ember-serializer with all npm packages installed. Try it out:

var geddyEmberSerializer = require("geddy-ember-serializer")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

geddy-ember-serializer v1.0.4

A module for converting Geddy's Model output into a sideloaded payloaded that Ember-Data prefers.

Geddy Ember Serializer

Geddy and Ember.js Play Together!

If you're using the Model ORM, you may be using the Geddy framework. You also might be using it on its own!

Geddy has some great nested object retrieval, but Ember-Data requires slightly differently structured data. This module creates a bridge between the two data structures.

  var serializer = require('geddy-model-serializer');

  // You must call init at least once, passing in a Model instance,
  // with your schema loaded into it:
  serializer.init( geddy.model );

  geddy.model.User.first({}, { includes: 'Books' }, function (err, user){
    if (err) throw err;

    /* Model returns nested relationships in a format like this:

    var user === {
      id: 1,
      name: "Joe Schmoe",
      books: [
        {
          id:54,
          title: "Huck Finn"
        },
        {
          id:99,
          title: "The Peripheral"
        }
      ]
    };

    */


    /* With this module, we can structure the same data like this: */
    serializer.init(); // Call between serializations to clear the store out.
    serizer.digest( user ); // Feed it as many models as you want, with relationships loaded!
    var result = serializer.serialize(); // Does the actual conversion on this function call:
    console.dir( result );

    /* Result would look like this:

      var result = {
        users:[
          {
            id: 1,
            name: "Joe Schmoe",
            books: [54, 99]
          }
        ],

        books:[
          {
            id:54,
            title: "Huck Finn"
          },
          {
            id:99,
            title: "The Peripheral"
          }
        ]
      }

    */
  });
}
RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free