A tool to help you model your schema and provide a navigatable tree rather than magic strings everywhere.
npm install firebase-pathfirebase-path helps you get rid of magic strings when addresing nodes of your database.
You supply firebase-path with a JSON model of your document structure and then you can use a JSON path like API
to create the compound paths you need to interact with the firebase API.
crackling-fire-1555
|-- books
|-- -KNoPkHeKNH4M2vogoWG
| |-- author: "Wells, H.G."
| |-- title: "The Time Machine"
|
|-- -KNwSOCCgPxnJr_lfdPR
| |-- author: "Azimov, Isaac"
| |-- title: "Caves of Steel"
|
|-- -KNwrrfKCZQU2z303jsj
|-- author: "Verne, Jules"
|-- title: "Mysterious Island"
`
It is made up of both well defined nodes and some that were created with the Firebase push API.
To create a reference to the book by Jules Verne you would need this path `"books/-KNwrrfKCZQU2z303jsj"`
There are multiple ways to construct a path using constants, but all seemed lacking.
firebase-path lets you define a simple json schema
`
const schema = {
books: {
'': {
author: {},
title: {}
}
}
}
`
And convert that to an object tree
`
const children = firebasePath.fromJson(schema)
`
Using that tree, you can request a path
`
const authorPath = children
.books
.bookId('-KNwrrfKCZQU2z303jsj')
.author
.path()
// 'books/-KNwrrfKCZQU2z303jsj/author'
const ref = firebase.database().ref(authorPath)
``