Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

lfernando-silva/firestore-layer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

53 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

firestore-layer

Nodejs firestore interface to make easily requests firestore.

Pre-Requirements

Installing

$ npm install --save firestore-layer

Requiring

  • Pass the Service Account Key JSON file into the lib constructor
    const FirestoreDatalayer = require('firestore-layer')
    const serviceAccountKeyFile = require('path-to/serviceAccountKeyFile')
    
    const datalayer = FirestoreDatalayer(serviceAccountKeyFile) 
    
    //queries can be built now   

Querying

datalayer.query(functionName, params, [options])
  • Returns a promise with the result or error.

Params:

  • functionName (STRING - REQUIRED) => The query name (see below)
  • params (Object - REQUIRED) => The query params for each function (see below)
  • options (Object - OPTIONAL) => Optional params to each query (see below)

functionName

The following functions are available:

  • findById
  • find
  • upsert
  • remove

params

Each query requires its owm params

lib.query('findById', {collection, id : 'document id at firestore'})
//Executes db.collection(collection).doc(id).get()

lib.query('find', {collection, criteria: [{field,matchCriteria,matchValue}]})
//Executes db.collection(collection).where(fieldName, matchCriteria, matchValue).get()

lib.query('upsert', {collection, ...doc, id })
//Executes db.collection(collection).doc() .set(doc) or .add()

lib.query('remove', {collection, ...doc, id })
//Executes db.collection(collection).doc(id).delete()

options

  • rawData (BOOLEAN) => Retrieve raw response from firebase on search queryies. Default is FALSE (returns just doc.data())
  • returning (BOOLEAN) => Return the created/updated doc. Default is FALSE.
  • merge (BOOLEAN) => Perform a pontual update, and DONT replace the original document. Equivalent to update with $set at mongoDb. Default is FALSE.
  • orderBy (ARRAY OF OBJECTS) => Retrieve the result ordered by criteria. Such criteria must be an object with format {field,order}. Default is the index criteria order.
  • limit (NUMBER) => Returns a set of documents with lenght as the limit passed. Default is total number of documents that match the criteria.
  • all (BOOLEAN) => Returns all documents at collection

Notes

Tests (not all covered)

$ npm test

Todos

  • Realtime updates
  • Follow the firestore upgrades

Contributions

  • Is welcome.
  • For now, its just make your PR with a Jest test.

License

MIT

About

Sample nodejs firestore layer as a data a layer

Resources

License

Stars

Watchers

Forks

Packages

No packages published