firestore-rest-adapter

Serializes and deserializes firebase typed JSON

Usage no npm install needed!

<script type="module">
  import firestoreRestAdapter from 'https://cdn.skypack.dev/firestore-rest-adapter';
</script>

README

Functionality

The Firestore v1 REST API responds to requests for document fields with a typed data format outlined here. This data is structured like the following:

{
  fields: {
    myObj: {
      mapValue: {
        fields: {
          myString: {
            stringValue: 'my string'
          }
        }
      }
    },
    myArray: {
      arrayValue: {
        values: [
          {
            myInt: {
              integerValue: '5'
            }
          }
        ]
      }
    }
  }
}

This package converts the data into a more workable object format:

{
  myObj: {
    myString: 'my string'
  },
  myArray: [5]
}

You can mutate the resulting object however you desire. When you need to write the object back to Firestore, call serializeDocument on the object and include it as the body of your request.

Installation

yarn add firestore-rest-adapter
# OR
npm i --save firestore-rest-adapter

Usage

import { normalizeDocument, serializeDocument } from 'firestore-rest-adapter';

const getDoc = async () => {
  const res = await fetch(
    // `?fields=fields` query parameter to fetch document fields
    'https://firestore.googleapis.com/v1/path/to/document?fields=fields',
    {
      method: 'GET',
      headers: {
        authorization: // access token
      },
    }
  );
  const docData = await res.json();

  // Normalize Firestore Value into workable object
  return normalizeDocument(docData);
};

const writeDoc = async (obj) => {
  // Serialize obj into Firestore Value
  const doc = serializeDocument(obj);

  return fetch(
    'https://firestore.googleapis.com/v1/path/to/document?fields=fields',
    {
      method: 'PATCH',
      headers: {
        authorization: // access token
      },
      body: JSON.stringify(doc),
    }
  );
};

API

Method name Description
normalizeDocument(doc: Object) Converts an entire Firestore document's fields into a normalized object, removing the top-level fields key.
normalizeField(field: Object) Converts a Firestore Value into the corresponding primitive. (e.g {stringValue: 'foo'} => 'foo').

This method works recursively for iterable Values.
serializeDocument(obj: Object) Serializes an Object and its fields into a Firestore Value, inserting a top level fields key. (e.g {foo: 'bar'} => {fields: {foo: {stringValue: 'bar'}}})
serializeField(field: Object) Serializes an Object into a Firestore Value. (e.g 'foo' => {stringValue: 'foo'}).

This method works recursively for iterable Values.