Skip to content

feature(collection): add BeforeRequest event#596

Closed
andreialecu wants to merge 4 commits into
deployd:masterfrom
andreialecu:feature-beforerequest
Closed

feature(collection): add BeforeRequest event#596
andreialecu wants to merge 4 commits into
deployd:masterfrom
andreialecu:feature-beforerequest

Conversation

@andreialecu

Copy link
Copy Markdown
Contributor

New event that allows inspecting a request prior to the database being touched, and before every other event is executed.

Allows canceling the request early (for example for authorization purposes), or modifying the query before it is executed by mongo depending on custom logic written in the event.

Gives access to me, ctx, event (one of: GET, PUT, POST, LOGIN, LOGOUT) variables in the event script. You can use all the cancel() related methods and end the request right there.

On PUT or POST, there are two additional variables available: this and data - containing the item being saved.

New event that allows inspecting a request prior to the database being touched, and before every other event is executed.

Allows canceling the request early (for example for authorization purposes), or modifying the query before it is executed by mongo depending on custom logic written in the event.

Gives access to `me`, `ctx`, `event` (GET/PUT/POST/LOGIN/LOGOUT) variables in the event script.

On PUT or POST, there are two additional variables available: `this` and `data` - containing the item being saved.
@andreialecu

Copy link
Copy Markdown
Contributor Author

Example of a hard limit of 20 records per GET query:

if (!me) cancel("Not authorized", 401); // don't allow anyone in that is not authorized

switch (event) {
    case "GET":
        if (!ctx.query.$limit || ctx.query.$limit > 20) ctx.query.$limit = 20; // max 20 results
        break;
    case "PUT":
        // something for put
        break;
    case "POST":
        // something for post
        break;
}

@andreialecu

Copy link
Copy Markdown
Contributor Author

Also another example:

Say instead of deleting a record entirely, you just set deleted: true on it, in a field. This will make sure all GET requests return just non-deleted records:

switch (event) {
    case "GET":
        ctx.query = ctx.query || {};
        ctx.query.deleted = {$ne:true};
        break;
} 

Previously, there was no good way to enforce this. If you did cancelIf(this.deleted) in the GET event and combined it with $limit, mongo would limit results to your $limit, but then deployd would filter deleted items out via the script, so you'd end up with an array length of $limit - number of deleted records in this returned batch back. Which wasn't right.

Let's not merge this for a few more days, as I'm still testing it so I might make small changes.

I will leave a note here when I think it's fully ready.

@andreialecu

Copy link
Copy Markdown
Contributor Author

Alright, @ericfong:

Been testing this for the past couple of weeks and it is stable on our end. If anything comes up, we can fix it later. It doesn't affect anything at all unless it is used.

Add ?w=1 to the link for easier code reviewing:
https://github.com/deployd/deployd/pull/596/files?w=1

Comment thread lib/resources/user-collection.js Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var loginDomain = { event: "LOGIN" }; 

??

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops.

@ericfong

Copy link
Copy Markdown
Contributor

Great Thx! that is super useful for GET.

Any guideline for using BeforeRequest with Validate in POST or PUT ?

@andreialecu

Copy link
Copy Markdown
Contributor Author

BeforeRequest does not replace Validate. It is still recommended to validate updates in the Validate event.

You can do some checks inside BeforeRequest that apply to all of PUT/POST/DELETE if you need to, mostly for authorization purposes or other special requirements your scenario may require.

@ericfong

Copy link
Copy Markdown
Contributor

Thanks for your contribution!
Landed as 4960d07

@ericfong ericfong closed this Jul 19, 2015
@andreialecu andreialecu mentioned this pull request Jul 24, 2015
@docnoe

docnoe commented Aug 4, 2015

Copy link
Copy Markdown
Contributor

Awesome!
I wrote the "dpd-beforeget" module to be able to cancel GET requests early. Your solution however is much more felxible, thank you!!

@andreialecu andreialecu deleted the feature-beforerequest branch November 3, 2017 12:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants