Matching a saved object with a query

Hi all,

I’m wondering if anyone knows there’s a local query method that can be used to compare a query to a saved object.

Example:

const obj = await new Parse.Query("Test").first();
const query = new Parse.Query("Test").equalTo("foo", "bar");
query.isInQuery(obj) // some method that tells me whether obj is in the query

At the moment my workaround is (not ideal as it creates a network request):

const obj = await new Parse.Query("Test").first();
const isInQuery = async (query, object) => {
  const isFound = await query.equalTo("objectId", object.id).first()
  return !!isFound
}
const query = await new Parse.Query("Test").equalTo("foo", "bar").first();
await isInQuery(query, obj)

That’s an interesting question. Your question “how to know whether an object matches a query” seems to be just a different phrasing for “how to know whether a query matches an object”. In other words you want to change the data source from the database to an in-memory list of objects.

Something like this:

const objects = [obj1, obj2, obj3];
const query = new Parse.Query("Test").equalTo("foo", "bar");
query.dataSource = objects; // prevents network request and uses local objects instead
const count = await query.count();
const isInQuery = count > 0;

I think that would require to replicate the query logic on the SDK level. This logic is currently only in the Parse Server codebase.

This sounds like a XY problem where the solution is to avoid the question in the first place by reconsidering the app logic.

Well the logic is as follows:

let objects = [];
let latestQuery = null;
const getObjects = async () => {
  latestQuery = new Parse.Query("Test").equalTo("condition1", true) // more conditions
  objects = await latestQuery.find();
}
filter.on("change", getObjects); // this is called every time a button is clicked
const subscription = await new Parse.Query("Test").equalTo("user", user).subscribe();
subscription.on("update", (obj) => {
  if (latestQuery.matchesObject(obj)) { // this method doesn't exist, wondering if there's something like this
    objects.push(obj);
  }
});

The alternative is to unsubscribe and re-subscribe with the additional query constraints, but there is a risk that an update event will not fire inbetween unsubscribe and subscribe, and there are other events that I would like to listen to as well.