1.0.0 • Published 6 years ago

firebase-extractions v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
6 years ago

About

firebase-extractions is a small but useful Firebase library when dealing with Realtime Database and Firestore in a NodeJS environment and want the ability to quickly convert database results from the Realtime/Firestore classes to JavaScript arrays for generic use.

Installation

  1. npm install firebase-extractions
  2. Use any 3 of the Firebase extractions you would like by requiring them.

Extractions

documentSnapshotExtract(documentSnapshot, options)

Options

  • id (String) - Determines what the key of the document will be transformed into inside the document in the array. Defaults to id.

  • createTime (Boolean) - Determines if the Firestore-created createTime field is included in each of the documents in the array. Defaults to false.

  • updateTime (Boolean) - Determines if the Firestore-managed updateTime field is included in each of the documents in the array. Defaults to false.

Usage

const {documentSnapshotExtract} = require('firebase-extractions/firestore');

async function myFunction() {
  const documentSnapshot = await firebase.firestore().collection('starWarsCharacters').doc('8NTYBpwuIMqTOthjFQmo').get();
  const documentObject = documentSnapshotExtract(documentSnapshot);
  return documentObject;
}
// starWarsCharacters.8NTYBpwuIMqTOthjFQmo in Firestore contains:
{
  awesome: true,
  name: 'Han Solo'
}

// documentSnapshotExtract will return:
{
  awesome: true,
  name: 'Han Solo',
  id: '8NTYBpwuIMqTOthjFQmo'
}

querySnapshotToArray(querySnapshot, options)

Options

  • id (String) - Determines what the key of the document will be transformed into inside the document in the array. Defaults to id.

  • createTime (Boolean) - Determines if the Firestore-created createTime field is included in each of the documents in the array. Defaults to false.

  • updateTime (Boolean) - Determines if the Firestore-managed updateTime field is included in each of the documents in the array. Defaults to false.

Usage

const {querySnapshotToArray} = require('firebase-extractions/firestore');

async function myFunction() {
  const querySnapshot = await firebase.firestore().collection('starWarsCharacters').where('awesome', '==', true).get();
  const queryArray = querySnapshotToArray(querySnapshot);
  return queryArray;
}
// The starWarsCharacters collection in Firestore contains three documents:
  { awesome: true, name: 'Han Solo' }
  { name: 'Chewbacca', awesome: true }
  { awesome: true, name: 'Luke Skywalker' }

// querySnapshotToArray will return:
[
  { awesome: true, name: 'Han Solo', id: '8NTYBpwuIMqTOthjFQmo' },
  { name: 'Chewbacca', awesome: true, id: 'FE8WR6xpp1lOzFekIYUg' },
  { awesome: true, name: 'Luke Skywalker', id: 'WzOckx5xi1Ey4RyvidQN' }
]

dataSnapshotToArray(dataSnapshot, options)

Options

  • id (String) - Determines what the key of the document will be transformed into inside the document in the array. Defaults to id.

Usage

const {dataSnapshotToArray} = require('firebase-extractions/realtime');

async function myFunction() {
  const dataSnapshot = await firebase.database().ref('myRef').once('value');
  const dataArray = dataSnapshotToArray(dataSnapshot);
  return dataArray;
}
// myRef in Realtime Database contains:
{
  '0': { value: 0.5088880012102084 },
  '1': { value: 0.9535393149597968 },
  '2': { value: 0.4611710290005855 },
  '3': { value: 0.2894852393934524 },
  '4': { value: 0.8502272343304937 },
  '5': { value: 0.0933006449631725 },
  '6': { value: 0.9143575962510133 },
  '7': { value: 0.2773014226416699 },
  '8': { value: 0.3828858181135896 },
  '9': { value: 0.9859912263060917 },
  '10': { value: 0.913326366088267 }
}

// dataSnapshotToArray will return:
[
  { value: 0.5088880012102084, id: '0' },
  { value: 0.9535393149597968, id: '1' },
  { value: 0.4611710290005855, id: '2' },
  { value: 0.2894852393934524, id: '3' },
  { value: 0.8502272343304937, id: '4' },
  { value: 0.0933006449617925, id: '5' },
  { value: 0.9143575962510133, id: '6' },
  { value: 0.2773014226416699, id: '7' },
  { value: 0.3828858181135946, id: '8' },
  { value: 0.9859912263060917, id: '9' },
  { value: 0.9133263616088267, id: '10' }
]