templates/basic/api/blueprints/_util/actionUtil.js
function ( model, records, associations, sideload ) {
    sideload = sideload || false;

    var plural = Array.isArray( records ) ? true : false;

    var documentIdentifier = plural ? pluralize( model.globalId ) : model.globalId;

    //turn id into camelCase for ember
    documentIdentifier = camelCase(documentIdentifier);

    var json = {};

    json[ documentIdentifier ] = plural ? [] : {};

    if ( sideload ) {
      // prepare for sideloading
      forEach( associations, function ( assoc ) {
        var assocName;
        if (assoc.type === 'collection') {
          assocName = pluralize(camelCase(sails.models[assoc.collection].globalId));
        } else {
          assocName = pluralize(camelCase(sails.models[assoc.model].globalId));
        }

        // initialize jsoning object
        if ( !json.hasOwnProperty( assoc.alias ) ) {
          json[ assocName ] = [];
        }
      } );
    }

    var prepareOneRecord = function ( record ) {
      // get rid of the record's prototype ( otherwise the .toJSON called in res.send would re-insert embedded records)
      record = create( {}, record.toJSON() );
      forEach( associations, function ( assoc ) {
        var assocName;
        if (assoc.type === 'collection') {
          assocName = pluralize(camelCase(sails.models[assoc.collection].globalId));
        } else {
          assocName = pluralize(camelCase(sails.models[assoc.model].globalId));
        }

        if ( assoc.type === "collection" && record[ assoc.alias ] && record[ assoc.alias ].length > 0 ) {
          if ( sideload ) json[ assocName ] = json[ assocName ].concat( record[ assoc.alias ] );
          record[ assoc.alias ] = pluck( record[ assoc.alias ], 'id' );
        }
        if ( assoc.type === "model" && record[ assoc.alias ] ) {
          if ( sideload ) json[ assocName ] = json[ assocName ].concat( record[ assoc.alias ] );
          record[ assoc.alias ] = record[ assoc.alias ].id;
        }
      } );
      return record;
    };

    // many or just one?
    if ( plural ) {
      forEach( records, function ( record ) {
        json[ documentIdentifier ] = json[ documentIdentifier ].concat( prepareOneRecord( record ) );
      } );
    } else {
      json[ documentIdentifier ] = prepareOneRecord( records );
    }

    if ( sideload ) {
      // filter duplicates in sideloaded records
      forEach( json, function ( array, key ) {
        if ( !plural && key === documentIdentifier ) return;
        json[ key ] = uniq( array, function ( record ) {
          return record.id;
        } );
      } );
    }

    return json;
  }
function ( model, records, associations, sideload ) {
    sideload = sideload || false;

    var plural = Array.isArray( records ) ? true : false;

    var documentIdentifier = plural ? pluralize( model.globalId ) : model.globalId;

    //turn id into camelCase for ember
    documentIdentifier = camelCase(documentIdentifier);

    var json = {};

    json[ documentIdentifier ] = plural ? [] : {};

    if ( sideload ) {
      // prepare for sideloading
      forEach( associations, function ( assoc ) {
        var assocName;
        if (assoc.type === 'collection') {
          assocName = pluralize(camelCase(sails.models[assoc.collection].globalId));
        } else {
          assocName = pluralize(camelCase(sails.models[assoc.model].globalId));
        }

        // initialize jsoning object
        if ( !json.hasOwnProperty( assoc.alias ) ) {
          json[ assocName ] = [];
        }
      } );
    }

    var prepareOneRecord = function ( record ) {
      // get rid of the record's prototype ( otherwise the .toJSON called in res.send would re-insert embedded records)
      record = create( {}, record.toJSON() );
      forEach( associations, function ( assoc ) {
        var assocName;
        if (assoc.type === 'collection') {
          assocName = pluralize(camelCase(sails.models[assoc.collection].globalId));
        } else {
          assocName = pluralize(camelCase(sails.models[assoc.model].globalId));
        }

        if ( assoc.type === "collection" && record[ assoc.alias ] && record[ assoc.alias ].length > 0 ) {
          if ( sideload ) json[ assocName ] = json[ assocName ].concat( record[ assoc.alias ] );
          record[ assoc.alias ] = pluck( record[ assoc.alias ], 'id' );
        }
        if ( assoc.type === "model" && record[ assoc.alias ] ) {
          if ( sideload ) json[ assocName ] = json[ assocName ].concat( record[ assoc.alias ] );
          record[ assoc.alias ] = record[ assoc.alias ].id;
        }
      } );
      return record;
    };

    // many or just one?
    if ( plural ) {
      forEach( records, function ( record ) {
        json[ documentIdentifier ] = json[ documentIdentifier ].concat( prepareOneRecord( record ) );
      } );
    } else {
      json[ documentIdentifier ] = prepareOneRecord( records );
    }

    if ( sideload ) {
      // filter duplicates in sideloaded records
      forEach( json, function ( array, key ) {
        if ( !plural && key === documentIdentifier ) return;
        json[ key ] = uniq( array, function ( record ) {
          return record.id;
        } );
      } );
    }

    return json;
  }
