Nodejs firestore interface to make easily requests firestore.
- NodeJS
- Setup Firebase admin
- Service Account Key JSON file
$ npm install --save firestore-layer
- 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
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)
The following functions are available:
- findById
- find
- upsert
- remove
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()
- 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
- Firestore is in Beta version. This library may be unstable.
- This module is transpiled using Babel to NodeJS 6.11 (to work at firebase cloud functions)
- To perform find queries with sort, is necessary to create an index for EACH document field at firestore. In this case, we CAN NOT change the document structure (make an schema).
$ npm test
- Realtime updates
- Follow the firestore upgrades
- Is welcome.
- For now, its just make your PR with a Jest test.
MIT