firestore-rest-parser
v1.1.1
Published
Parse and use Firestore REST API JSON as a pure js object ✨
Downloads
28
Maintainers
Readme
firestore-rest-parser
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 | |