Skip to content

Commit e23e50f

Browse files
Merge pull request #14 from firebase/1.0.0
Update snippets for v1.0.0
2 parents cf5298f + fbd1bcb commit e23e50f

File tree

14 files changed

+852
-1245
lines changed

14 files changed

+852
-1245
lines changed

.eslintrc.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111
"sourceType": "module"
1212
},
1313
"rules": {
14-
"indent": [
15-
"error",
16-
4
17-
],
1814
"linebreak-style": [
1915
"error",
2016
"unix"
@@ -30,4 +26,4 @@
3026
"no-console": 0,
3127
"no-unused-vars": 0
3228
}
33-
}
29+
}

auth/get_service_account_tokens.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ const serviceAccount = require('path/to/serviceAccountKey.json');
2121
const credential = admin.credential.cert(serviceAccount);
2222

2323
credential.getAccessToken().then((accessTokenInfo) => {
24-
const accessToken = accessTokenInfo.access_token;
25-
const expirationTime = accessTokenInfo.expires_in;
24+
const accessToken = accessTokenInfo.access_token;
25+
const expirationTime = accessTokenInfo.expires_in;
2626

27-
// Attach accessToken to HTTPS request in the "Authorization: Bearer" header
28-
// After expirationTime, you must generate a new access token
29-
// [START_EXCLUDE]
30-
console.log(`The token ${accessToken} expires in ${expirationTime}`);
31-
// [END_EXCLUDE]
27+
// Attach accessToken to HTTPS request in the "Authorization: Bearer" header
28+
// After expirationTime, you must generate a new access token
29+
// [START_EXCLUDE]
30+
console.log(`The token ${accessToken} expires in ${expirationTime}`);
31+
// [END_EXCLUDE]
3232
});
3333
// [END get_service_account_tokens]
3434

Lines changed: 131 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -1,162 +1,162 @@
11
const functions = require('firebase-functions');
22

33
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]
1111
}
1212

1313
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]
2828
}
2929

3030
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]
4848
}
4949

5050
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]
6262
}
6363

6464
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]
7979
}
8080

8181
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]
9393
}
9494

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'];
100100

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]
104104
}
105105

106106
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]
132132
}
133133

134134
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]
146146
}
147147

148148
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]
162162
}

firestore/extend-with-functions/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"description": "Cloud Functions and Firestore",
44
"main": "index.js",
55
"dependencies": {
6-
"firebase-functions": "^0.8.1",
7-
"firebase-admin": "^5.4.0",
6+
"firebase-functions": "^1.0.0",
7+
"firebase-admin": "~5.11.0",
88
"@google-cloud/firestore": "^0.8.0"
99
}
1010
}

firestore/full-text-search/README.md

Lines changed: 0 additions & 11 deletions
This file was deleted.

firestore/full-text-search/functions/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)