Show:
Extends JSONSerializer

Constructor

RestSerializer ()

Defined in src/serializer.js:7

Methods

_defineArrayResponse
(
  • key
  • records
)
Object
private

Reformats the record into a RESTful object with the record name as the key. In addition, this will add a custom toJSON method on the response object that will serialize the response when sent through something like express#res.send, retaining the relationships on the instance, but removing all other extraneous data (see Sequelize instance#toJSON)

Parameters:

  • key String

    the name of the record (e.g. users)

  • records Array

    Array of sequelize instances

    Returns:

    Example:

    serializer._defineArrayResponse("users", [{
      dataValues: {
        firstName: "Hank",
        lastName: "Hill",
        projects: [1, 2]
      },
      someExtraneousProp: "foo"
    }]);
    
    /**
     * {
     *   users: [{
     *     dataValues: {
     *       firstName: "Hank",
     *       lastName: "Hill",
     *       projects: [1, 2],
     *     },
     *     someExtraneousProp: "foo",
     *     toJSON() {
     *     }
     *   }]
     * }
     *
     * response.toJSON()
     *
     * {
     *   "users": [{
     *     firstName: "Hank",
     *     lastName: "Hill",
     *     projects: [1, 2],
     *   }]
     * }
    
    _defineSingularResponse
    (
    • key
    • record
    )
    Object
    private

    Similar to _defineArrayResponse, the difference being that this takes a single record and returns a singular response

    Parameters:

    • key String

      the name of the record (e.g. users)

    • record Object

      Sequelize instance

    Returns:

    Example:

    serializer._defineSingularResponse("user", {
      dataValues: {
        firstName: "Hank",
        lastName: "Hill",
        projects: [1, 2]
      },
      someExtraneousProp: "foo",
    });
    
    /**
     * {
     *   user: {
     *     dataValues: {
     *       firstName: "Hank",
     *       lastName: "Hill",
     *       projects: [1, 2],
     *     someExtraneousProp: "foo",
     *     toJSON() {
     *     }
     *   }
     * }
     *
     * response.toJSON()
     *
     * {
     *   "user": [{
     *     "firstName": "Hank",
     *     "lastName": "Hill",
     *     "projects": [1, 2],
     *   }]
     * }
    
    getRelationships
    (
    • instance
    • association
    )
    Array

    Returns an array of ids for a give hasMany/belongsToMany relatioship

    Parameters:

    • instance Object

      Sequelize model instance

    • association Object

      Sequelize model instance

    Returns:

    Example:

    return orm.findOne("user", 1).then(user => {
      return serializer.getRelationships(user, user.Model.associations.Project);
    }).then(relationships => {
      /**
       * [1, 2, 3]
       *
    });
    
    keyForRecord
    (
    • instance
    • singular
    )
    String

    Returns the name string for the record

    Parameters:

    • instance Object

      sequelize model instance

    • singular Boolean

      singular or plural name

    Returns:

    String:

    name string for record root

    Example:

    return orm.findOne("user", 1).then(user => {
      const res = {};
      const resKey = serializer.keyForRecord(user, true);
    
      res[resKey] = user.toJSON();
    
      return res;
    });
    
    keyForRelationship
    (
    • relationship
    )
    String

    Return the object key for a relationship

    Parameters:

    • relationship String

      the relationship name (e.g. Projects)

    Returns:

    String:

    name string for the relationship

    Example:

    return serializer.keyForRelationship("Projects").then(key => {
      // "projects"
    });
    
    normalizeArrayResponse
    (
    • instances
    )
    Promise

    Takes an array of Sequelize instances and returns an object with a root key based on the model name and an array of pojo records

    Parameters:

    • instances Array

      Sequelize instances

    Returns:

    Promise:

    <Object, Error>

    Example:

    return orm.findAll("user").then(users => {
      return serializer.normalizeArrayResponse(instances);
    }).then(response => {
      /**
       * {
       *   users: [{
       *   }]
       * }
    });
    
    normalizeRelationships
    (
    • instance
    • payload
    )
    Promis

    Parameters:

    • instance Object

      Sequelize model instance

    • payload Object

      Pojo representation of Sequelize model instance

    Returns:

    Promis:

    <Object, Error>

    Example:

    return store.findOne("user", 1).then(user => {
      return serializer.normalizeRelationships(user, user.toJSON());
    }).then(response => {
      /**
       * {
       *   user: {
       *     projects: [1, 2, 3]
       *   }
       * }
    });
    
    normalizeSingularResponse
    (
    • instance
    )
    Promise

    Takes a single Sequelize instance and returns an object with a root key based on the model name and a pojo record

    Parameters:

    • instance Object

      Sequelize model instance

    Returns:

    Promise:

    <Object, Error>

    Example:

    return orm.findOne("user", 1).then(user => {
      return serializer.normalizeSingularResponse(instance, "findOne");
    }).then(response => {
      /**
       * {
       *   user: {
       *   }
       * }
    });