Skip to content

Commit

Permalink
Delete relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
mikec committed Oct 26, 2015
1 parent b69ed3d commit bdd45d6
Showing 1 changed file with 54 additions and 7 deletions.
61 changes: 54 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ var app, options;
var hooks = {};
var modelMap = {};
var identifierMap = {};
var modelNameMap = {};
var collectionNameMap = {};

var kalamata = module.exports = function(_app_, _options_) {
app = _app_;
Expand Down Expand Up @@ -57,18 +59,23 @@ kalamata.expose = function(model, _opts_) {
capitalize(opts.modelName) :
modelName(opts.endpointName);

modelMap[decapitalize(opts.modelName)] =
modelMap[decapitalize(opts.collectionName)] = model;
identifierMap[decapitalize(opts.modelName)] =
identifierMap[decapitalize(opts.collectionName)] = opts.identifier;
var modelNameLower = decapitalize(opts.modelName);
var collectionNameLower = decapitalize(opts.collectionName);

hooks[decapitalize(opts.modelName)] = hooks[decapitalize(opts.collectionName)] = {
modelMap[modelNameLower] =
modelMap[collectionNameLower] = model;
identifierMap[modelNameLower] =
identifierMap[collectionNameLower] = opts.identifier;
modelNameMap[collectionNameLower] = modelNameLower;
collectionNameMap[modelNameLower] = collectionNameLower;

hooks[modelNameLower] = hooks[collectionNameLower] = {
before: hookArrays(),
after: hookArrays()
};

var beforeHooks = hooks[decapitalize(opts.modelName)].before;
var afterHooks = hooks[decapitalize(opts.modelName)].after;
var beforeHooks = hooks[modelNameLower].before;
var afterHooks = hooks[modelNameLower].after;

createHookFunctions();
configureEndpoints();
Expand Down Expand Up @@ -264,6 +271,46 @@ kalamata.expose = function(model, _opts_) {
sendResponse(res, true);
}).catch(next);
});

app.delete(options.apiRoot + opts.endpointName + '/:identifier/:relation',
function(req, res, next) {
var rel = req.params.relation;
var mod = new model(getModelAttrs(req));
mod.fetch().then(function(m) {
var fKey = m[rel]().relatedData.foreignKey;
return m.set(fKey, null).save();
}).then(function() {
sendResponse(res, true);
}).catch(next);
});

app.delete(options.apiRoot + opts.endpointName + '/:identifier/:relation/:rIdentifier',
function(req, res, next) {
var rel = req.params.relation;
var rModel = modelMap[rel];
var rId = identifierMap[rel];
var mod = new model(getModelAttrs(req));
var relMod;

mod.fetch().then(function(m) {
var rModAttrs = {};
rModAttrs[rId] = req.params.rIdentifier;
return (new rModel(rModAttrs)).fetch().then(function(rMod) {
if(rMod) {
var modelName = modelNameMap[opts.endpointName];
var fKey = rMod[modelName]().relatedData.foreignKey;
return rMod.set(fKey, null).save();
} else {
throw new Error('Delete relationship failed: ' +
'Could not find ' + rel +
' model ' + JSON.stringify(rModAttrs));
}
});
}).then(function() {
sendResponse(res, true);
}).catch(next);

});
}

function runMultiHooks() {
Expand Down

0 comments on commit bdd45d6

Please sign in to comment.