Skip to content

Commit bc41f56

Browse files
authored
refactor(db)!: reference is now instance of a Database reference (#78)
1 parent 2be43af commit bc41f56

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

samples/basic_db/functions/main.py

+4
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,22 @@
1212
@db_fn.on_value_written(reference="hello/world")
1313
def onwriteexample(event: db_fn.Event[db_fn.Change]) -> None:
1414
print("Hello from db write event:", event)
15+
print("Reference path:", event.reference.path)
1516

1617

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

2123

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

2629

2730
@db_fn.on_value_updated(reference="hello/world")
2831
def onupdatedexample(event: db_fn.Event[db_fn.Change]) -> None:
2932
print("Hello from db updated event:", event)
33+
print("Reference path:", event.reference.path)

src/firebase_functions/db_fn.py

+17-6
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323
import firebase_functions.private.path_pattern as _path_pattern
2424
import firebase_functions.core as _core
2525
import cloudevents.http as _ce
26+
import firebase_admin as _fa
27+
import firebase_admin.db as _db
2628

29+
from firebase_admin.db import Reference
2730
from firebase_functions.options import DatabaseOptions
2831
from firebase_functions.core import Change, T
2932

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

52-
reference: str
55+
reference: Reference
5356
"""
54-
The database reference path.
57+
The database reference.
5558
"""
5659

5760
location: str
@@ -96,16 +99,24 @@ def _db_endpoint_handler(
9699
before=before,
97100
after=after,
98101
)
102+
if _fa._DEFAULT_APP_NAME not in _fa._apps:
103+
_fa.initialize_app()
104+
app = _fa.get_app()
99105
event_instance = event_attributes["instance"]
100-
event_ref = event_attributes["ref"]
106+
event_database_host = event_attributes["firebasedatabasehost"]
107+
database_reference = _db.reference(
108+
path=event_attributes["ref"],
109+
app=app,
110+
url=f"https://{event_instance}.{event_database_host}",
111+
)
101112
params: dict[str, str] = {
102-
**ref_pattern.extract_matches(event_ref),
113+
**ref_pattern.extract_matches(event_attributes["ref"]),
103114
**instance_pattern.extract_matches(event_instance),
104115
}
105116
database_event = Event(
106-
firebase_database_host=event_attributes["firebasedatabasehost"],
117+
firebase_database_host=event_database_host,
107118
instance=event_instance,
108-
reference=event_ref,
119+
reference=database_reference,
109120
location=event_attributes["location"],
110121
specversion=event_attributes["specversion"],
111122
id=event_attributes["id"],

0 commit comments

Comments
 (0)