@tjbroodryk/firefetch
v1.0.2
Published
Non-observable wrapper to fetch data from firebase
Downloads
8
Maintainers
Readme
FireFetch
A Promise-based Library for fetching data from firebase. Especially useful for when you have firebase queries that depend on the result of other queries, and you dont want to use all of RxJS' might.
Install
npm install -s @tjbroodryk/firefetch
Usage
More documentation can be seen in the docs directory
Import the package
import firefetch from '@tjbroodryk/firefetch'
Initialisation
Can be done in two ways:
import * as firebase from 'firebase';
firebase.initializeApp(config.default)
firefetch.init(firebase)
which uses an exisitng firebase instance
or
firefetch.init({
apiKey: <apikey>,
authDomain: <authdomain>,
databaseURL: <databaseurl>,
projectId: <projectid>,
storageBucket: <storagebucket>,
messagingSenderId: <messagesenderid>
})
which creates a firebase instance (if none exisits) using the specified config
Base Path
You can specify a base path which gets prepended onto each query
firefetch.base('flamelink/environments/production/content/')
Querying
Basic, array query
firefetch.fetch('blogSlugs/en-US/', true).now().then(res => {
const resultArray = res.array()
//do something with result
})
Basic, single query
firefetch.fetch('blogSlugs/en-US/three-trends-transforming-local-marketing-online').now().then(res => {
const resultObject = res.single()
//do something with result
})
Query that filters by where methods
firefetch.fetch('blogSlugs/en-US/', true)
.where(item => item.key() == 'three-trends-transforming-local-marketing-online')
.where(item => item.val() == '1526560224416')
.now()
.then(res => {
const resArray = res.array()
})
Query that uses the first queries result for the second query, and then filters the second query using the where method
firefetch.fetch('blogSlugs/en-US/', true)
.next(item => `blog/en-US/${item.val()}`, true)
.where(elem => elem.val() != null && elem.val().postStatus == 'publish')
.now()
.then(res => {
const resArray = res.array()
})
Query utlising all availible methods
firefetch.fetch('blogSlugs/en-US/', true)
.next(item => `blog/en-US/${item.val()}`, true)
.where(elem => elem.val() != null && elem.val().postStatus == 'publish')
.each(item => {
console.log(`key : ${item.key()}, value : ${item.val()}`)
})
.map(item => {
const val = item.val()
val.postTitle = val.postTitle.toUpperCase()
return new ResultValue(item.key(), val)
})
.now()
.then(res => {
const resArray = res.array()
//do something
})
A couple things to note
fetch()
is allways the first method in the chain.- No query is executed until the
now()
method is called. - All other methods such as
map()
,where()
, etc return an instance of AnyQuery which allows you to chain them infinitely. - If the result of your query is expected to be an array, use
fetch(<path>, true)
ornext(<path>, true)
- the boolean parameter tells the library whether or not to convert the firebase "array" to an actual array. The default for the param is false. - Because the now() method returns a promise, you can use
async
.
Testing
Clone the repo and run:
npm run test
The current tests use an actual firebase rtdb, so the credentials are obviously not uploaded to github.
Testing - TODO
- Use mock firebase rtdb for unit tests
Authors
- Tj Broodryk
See also the list of contributors who participated in this project.
License
This project is licensed under the MIT License - see the LICENSE.md file for details