Today I ran into an issue where I was unable to determine if a delete operation completed successfully in the 'after delete'
hook. This is problematic, as it requires an additional database operation to determine if the delete was a success, or a failure.
UserFollowers.observe('after delete', function observer(ctx, next) {
var instance = ctx.instance; // ctx.instance is null for some reason.
logger.info('Starting after delete');
logger.info('Ctx:',JSON.stringify(ctx));
// Ctx: {"where":{"followSrcId":2,"followDestId":1},"hookState":{},"options":{}}
logger.info('Ctx Non-Enumerable Properties:', Object.getOwnPropertyNames(ctx));
// Ctx Non-Enumerable Properties: ["Model","where","hookState","options"]
// Note that Model is the only non-enumerable property not spit out by JSON.stringify()
});
There's no indication of whether the operation was a success or a failure, and the 'after delete'
hook gets called regardless of the success of the operation. (Verified manually in the database)
Additionally, the only way to determine if the operation was successful is to attempt to select the record that existed prior to it.
Is there any way to get this?
You can setup a ChangeStream that will send an event whenever the model on the server is changed. Only caveat is on a DELETE, you will get an event when the operation is successful, even if there was no record that matched the request. So, for example, if I try to delete the same record with name=foo
twice, I'll get this back twice:
{"target":"foo","where":{"name":"foo"},"type":"remove"}
https://docs.strongloop.com/display/public/LB/Realtime+server-sent+events
Edit: You can also create and access a change stream through a built-in REST API