title: Writing an abstraction layer for Google Firestore with Node.js, programmatically adding `where` conditions
body: <p>To create a reusable function and to allow easy replacement of database backends, I'd like to write a simple abstraction layer for Google Firestore.</p>
  
  <p>However, there's the problem that I can't programmatically daisy-chain <code>where</code> conditions. Is there a way to pass in an array of <code>where</code> conditions and pass them to Firestore when executing the query somehow? </p>
  
  <p>This is my current approach, but it doesn't work. The conditions are simply ignore and the first result is returned.</p>
  
  <pre><code>export const getOne = async (
    req: any,
    entity: string,
    select: string[] = [],
    where: any[] = [],
  ) =&gt; {
    // Get entity reference from firestore.
    const entityRef = req.firebaseServer.firestore().collection(entity);
    // Select fields.
    const query = entityRef.select(...select);
    // Add where conditions.
    where.forEach((condition) =&gt; {
      query.where(...condition);
    });
    // Limit to 1 result.
    query.limit(1);
    // Run query.
    return await query.get();
  };
  </code></pre>
  
  <p>Example call:</p>
  
  <pre><code>return await getOne(
    req,
    'item',
    ['title', 'language', 'uuid'],
    [
      ['categoryId', '==', req.params.categoryId],
      ['language', '==', req.params.lang],
    ],
  );
  </code></pre>