Skip to content

Commit

Permalink
Fixed bug with foreign key deletion in models
Browse files Browse the repository at this point in the history
  • Loading branch information
MKHenson committed May 20, 2016
1 parent afbe843 commit be155f8
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions server/src/models/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,10 @@ export abstract class Model
if (!foreignModel)
continue;

var instances = await foreignModel.findInstances<IModelEntry>( <IModelEntry>{ _id : requiredDependencies[i]._id } );

for (var ii = 0, il = instances.length; ii < il; ii++)
promises.push( this.deleteInstance( instances[ii] ) );
promises.push( foreignModel.deleteInstances( <IModelEntry>{ _id : requiredDependencies[i]._id } ) );
}

await Promise.all(promises);
var dependenciesResults = await Promise.all(promises);

// Remove the original instance from the DB
var deleteResult = await this.collection.deleteMany( <IModelEntry>{ _id : instance.dbEntry._id });
Expand Down Expand Up @@ -368,6 +365,10 @@ export abstract class Model
var json = instance.schema.serialize();
var collection = this.collection;
var updateResult = await collection.updateOne({ _id: (<IModelEntry>instance)._id }, { $set: json });

// Now that everything has been added, we can do some post insert/update validation
await instance.schema.postValidation<T>( instance, this._collectionName );

toRet.tokens.push({ error: false, instance: instance });

} catch ( err ) {
Expand Down Expand Up @@ -480,8 +481,17 @@ export abstract class Model
var insertResult = await collection.insertMany(documents);

// Assign the ID's
for (var i = 0, l = insertResult.ops.length; i < l; i++)
for (var i = 0, l = insertResult.ops.length; i < l; i++) {
instances[i]._id = insertResult.ops[i]._id;
instances[i].dbEntry = insertResult.ops[i];
}

// Now that everything has been added, we can do some post insert/update validation
var postValidationPromises : Array<Promise<Schema>> = [];
for (var i = 0, l = instances.length; i < l; i++)
postValidationPromises.push( instances[i].schema.postValidation<T>(instances[i], this._collectionName ) );

await Promise.all<Schema>(postValidationPromises);

return instances;
}
Expand Down

0 comments on commit be155f8

Please sign in to comment.