DEV Community 👩‍💻👨‍💻

Cover image for Fixing Firebase Firestore Queries with firestore-db for web and react-native-firebase
Ranjan Singh
Ranjan Singh

Posted on

Fixing Firebase Firestore Queries with firestore-db for web and react-native-firebase

Motivation :

I have been working with firestore for almost 4 years now for Web and and Native apps with React Native and also been annoyed by few of the quarks of the firestore because I mainly used Mongodb or SQL.

  • Channing every single condition with another where function
    Like : dbref.where("name","==","John").where(....) and so on and then orderBy for each one as well as limit.

  • Everytime it returns you have to do doc.data() to get the actual data if It's an query then map each item and do .data() on them.

  • Doing anykind of search is Nightmare doesn't supports full text search but partial search is also annoying.

Solution:

Firestore-db : A utility package for firebase firestore and react-native-firebase to perform firestore queries easily with syntax inspired by mongoose like find,fineOne and also enable limited search options aswell.

Installation

npm install firestore-db
or
yarn add firestore-db

Examples

importing queryBuilder and creating an instance for web or node js

import firebase from 'firebase/app';
import 'firebase/auth';
import 'firebase/firestore';
import { queryBuilder } from 'firestore-db';

const Users = queryBuilder(firebase.firestore().collection("users"));

Enter fullscreen mode Exit fullscreen mode

for react native

import firestore from '@react-native-firebase/firestore'; 
import { queryBuilder } from 'firestore-db';
const Users = queryBuilder(firebase.firestore().collection("users"));

Enter fullscreen mode Exit fullscreen mode

Queries

find() : find can take an object containing key value pair where query can be string, array or object with operator and value. the param bjects can also take orderBy and limit and returns and array of results no need to call data() functions to get the results.
ex :

Users.find({
    firstName:"John",
    city :["New York","Los Angeles"],
    age : {">=",18},
    limit:10,
    orderBy:"createdAt",
    //or  orderBy: ['createdAt', 'asc']  // with acs/desc
    //or orderBy: [['userId', 'desc'], ['createdAt', 'asc']] // multiple orderby
    })

Enter fullscreen mode Exit fullscreen mode

here are all available functions and request params

Function name Description Params Return
findOne() returns First Matching result same as find() object Ex: {name:"",age:"",id:""}
findById() Finds Matching doc with id id object Ex: {name:"",age:"",id:""}
create() Creates New Doc object Ex: {name:"",age:"",} Resove/Reject
update() Updates Existing Doc id,updatedObject Ex: "uudefdsds",{name:"",age:""} updatedObject
deleteOne() Delete one Doc with id id Ex: "uudefdsds" Resove/Reject
deleteMany() Deletes all docs from list of ids Array Ex: [id1,id2,...] Resove/Reject
search() Simple Text Search with matching Prefix or Whole Text key and searchterm Ex: "name","Joh" updatedObject

Thanks for reading try it out once.

Top comments (0)

Join us at DEV

Find what you were looking for? Join hundreds of thousands of developers on DEV so you can:

 
🌚 Enable dark mode
🔠 Change your default font
📚 Adjust your experience level to see more relevant content