Skip to content

Add success and error methods to query chains. #316

@ptnplanet

Description

@ptnplanet

I am tired of writing

if (err) { return cb(err); }

on the first line in all of my callbacks. It's redundant code. ORM itself uses this line 50 times.

I would like to suggest adding an EventEmitter or Promise architecture to ORM that provides an api similar to this widely used approach:

EventEmitter.success(function (data) { /* ... */ });
EventEmitter.error(function (err) { /* ... */ });
EventEmitter.complete(function (err, data) { /* ... */ });

So instead of writing

Model.find(/* ... */).run(function (err, results) {
    if (err) { return errorCallback(err); }
    /* do something with results */
});

with error handling in all of the callbacks, its much nicer to write

Model.find(/* ... */).success(function (results) {
    /* do something with results */
}).error(errorCallback);

and let a dedicated errorCallback handle the error.

Now, there are two possible ways on integrating this. Option one is to add success and error functions to the chains and proxy their run method. Option two is to add a dedicated EventEmitter to ORM and replace basically all occurrences of return cb(/* ... */) with something along the line of

return new EventEmitter(function (emitter) {
    /* do something that might cause errors */
    if (err) {
        emitter.emit('error', err);
    } else {
        emitter.emit('success', results);
    }
});

Actually, this would only have to be done on the driver and other lower levels. Higher levels can adapt to the api of the returned event emitters.

What do you guys think? Maybe something for 3.x? I'm happy to contribute.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions