I'd like a client side app to send a request to a back end that then does a get query to a cloud database. The tricky part I don't know how to do, is to build a chain of .where()
methods on the query based on input data.
A successfully build method chain may look like this:
db
.collection('cities')
.get()
.where('storeId', '==', '1029')
.where('state', '==', 'CA')
.where('population, '<=', '100000')
.where('district', '==', 'purple');
// example of how I'd pass data to a wrapper method to build the above.
instance.getSome('1029', "cities", {
state : ["==", "CA"]
, population : ["<=", "100000"]
, district : ["==", "purple"]
})
Is it possible to do this? I only have this so far, I'm not sure how to convert a string or similar into a method chain
async getSome(storeId, collection, whereClauses) {
}
Update
Based on an answer below this is a step by step work in progress:
public async getSome(collection: string, whereClauses: object): Promise<object> {
const baseQuery = db
.collection(collection)
.get()
.where("storeId", "==", this.shopDomain);
const whereClause2dArray = Object.entries(whereClauses);
const stitchedQuery = whereClause2dArray
.reduce((accumulatorQuery, [column, [operation, value]]) => {
return accumulatorQuery.where(column, operation, value);
}, baseQuery);
return await stitchedQuery();
}