23
23
import firebase_functions .private .path_pattern as _path_pattern
24
24
import firebase_functions .core as _core
25
25
import cloudevents .http as _ce
26
+ import firebase_admin as _fa
27
+ import firebase_admin .db as _db
26
28
29
+ from firebase_admin .db import Reference
27
30
from firebase_functions .options import DatabaseOptions
28
31
from firebase_functions .core import Change , T
29
32
@@ -49,9 +52,9 @@ class Event(_core.CloudEvent[T]):
49
52
The instance ID portion of the fully qualified resource name.
50
53
"""
51
54
52
- reference : str
55
+ reference : Reference
53
56
"""
54
- The database reference path .
57
+ The database reference.
55
58
"""
56
59
57
60
location : str
@@ -96,16 +99,24 @@ def _db_endpoint_handler(
96
99
before = before ,
97
100
after = after ,
98
101
)
102
+ if _fa ._DEFAULT_APP_NAME not in _fa ._apps :
103
+ _fa .initialize_app ()
104
+ app = _fa .get_app ()
99
105
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
+ )
101
112
params : dict [str , str ] = {
102
- ** ref_pattern .extract_matches (event_ref ),
113
+ ** ref_pattern .extract_matches (event_attributes [ "ref" ] ),
103
114
** instance_pattern .extract_matches (event_instance ),
104
115
}
105
116
database_event = Event (
106
- firebase_database_host = event_attributes [ "firebasedatabasehost" ] ,
117
+ firebase_database_host = event_database_host ,
107
118
instance = event_instance ,
108
- reference = event_ref ,
119
+ reference = database_reference ,
109
120
location = event_attributes ["location" ],
110
121
specversion = event_attributes ["specversion" ],
111
122
id = event_attributes ["id" ],
0 commit comments