Skip to content

Commit

Permalink
Removed foreign keys from target when source is deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
MKHenson committed May 26, 2016
1 parent b70a58d commit a030bdc
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 6 deletions.
26 changes: 26 additions & 0 deletions server/dist/src/models/schema-items/schema-foreign-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,32 @@ class SchemaForeignKey extends schema_item_1.SchemaItem {
return;
});
}
/**
* Called after a model instance is deleted. Useful for any schema item cleanups.
* @param {ModelInstance<T>} instance The model instance that was deleted
* @param {string} collection The DB collection that the model was deleted from
*/
postDelete(instance, collection) {
return __awaiter(this, void 0, Promise, function* () {
// If they key is required then it must exist
var model = model_1.Model.getByName(this.targetCollection);
if (!model)
return;
if (!this.value || this.value == "")
return;
// We can assume the value is object id by this point
var result = yield model.findOne({ _id: this.value });
if (!result)
return;
var query;
if (this.optionalKey)
query = { $pull: { _optionalDependencies: { _id: instance.dbEntry._id } } };
else
query = { $pull: { _requiredDependencies: { _id: instance.dbEntry._id } } };
yield model.collection.updateOne({ _id: this._targetDoc.dbEntry._id }, query);
return;
});
}
/**
* Gets the value of this item
* @param {ISchemaOptions} options [Optional] A set of options that can be passed to control how the data must be returned
Expand Down
37 changes: 34 additions & 3 deletions server/dist/src/models/schema-items/schema-id-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class SchemaIdArray extends schema_item_1.SchemaItem {
* @param {string} targetCollection [Optional] Specify the model name to which all the ids belong. If set
* the item can expand objects on retreival.
*/
constructor(name, val, minItems = 0, maxItems = 10000, targetCollection = "") {
constructor(name, val, minItems = 0, maxItems = 10000, targetCollection = null) {
super(name, val);
this.maxItems = maxItems;
this.minItems = minItems;
Expand Down Expand Up @@ -71,7 +71,7 @@ class SchemaIdArray extends schema_item_1.SchemaItem {
if (transformedValue.length > this.maxItems)
throw new Error(`You have selected too many items for ${this.name}, please only use up to ${this.maxItems}`);
// If no collection - then return
if (this.targetCollection == "")
if (!this.targetCollection)
return true;
if (this.value.length == 0)
return true;
Expand Down Expand Up @@ -115,6 +115,37 @@ class SchemaIdArray extends schema_item_1.SchemaItem {
return;
});
}
/**
* Called after a model instance is deleted. Useful for any schema item cleanups.
* @param {ModelInstance<T>} instance The model instance that was deleted
* @param {string} collection The DB collection that the model was deleted from
*/
postDelete(instance, collection) {
return __awaiter(this, void 0, Promise, function* () {
if (!this.targetCollection)
return;
// If they key is required then it must exist
var model = model_1.Model.getByName(this.targetCollection);
if (!model)
return;
if (!this.value || this.value.length == 0)
return;
// Get all the instances
var promises = [];
var query = { $or: [] };
var arr = this.value;
for (var i = 0, l = arr.length; i < l; i++)
query.$or.push({ _id: arr[i] });
var results = yield model.findInstances(query);
if (!results || results.length == 0)
return;
var pullQueries = [];
for (var i = 0, l = results.length; i < l; i++)
pullQueries.push(model.collection.updateOne({ _id: results[i].dbEntry._id }, { $pull: { _arrayDependencies: { _id: instance.dbEntry._id } } }));
yield Promise.all(pullQueries);
return;
});
}
/**
* Gets the value of this item
* @param {ISchemaOptions} options [Optional] A set of options that can be passed to control how the data must be returned
Expand All @@ -128,7 +159,7 @@ class SchemaIdArray extends schema_item_1.SchemaItem {
return this.value;
if (options.expandSchemaBlacklist && options.expandSchemaBlacklist.indexOf(this.name) != -1)
return this.value;
if (this.targetCollection == "")
if (!this.targetCollection)
return this.value;
var model = model_1.Model.getByName(this.targetCollection);
if (!model)
Expand Down
31 changes: 31 additions & 0 deletions server/src/models/schema-items/schema-foreign-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,37 @@ export class SchemaForeignKey extends SchemaItem<ObjectID | string | Modepress.I
return;
}

/**
* Called after a model instance is deleted. Useful for any schema item cleanups.
* @param {ModelInstance<T>} instance The model instance that was deleted
* @param {string} collection The DB collection that the model was deleted from
*/
public async postDelete<T extends Modepress.IModelEntry>( instance: ModelInstance<T>, collection : string ): Promise<void>
{
// If they key is required then it must exist
var model = Model.getByName(this.targetCollection);
if (!model)
return;

if (!this.value || this.value == "")
return;

// We can assume the value is object id by this point
var result = await model.findOne<Modepress.IModelEntry>( { _id : <ObjectID>this.value } );
if (!result)
return;

var query;

if (this.optionalKey)
query = { $pull: { _optionalDependencies: { _id : instance.dbEntry._id } } };
else
query = { $pull: { _requiredDependencies: { _id : instance.dbEntry._id } } };

await model.collection.updateOne( <Modepress.IModelEntry>{ _id : this._targetDoc.dbEntry._id }, query );
return;
}

/**
* Gets the value of this item
* @param {ISchemaOptions} options [Optional] A set of options that can be passed to control how the data must be returned
Expand Down
48 changes: 45 additions & 3 deletions server/src/models/schema-items/schema-id-array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class SchemaIdArray extends SchemaItem<Array<string | ObjectID | Modepres
* @param {string} targetCollection [Optional] Specify the model name to which all the ids belong. If set
* the item can expand objects on retreival.
*/
constructor(name: string, val: Array<string>, minItems: number = 0, maxItems: number = 10000, targetCollection: string = "")
constructor(name: string, val: Array<string>, minItems: number = 0, maxItems: number = 10000, targetCollection: string = null)
{
super(name, val);
this.maxItems = maxItems;
Expand Down Expand Up @@ -81,7 +81,7 @@ export class SchemaIdArray extends SchemaItem<Array<string | ObjectID | Modepres
throw new Error(`You have selected too many items for ${this.name}, please only use up to ${this.maxItems}`);

// If no collection - then return
if (this.targetCollection == "")
if (!this.targetCollection)
return true;

if (this.value.length == 0)
Expand Down Expand Up @@ -137,6 +137,48 @@ export class SchemaIdArray extends SchemaItem<Array<string | ObjectID | Modepres
return;
}

/**
* Called after a model instance is deleted. Useful for any schema item cleanups.
* @param {ModelInstance<T>} instance The model instance that was deleted
* @param {string} collection The DB collection that the model was deleted from
*/
public async postDelete<T extends Modepress.IModelEntry>( instance: ModelInstance<T>, collection : string ): Promise<void>
{
if (!this.targetCollection)
return;

// If they key is required then it must exist
var model = Model.getByName(this.targetCollection);
if (!model)
return;

if (!this.value || this.value.length == 0)
return;

// Get all the instances
var promises : Array<Promise<UpdateWriteOpResult>> = [];
var query = { $or : [] };
var arr = this.value;

for (var i = 0, l = arr.length; i < l; i++)
query.$or.push( <Modepress.IModelEntry>{ _id :<ObjectID>arr[i] } );

var results = await model.findInstances<Modepress.IModelEntry>( query );
if (!results || results.length == 0)
return;

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

for (var i = 0, l = results.length; i < l; i++)
pullQueries.push( model.collection.updateOne(
<Modepress.IModelEntry>{ _id : results[i].dbEntry._id },
{ $pull: { _arrayDependencies: { _id : instance.dbEntry._id } } }
));

await Promise.all(pullQueries);
return;
}

/**
* Gets the value of this item
* @param {ISchemaOptions} options [Optional] A set of options that can be passed to control how the data must be returned
Expand All @@ -153,7 +195,7 @@ export class SchemaIdArray extends SchemaItem<Array<string | ObjectID | Modepres
if (options.expandSchemaBlacklist && options.expandSchemaBlacklist.indexOf(this.name) != -1)
return this.value;

if (this.targetCollection == "")
if (!this.targetCollection)
return this.value;

var model = Model.getByName(this.targetCollection);
Expand Down

0 comments on commit a030bdc

Please sign in to comment.