# Query \`include\` doesn't trigger \`beforeFind\` trigger for included objects

**URL:** <https://community.parseplatform.org/t/query-include-doesnt-trigger-beforefind-trigger-for-included-objects/1964>\
**Category:** Cloud Code\
**Tags:** query, cloud-code-trigger, before-find\
**Created:** [September 7, 2021, 6:46am UTC](https://community.parseplatform.org/t/query-include-doesnt-trigger-beforefind-trigger-for-included-objects/1964 "2021-09-07T06:46:54Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![hariprasadiit](https://community.parseplatform.org/user_avatar/community.parseplatform.org/hariprasadiit/32/379_2.png) [@hariprasadiit](https://community.parseplatform.org/u/hariprasadiit)\
**Post date:** [September 7, 2021, 6:46am UTC](https://community.parseplatform.org/t/query-include-doesnt-trigger-beforefind-trigger-for-included-objects/1964/1 "2021-09-07T06:46:54Z")

</div>

Hello,

When I was testing parse server I’ve found that, `beforeFind` trigger is not triggered for included objects in `find` or `get` queries using `include` option.

Is this working as expected?

We’re applying custom ACLs in `beforeFInd` trigger. Without `beforeFind` triggers for included objects we’re not able to apply ACLs on included objects.

If this is intentional, could you please help me with alternative solution?

---

<div class="post-metadata">

**Author:** ![davimacedo](https://community.parseplatform.org/user_avatar/community.parseplatform.org/davimacedo/32/316_2.png) [@davimacedo](https://community.parseplatform.org/u/davimacedo)\
**Post date:** [September 7, 2021, 4:27pm UTC](https://community.parseplatform.org/t/query-include-doesnt-trigger-beforefind-trigger-for-included-objects/1964/2 "2021-09-07T16:27:54Z")

</div>

Could you please give an example and/or share some code?

---

<div class="post-metadata">

**Author:** ![hariprasadiit](https://community.parseplatform.org/user_avatar/community.parseplatform.org/hariprasadiit/32/379_2.png) [@hariprasadiit](https://community.parseplatform.org/u/hariprasadiit)\
**Post date:** [September 7, 2021, 5:20pm UTC](https://community.parseplatform.org/t/query-include-doesnt-trigger-beforefind-trigger-for-included-objects/1964/3 "2021-09-07T17:20:02Z")

</div>

Thanks for the reply. Here’s an example

**Client Side code:**

```auto
const company = new Parse.Object('Company');
company.id = 'company_id';

const query = new Parse.Query('Employee');
query.equalTo('company', company);
query.include('user'); // user is an instance of _User
const results = await query.find()

```

**Cloud Code**

```auto
Parse.beforeFind('_User', (req) => {
  // This is triggered for Employee class but not for _User class, when above query is executed
  
  // custom acl code using casbin library. Parse ACL is set to public read/write on all classes
})

```

Please let me know if more information is needed.

---

<div class="post-metadata">

**Author:** ![davimacedo](https://community.parseplatform.org/user_avatar/community.parseplatform.org/davimacedo/32/316_2.png) [@davimacedo](https://community.parseplatform.org/u/davimacedo)\
**Post date:** [September 8, 2021, 4:14pm UTC](https://community.parseplatform.org/t/query-include-doesnt-trigger-beforefind-trigger-for-included-objects/1964/4 "2021-09-08T16:14:06Z")

</div>

I’ve just checked the code and unfortunately beforeFind trigger when not fire in this case for the include query. Basically the trigger is called one layer over ([parse-server/rest.js at 9ea355b4635226ae4da17c8cc5fb0321e3fdec5e · parse-community/parse-server · GitHub](https://github.com/parse-community/parse-server/blob/9ea355b4635226ae4da17c8cc5fb0321e3fdec5e/src/rest.js#L27)) the one that is internally used to run the query to include the sub objects ([parse-server/RestQuery.js at master · parse-community/parse-server · GitHub](https://github.com/parse-community/parse-server/blob/master/src/RestQuery.js#L894)). An workaround could be handling this on beforeFind or afterFind trigger of the Employee class.

---

<div class="post-metadata">

**Author:** ![reptilehaus](https://community.parseplatform.org/user_avatar/community.parseplatform.org/reptilehaus/32/1259_2.png) [@reptilehaus](https://community.parseplatform.org/u/reptilehaus)\
**Post date:** [August 4, 2022, 6:42am UTC](https://community.parseplatform.org/t/query-include-doesnt-trigger-beforefind-trigger-for-included-objects/1964/5 "2022-08-04T06:42:27Z")

</div>

@hariprasadiit Im curious about your use of casbin for ACL, what are the benefits of using it over the native backed in parse ACL/Role system ?

---

<div class="post-metadata">

**Author:** ![hariprasadiit](https://community.parseplatform.org/user_avatar/community.parseplatform.org/hariprasadiit/32/379_2.png) [@hariprasadiit](https://community.parseplatform.org/u/hariprasadiit)\
**Post date:** [August 4, 2022, 9:45am UTC](https://community.parseplatform.org/t/query-include-doesnt-trigger-beforefind-trigger-for-included-objects/1964/6 "2022-08-04T09:45:04Z")

</div>

it’s more flexible. I can implement both ABAC and RBAC using Casbin in one place and it’s portable to other environments.

Main reason is to support multi tenancy, which is a bit complex with Parse ACL/Role system

---

<div class="post-metadata">

**Author:** ![reptilehaus](https://community.parseplatform.org/user_avatar/community.parseplatform.org/reptilehaus/32/1259_2.png) [@reptilehaus](https://community.parseplatform.org/u/reptilehaus)\
**Post date:** [August 4, 2022, 10:13am UTC](https://community.parseplatform.org/t/query-include-doesnt-trigger-beforefind-trigger-for-included-objects/1964/7 "2022-08-04T10:13:35Z")

</div>

This is exactly what Im trying to build at the moment, multi tenant SaaS solution. Could I pick your brain on how you are implementing this ?

---

<div class="post-metadata">

**Author:** ![dblythy](https://community.parseplatform.org/user_avatar/community.parseplatform.org/dblythy/32/231_2.png) [@dblythy](https://community.parseplatform.org/u/dblythy)\
**Post date:** [August 4, 2022, 11:08am UTC](https://community.parseplatform.org/t/query-include-doesnt-trigger-beforefind-trigger-for-included-objects/1964/8 "2022-08-04T11:08:26Z")

</div>

You can enforce `select` and `exclude` for the first class. You can also perform mutations in afterFind.

```auto
Parse.Cloud.beforeFind('Employee', async ({query, user}) => {
  query.exclude("user")
// or

  const user = query._where?.user;
  // do custom logic here
});

```

---

<div class="post-metadata">

**Author:** ![hariprasadiit](https://community.parseplatform.org/user_avatar/community.parseplatform.org/hariprasadiit/32/379_2.png) [@hariprasadiit](https://community.parseplatform.org/u/hariprasadiit)\
**Post date:** [August 6, 2022, 6:50am UTC](https://community.parseplatform.org/t/query-include-doesnt-trigger-beforefind-trigger-for-included-objects/1964/9 "2022-08-06T06:50:56Z")

</div>

Thanks for the suggestion. I thought of doing the same.

As per the design should `include` trigger cloud code trigger or it shouldn’t? isn’t `include` synonymous to fetching that from client side?

---

<div class="post-metadata">

**Author:** ![hariprasadiit](https://community.parseplatform.org/user_avatar/community.parseplatform.org/hariprasadiit/32/379_2.png) [@hariprasadiit](https://community.parseplatform.org/u/hariprasadiit)\
**Post date:** [August 6, 2022, 6:52am UTC](https://community.parseplatform.org/t/query-include-doesnt-trigger-beforefind-trigger-for-included-objects/1964/10 "2022-08-06T06:52:56Z")

</div>

It’ll be a lengthy discussion if I were to mention implementation details here. Could you please be specific on where you need help here? Casbin model and policies?

---

<div class="post-metadata">

**Author:** ![reptilehaus](https://community.parseplatform.org/user_avatar/community.parseplatform.org/reptilehaus/32/1259_2.png) [@reptilehaus](https://community.parseplatform.org/u/reptilehaus)\
**Post date:** [August 6, 2022, 7:57am UTC](https://community.parseplatform.org/t/query-include-doesnt-trigger-beforefind-trigger-for-included-objects/1964/11 "2022-08-06T07:57:25Z")

</div>

Just how it works along side Parse. My desired architecture would be separate users by domain/org and under each org have a set of roles which are identical but at least are separated on a domain by domain basis.

Right now im using parse ACL but its restrictive for multi tenant setups

---

<div class="post-metadata">

**Author:** ![reptilehaus](https://community.parseplatform.org/user_avatar/community.parseplatform.org/reptilehaus/32/1259_2.png) [@reptilehaus](https://community.parseplatform.org/u/reptilehaus)\
**Post date:** [August 6, 2022, 7:58am UTC](https://community.parseplatform.org/t/query-include-doesnt-trigger-beforefind-trigger-for-included-objects/1964/12 "2022-08-06T07:58:16Z")

</div>

btw I think .include() is used when you have a pointer on an object to pull in that full object into the query result ( kind of like .fetch() )

---

<div class="post-metadata">

**Author:** ![woutercouvaras](https://community.parseplatform.org/user_avatar/community.parseplatform.org/woutercouvaras/32/760_2.png) [@woutercouvaras](https://community.parseplatform.org/u/woutercouvaras)\
**Post date:** [September 13, 2022, 12:07pm UTC](https://community.parseplatform.org/t/query-include-doesnt-trigger-beforefind-trigger-for-included-objects/1964/13 "2022-09-13T12:07:34Z")

</div>

Hi folks,

I’ve got a question that is kind of related, though slightly different…

I’m storing some rather nested data that contains pointers, either to media or other documents, etc.

I’ve written some code dynamically build the `include` paths on the server so that the data all gets automatically included when fetching a given doc/page. This is all working pretty nicely, but when it comes to updating/saving a doc, I need to convert them all back to pointers, but `include` removes the `class` reference, which makes this a bit tricky.

If `include` could keep the `class` reference in the result, this would make it easier to solve.

Does anyone have a suggestion to solve this?

---

<div class="post-metadata">

**Author:** ![davimacedo](https://community.parseplatform.org/user_avatar/community.parseplatform.org/davimacedo/32/316_2.png) [@davimacedo](https://community.parseplatform.org/u/davimacedo)\
**Post date:** [September 26, 2022, 3:11pm UTC](https://community.parseplatform.org/t/query-include-doesnt-trigger-beforefind-trigger-for-included-objects/1964/14 "2022-09-26T15:11:47Z")

</div>

Maybe you can check the schema.
