11
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
-
15
-
16
14
"""
17
15
Module for Cloud Functions that are triggered by the Firebase Realtime Database.
18
16
"""
21
19
import functools as _functools
22
20
import typing as _typing
23
21
import datetime as _dt
24
- import firebase_functions .options as _options
25
22
import firebase_functions .private .util as _util
26
23
import firebase_functions .core as _core
27
24
import cloudevents .http as _ce
28
- from firebase_functions .core import Change # Exported for user ease of typing events
25
+
26
+ from firebase_functions .options import DatabaseOptions
29
27
30
28
_event_type_written = "google.firebase.database.ref.v1.written"
31
29
_event_type_created = "google.firebase.database.ref.v1.created"
32
30
_event_type_updated = "google.firebase.database.ref.v1.updated"
33
31
_event_type_deleted = "google.firebase.database.ref.v1.deleted"
34
32
35
33
34
+ @_dataclass .dataclass (frozen = True )
35
+ class Change (_typing .Generic [_core .T ]):
36
+ """
37
+ * The Functions interface for events that change state, such as
38
+ * Realtime Database `on_value_written`.
39
+ """
40
+
41
+ before : _core .T
42
+ """
43
+ The state of data before the change.
44
+ """
45
+
46
+ after : _core .T
47
+ """
48
+ The state of data after the change.
49
+ """
50
+
51
+
36
52
@_dataclass .dataclass (frozen = True )
37
53
class Event (_core .CloudEvent [_core .T ]):
38
54
"""
@@ -87,7 +103,7 @@ def _db_endpoint_handler(
87
103
# Merge delta into data to generate an 'after' view of the data.
88
104
if isinstance (before , dict ) and isinstance (after , dict ):
89
105
after = _util .prune_nones ({** before , ** after })
90
- database_event_data = _core . Change (
106
+ database_event_data = Change (
91
107
before = before ,
92
108
after = after ,
93
109
)
@@ -111,7 +127,7 @@ def _db_endpoint_handler(
111
127
func (database_event )
112
128
113
129
114
- @_util .copy_func_kwargs (_options . DatabaseOptions )
130
+ @_util .copy_func_kwargs (DatabaseOptions )
115
131
def on_value_written (** kwargs ) -> _typing .Callable [[_C1 ], _C1 ]:
116
132
"""
117
133
Event handler which triggers when data is created, updated, or deleted in Realtime Database.
@@ -124,8 +140,14 @@ def on_value_written(**kwargs) -> _typing.Callable[[_C1], _C1]:
124
140
def example(event: Event[Change[object]]) -> None:
125
141
pass
126
142
143
+ :param \\ *\\ *kwargs: Database options.
144
+ :type \\ *\\ *kwargs: as :exc:`firebase_functions.options.DatabaseOptions`
145
+ :rtype: :exc:`typing.Callable`
146
+ \\ [ \\ [ :exc:`firebase_functions.db.Event` \\ [
147
+ :exc:`firebase_functions.db.Change` \\ ] \\ ], `None` \\ ]
148
+ A function that takes a Database Event and returns None.
127
149
"""
128
- options = _options . DatabaseOptions (** kwargs )
150
+ options = DatabaseOptions (** kwargs )
129
151
130
152
def on_value_written_inner_decorator (func : _C1 ):
131
153
@@ -143,7 +165,7 @@ def on_value_written_wrapped(raw: _ce.CloudEvent):
143
165
return on_value_written_inner_decorator
144
166
145
167
146
- @_util .copy_func_kwargs (_options . DatabaseOptions )
168
+ @_util .copy_func_kwargs (DatabaseOptions )
147
169
def on_value_updated (** kwargs ) -> _typing .Callable [[_C1 ], _C1 ]:
148
170
"""
149
171
Event handler which triggers when data is updated in Realtime Database.
@@ -156,8 +178,14 @@ def on_value_updated(**kwargs) -> _typing.Callable[[_C1], _C1]:
156
178
def example(event: Event[Change[object]]) -> None:
157
179
pass
158
180
181
+ :param \\ *\\ *kwargs: Database options.
182
+ :type \\ *\\ *kwargs: as :exc:`firebase_functions.options.DatabaseOptions`
183
+ :rtype: :exc:`typing.Callable`
184
+ \\ [ \\ [ :exc:`firebase_functions.db.Event` \\ [
185
+ :exc:`firebase_functions.db.Change` \\ ] \\ ], `None` \\ ]
186
+ A function that takes a Database Event and returns None.
159
187
"""
160
- options = _options . DatabaseOptions (** kwargs )
188
+ options = DatabaseOptions (** kwargs )
161
189
162
190
def on_value_updated_inner_decorator (func : _C1 ):
163
191
@@ -175,7 +203,7 @@ def on_value_updated_wrapped(raw: _ce.CloudEvent):
175
203
return on_value_updated_inner_decorator
176
204
177
205
178
- @_util .copy_func_kwargs (_options . DatabaseOptions )
206
+ @_util .copy_func_kwargs (DatabaseOptions )
179
207
def on_value_created (** kwargs ) -> _typing .Callable [[_C2 ], _C2 ]:
180
208
"""
181
209
Event handler which triggers when data is created in Realtime Database.
@@ -185,10 +213,17 @@ def on_value_created(**kwargs) -> _typing.Callable[[_C2], _C2]:
185
213
.. code-block:: python
186
214
187
215
@on_value_created(reference="*")
188
- def example(event):
216
+ def example(event: Event[object] ):
189
217
pass
218
+
219
+ :param \\ *\\ *kwargs: Database options.
220
+ :type \\ *\\ *kwargs: as :exc:`firebase_functions.options.DatabaseOptions`
221
+ :rtype: :exc:`typing.Callable`
222
+ \\ [ \\ [ :exc:`firebase_functions.db.Event` \\ [
223
+ :exc:`object` \\ ] \\ ], `None` \\ ]
224
+ A function that takes a Database Event and returns None.
190
225
"""
191
- options = _options . DatabaseOptions (** kwargs )
226
+ options = DatabaseOptions (** kwargs )
192
227
193
228
def on_value_created_inner_decorator (func : _C2 ):
194
229
@@ -206,7 +241,7 @@ def on_value_created_wrapped(raw: _ce.CloudEvent):
206
241
return on_value_created_inner_decorator
207
242
208
243
209
- @_util .copy_func_kwargs (_options . DatabaseOptions )
244
+ @_util .copy_func_kwargs (DatabaseOptions )
210
245
def on_value_deleted (** kwargs ) -> _typing .Callable [[_C2 ], _C2 ]:
211
246
"""
212
247
Event handler which triggers when data is deleted in Realtime Database.
@@ -219,8 +254,14 @@ def on_value_deleted(**kwargs) -> _typing.Callable[[_C2], _C2]:
219
254
def example(event: Event[object]) -> None:
220
255
pass
221
256
257
+ :param \\ *\\ *kwargs: Database options.
258
+ :type \\ *\\ *kwargs: as :exc:`firebase_functions.options.DatabaseOptions`
259
+ :rtype: :exc:`typing.Callable`
260
+ \\ [ \\ [ :exc:`firebase_functions.db.Event` \\ [
261
+ :exc:`object` \\ ] \\ ], `None` \\ ]
262
+ A function that takes a Database Event and returns None.
222
263
"""
223
- options = _options . DatabaseOptions (** kwargs )
264
+ options = DatabaseOptions (** kwargs )
224
265
225
266
def on_value_deleted_inner_decorator (func : _C2 ):
226
267
0 commit comments