-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add node8 firebase snippets #719
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -123,3 +123,61 @@ exports.helloGCSGeneric = (data, context) => { | |
console.log(` Updated: ${file.updated}`); | ||
}; | ||
// [END functions_helloworld_storage_generic_node8] | ||
|
||
// [START functions_firebase_rtdb_node8] | ||
/** | ||
* Triggered by a change to a Firebase RTDB reference. | ||
* | ||
* @param {object} data The event payload. | ||
* @param {object} context The event metadata. | ||
*/ | ||
exports.helloRTDB = (data, context) => { | ||
const triggerResource = context.resource; | ||
|
||
console.log(`Function triggered by change to: ${triggerResource}`); | ||
console.log(`Admin?: ${!!data.admin}`); | ||
console.log(`Delta:`); | ||
console.log(JSON.stringify(data.delta, null, 2)); | ||
}; | ||
// [END functions_firebase_rtdb_node8] | ||
|
||
// [START functions_firebase_firestore_node8] | ||
/** | ||
* Triggered by a change to a Firestore document. | ||
* | ||
* @param {object} data The event payload. | ||
* @param {object} context The event metadata. | ||
*/ | ||
exports.helloFirestore = (data, context) => { | ||
const triggerResource = context.resource; | ||
|
||
console.log(`Function triggered by change to: ${triggerResource}`); | ||
|
||
console.log(`\nOld value:`); | ||
console.log(JSON.stringify(data.oldValue, null, 2)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the double space necessary? I personally would prefer shorter code without the extra params. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not strictly necessary, but makes it easier to read. (Without it, everything is compressed into one line.) |
||
|
||
console.log(`\nNew value:`); | ||
console.log(JSON.stringify(data.value, null, 2)); | ||
}; | ||
// [END functions_firebase_firestore_node8] | ||
|
||
// [START functions_firebase_auth_node8] | ||
/** | ||
* Triggered by creation or deletion of a Firebase Auth user object. | ||
* | ||
* @param {object} data The event payload. | ||
* @param {object} context The event metadata. | ||
*/ | ||
exports.helloAuth = (data, context) => { | ||
try { | ||
console.log(`Function triggered by creation or deletion of user: ${data.uid}`); | ||
console.log(`Created at: ${data.metadata.createdAt}`); | ||
|
||
if (data.email) { | ||
console.log(`Email: ${data.email}`); | ||
} | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
}; | ||
// [END functions_firebase_auth_node8] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/** | ||
* Copyright 2018, Google LLC. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const uuid = require('uuid'); | ||
const test = require('ava'); | ||
const utils = require('@google-cloud/nodejs-repo-tools'); | ||
|
||
const program = require('../'); | ||
|
||
test.beforeEach(utils.stubConsole); | ||
test.afterEach.always(utils.restoreConsole); | ||
|
||
test.serial('should monitor Firebase RTDB', t => { | ||
const dataId = uuid.v4(); | ||
const resourceId = uuid.v4(); | ||
|
||
const data = { | ||
admin: true, | ||
delta: { | ||
id: dataId | ||
} | ||
}; | ||
const context = { | ||
resource: resourceId | ||
}; | ||
|
||
program.helloRTDB(data, context); | ||
|
||
t.true(console.log.firstCall.args[0].includes(resourceId)); | ||
t.deepEqual(console.log.secondCall.args, ['Admin?: true']); | ||
t.true(console.log.getCall(3).args[0].includes(dataId)); | ||
}); | ||
|
||
test.serial('should monitor Firestore', t => { | ||
const resourceId = uuid.v4(); | ||
|
||
const context = { | ||
resource: resourceId | ||
}; | ||
const data = { | ||
oldValue: { uuid: uuid.v4() }, | ||
value: { uuid: uuid.v4() } | ||
}; | ||
|
||
program.helloFirestore(data, context); | ||
|
||
t.true(console.log.firstCall.args[0].includes(resourceId)); | ||
t.true(console.log.calledWith(JSON.stringify(data.oldValue, null, 2))); | ||
t.true(console.log.calledWith(JSON.stringify(data.value, null, 2))); | ||
}); | ||
|
||
test.serial('should monitor Auth', t => { | ||
const userId = uuid.v4(); | ||
const dateString = (new Date()).toISOString(); | ||
const emailString = `${uuid.v4()}@${uuid.v4()}.com`; | ||
|
||
const data = { | ||
uid: userId, | ||
metadata: { | ||
createdAt: dateString | ||
}, | ||
email: emailString | ||
}; | ||
|
||
program.helloAuth(data, null); | ||
|
||
t.true(console.log.firstCall.args[0].includes(userId)); | ||
t.true(console.log.secondCall.args[0].includes(dateString)); | ||
t.true(console.log.thirdCall.args[0].includes(emailString)); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@stew-r is this correct, or will
data.admin
always beundefined
?