Search code examples
javascriptgoogle-cloud-firestorejavascript-objects

How to compare two different Firestore queries?


I want to keep track of different programmatically created Firestore queries in a web app so I know when to search on the database and when to use a cache. The problem is I can't seem to get any two queries to compare to each other successfully.

let query1 = myCol
    .where("someProp", "==", false)
    .where("someOtherProp", "==", "something else")
    .orderBy("propX")
    ;
let query2 = myCol
    .where("someProp", "==", true)
    .where("someOtherProp", "==", "something")
    .orderBy("propY")
    ;

console.log("haveSameProperties(query1, query2) ", haveSameProperties(query1, query2)); //prints true
console.log("query1.toString()==query2.toString()", (query1.toString()==query2.toString())); //prints true
console.log("areDataObjectsEqual(query1, query2)", areDataObjectsEqual(query1, query2)); //type error

Where my util functions are:

export function haveSameProperties(obj1, obj2) {
    const obj1Length = Object.keys(obj1).length;
    const obj2Length = Object.keys(obj2).length;
    if(obj1Length === obj2Length) {
        return Object.keys(obj1).every(
            key => obj2.hasOwnProperty(key));
    }
    return false;
}

export function areDataObjectsEqual(obj1, obj2) {
    return JSON.stringify(obj1) === JSON.stringify(obj2);
}

How can I save and compare Firestore queries in Javascript?


Solution

  • Use isEqual() as stated in the documentation here.

    query1.isEqual(query2); //false