Skip to content

Commit

Permalink
build
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Piccolo committed Jun 30, 2019
1 parent c38811f commit de4be5d
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 47 deletions.
56 changes: 39 additions & 17 deletions dist/bundle.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,13 @@ function _setRelationShipKeyToValues({
currentIncludes,
name
}) {
const directIncludesRalationships = currentIncludes.map(
relation => relation.split(".")[0]
);
const directIncludesRalationships = currentIncludes
.map(relation => relation && relation.split("."))
.flat();

if (!directIncludesRalationships.includes(name))
return nextRelationshipObjects;

if (!(name in nextRelationshipObjects)) {
if (relationType === "hasMany") {
nextRelationshipObjects[name] = [];
Expand All @@ -275,6 +277,7 @@ function _setRelationShipKeyToValues({
}
}
if (!resources[type]) return nextRelationshipObjects;

const relationData = resources[type][id];
if (!relationData) return nextRelationshipObjects;

Expand All @@ -288,9 +291,13 @@ function _setRelationShipKeyToValues({
name,
relationData
);

nestedResourceData.forEach(
([nestedResourceName, nestedResourceType, nestedResourceIds]) => {
([
nestedResourceName,
nestedResourceType,
nestedResourceIds,
doubleNestedResourceName
]) => {
const nestedResources = _convertWithNestedResources(
conversion,
relationClass,
Expand All @@ -300,7 +307,8 @@ function _setRelationShipKeyToValues({
relationModel,
nestedResourceName,
nestedResourceType,
nestedResourceIds
nestedResourceIds,
doubleNestedResourceName
);

if (relationType === "hasMany") {
Expand Down Expand Up @@ -339,7 +347,8 @@ function _convertWithNestedResources(
relationModel,
nestedResourceName,
nestedResourceType,
nestedResourceIds
nestedResourceIds,
doubleNestedResourceName
) {
const query =
relationModel &&
Expand All @@ -356,6 +365,7 @@ function _convertWithNestedResources(
nestedResponse = query.klass
.query(resources)
.where({id: nestedResourceIds})
.includes([doubleNestedResourceName])
.toObjects();
} else if (model && model.toObject) {
nestedResponse = model.toObject();
Expand All @@ -382,13 +392,13 @@ function _buildRelationModel(
let relationModel, nestedResourceType, nestedResourceIds, nestedResourceNames;

nestedResourceNames = [];

currentIncludes
.filter(
relation =>
relation.split(".")[0] == type || relation.split(".")[0] == name
)
.filter(relation => {
return relation.split(".")[0] == type || relation.split(".")[0] == name;
})
.forEach(includesName => {
const splitName = includesName.split(".")[1];
const splitName = includesName.split(/\.(.+)/)[1];
if (splitName && splitName.includes("[")) {
const nestedNames = splitName
.replace(/[\[\]']+/g, "")
Expand All @@ -404,17 +414,24 @@ function _buildRelationModel(
const nestedResourceData = nestedResourceNames.map(nestedResourceName => {
nestedResourceType = null;
nestedResourceIds = null;
let singleNestedResourceName, doubleNestedResourceName;

if (nestedResourceName) {
// sets the nested class if it is a has many relationship
[
singleNestedResourceName,
doubleNestedResourceName
] = nestedResourceName.split(/\.(.+)/);

// sets the nested class if it is a hasMany relationship
let nestedClass = relationClass.belongsTo.filter(
klass => nestedResourceName === klass.singularName()
klass => singleNestedResourceName === klass.singularName()
)[0];

// handles the belongsTo cases
if (nestedClass) {
const belongsToData = get(
resources,
`${relationClass.pluralName()}.${id}.relationships.${nestedResourceName}.data`
`${relationClass.pluralName()}.${id}.relationships.${singleNestedResourceName}.data`
);
if (belongsToData) {
nestedResourceType = belongsToData.type;
Expand All @@ -427,7 +444,7 @@ function _buildRelationModel(
relationClass.hasMany.reduce((nestedClassData, klass) => {
let nestedRelationshipData = get(
resources,
`${relationClass.pluralName()}.${id}.relationships.${nestedResourceName}.data`
`${relationClass.pluralName()}.${id}.relationships.${singleNestedResourceName}.data`
);
if (!nestedRelationshipData) {
nestedRelationshipData = [];
Expand Down Expand Up @@ -472,7 +489,12 @@ function _buildRelationModel(
);
}
}
return [nestedResourceName, nestedResourceType, nestedResourceIds];
return [
singleNestedResourceName,
nestedResourceType,
nestedResourceIds,
doubleNestedResourceName
];
});

return [relationModel, nestedResourceData];
Expand Down
56 changes: 39 additions & 17 deletions dist/bundle.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,13 @@ function _setRelationShipKeyToValues({
currentIncludes,
name
}) {
const directIncludesRalationships = currentIncludes.map(
relation => relation.split(".")[0]
);
const directIncludesRalationships = currentIncludes
.map(relation => relation && relation.split("."))
.flat();

if (!directIncludesRalationships.includes(name))
return nextRelationshipObjects;

if (!(name in nextRelationshipObjects)) {
if (relationType === "hasMany") {
nextRelationshipObjects[name] = [];
Expand All @@ -269,6 +271,7 @@ function _setRelationShipKeyToValues({
}
}
if (!resources[type]) return nextRelationshipObjects;

const relationData = resources[type][id];
if (!relationData) return nextRelationshipObjects;

Expand All @@ -282,9 +285,13 @@ function _setRelationShipKeyToValues({
name,
relationData
);

nestedResourceData.forEach(
([nestedResourceName, nestedResourceType, nestedResourceIds]) => {
([
nestedResourceName,
nestedResourceType,
nestedResourceIds,
doubleNestedResourceName
]) => {
const nestedResources = _convertWithNestedResources(
conversion,
relationClass,
Expand All @@ -294,7 +301,8 @@ function _setRelationShipKeyToValues({
relationModel,
nestedResourceName,
nestedResourceType,
nestedResourceIds
nestedResourceIds,
doubleNestedResourceName
);

if (relationType === "hasMany") {
Expand Down Expand Up @@ -333,7 +341,8 @@ function _convertWithNestedResources(
relationModel,
nestedResourceName,
nestedResourceType,
nestedResourceIds
nestedResourceIds,
doubleNestedResourceName
) {
const query =
relationModel &&
Expand All @@ -350,6 +359,7 @@ function _convertWithNestedResources(
nestedResponse = query.klass
.query(resources)
.where({id: nestedResourceIds})
.includes([doubleNestedResourceName])
.toObjects();
} else if (model && model.toObject) {
nestedResponse = model.toObject();
Expand All @@ -376,13 +386,13 @@ function _buildRelationModel(
let relationModel, nestedResourceType, nestedResourceIds, nestedResourceNames;

nestedResourceNames = [];

currentIncludes
.filter(
relation =>
relation.split(".")[0] == type || relation.split(".")[0] == name
)
.filter(relation => {
return relation.split(".")[0] == type || relation.split(".")[0] == name;
})
.forEach(includesName => {
const splitName = includesName.split(".")[1];
const splitName = includesName.split(/\.(.+)/)[1];
if (splitName && splitName.includes("[")) {
const nestedNames = splitName
.replace(/[\[\]']+/g, "")
Expand All @@ -398,17 +408,24 @@ function _buildRelationModel(
const nestedResourceData = nestedResourceNames.map(nestedResourceName => {
nestedResourceType = null;
nestedResourceIds = null;
let singleNestedResourceName, doubleNestedResourceName;

if (nestedResourceName) {
// sets the nested class if it is a has many relationship
[
singleNestedResourceName,
doubleNestedResourceName
] = nestedResourceName.split(/\.(.+)/);

// sets the nested class if it is a hasMany relationship
let nestedClass = relationClass.belongsTo.filter(
klass => nestedResourceName === klass.singularName()
klass => singleNestedResourceName === klass.singularName()
)[0];

// handles the belongsTo cases
if (nestedClass) {
const belongsToData = get(
resources,
`${relationClass.pluralName()}.${id}.relationships.${nestedResourceName}.data`
`${relationClass.pluralName()}.${id}.relationships.${singleNestedResourceName}.data`
);
if (belongsToData) {
nestedResourceType = belongsToData.type;
Expand All @@ -421,7 +438,7 @@ function _buildRelationModel(
relationClass.hasMany.reduce((nestedClassData, klass) => {
let nestedRelationshipData = get(
resources,
`${relationClass.pluralName()}.${id}.relationships.${nestedResourceName}.data`
`${relationClass.pluralName()}.${id}.relationships.${singleNestedResourceName}.data`
);
if (!nestedRelationshipData) {
nestedRelationshipData = [];
Expand Down Expand Up @@ -466,7 +483,12 @@ function _buildRelationModel(
);
}
}
return [nestedResourceName, nestedResourceType, nestedResourceIds];
return [
singleNestedResourceName,
nestedResourceType,
nestedResourceIds,
doubleNestedResourceName
];
});

return [relationModel, nestedResourceData];
Expand Down
41 changes: 28 additions & 13 deletions dist/bundle.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -16921,9 +16921,11 @@
name = _ref6.name;

var directIncludesRalationships = currentIncludes.map(function (relation) {
return relation.split(".")[0];
});
return relation && relation.split(".");
}).flat();

if (!directIncludesRalationships.includes(name)) return nextRelationshipObjects;

if (!(name in nextRelationshipObjects)) {
if (relationType === "hasMany") {
nextRelationshipObjects[name] = [];
Expand All @@ -16932,6 +16934,7 @@
}
}
if (!resources[type]) return nextRelationshipObjects;

var relationData = resources[type][id];
if (!relationData) return nextRelationshipObjects;

Expand All @@ -16942,12 +16945,13 @@
nestedResourceData = _buildRelationModel3[1];

nestedResourceData.forEach(function (_ref7) {
var _ref8 = _slicedToArray$2(_ref7, 3),
var _ref8 = _slicedToArray$2(_ref7, 4),
nestedResourceName = _ref8[0],
nestedResourceType = _ref8[1],
nestedResourceIds = _ref8[2];
nestedResourceIds = _ref8[2],
doubleNestedResourceName = _ref8[3];

var nestedResources = _convertWithNestedResources(conversion, relationClass, resources, id, relationData, relationModel, nestedResourceName, nestedResourceType, nestedResourceIds);
var nestedResources = _convertWithNestedResources(conversion, relationClass, resources, id, relationData, relationModel, nestedResourceName, nestedResourceType, nestedResourceIds, doubleNestedResourceName);

if (relationType === "hasMany") {
var objIndex = nextRelationshipObjects[name].findIndex(function (obj) {
Expand All @@ -16969,14 +16973,14 @@
return nextRelationshipObjects;
}

function _convertWithNestedResources(conversion, relationClass, resources, id, relationData, relationModel, nestedResourceName, nestedResourceType, nestedResourceIds) {
function _convertWithNestedResources(conversion, relationClass, resources, id, relationData, relationModel, nestedResourceName, nestedResourceType, nestedResourceIds, doubleNestedResourceName) {
var query = relationModel && relationModel[nestedResourceType] && relationModel[nestedResourceType]();

var model = relationModel && relationModel[nestedResourceName] && relationModel[nestedResourceName]();

var nestedResponse = void 0;
if (query && query.toObjects) {
nestedResponse = query.klass.query(resources).where({ id: nestedResourceIds }).toObjects();
nestedResponse = query.klass.query(resources).where({ id: nestedResourceIds }).includes([doubleNestedResourceName]).toObjects();
} else if (model && model.toObject) {
nestedResponse = model.toObject();
}
Expand All @@ -16993,10 +16997,11 @@
nestedResourceNames = void 0;

nestedResourceNames = [];

currentIncludes.filter(function (relation) {
return relation.split(".")[0] == type || relation.split(".")[0] == name;
}).forEach(function (includesName) {
var splitName = includesName.split(".")[1];
var splitName = includesName.split(/\.(.+)/)[1];
if (splitName && splitName.includes("[")) {
var nestedNames = splitName.replace(/[\[\]']+/g, "").split(",").map(function (rn) {
return rn.trim();
Expand All @@ -17011,23 +17016,33 @@
var nestedResourceData = nestedResourceNames.map(function (nestedResourceName) {
nestedResourceType = null;
nestedResourceIds = null;
var singleNestedResourceName = void 0,
doubleNestedResourceName = void 0;

if (nestedResourceName) {
// sets the nested class if it is a has many relationship

// sets the nested class if it is a hasMany relationship
var _nestedResourceName$s = nestedResourceName.split(/\.(.+)/);

var _nestedResourceName$s2 = _slicedToArray$2(_nestedResourceName$s, 2);

singleNestedResourceName = _nestedResourceName$s2[0];
doubleNestedResourceName = _nestedResourceName$s2[1];
var nestedClass = relationClass.belongsTo.filter(function (klass) {
return nestedResourceName === klass.singularName();
return singleNestedResourceName === klass.singularName();
})[0];

// handles the belongsTo cases
if (nestedClass) {
var belongsToData = get(resources, relationClass.pluralName() + "." + id + ".relationships." + nestedResourceName + ".data");
var belongsToData = get(resources, relationClass.pluralName() + "." + id + ".relationships." + singleNestedResourceName + ".data");
if (belongsToData) {
nestedResourceType = belongsToData.type;
nestedResourceIds = [belongsToData.id];
}
} else {
// handles the hasMany cases
var nestedClassDataArray = relationClass.hasMany && relationClass.hasMany.reduce(function (nestedClassData, klass) {
var nestedRelationshipData = get(resources, relationClass.pluralName() + "." + id + ".relationships." + nestedResourceName + ".data");
var nestedRelationshipData = get(resources, relationClass.pluralName() + "." + id + ".relationships." + singleNestedResourceName + ".data");
if (!nestedRelationshipData) {
nestedRelationshipData = [];
}
Expand Down Expand Up @@ -17062,7 +17077,7 @@
}, relationData.attributes), relationClass.hasMany, relationClass.belongsTo);
}
}
return [nestedResourceName, nestedResourceType, nestedResourceIds];
return [singleNestedResourceName, nestedResourceType, nestedResourceIds, doubleNestedResourceName];
});

return [relationModel, nestedResourceData];
Expand Down

0 comments on commit de4be5d

Please sign in to comment.