Parse Server edit Relations on Object very slow












0















I've got the following function which works as expected on Parse Server cloud code, however it's painfully slow.



The nested for loops which are internally calling queries and save functions are undoubtedly the root cause.



How can I refactor this code so that there is some async processing or even better what methods are there to remove / edit the relations on an object, the documentation around this is very poor.



ClientLabels.applyClientLabels = async (req, res) => {

const { clients, labels } = req.params;
const user = req.user;
const objectIds = clients.map((client) => client.objectId);
const clientSaveList = ;
const clientClass = Parse.Object.extend('Clients');
const query = new Parse.Query(clientClass);
query.containedIn("objectId", objectIds);
const queryResult = await query.find({ sessionToken: user.getSessionToken() })

try {
for (const client of queryResult) {

const labelRelation = client.relation('labels');
const relatedLabels = await labelRelation.query().find({ sessionToken: user.getSessionToken() });
labelRelation.remove(relatedLabels);

for (const label of labels) {
label.className = "ClientLabels";
const labelRelationObj = Parse.Object.fromJSON(label)
labelRelation.add(labelRelationObj);
};
clientSaveList.push(client);
};

const saved = await Parse.Object.saveAll(clientSaveList, { sessionToken: user.getSessionToken() })
res.success(saved);

} catch (e) {
res.error(e);
};
}


Explanation of some weirdness:
I am having to call Parse.Object.fromJSON in order to make the client side label object a ParseObjectSubClass and allow operations on it such as adding relations.



You cannot use include on a relation query as you would with a Pointer, so there needs to be a query for relations all on it's own. An array of pointers was ruled out as there is going to be an unknown amount of labels applied.










