Search code examples
node.jsparse-platformparse-server

Parse Server edit Relations on Object very slow


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.


Solution

  • 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);
      };
    }