https://github.com/jacobweisenburger/freerstore
Firestore cost optimizer
https://github.com/jacobweisenburger/freerstore
firebase firestore typescript zod
Last synced: over 1 year ago
JSON representation
Firestore cost optimizer
- Host: GitHub
- URL: https://github.com/jacobweisenburger/freerstore
- Owner: JacobWeisenburger
- License: mit
- Created: 2023-04-10T20:41:48.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2025-03-04T18:40:12.000Z (over 1 year ago)
- Last Synced: 2025-03-18T10:21:27.443Z (over 1 year ago)
- Topics: firebase, firestore, typescript, zod
- Language: TypeScript
- Homepage:
- Size: 489 KB
- Stars: 24
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
Freerstore
Firestore cost optimizer
## Table of contents
- [Purpose](#purpose)
- [Contribute](#contribute)
- [Installation](#installation)
- [From npm (Node/Bun)](#from-npm-nodebun)
- [Getting Started](#getting-started)
- [import](#import)
- [TODO](#todo)
## Purpose
To optimize firestore costs by:
- batching and debouncing writes
- caching documents in IndexedDB
To provide a better developer experience by:
- validating documents with `zod`
## Contribute
Always open to ideas. Positive or negative, all are welcome. Feel free to contribute an [issue](https://github.com/JacobWeisenburger/freerstore/issues) or [PR](https://github.com/JacobWeisenburger/freerstore/pulls).
## Installation
[npmjs.com/package/freerstore](https://www.npmjs.com/package/freerstore)
```sh
npm install freerstore firebase zod
```
## Getting Started
```ts
import { z } from 'zod'
import { initializeApp } from 'firebase/app'
import { freerstore } from 'freerstore'
// get your firebase config from:
// https://console.firebase.google.com
const firebaseConfig = {
apiKey: '',
authDomain: '',
projectId: '',
storageBucket: '',
messagingSenderId: '',
appId: ''
}
const firebaseApp = initializeApp( firebaseConfig )
// only supports top level collections currently
// feel free to open a PR to add support for subcollections
const collectionName = 'foobar'
const documentSchema = z.object( {
name: z.string(),
exp: z.number(),
} )
type DocumentData = z.infer
// type DocumentData = {
// name: string
// exp: number
// }
const collection = freerstore.getCollection( {
firebaseApp,
name: collectionName,
documentSchema,
} )
const unsubscribe = collection.onSnapshot( resultsMap => {
// resultsMap: Map
// Do something with the results
resultsMap.forEach( ( result, id ) => {
if ( result.success ) {
// result.data: DocumentData & {
// freerstore: {
// modifiedAt: string /* ISO date string */
// }
// }
console.log( id, result.data )
} else {
// result.error: ZodError
console.error( id, result.error )
}
} )
} )
setTimeout( () => {
/* setDocs handles batches for you */
collection.setDocs( {
someId1: { name: 'Frodo', exp: 13 },
someId2: { name: 'Aragorn', exp: 87 },
someId3: { name: 'Gandalf', exp: 9999 },
} )
}, 1000 )
// unsubscribe at some point to prevent memory leaks
setTimeout( () => {
unsubscribe()
}, 10_000 )
```
## TODO
Always open to ideas. Positive or negative, all are welcome. Feel free to contribute an [issue](https://github.com/JacobWeisenburger/freerstore/issues) or [PR](https://github.com/JacobWeisenburger/freerstore/pulls).
- Logo
- GitHub Actions
- Auto publish to npm