|
1 | 1 | const functions = require('firebase-functions');
|
2 | 2 |
|
3 | 3 | function triggerSpecificDocument() {
|
4 |
| - // [START trigger_specific_document] |
5 |
| - // Listen for any change on document `marie` in collection `users` |
6 |
| - exports.myFunctionName = functions.firestore |
7 |
| - .document('users/marie').onWrite((event) => { |
8 |
| - // ... Your code here |
9 |
| - }); |
10 |
| - // [END trigger_specific_document] |
| 4 | + // [START trigger_specific_document] |
| 5 | + // Listen for any change on document `marie` in collection `users` |
| 6 | + exports.myFunctionName = functions.firestore |
| 7 | + .document('users/marie').onWrite((change, context) => { |
| 8 | + // ... Your code here |
| 9 | + }); |
| 10 | + // [END trigger_specific_document] |
11 | 11 | }
|
12 | 12 |
|
13 | 13 | function triggerNewDocument() {
|
14 |
| - // [START trigger_new_document] |
15 |
| - exports.createUser = functions.firestore |
16 |
| - .document('users/{userId}') |
17 |
| - .onCreate(event => { |
18 |
| - // Get an object representing the document |
19 |
| - // e.g. {'name': 'Marie', 'age': 66} |
20 |
| - const newValue = event.data.data(); |
21 |
| - |
22 |
| - // access a particular field as you would any JS property |
23 |
| - const name = newValue.name; |
24 |
| - |
25 |
| - // perform desired operations ... |
26 |
| - }); |
27 |
| - // [END trigger_new_document] |
| 14 | + // [START trigger_new_document] |
| 15 | + exports.createUser = functions.firestore |
| 16 | + .document('users/{userId}') |
| 17 | + .onCreate((snap, context) => { |
| 18 | + // Get an object representing the document |
| 19 | + // e.g. {'name': 'Marie', 'age': 66} |
| 20 | + const newValue = snap.data(); |
| 21 | + |
| 22 | + // access a particular field as you would any JS property |
| 23 | + const name = newValue.name; |
| 24 | + |
| 25 | + // perform desired operations ... |
| 26 | + }); |
| 27 | + // [END trigger_new_document] |
28 | 28 | }
|
29 | 29 |
|
30 | 30 | function triggerDocumentUpdated() {
|
31 |
| - // [START trigger_document_updated] |
32 |
| - exports.updateUser = functions.firestore |
33 |
| - .document('users/{userId}') |
34 |
| - .onUpdate(event => { |
35 |
| - // Get an object representing the document |
36 |
| - // e.g. {'name': 'Marie', 'age': 66} |
37 |
| - const newValue = event.data.data(); |
38 |
| - |
39 |
| - // ...or the previous value before this update |
40 |
| - const previousValue = event.data.previous.data(); |
41 |
| - |
42 |
| - // access a particular field as you would any JS property |
43 |
| - const name = newValue.name; |
44 |
| - |
45 |
| - // perform desired operations ... |
46 |
| - }); |
47 |
| - // [END trigger_document_updated] |
| 31 | + // [START trigger_document_updated] |
| 32 | + exports.updateUser = functions.firestore |
| 33 | + .document('users/{userId}') |
| 34 | + .onUpdate((change, context) => { |
| 35 | + // Get an object representing the document |
| 36 | + // e.g. {'name': 'Marie', 'age': 66} |
| 37 | + const newValue = change.after.data(); |
| 38 | + |
| 39 | + // ...or the previous value before this update |
| 40 | + const previousValue = change.before.data(); |
| 41 | + |
| 42 | + // access a particular field as you would any JS property |
| 43 | + const name = newValue.name; |
| 44 | + |
| 45 | + // perform desired operations ... |
| 46 | + }); |
| 47 | + // [END trigger_document_updated] |
48 | 48 | }
|
49 | 49 |
|
50 | 50 | function triggerDocumentDeleted() {
|
51 |
| - // [START trigger_document_deleted] |
52 |
| - exports.deleteUser = functions.firestore |
53 |
| - .document('users/{userID}') |
54 |
| - .onDelete(event => { |
55 |
| - // Get an object representing the document prior to deletion |
56 |
| - // e.g. {'name': 'Marie', 'age': 66} |
57 |
| - const deletedValue = event.data.previous.data(); |
58 |
| - |
59 |
| - // perform desired operations ... |
60 |
| - }); |
61 |
| - // [END trigger_document_deleted] |
| 51 | + // [START trigger_document_deleted] |
| 52 | + exports.deleteUser = functions.firestore |
| 53 | + .document('users/{userID}') |
| 54 | + .onDelete((snap, context) => { |
| 55 | + // Get an object representing the document prior to deletion |
| 56 | + // e.g. {'name': 'Marie', 'age': 66} |
| 57 | + const deletedValue = snap.data(); |
| 58 | + |
| 59 | + // perform desired operations ... |
| 60 | + }); |
| 61 | + // [END trigger_document_deleted] |
62 | 62 | }
|
63 | 63 |
|
64 | 64 | function triggerDocumentAnyChange() {
|
65 |
| - // [START trigger_document_any_change] |
66 |
| - exports.modifyUser = functions.firestore |
67 |
| - .document('users/{userID}') |
68 |
| - .onWrite(event => { |
69 |
| - // Get an object with the current document value. |
70 |
| - // If the document does not exist, it has been deleted. |
71 |
| - const document = event.data.exists ? event.data.data() : null; |
72 |
| - |
73 |
| - // Get an object with the previous document value (for update or delete) |
74 |
| - const oldDocument = event.data.previous.data(); |
75 |
| - |
76 |
| - // perform desired operations ... |
77 |
| - }); |
78 |
| - // [END trigger_document_any_change] |
| 65 | + // [START trigger_document_any_change] |
| 66 | + exports.modifyUser = functions.firestore |
| 67 | + .document('users/{userID}') |
| 68 | + .onWrite((change, context) => { |
| 69 | + // Get an object with the current document value. |
| 70 | + // If the document does not exist, it has been deleted. |
| 71 | + const document = change.after.exists ? change.after.data() : null; |
| 72 | + |
| 73 | + // Get an object with the previous document value (for update or delete) |
| 74 | + const oldDocument = change.before.data(); |
| 75 | + |
| 76 | + // perform desired operations ... |
| 77 | + }); |
| 78 | + // [END trigger_document_any_change] |
79 | 79 | }
|
80 | 80 |
|
81 | 81 | function readingData() {
|
82 |
| - // [START reading_data] |
83 |
| - exports.updateUser = functions.firestore |
84 |
| - .document('users/{userId}') |
85 |
| - .onUpdate(event => { |
86 |
| - // Get an object representing the current document |
87 |
| - const newValue = event.data.data(); |
88 |
| - |
89 |
| - // ...or the previous value before this update |
90 |
| - const previousValue = event.data.previous.data(); |
91 |
| - }); |
92 |
| - // [END reading_data] |
| 82 | + // [START reading_data] |
| 83 | + exports.updateUser = functions.firestore |
| 84 | + .document('users/{userId}') |
| 85 | + .onUpdate((change, context) => { |
| 86 | + // Get an object representing the current document |
| 87 | + const newValue = change.after.data(); |
| 88 | + |
| 89 | + // ...or the previous value before this update |
| 90 | + const previousValue = change.before.data(); |
| 91 | + }); |
| 92 | + // [END reading_data] |
93 | 93 | }
|
94 | 94 |
|
95 |
| -function readingDataWithGet(event) { |
96 |
| - // [START reading_data_with_get] |
97 |
| - // Fetch data using standard accessors |
98 |
| - const age = event.data.data().age; |
99 |
| - const name = event.data.data()['name']; |
| 95 | +function readingDataWithGet(snap) { |
| 96 | + // [START reading_data_with_get] |
| 97 | + // Fetch data using standard accessors |
| 98 | + const age = snap.data().age; |
| 99 | + const name = snap.data()['name']; |
100 | 100 |
|
101 |
| - // Fetch data using built in accessor |
102 |
| - const experience = event.data.get('experience'); |
103 |
| - // [END reading_data_with_get] |
| 101 | + // Fetch data using built in accessor |
| 102 | + const experience = snap.get('experience'); |
| 103 | + // [END reading_data_with_get] |
104 | 104 | }
|
105 | 105 |
|
106 | 106 | function writingData() {
|
107 |
| - // [START writing_data] |
108 |
| - // Listen for updates to any `user` document. |
109 |
| - exports.countNameChanges = functions.firestore |
110 |
| - .document('users/{userId}') |
111 |
| - .onUpdate((event) => { |
112 |
| - // Retrieve the current and previous value |
113 |
| - const data = event.data.data(); |
114 |
| - const previousData = event.data.previous.data(); |
115 |
| - |
116 |
| - // We'll only update if the name has changed. |
117 |
| - // This is crucial to prevent infinite loops. |
118 |
| - if (data.name == previousData.name) return; |
119 |
| - |
120 |
| - // Retrieve the current count of name changes |
121 |
| - let count = data.name_change_count; |
122 |
| - if (!count) { |
123 |
| - count = 0; |
124 |
| - } |
125 |
| - |
126 |
| - // Then return a promise of a set operation to update the count |
127 |
| - return event.data.ref.set({ |
128 |
| - name_change_count: count + 1 |
129 |
| - }, {merge: true}); |
130 |
| - }); |
131 |
| - // [END writing_data] |
| 107 | + // [START writing_data] |
| 108 | + // Listen for updates to any `user` document. |
| 109 | + exports.countNameChanges = functions.firestore |
| 110 | + .document('users/{userId}') |
| 111 | + .onUpdate((change, context) => { |
| 112 | + // Retrieve the current and previous value |
| 113 | + const data = change.after.data(); |
| 114 | + const previousData = change.before.data(); |
| 115 | + |
| 116 | + // We'll only update if the name has changed. |
| 117 | + // This is crucial to prevent infinite loops. |
| 118 | + if (data.name == previousData.name) return; |
| 119 | + |
| 120 | + // Retrieve the current count of name changes |
| 121 | + let count = data.name_change_count; |
| 122 | + if (!count) { |
| 123 | + count = 0; |
| 124 | + } |
| 125 | + |
| 126 | + // Then return a promise of a set operation to update the count |
| 127 | + return change.after.ref.set({ |
| 128 | + name_change_count: count + 1 |
| 129 | + }, {merge: true}); |
| 130 | + }); |
| 131 | + // [END writing_data] |
132 | 132 | }
|
133 | 133 |
|
134 | 134 | function basicWildcard() {
|
135 |
| - // [START basic_wildcard] |
136 |
| - // Listen for changes in all documents and all sub-collections |
137 |
| - exports.useWildcard = functions.firestore |
138 |
| - .document('users/{userId}') |
139 |
| - .onWrite((event) => { |
140 |
| - // If we set `/users/marie` to {name: "marie"} then |
141 |
| - // event.params.userId == "marie" |
142 |
| - // ... and ... |
143 |
| - // event.data.data() == {name: "Marie"} |
144 |
| - }); |
145 |
| - // [END basic_wildcard] |
| 135 | + // [START basic_wildcard] |
| 136 | + // Listen for changes in all documents and all sub-collections |
| 137 | + exports.useWildcard = functions.firestore |
| 138 | + .document('users/{userId}') |
| 139 | + .onWrite((change, context) => { |
| 140 | + // If we set `/users/marie` to {name: "marie"} then |
| 141 | + // context.params.userId == "marie" |
| 142 | + // ... and ... |
| 143 | + // change.after.data() == {name: "Marie"} |
| 144 | + }); |
| 145 | + // [END basic_wildcard] |
146 | 146 | }
|
147 | 147 |
|
148 | 148 | function multiWildcard() {
|
149 |
| - // [START multi_wildcard] |
150 |
| - // Listen for changes in all documents and all subcollections |
151 |
| - exports.useMultipleWildcards = functions.firestore |
152 |
| - .document('users/{userId}/{messageCollectionId}/{messageId}') |
153 |
| - .onWrite((event) => { |
154 |
| - // If we set `/users/marie/incoming_messages/134` to {body: "Hello"} then |
155 |
| - // event.params.userId == "marie"; |
156 |
| - // event.params.messageCollectionId == "incoming_messages"; |
157 |
| - // event.params.messageId == "134"; |
158 |
| - // ... and ... |
159 |
| - // event.data.data() == {body: "Hello"} |
160 |
| - }); |
161 |
| - // [END multi_wildcard] |
| 149 | + // [START multi_wildcard] |
| 150 | + // Listen for changes in all documents and all subcollections |
| 151 | + exports.useMultipleWildcards = functions.firestore |
| 152 | + .document('users/{userId}/{messageCollectionId}/{messageId}') |
| 153 | + .onWrite((change, context) => { |
| 154 | + // If we set `/users/marie/incoming_messages/134` to {body: "Hello"} then |
| 155 | + // context.params.userId == "marie"; |
| 156 | + // context.params.messageCollectionId == "incoming_messages"; |
| 157 | + // context.params.messageId == "134"; |
| 158 | + // ... and ... |
| 159 | + // change.after.data() == {body: "Hello"} |
| 160 | + }); |
| 161 | + // [END multi_wildcard] |
162 | 162 | }
|
0 commit comments