function ( model , records , associations , sideload ) { sideload = sideload || false ; var plural = Array . isArray ( records ) ? true : false ; var documentIdentifier = plural ? pluralize ( model . globalId ) : model . globalId ; //turn id into camelCase for ember documentIdentifier = camelCase ( documentIdentifier ) ; var json = { } ; json [ documentIdentifier ] = plural ? [ ] : { } ; if ( sideload ) { // prepare for sideloading forEach ( associations , function ( assoc ) { var assocName ; if ( assoc . type === 'collection' ) { assocName = pluralize ( camelCase ( sails . models [ assoc . collection ] . globalId ) ) ; } else { assocName = pluralize ( camelCase ( sails . models [ assoc . model ] . globalId ) ) ; } // initialize jsoning object if ( ! json . hasOwnProperty ( assoc . alias ) ) { json [ assocName ] = [ ] ; } } ) ; } var prepareOneRecord = function ( record ) { // get rid of the record's prototype ( otherwise the .toJSON called in res.send would re-insert embedded records) record = create ( { } , record . toJSON ( ) ) ; forEach ( associations , function ( assoc ) { var assocName ; if ( assoc . type === 'collection' ) { assocName = pluralize ( camelCase ( sails . models [ assoc . collection ] . globalId ) ) ; } else { assocName = pluralize ( camelCase ( sails . models [ assoc . model ] . globalId ) ) ; } if ( assoc . type === "collection" && record [ assoc . alias ] && record [ assoc . alias ] . length > 0 ) { if ( sideload ) json [ assocName ] = json [ assocName ] . concat ( record [ assoc . alias ] ) ; record [ assoc . alias ] = pluck ( record [ assoc . alias ] , 'id' ) ; } if ( assoc . type === "model" && record [ assoc . alias ] ) { if ( sideload ) json [ assocName ] = json [ assocName ] . concat ( record [ assoc . alias ] ) ; record [ assoc . alias ] = record [ assoc . alias ] . id ; } } ) ; return record ; } ; // many or just one? if ( plural ) { forEach ( records , function ( record ) { json [ documentIdentifier ] = json [ documentIdentifier ] . concat ( prepareOneRecord ( record ) ) ; } ) ; } else { json [ documentIdentifier ] = prepareOneRecord ( records ) ; } if ( sideload ) { // filter duplicates in sideloaded records forEach ( json , function ( array , key ) { if ( ! plural && key === documentIdentifier ) return ; json [ key ] = uniq ( array , function ( record ) { return record . id ; } ) ; } ) ; } return json ; }
Prepare records and populated associations to be consumed by Ember's DS.RESTAdapter

@param {Collection} model Waterline collection object (returned from parseModel)
@param {Array|Object} records A record or an array of records returned from a Waterline query
@param {Associations} associations Definition of the associations, from `req.option.associations`
@param {Boolean} sideload Sideload embedded records or reduce them to primary keys?
@return {Object} The returned structure can be consumed by DS.RESTAdapter when passed to res.json()
Prepare records and populated associations to be consumed by Ember s DS . RESTAdapter