Skip to content

Commit

Permalink
Created the framework code for removing model dependencies from forei…
Browse files Browse the repository at this point in the history
…gn keys
  • Loading branch information
MKHenson committed Apr 27, 2016
1 parent 7cfcab6 commit c206d0b
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 13 deletions.
2 changes: 1 addition & 1 deletion server/dist/definitions/modepress-api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ declare module Modepress
{
_id?: any;
_requiredDependencies?: Array<{ collection: string, _id : any }>
_optionalDependencies?: Array<{ collection: string, _id : any }>
_optionalDependencies?: Array<{ collection: string, propertyName: string, _id : any }>
}

/**
Expand Down
63 changes: 58 additions & 5 deletions server/dist/src/models/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,17 +233,70 @@ var Model = (function () {
});
};
/**
* Deletes a instance and all its dependencies are updated or deleted accordingly
* @returns {Promise<any>}
*/
Model.prototype.deleteInstance = function (instance) {
var that = this;
var foreignModel;
var optionalDependencies = instance.dbEntry._optionalDependencies;
var requiredDependencies = instance.dbEntry._requiredDependencies;
var promises = [];
return new Promise(function (resolve, reject) {
// Nullify all dependencies that are optional
if (optionalDependencies)
for (var i = 0, l = optionalDependencies.length; i < l; i++) {
foreignModel = Model.getByName(optionalDependencies[i].collection);
if (!foreignModel)
continue;
var setToken = { $set: {} };
setToken.$set[optionalDependencies[i].propertyName] = null;
promises.push(foreignModel.collection.updateOne({ _id: optionalDependencies[i]._id }, setToken));
}
// For those dependencies that are required, we delete the instances
if (requiredDependencies)
for (var i = 0, l = requiredDependencies.length; i < l; i++) {
foreignModel = Model.getByName(requiredDependencies[i].collection);
if (!foreignModel)
continue;
foreignModel.findInstances({ _id: requiredDependencies[i]._id }).then(function (instances) {
instances.forEach(function (instanceToDelete) {
promises.push(that.deleteInstance(instanceToDelete));
});
});
}
Promise.all(promises).then(function () {
// Remove the original instance from the DB
that.collection.deleteMany({ _id: instance.dbEntry._id }).then(function (deleteResult) {
resolve();
}).catch(function (err) {
reject(err);
});
}).catch(function (err) {
reject(err);
});
});
};
/**
* Deletes a number of instances based on the selector. The promise reports how many items were deleted
* @returns {Promise<number>}
*/
Model.prototype.deleteInstances = function (selector) {
var model = this;
var that = this;
return new Promise(function (resolve, reject) {
var collection = model.collection;
collection.deleteMany(selector).then(function (deleteResult) {
resolve(deleteResult.deletedCount);
}).catch(function (err) {
reject(err);
that.findInstances(selector).then(function (instances) {
if (!instances || instances.length == 0)
return resolve(0);
var promises = [];
instances.forEach(function (instance, index) {
promises.push(that.deleteInstance(instance));
});
Promise.all(promises).then(function () {
resolve(instances.length);
}).catch(function (err) {
reject(err);
});
});
});
};
Expand Down
2 changes: 1 addition & 1 deletion server/src/definitions/custom/modepress-api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{
_id?: any;
_requiredDependencies?: Array<{ collection: string, _id : any }>
_optionalDependencies?: Array<{ collection: string, _id : any }>
_optionalDependencies?: Array<{ collection: string, propertyName: string, _id : any }>
}

/**
Expand Down
81 changes: 75 additions & 6 deletions server/src/models/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,21 +315,90 @@ export abstract class Model
});
}

/**
* Deletes a instance and all its dependencies are updated or deleted accordingly
* @returns {Promise<any>}
*/
private deleteInstance( instance : ModelInstance<IModelEntry> ) : Promise<any>
{
var that = this;
var foreignModel : Model;
var optionalDependencies = instance.dbEntry._optionalDependencies;
var requiredDependencies = instance.dbEntry._requiredDependencies;
var promises : Array<Promise<any>> = [];

return new Promise(function( resolve, reject ) {

// Nullify all dependencies that are optional
if (optionalDependencies)
for ( var i = 0, l = optionalDependencies.length; i < l; i++ )
{
foreignModel = Model.getByName( optionalDependencies[i].collection );
if (!foreignModel)
continue;

var setToken = { $set : {} };
setToken.$set[optionalDependencies[i].propertyName] = null;
promises.push( foreignModel.collection.updateOne( <IModelEntry>{ _id : optionalDependencies[i]._id }, setToken ) );
}

// For those dependencies that are required, we delete the instances
if (requiredDependencies)
for ( var i = 0, l = requiredDependencies.length; i < l; i++ )
{
foreignModel = Model.getByName( requiredDependencies[i].collection );
if (!foreignModel)
continue;

foreignModel.findInstances<IModelEntry>( <IModelEntry>{ _id : requiredDependencies[i]._id } ).then(function( instances ){
instances.forEach( function( instanceToDelete ){
promises.push( that.deleteInstance( instanceToDelete ) );
});
});
}

Promise.all(promises).then(function() {

// Remove the original instance from the DB
that.collection.deleteMany( <IModelEntry>{ _id : instance.dbEntry._id }).then( function ( deleteResult ) {
resolve();
}).catch(function(err: Error) {
reject(err);
});

}).catch(function( err: Error ) {
reject(err);
});
});
}

/**
* Deletes a number of instances based on the selector. The promise reports how many items were deleted
* @returns {Promise<number>}
*/
deleteInstances(selector: any): Promise<number>
{
var model = this;
var that = this;

return new Promise<number>(function (resolve, reject)
{
var collection = model.collection;
collection.deleteMany(selector).then( function ( deleteResult ) {
resolve(deleteResult.deletedCount);
}).catch(function(err: Error){
reject(err);
that.findInstances<IModelEntry>(selector).then(function( instances )
{
if (!instances || instances.length == 0)
return resolve(0);

var promises : Array<Promise<any>> = [];

instances.forEach(function(instance, index) {
promises.push(that.deleteInstance(instance));
});

Promise.all(promises).then(function() {
resolve(instances.length)
}).catch(function(err: Error){
reject(err);
});
});
});
}
Expand Down Expand Up @@ -359,7 +428,7 @@ export abstract class Model
if (!instances || instances.length == 0)
return resolve(toRet);

instances.forEach(function (instance: ModelInstance<T>, index)
instances.forEach(function (instance, index)
{
// If we have data, then set the variables
if (data)
Expand Down

0 comments on commit c206d0b

Please sign in to comment.