From 9ad1b5cc221a2c7b22c9edd037ac9ada2a34943b Mon Sep 17 00:00:00 2001 From: Salakar Date: Wed, 3 May 2023 17:28:32 +0100 Subject: [PATCH] refactor(db)!: reference is now instance of a Database reference Formerly a string only. --- samples/basic_db/functions/main.py | 4 ++++ src/firebase_functions/db_fn.py | 23 +++++++++++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/samples/basic_db/functions/main.py b/samples/basic_db/functions/main.py index 7fd134e..6b825eb 100644 --- a/samples/basic_db/functions/main.py +++ b/samples/basic_db/functions/main.py @@ -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) diff --git a/src/firebase_functions/db_fn.py b/src/firebase_functions/db_fn.py index 410d5f8..81f91c0 100644 --- a/src/firebase_functions/db_fn.py +++ b/src/firebase_functions/db_fn.py @@ -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 @@ -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 @@ -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"],