Skip to content

Commit b68c782

Browse files
authored
Add extend-woth-functions snippets (#12)
1 parent 577fcbb commit b68c782

File tree

3 files changed

+173
-0
lines changed

3 files changed

+173
-0
lines changed
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
const functions = require('firebase-functions');
2+
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]
11+
}
12+
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]
28+
}
29+
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]
48+
}
49+
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]
62+
}
63+
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]
79+
}
80+
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]
93+
}
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'];
100+
101+
// Fetch data using built in accessor
102+
const experience = event.data.get('experience');
103+
// [END reading_data_with_get]
104+
}
105+
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]
132+
}
133+
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]
146+
}
147+
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 == "malcolm";
156+
// event.params.messageCollectionId == "incoming_messages";
157+
// event.params.messageId == "134";
158+
// ... and ...
159+
// event.data.data() == {body: "Hello"}
160+
});
161+
// [END multi_wildcard]
162+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "extend-with-functions",
3+
"description": "Cloud Functions and Firestore",
4+
"main": "index.js",
5+
"dependencies": {
6+
"firebase-functions": "^0.8.1",
7+
"firebase-admin": "^5.4.0",
8+
"@google-cloud/firestore": "^0.8.0"
9+
}
10+
}

lerna.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"lerna": "2.8.0",
33
"packages": [
44
"firestore/main",
5+
"firestore/extend-with-functions",
56
"firestore/full-text-search",
67
"firestore/presence",
78
"auth"

0 commit comments

Comments
 (0)