1.1.1 • Published 4 years ago

firebase-nodejs-helpers v1.1.1

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

Node.js helper classes for Firebase (firebase-nodejs-helpers)

Installation

npm install firebase-nodejs-helpers

Usage

// index.js
const { FirebaseAdmin } = require('firebase-nodejs-helpers')
const config = require(`path/to/config/${process.env.GCLOUD_PROJECT}.json`)

FirebaseAdmin.init({ config })
// store.js
const { Firestore } = require('firebase-nodejs-helpers')

class Store {
  constructor (path) {
    this.path = path
  }

  list (conditions, orderBy, limit) {
    return Firestore.list(this.path, { conditions, orderBy, limit })
      .then(({ docs }) => (
        docs.map((doc) => {
          const { id } = doc

          return Object.assign({}, doc.data(), { id })
        })
      ))
  }
}

Documentation

FirebaseAdmin

Initialize firebase-admin
const config = require('./path/to/service-account.json')

FirebaseAdmin.init({ config })

Firestore

Get a collection of documents
// Example
Firestore.list(
  'path/to/collection',
  {
    conditions: [
      {
        field: 'field-name',
        operator: '==',
        value: 'field-value'
      }
    ],
    orderBy: 'field-name|order',
    limit: 10
  }
)
Get the document from the collection
// Example
Firestore.get('path/to/document')
Create a new document in the collection
// Example
Firestore.create(
  'path/to/collection',
  { /* some data */ }
)
Create a new document or update the document
// Example
Firestore.set(
  'path/to/document',
  { /* some data */ },
  true // merging document data, false - set new document data
)
Update the document data
// Example
Firestore.update(
  'path/to/document',
  { /* some data */ }
)
Remove the document from the collection
// Example
Firestore.delete('path/to/document')
Create a new or update the documents collection
// Example
const options = {
    merge: false
};

Firestore.batchSet('path/to/document', 'id', payload, options)
1.1.1

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago