Skip to content

Skip polymorphic has many update #310

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

Open
wants to merge 5 commits 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
97 changes: 50 additions & 47 deletions lib/relationships.js
Original file line number Diff line number Diff line change
@@ -1,81 +1,84 @@
'use strict'
"use strict";

var _ = require('lodash')
var utils = require('./utils')
const linkRelatedModels = require(
'./utilities/relationship-utils'
).linkRelatedModels
var _ = require("lodash");
var utils = require("./utils");
const linkRelatedModels =
require("./utilities/relationship-utils").linkRelatedModels;

module.exports = function (app, options) {
// get remote methods.
// set strong-remoting for more information
// https://github.com/strongloop/strong-remoting
var remotes = app.remotes()
var id, data, model
var remotes = app.remotes();
var id, data, model;

remotes.before('**', function (ctx, next) {
remotes.before("**", function (ctx, next) {
if (utils.shouldNotApplyJsonApi(ctx, options)) {
return next()
return next();
}

var allowedMethodNames = ['updateAttributes', 'patchAttributes']
var methodName = ctx.method.name
if (allowedMethodNames.indexOf(methodName) === -1) return next()
var allowedMethodNames = ["updateAttributes", "patchAttributes"];
var methodName = ctx.method.name;
if (allowedMethodNames.indexOf(methodName) === -1) return next();

id = ctx.req.params.id
data = options.data
model = utils.getModelFromContext(ctx, app)
id = ctx.req.params.id;
data = options.data;
model = utils.getModelFromContext(ctx, app);

relationships(model, id, data).then(() => next()).catch(err => next(err))
})
relationships(model, id, data, ctx.args.options)
.then(() => next())
.catch((err) => next(err));
});

// for create
remotes.after('**', function (ctx, next) {
remotes.after("**", function (ctx, next) {
if (utils.shouldNotApplyJsonApi(ctx, options)) {
return next()
return next();
}

if (ctx.method.name !== 'create') return next()
if (ctx.method.name !== "create") return next();

if (ctx.result && ctx.result.data) {
id = ctx.result.data.id
data = options.data
model = utils.getModelFromContext(ctx, app)
relationships(model, id, data).then(() => next()).catch(err => next(err))
return
id = ctx.result.data.id;
data = options.data;
model = utils.getModelFromContext(ctx, app);
relationships(model, id, data, ctx.args.options)
.then(() => next())
.catch((err) => next(err));
return;
}

next()
})
}
next();
});
};

function extractIdsFromResource (resource) {
function extractIdsFromResource(resource) {
if (_.isArray(resource)) {
return _.map(resource, 'id')
return _.map(resource, "id");
}
return _.get(resource, 'id', null)
return _.get(resource, "id", null);
}

function relationships (model, id, payload) {
if (!id || !model) return
const relationships = _.get(payload, 'data.relationships', {})
function relationships(model, id, payload, ctxOptions) {
if (!id || !model) return;
const relationships = _.get(payload, "data.relationships", {});

return Promise.all(
Object.keys(relationships).map(relationName => {
const relationship = relationships[relationName]
const relationDefn = model.relations[relationName]
if (!relationDefn) return
Object.keys(relationships).map((relationName) => {
const relationship = relationships[relationName];
const relationDefn = model.relations[relationName];
if (!relationDefn) return;

const type = relationDefn.type
const modelTo = relationDefn.modelTo
const type = relationDefn.type;
const modelTo = relationDefn.modelTo;

// don't handle belongsTo in relationships function
if (!modelTo || type === 'belongsTo') return
if (!modelTo || type === "belongsTo") return;

const data = extractIdsFromResource(relationship.data)
const from = { model, id }
const to = { model: modelTo, data }
return linkRelatedModels(relationName, from, to)
const data = extractIdsFromResource(relationship.data);
const from = { model, id };
const to = { model: modelTo, data };
return linkRelatedModels(relationName, from, to, ctxOptions);
})
)
);
}
11 changes: 7 additions & 4 deletions lib/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,7 @@ function parseRelations (data, relations, options) {
}

var relationship = null

if (!_.isUndefined(fk) && relation.modelTo !== relation.modelFrom) {
if (!_.isUndefined(fk) && !relation.multiple) {
if (_.isArray(fk)) {
relationship = makeRelations(toType, fk, options)
} else {
Expand All @@ -272,8 +271,12 @@ function parseRelations (data, relations, options) {
} else if (relation.type === 'belongsTo') {
relationships[name].data = null
}
})

if (relation.modelTo === relation.modelFrom) {
delete relationships[name].data
}

})
return relationships
}

Expand Down Expand Up @@ -505,4 +508,4 @@ function createCompoundIncludes (
compoundInclude.attributes = relationship

return compoundInclude
}
}
Loading