Skip to content

refactor(db)!: reference is now instance of a Database reference #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions samples/basic_db/functions/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,22 @@
@db_fn.on_value_written(reference="hello/world")
def onwriteexample(event: db_fn.Event[db_fn.Change]) -> None:
print("Hello from db write event:", event)
print("Reference path:", event.reference.path)


@db_fn.on_value_created(reference="hello/world")
def oncreatedexample(event: db_fn.Event) -> None:
print("Hello from db create event:", event)
print("Reference path:", event.reference.path)


@db_fn.on_value_deleted(reference="hello/world")
def ondeletedexample(event: db_fn.Event) -> None:
print("Hello from db delete event:", event)
print("Reference path:", event.reference.path)


@db_fn.on_value_updated(reference="hello/world")
def onupdatedexample(event: db_fn.Event[db_fn.Change]) -> None:
print("Hello from db updated event:", event)
print("Reference path:", event.reference.path)
23 changes: 17 additions & 6 deletions src/firebase_functions/db_fn.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
import firebase_functions.private.path_pattern as _path_pattern
import firebase_functions.core as _core
import cloudevents.http as _ce
import firebase_admin as _fa
import firebase_admin.db as _db

from firebase_admin.db import Reference
from firebase_functions.options import DatabaseOptions
from firebase_functions.core import Change, T

Expand All @@ -49,9 +52,9 @@ class Event(_core.CloudEvent[T]):
The instance ID portion of the fully qualified resource name.
"""

reference: str
reference: Reference
"""
The database reference path.
The database reference.
"""

location: str
Expand Down Expand Up @@ -96,16 +99,24 @@ def _db_endpoint_handler(
before=before,
after=after,
)
if _fa._DEFAULT_APP_NAME not in _fa._apps:
_fa.initialize_app()
app = _fa.get_app()
event_instance = event_attributes["instance"]
event_ref = event_attributes["ref"]
event_database_host = event_attributes["firebasedatabasehost"]
database_reference = _db.reference(
path=event_attributes["ref"],
app=app,
url=f"https://{event_instance}.{event_database_host}",
)
params: dict[str, str] = {
**ref_pattern.extract_matches(event_ref),
**ref_pattern.extract_matches(event_attributes["ref"]),
**instance_pattern.extract_matches(event_instance),
}
database_event = Event(
firebase_database_host=event_attributes["firebasedatabasehost"],
firebase_database_host=event_database_host,
instance=event_instance,
reference=event_ref,
reference=database_reference,
location=event_attributes["location"],
specversion=event_attributes["specversion"],
id=event_attributes["id"],
Expand Down