Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add synchronous deserialization method #258

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions lib/deserializer-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,30 @@ module.exports = function (jsonapi, data, opts) {
});
}

function findIncludedSync(relationshipData, ancestry) {
if (!jsonapi.included || !relationshipData) { return null; }

var included = _find(jsonapi.included, {
id: relationshipData.id,
type: relationshipData.type
});

if (included) {
// To prevent circular references, check if the record type
// has already been processed in this thread
if (ancestry.indexOf(included.type) > -1) {
return _extend(extractAttributes(included));
}


var attributes = extractAttributes(included);
var relationships = extractRelationshipsSync(included, ancestry + ':' + included.type + included.id);
return _extend(attributes, relationships);
} else {
return null;
}
}

function keyForAttribute(attribute) {
if (isPlainObject(attribute)) {
return _transform(attribute, function (result, value, key) {
Expand Down Expand Up @@ -128,6 +152,33 @@ module.exports = function (jsonapi, data, opts) {
});
}

function extractRelationshipsSync(from, ancestry) {
if (!from.relationships) { return; }
var dest = {};

Object.keys(from.relationships).forEach(function (key) {
var relationship = from.relationships[key];

if (relationship.data === null) {
dest[keyForAttribute(key)] = null;
} else if (Array.isArray(relationship.data)) {
var arrayIncludes = relationship.data.map(function (relationshipData) {
return extractIncludesSync(relationshipData, ancestry);
})
if (arrayIncludes) { dest[keyForAttribute(key)] = arrayIncludes; }

return arrayIncludes
} else {
var includes = extractIncludesSync(relationship.data, ancestry)
if (includes) { dest[keyForAttribute(key)] = includes; }

return includes
}
});

return dest
}

function extractIncludes(relationshipData, ancestry) {
return findIncluded(relationshipData, ancestry)
.then(function (included) {
Expand All @@ -144,6 +195,18 @@ module.exports = function (jsonapi, data, opts) {
});
}

function extractIncludesSync(relationshipData, ancestry) {
var included = findIncludedSync(relationshipData, ancestry)
var valueForRelationship = getValueForRelationship(relationshipData,
included);

if (valueForRelationship && valueForRelationship.then) {
throw new Error("Can not pass a promise in valueForRelationship when using deserialzeSync!")
}

return valueForRelationship;
}

this.perform = function () {
return Promise
.all([extractAttributes(data), extractRelationships(data, data.type + data.id)])
Expand All @@ -165,4 +228,22 @@ module.exports = function (jsonapi, data, opts) {
return record;
});
};

this.performSync = function () {
var attributes = extractAttributes(data);
var relationships = extractRelationshipsSync(data, data.type + data.id);
var record = _extend(attributes, relationships);

// Links
if (jsonapi.links) {
record.links = jsonapi.links;
}

// If option is present, transform record
if (opts && opts.transform) {
record = opts.transform(record);
}

return record;
};
};
19 changes: 19 additions & 0 deletions lib/deserializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,23 @@ module.exports = function (opts) {
return resource();
}
};

this.deserializeSync = function (jsonapi) {
function collection() {
return jsonapi.data.map(function (d) {
return new DeserializerUtils(jsonapi, d, opts).performSync();
})
}

function resource() {
return new DeserializerUtils(jsonapi, jsonapi.data, opts)
.performSync();
}

if (Array.isArray(jsonapi.data)) {
return collection();
} else {
return resource();
}
};
};
Loading