RestSerializer
Table of Contents
Constructor
RestSerializer
()
Methods
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
Stringthe name of the record (e.g. users)
-
records
ArrayArray 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],
* }]
* }
Similar to _defineArrayResponse, the difference being that this takes a single record and returns a singular response
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],
* }]
* }
Returns an array of ids for a give hasMany/belongsToMany relatioship
Returns:
Example:
return orm.findOne("user", 1).then(user => {
return serializer.getRelationships(user, user.Model.associations.Project);
}).then(relationships => {
/**
* [1, 2, 3]
*
});
Returns the name string for the record
Returns:
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;
});
Return the object key for a relationship
Parameters:
-
relationship
Stringthe relationship name (e.g.
Projects
)
Returns:
name string for the relationship
Example:
return serializer.keyForRelationship("Projects").then(key => {
// "projects"
});
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
ArraySequelize instances
Returns:
<Object, Error>
Example:
return orm.findAll("user").then(users => {
return serializer.normalizeArrayResponse(instances);
}).then(response => {
/**
* {
* users: [{
* }]
* }
});
normalizeRelationships
-
instance
-
payload
Parameters:
Returns:
<Object, Error>
Example:
return store.findOne("user", 1).then(user => {
return serializer.normalizeRelationships(user, user.toJSON());
}).then(response => {
/**
* {
* user: {
* projects: [1, 2, 3]
* }
* }
});
Takes a single Sequelize instance and returns an object with a root key based on the model name and a pojo record
Parameters:
-
instance
ObjectSequelize model instance
Returns:
<Object, Error>
Example:
return orm.findOne("user", 1).then(user => {
return serializer.normalizeSingularResponse(instance, "findOne");
}).then(response => {
/**
* {
* user: {
* }
* }
});