share|improve this question





























    0















    I've got the following function which works as expected on Parse Server cloud code, however it's painfully slow.



    The nested for loops which are internally calling queries and save functions are undoubtedly the root cause.



    How can I refactor this code so that there is some async processing or even better what methods are there to remove / edit the relations on an object, the documentation around this is very poor.



    ClientLabels.applyClientLabels = async (req, res) => {

    const { clients, labels } = req.params;
    const user = req.user;
    const objectIds = clients.map((client) => client.objectId);
    const clientSaveList = ;
    const clientClass = Parse.Object.extend('Clients');
    const query = new Parse.Query(clientClass);
    query.containedIn("objectId", objectIds);
    const queryResult = await query.find({ sessionToken: user.getSessionToken() })

    try {
    for (const client of queryResult) {

    const labelRelation = client.relation('labels');
    const relatedLabels = await labelRelation.query().find({ sessionToken: user.getSessionToken() });
    labelRelation.remove(relatedLabels);

    for (const label of labels) {
    label.className = "ClientLabels";
    const labelRelationObj = Parse.Object.fromJSON(label)
    labelRelation.add(labelRelationObj);
    };
    clientSaveList.push(client);
    };

    const saved = await Parse.Object.saveAll(clientSaveList, { sessionToken: user.getSessionToken() })
    res.success(saved);

    } catch (e) {
    res.error(e);
    };
    }


    Explanation of some weirdness:
    I am having to call Parse.Object.fromJSON in order to make the client side label object a ParseObjectSubClass and allow operations on it such as adding relations.



    You cannot use include on a relation query as you would with a Pointer, so there needs to be a query for relations all on it's own. An array of pointers was ruled out as there is going to be an unknown amount of labels applied.










    share|improve this question



























      0












      0








      0








      I've got the following function which works as expected on Parse Server cloud code, however it's painfully slow.



      The nested for loops which are internally calling queries and save functions are undoubtedly the root cause.



      How can I refactor this code so that there is some async processing or even better what methods are there to remove / edit the relations on an object, the documentation around this is very poor.



      ClientLabels.applyClientLabels = async (req, res) => {

      const { clients, labels } = req.params;
      const user = req.user;
      const objectIds = clients.map((client) => client.objectId);
      const clientSaveList = ;
      const clientClass = Parse.Object.extend('Clients');
      const query = new Parse.Query(clientClass);
      query.containedIn("objectId", objectIds);
      const queryResult = await query.find({ sessionToken: user.getSessionToken() })

      try {
      for (const client of queryResult) {

      const labelRelation = client.relation('labels');
      const relatedLabels = await labelRelation.query().find({ sessionToken: user.getSessionToken() });
      labelRelation.remove(relatedLabels);

      for (const label of labels) {
      label.className = "ClientLabels";
      const labelRelationObj = Parse.Object.fromJSON(label)
      labelRelation.add(labelRelationObj);
      };
      clientSaveList.push(client);
      };

      const saved = await Parse.Object.saveAll(clientSaveList, { sessionToken: user.getSessionToken() })
      res.success(saved);

      } catch (e) {
      res.error(e);
      };
      }


      Explanation of some weirdness:
      I am having to call Parse.Object.fromJSON in order to make the client side label object a ParseObjectSubClass and allow operations on it such as adding relations.



      You cannot use include on a relation query as you would with a Pointer, so there needs to be a query for relations all on it's own. An array of pointers was ruled out as there is going to be an unknown amount of labels applied.










      share|improve this question
















      I've got the following function which works as expected on Parse Server cloud code, however it's painfully slow.



      The nested for loops which are internally calling queries and save functions are undoubtedly the root cause.



      How can I refactor this code so that there is some async processing or even better what methods are there to remove / edit the relations on an object, the documentation around this is very poor.



      ClientLabels.applyClientLabels = async (req, res) => {

      const { clients, labels } = req.params;
      const user = req.user;
      const objectIds = clients.map((client) => client.objectId);
      const clientSaveList = ;
      const clientClass = Parse.Object.extend('Clients');
      const query = new Parse.Query(clientClass);
      query.containedIn("objectId", objectIds);
      const queryResult = await query.find({ sessionToken: user.getSessionToken() })

      try {
      for (const client of queryResult) {

      const labelRelation = client.relation('labels');
      const relatedLabels = await labelRelation.query().find({ sessionToken: user.getSessionToken() });
      labelRelation.remove(relatedLabels);

      for (const label of labels) {
      label.className = "ClientLabels";
      const labelRelationObj = Parse.Object.fromJSON(label)
      labelRelation.add(labelRelationObj);
      };
      clientSaveList.push(client);
      };

      const saved = await Parse.Object.saveAll(clientSaveList, { sessionToken: user.getSessionToken() })
      res.success(saved);

      } catch (e) {
      res.error(e);
      };
      }


      Explanation of some weirdness:
      I am having to call Parse.Object.fromJSON in order to make the client side label object a ParseObjectSubClass and allow operations on it such as adding relations.



      You cannot use include on a relation query as you would with a Pointer, so there needs to be a query for relations all on it's own. An array of pointers was ruled out as there is going to be an unknown amount of labels applied.







      node.js parse-platform parse-server






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 2 at 21:07







      Taylorsuk

















      asked Jan 2 at 20:52









      TaylorsukTaylorsuk

      772832




      772832
























          1 Answer
          1






          active

          oldest

          votes


















          1














          There are a few things that can be done: (1) The creation of labels in the inner loop is invariant relative to the outer loop, so that can be done one time, at the start. (2) There's no need to query the relation if you're just going to remove the related objects. Use unset() and add to replace the relations. (3) This won't save much computation, but clientSaveList is superfluous, we can just save the query result...



          ClientLabels.applyClientLabels = async (req, res) => {
          const { clients, labels } = req.params;
          const objectIds = clients.map((client) => client.objectId);
          let labelObjects = labels.map(label => {
          label.className = "ClientLabels";
          return Parse.Object.fromJSON(label)
          });
          const query = new Parse.Query('Clients');
          query.containedIn("objectId", objectIds);
          const sessionToken = req.user.getSessionToken;
          const queryResult = await query.find({ sessionToken: sessionToken })

          try {
          for (const client of queryResult) {
          client.unset('labels');
          client.relation('labels').add(labelObjects);
          };
          const saved = await Parse.Object.saveAll(queryResult, { sessionToken: sessionToken })
          res.success(saved);
          } catch (e) {
          res.error(e);
          };
          }





          share|improve this answer
























          • Thank you, this does work better. However, I do have to perform a relation query in order to selectively remove the ones which aren't needed. This means I've had to change the whole tactic and use pointers so I can use the include() on the Client class. I really appreciate your answer, hopefully it might help someone else too.

            – Taylorsuk
            Jan 5 at 20:15












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54013032%2fparse-server-edit-relations-on-object-very-slow%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          There are a few things that can be done: (1) The creation of labels in the inner loop is invariant relative to the outer loop, so that can be done one time, at the start. (2) There's no need to query the relation if you're just going to remove the related objects. Use unset() and add to replace the relations. (3) This won't save much computation, but clientSaveList is superfluous, we can just save the query result...



          ClientLabels.applyClientLabels = async (req, res) => {
          const { clients, labels } = req.params;
          const objectIds = clients.map((client) => client.objectId);
          let labelObjects = labels.map(label => {
          label.className = "ClientLabels";
          return Parse.Object.fromJSON(label)
          });
          const query = new Parse.Query('Clients');
          query.containedIn("objectId", objectIds);
          const sessionToken = req.user.getSessionToken;
          const queryResult = await query.find({ sessionToken: sessionToken })

          try {
          for (const client of queryResult) {
          client.unset('labels');
          client.relation('labels').add(labelObjects);
          };
          const saved = await Parse.Object.saveAll(queryResult, { sessionToken: sessionToken })
          res.success(saved);
          } catch (e) {
          res.error(e);
          };
          }





          share|improve this answer
























          • Thank you, this does work better. However, I do have to perform a relation query in order to selectively remove the ones which aren't needed. This means I've had to change the whole tactic and use pointers so I can use the include() on the Client class. I really appreciate your answer, hopefully it might help someone else too.

            – Taylorsuk
            Jan 5 at 20:15
















          1














          There are a few things that can be done: (1) The creation of labels in the inner loop is invariant relative to the outer loop, so that can be done one time, at the start. (2) There's no need to query the relation if you're just going to remove the related objects. Use unset() and add to replace the relations. (3) This won't save much computation, but clientSaveList is superfluous, we can just save the query result...



          ClientLabels.applyClientLabels = async (req, res) => {
          const { clients, labels } = req.params;
          const objectIds = clients.map((client) => client.objectId);
          let labelObjects = labels.map(label => {
          label.className = "ClientLabels";
          return Parse.Object.fromJSON(label)
          });
          const query = new Parse.Query('Clients');
          query.containedIn("objectId", objectIds);
          const sessionToken = req.user.getSessionToken;
          const queryResult = await query.find({ sessionToken: sessionToken })

          try {
          for (const client of queryResult) {
          client.unset('labels');
          client.relation('labels').add(labelObjects);
          };
          const saved = await Parse.Object.saveAll(queryResult, { sessionToken: sessionToken })
          res.success(saved);
          } catch (e) {
          res.error(e);
          };
          }





          share|improve this answer
























          • Thank you, this does work better. However, I do have to perform a relation query in order to selectively remove the ones which aren't needed. This means I've had to change the whole tactic and use pointers so I can use the include() on the Client class. I really appreciate your answer, hopefully it might help someone else too.

            – Taylorsuk
            Jan 5 at 20:15














          1












          1








          1







          There are a few things that can be done: (1) The creation of labels in the inner loop is invariant relative to the outer loop, so that can be done one time, at the start. (2) There's no need to query the relation if you're just going to remove the related objects. Use unset() and add to replace the relations. (3) This won't save much computation, but clientSaveList is superfluous, we can just save the query result...



          ClientLabels.applyClientLabels = async (req, res) => {
          const { clients, labels } = req.params;
          const objectIds = clients.map((client) => client.objectId);
          let labelObjects = labels.map(label => {
          label.className = "ClientLabels";
          return Parse.Object.fromJSON(label)
          });
          const query = new Parse.Query('Clients');
          query.containedIn("objectId", objectIds);
          const sessionToken = req.user.getSessionToken;
          const queryResult = await query.find({ sessionToken: sessionToken })

          try {
          for (const client of queryResult) {
          client.unset('labels');
          client.relation('labels').add(labelObjects);
          };
          const saved = await Parse.Object.saveAll(queryResult, { sessionToken: sessionToken })
          res.success(saved);
          } catch (e) {
          res.error(e);
          };
          }





          share|improve this answer













          There are a few things that can be done: (1) The creation of labels in the inner loop is invariant relative to the outer loop, so that can be done one time, at the start. (2) There's no need to query the relation if you're just going to remove the related objects. Use unset() and add to replace the relations. (3) This won't save much computation, but clientSaveList is superfluous, we can just save the query result...



          ClientLabels.applyClientLabels = async (req, res) => {
          const { clients, labels } = req.params;
          const objectIds = clients.map((client) => client.objectId);
          let labelObjects = labels.map(label => {
          label.className = "ClientLabels";
          return Parse.Object.fromJSON(label)
          });
          const query = new Parse.Query('Clients');
          query.containedIn("objectId", objectIds);
          const sessionToken = req.user.getSessionToken;
          const queryResult = await query.find({ sessionToken: sessionToken })

          try {
          for (const client of queryResult) {
          client.unset('labels');
          client.relation('labels').add(labelObjects);
          };
          const saved = await Parse.Object.saveAll(queryResult, { sessionToken: sessionToken })
          res.success(saved);
          } catch (e) {
          res.error(e);
          };
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 4 at 6:17









          danhdanh

          49.1k977114




          49.1k977114













          • Thank you, this does work better. However, I do have to perform a relation query in order to selectively remove the ones which aren't needed. This means I've had to change the whole tactic and use pointers so I can use the include() on the Client class. I really appreciate your answer, hopefully it might help someone else too.

            – Taylorsuk
            Jan 5 at 20:15



















          • Thank you, this does work better. However, I do have to perform a relation query in order to selectively remove the ones which aren't needed. This means I've had to change the whole tactic and use pointers so I can use the include() on the Client class. I really appreciate your answer, hopefully it might help someone else too.

            – Taylorsuk
            Jan 5 at 20:15

















          Thank you, this does work better. However, I do have to perform a relation query in order to selectively remove the ones which aren't needed. This means I've had to change the whole tactic and use pointers so I can use the include() on the Client class. I really appreciate your answer, hopefully it might help someone else too.

          – Taylorsuk
          Jan 5 at 20:15





          Thank you, this does work better. However, I do have to perform a relation query in order to selectively remove the ones which aren't needed. This means I've had to change the whole tactic and use pointers so I can use the include() on the Client class. I really appreciate your answer, hopefully it might help someone else too.

          – Taylorsuk
          Jan 5 at 20:15




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54013032%2fparse-server-edit-relations-on-object-very-slow%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          How to fix TextFormField cause rebuild widget in Flutter

          Npm cannot find a required file even through it is in the searched directory