Skip to content
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

Merged
merged 5 commits into from
Aug 27, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions functions/node8/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Copy link
Contributor Author

@ace-n ace-n Aug 23, 2018

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 be undefined?

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));
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 a change to a Firebase Auth user object.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as before: it's only creation or deletion, not a change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

*
* @param {object} data The event payload.
* @param {object} context The event metadata.
*/
exports.helloAuth = (data, context) => {
try {
console.log(`Function triggered by change to 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]