svelte-firebase-state

Firebase utilities for Svelte 5.

npm install svelte-firebase-state
GitHub Github
Version 1.3.2
Svelte Playground ->

CollectionState manages a Firestore collection.
Fetches data, listens for real-time updates, and provides utilities for adding, updating, and deleting documents.

Quick example:

demo

Parameters:

required

firestore Firestore

The firebase firestore instance.

required

path string | ((currentUser: User | null) => string)

Listen for real-time updates.

optional

auth Auth

The firebase auth instance.

optional

listen boolean

default = false

Listen for real-time updates.

optional

query (currentUser: User | null) => QueryConstraint[];

The query constraints

optional

fromFirestore (snapshot) => DataApp

default = snap => ({ ...snap.data(), id: snap.id })

Function that converts the Firestore data to the app data

optional

toFirestore (data: DataApp) => DataDb

default = data => data

Function that converts the app data to the firestore data

optional

aggregate AggregateSpec

An object defining aggregate queries to be performed on the collection. More info: https://firebase.google.com/docs/firestore/query-data/aggregation-queries

Properties:

data AppData[] | null | undefined

Reactive state that holds the collection data. The data is undefined when loading. The data is null when the collection could not be found.

loading boolean

Reactive state indicating if the collection data is loading.

Methods:

add (data: AppData) => Promise<string | void>

Add a document to the collection. Returns the id of the new document.

delete (docId: string) => Promise<void>

Remove a document from the collection.

refetch () => Promise<void>

Refetch the collection data.

refetch_aggregate_data () => Promise<void>

Refetch the aggregate collection data.

get_query_ref () => Promise<Query<DataApp, DataDb> | undefined>

Get the collection query reference.

get_collection_ref () => Promise<CollectionReference<DataApp, DataDb> | undefined>

Get the collection reference.

get_doc_ref (id: string) => DocumentReference | void

Get one of the collection document reference.

More examples:

Listen to a collection

demo

Fetch collection aggregations data

demo