npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

firestore-rest-parser

v1.1.1

Published

Parse and use Firestore REST API JSON as a pure js object ✨

Downloads

28

Readme

firestore-rest-parser

npm version

Parse and use Firestore REST API JSON as a pure js object ✨

Or convert js object to Firestore REST structure.

Turn this:

{
  "data": {
    "mapValue": {
      "fields": {
        "username": {
          "stringValue": "user"
        },
        "isAdmin": {
          "booleanValue": true
        }
      }
    }
  }
}

Into this:

{
  "data": {
    "username": "user",
    "isAdmin": true
  }
}

Or vice versa.

Features

  • Parse Firestore REST structure into js object
  • Convert js object to Firestore REST compatible structure with type conversion
  • Create full Firestore REST response structure

Installing

Using npm:

npm install firestore-rest-parser

Using yarn:

yarn add firestore-rest-parser

Example

import { parse } from 'firestore-rest-parser'

const obj = {
  name: 'resouce/name',
  fields: {
    permissions: {
      arrayValue: {
        values: [{ stringValue: 'createUsers' }],
      },
    },
    contacts: {
      mapValue: {
        email: {
          prop: {
            stringValue: '[email protected]'
          },
        },
      },
    },
    unreadMessages: {
        integerValue: 5
    }
  },
  createTime: '',
  updateTime: '',
}

const data = parse(obj)

/*
  console.log(data) => {
    permissions: ['createUsers'],
    contacts: { email: '[email protected]' },
    unreadMessages: 5
  }
*/

Usage

Parse

To parse Firestore REST structure use parse function.

import { parse } from 'firestore-rest-parser'

const firestoreObject = {
    fields: {
        prop: { integerValue: 1 }
    }
}

const data = parse(firestoreObject)

/*
  console.log(data) => {
    prop: 1
  }
 */

Convert

To convert js object to Firestore REST structure use convert function.

Note

Timestamp, Reference, Bytes, GeoPoint data types must be instances of type helper classes

import { convert } from 'firestore-rest-parser'

const data = {
  username: 'user',
  permissions: ['createUsers']
}

const res = convert(data)

/*
  console.log(res) => {
    username: {
      stringValue: 'user'
    },
    permissions: {
      arrayValue: {
        values: [
          { stringValue: 'createUsers' }
        ]
      }
    }
  }
 */

Type helpers

To store Date or timestamp value use Timestamp helper. Provided time will be converted to ISO format.

import { convert, Timestamp } from 'firestore-rest-parser'

const data = {
  date: new Timestamp(new Date()),
  timestamp: new Timestamp(1641727129175)
}

convert(data)

To store Buffer value use Bytes helper. Provided buffer will be converted to base64 string.

import { convert, Bytes } from 'firestore-rest-parser'

const data = {
  buff: new Bytes(Buffer.from('value'))
}

convert(data)

To store Reference (path to element in db) value use Reference helper.

import { convert, Reference } from 'firestore-rest-parser'

const data = {
  prop: new Reference('path/to/doc')
}

convert(data)

To store GeoPoint value use GeoPoint helper.

import { convert, GeoPoint } from 'firestore-rest-parser'

const data = {
  prop: new GeoPoint(0, 0)
}

convert(data)

Firestore REST object

convert function creates only part (fields) of the Firestore Rest object. To create full structure (with name, createTime, updateTime) use createRESTObject function.

import { convert, createRESTObject } from 'firestore-rest-parser'

const data = {
  username: 'user',
  permissions: ['createUsers']
}

const res = createRESTObject(convert(data), 'users/userId')

/* console.log(res) => {
     fields: { /.../ }
     name: 'users/userId',
     createTime: '',
     updateTime: ''
*/ }

Firestore type conversion

| Javascript Type | Firestore Type | Type helper required | |-------------------------|----------------|----------------------| | Null | Null | | | Boolean | Boolean | | | Number (int) | Integer | | | Number (float) | Double | | | Date or UTC timestamp | Timestamp | + | | String | String | | | Buffer (base64 string) | Bytes | + | | Reference (string path) | Reference | + | | GeoPoint | GeoPoint | + | | Array | Array | | | Object | Map | |