Skip to content
Merged
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
8 changes: 8 additions & 0 deletions common/models/peer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
"hasManyRelation": "profiles"
}
]
},
"PatchMany": {
"relations": [
{
"model": "topic",
"hasManyRelation": "topics"
}
]
}
},
"hidden": [
Expand Down
4 changes: 2 additions & 2 deletions server/middleware.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"compression": {},
"cors": {
"params": {
"origin": false,
"origin": true,
"credentials": true,
"maxAge": 86400
}
Expand Down Expand Up @@ -47,4 +47,4 @@
"final:after": {
"strong-error-handler": {}
}
}
}
17 changes: 8 additions & 9 deletions server/mixins/hasOne.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ module.exports = function (Model, options) {
options.relations.forEach(function (element) {
var modelName = element.model;
var relation = element.hasManyRelation;
var methodName = 'patch' + relation;

Model.post = function (id, data, cb) {
Model.postOne = function (id, data, cb) {
Model.findById(id, function (err, modelInstance) {
var relatedTo = modelInstance[relation];
relatedTo.count(function (err, count) {
Expand All @@ -28,7 +27,7 @@ module.exports = function (Model, options) {
});
};

Model.patch = function (id, data, cb) {
Model.patchOne = function (id, data, cb) {
Model.findById(id, function (err, modelInstance) {
var relatedTo = modelInstance[relation];
relatedTo(function (err, instances) {
Expand All @@ -50,7 +49,7 @@ module.exports = function (Model, options) {
});
};

Model.get = function (id, cb) {
Model.getOne = function (id, cb) {
Model.findById(id, function (err, modelInstance) {
if (err) {
cb(err);
Expand All @@ -64,7 +63,7 @@ module.exports = function (Model, options) {
});
};

Model.delete = function (id, cb) {
Model.deleteOne = function (id, cb) {
Model.findById(id, function (err, modelInstance) {
var relatedTo = modelInstance[relation];
relatedTo(function (err, instances) {
Expand All @@ -87,7 +86,7 @@ module.exports = function (Model, options) {
};

Model.remoteMethod(
'post',
'postOne',
{
accepts: [
{ arg: 'id', type: 'string', required: true },
Expand All @@ -100,7 +99,7 @@ module.exports = function (Model, options) {
);

Model.remoteMethod(
'patch',
'patchOne',
{
accepts: [
{ arg: 'id', type: 'string', required: true },
Expand All @@ -113,7 +112,7 @@ module.exports = function (Model, options) {
);

Model.remoteMethod(
'get',
'getOne',
{
accepts: [
{ arg: 'id', type: 'string', required: true },
Expand All @@ -124,7 +123,7 @@ module.exports = function (Model, options) {
}
);
Model.remoteMethod(
'delete',
'deleteOne',
{
accepts: [
{ arg: 'id', type: 'string', required: true }
Expand Down
86 changes: 86 additions & 0 deletions server/mixins/patchMany.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
module.exports = function (Model, options) {
'use strict';
options.relations.forEach(function (element) {
var modelName = element.model;
var relation = element.hasManyRelation;

Model.linkMany = function (id, data, cb) {
Model.findById(id, function (err, modelInstance) {
if (err) {
cb(err);
} else {
var relatedModel = modelInstance[relation];
var targetIds = data.targetIds;
if (targetIds.constructor === Array) {
targetIds.forEach(function (id) {
relatedModel.add(id, function (err, instanceData) {
if (err) {
console.log(err);
cb(err);
} else {
console.log(instanceData);
}
});
}, this);
cb(null, true);
} else {
cb(err, "Please input Array targetIds:[]");
}
}
});
};
Model.unlinkMany = function (id, data, cb) {
Model.findById(id, function (err, modelInstance) {
if (err) {
cb(err);
} else {
var relatedModel = modelInstance[relation];
var targetIds = data.targetIds;
if (targetIds.constructor === Array) {
targetIds.forEach(function (id) {
relatedModel.remove(id, function (err, instanceData) {
if (err) {
console.log(err);
cb(err);
} else {
console.log(instanceData);
}
});
}, this);
cb(null, true);
} else {
cb(err, "Please input Array targetIds:[]");
}
}
});
};


Model.remoteMethod(
'linkMany',
{
accepts: [
{ arg: 'id', type: 'string', required: true },
{ arg: 'data', type: 'object', required: true, http: { source: 'body' } }
],
description: "Links " + relation + " (please provide targetIds:[])",
returns: { arg: 'Object', type: 'object', root: true },
http: { path: '/:id/' + relation + '/rel', verb: 'patch' }
}
);
Model.remoteMethod(
'unlinkMany',
{
accepts: [
{ arg: 'id', type: 'string', required: true },
{ arg: 'data', type: 'object', required: true, http: { source: 'body' } }
],
description: "Unlinks " + relation + " (please provide targetIds:[])",
returns: { arg: 'Object', type: 'object', root: true },
http: { path: '/:id/' + relation + '/rel', verb: 'delete' }
}
);
}, this);


};