Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
August 19, 2022 08:29 pm GMT

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"));

for react native

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

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    })

here are all available functions and request params

Function nameDescriptionParamsReturn
findOne()returns First Matching resultsame as find()object Ex: {name:"",age:"",id:""}
findById()Finds Matching doc with ididobject Ex: {name:"",age:"",id:""}
create()Creates New Docobject Ex: {name:"",age:"",}Resove/Reject
update()Updates Existing Docid,updatedObject Ex: "uudefdsds",{name:"",age:""}updatedObject
deleteOne()Delete one Doc with idid Ex: "uudefdsds"Resove/Reject
deleteMany()Deletes all docs from list of idsArray Ex: [id1,id2,...]Resove/Reject
search()Simple Text Search with matching Prefix or Whole Textkey and searchterm Ex: "name","Joh"updatedObject

Thanks for reading try it out once.


Original Link: https://dev.to/ranjan/fixing-firebase-firestore-queries-with-firestore-db-for-web-and-react-native-firebase-1m5a

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To