Skip to content

Commit f660c2b

Browse files
author
Ace Nassri
committed
Add node8 firebase snippets (#719)
* Add node8 firebase snippets * Address sam's comments * Add (unit) tests for Firebase snippets * Move deps to devDeps * Fix quotes
1 parent b5d0e1f commit f660c2b

File tree

3 files changed

+146
-4
lines changed

3 files changed

+146
-4
lines changed

functions/node8/index.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,61 @@ exports.helloGCSGeneric = (data, context) => {
123123
console.log(` Updated: ${file.updated}`);
124124
};
125125
// [END functions_helloworld_storage_generic_node8]
126+
127+
// [START functions_firebase_rtdb_node8]
128+
/**
129+
* Triggered by a change to a Firebase RTDB reference.
130+
*
131+
* @param {object} data The event payload.
132+
* @param {object} context The event metadata.
133+
*/
134+
exports.helloRTDB = (data, context) => {
135+
const triggerResource = context.resource;
136+
137+
console.log(`Function triggered by change to: ${triggerResource}`);
138+
console.log(`Admin?: ${!!data.admin}`);
139+
console.log(`Delta:`);
140+
console.log(JSON.stringify(data.delta, null, 2));
141+
};
142+
// [END functions_firebase_rtdb_node8]
143+
144+
// [START functions_firebase_firestore_node8]
145+
/**
146+
* Triggered by a change to a Firestore document.
147+
*
148+
* @param {object} data The event payload.
149+
* @param {object} context The event metadata.
150+
*/
151+
exports.helloFirestore = (data, context) => {
152+
const triggerResource = context.resource;
153+
154+
console.log(`Function triggered by change to: ${triggerResource}`);
155+
156+
console.log(`\nOld value:`);
157+
console.log(JSON.stringify(data.oldValue, null, 2));
158+
159+
console.log(`\nNew value:`);
160+
console.log(JSON.stringify(data.value, null, 2));
161+
};
162+
// [END functions_firebase_firestore_node8]
163+
164+
// [START functions_firebase_auth_node8]
165+
/**
166+
* Triggered by creation or deletion of a Firebase Auth user object.
167+
*
168+
* @param {object} data The event payload.
169+
* @param {object} context The event metadata.
170+
*/
171+
exports.helloAuth = (data, context) => {
172+
try {
173+
console.log(`Function triggered by creation or deletion of user: ${data.uid}`);
174+
console.log(`Created at: ${data.metadata.createdAt}`);
175+
176+
if (data.email) {
177+
console.log(`Email: ${data.email}`);
178+
}
179+
} catch (err) {
180+
console.error(err);
181+
}
182+
};
183+
// [END functions_firebase_auth_node8]

functions/node8/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
"bugs": {
1919
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples/issues"
2020
},
21-
"dependencies": {
22-
"ava": "^0.25.0"
23-
},
2421
"devDependencies": {
25-
"semistandard": "^12.0.1"
22+
"@google-cloud/nodejs-repo-tools": "^2.3.3",
23+
"ava": "^0.25.0",
24+
"semistandard": "^12.0.1",
25+
"uuid": "^3.3.2"
2626
}
2727
}

functions/node8/test/index.test.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* Copyright 2018, Google LLC.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
'use strict';
17+
18+
const uuid = require('uuid');
19+
const test = require('ava');
20+
const utils = require('@google-cloud/nodejs-repo-tools');
21+
22+
const program = require('../');
23+
24+
test.beforeEach(utils.stubConsole);
25+
test.afterEach.always(utils.restoreConsole);
26+
27+
test.serial('should monitor Firebase RTDB', t => {
28+
const dataId = uuid.v4();
29+
const resourceId = uuid.v4();
30+
31+
const data = {
32+
admin: true,
33+
delta: {
34+
id: dataId
35+
}
36+
};
37+
const context = {
38+
resource: resourceId
39+
};
40+
41+
program.helloRTDB(data, context);
42+
43+
t.true(console.log.firstCall.args[0].includes(resourceId));
44+
t.deepEqual(console.log.secondCall.args, ['Admin?: true']);
45+
t.true(console.log.getCall(3).args[0].includes(dataId));
46+
});
47+
48+
test.serial('should monitor Firestore', t => {
49+
const resourceId = uuid.v4();
50+
51+
const context = {
52+
resource: resourceId
53+
};
54+
const data = {
55+
oldValue: { uuid: uuid.v4() },
56+
value: { uuid: uuid.v4() }
57+
};
58+
59+
program.helloFirestore(data, context);
60+
61+
t.true(console.log.firstCall.args[0].includes(resourceId));
62+
t.true(console.log.calledWith(JSON.stringify(data.oldValue, null, 2)));
63+
t.true(console.log.calledWith(JSON.stringify(data.value, null, 2)));
64+
});
65+
66+
test.serial('should monitor Auth', t => {
67+
const userId = uuid.v4();
68+
const dateString = (new Date()).toISOString();
69+
const emailString = `${uuid.v4()}@${uuid.v4()}.com`;
70+
71+
const data = {
72+
uid: userId,
73+
metadata: {
74+
createdAt: dateString
75+
},
76+
email: emailString
77+
};
78+
79+
program.helloAuth(data, null);
80+
81+
t.true(console.log.firstCall.args[0].includes(userId));
82+
t.true(console.log.secondCall.args[0].includes(dateString));
83+
t.true(console.log.thirdCall.args[0].includes(emailString));
84+
});

0 commit comments

Comments
 (0)