-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.dart
More file actions
142 lines (129 loc) · 4.51 KB
/
main.dart
File metadata and controls
142 lines (129 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import 'package:firebase_functions/firebase_functions.dart';
// =============================================================================
// Parameterized Configuration Examples
// =============================================================================
// Define parameters - these are read from environment variables at runtime
// and can be configured at deploy time via .env files or CLI prompts.
final welcomeMessage = defineString(
'WELCOME_MESSAGE',
ParamOptions(
defaultValue: 'Hello from Dart Functions!',
label: 'Welcome Message',
description: 'The greeting message returned by the helloWorld function',
),
);
final minInstances = defineInt(
'MIN_INSTANCES',
ParamOptions(
defaultValue: 0,
label: 'Minimum Instances',
description: 'Minimum number of instances to keep warm',
),
);
final isProduction = defineBoolean(
'IS_PRODUCTION',
ParamOptions(
defaultValue: false,
description: 'Whether this is a production deployment',
),
);
void main(List<String> args) {
fireUp(args, (firebase) {
// HTTPS onRequest example - using parameterized configuration
firebase.https.onRequest(
name: 'helloWorld',
// ignore: non_const_argument_for_const_parameter
options: HttpsOptions(
// Use parameters in options - evaluated at deploy time
minInstances: DeployOption.param(minInstances),
),
(request) async {
// Access parameter value at runtime
return Response.ok(welcomeMessage.value());
},
);
// Conditional configuration based on boolean parameter
firebase.https.onRequest(
name: 'configuredEndpoint',
// ignore: non_const_argument_for_const_parameter
options: HttpsOptions(
// Use thenElse for conditional configuration at deploy time
// isProduction.thenElse(trueValue, falseValue) returns an expression
memory: Memory.expression(isProduction.thenElse(2048, 512)),
),
(request) async {
// Access parameter value at runtime
final env = isProduction.value() ? 'production' : 'development';
return Response.ok('Running in $env mode');
},
);
// Pub/Sub trigger example
firebase.pubsub.onMessagePublished(
topic: 'my-topic',
(event) async {
final message = event.data;
print('Received Pub/Sub message:');
print(' ID: ${message?.messageId}');
print(' Published: ${message?.publishTime}');
print(' Data: ${message?.textData}');
print(' Attributes: ${message?.attributes}');
},
);
// Firestore trigger examples
firebase.firestore.onDocumentCreated(
document: 'users/{userId}',
(event) async {
final data = event.data?.data();
print('Document created: users/${event.params['userId']}');
print(' Name: ${data?['name']}');
print(' Email: ${data?['email']}');
},
);
firebase.firestore.onDocumentUpdated(
document: 'users/{userId}',
(event) async {
final before = event.data?.before?.data();
final after = event.data?.after?.data();
print('Document updated: users/${event.params['userId']}');
print(' Before: $before');
print(' After: $after');
},
);
firebase.firestore.onDocumentDeleted(
document: 'users/{userId}',
(event) async {
final data = event.data?.data();
print('Document deleted: users/${event.params['userId']}');
print(' Final data: $data');
},
);
firebase.firestore.onDocumentWritten(
document: 'users/{userId}',
(event) async {
final before = event.data?.before?.data();
final after = event.data?.after?.data();
print('Document written: users/${event.params['userId']}');
if (before == null && after != null) {
print(' Operation: CREATE');
} else if (before != null && after != null) {
print(' Operation: UPDATE');
} else if (before != null && after == null) {
print(' Operation: DELETE');
}
},
);
// Nested collection trigger example
firebase.firestore.onDocumentCreated(
document: 'posts/{postId}/comments/{commentId}',
(event) async {
final data = event.data?.data();
print(
'Comment created: posts/${event.params['postId']}/comments/${event.params['commentId']}',
);
print(' Text: ${data?['text']}');
print(' Author: ${data?['author']}');
},
);
print('Functions registered successfully!');
});
}