Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 1 addition & 5 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
"sourceType": "module"
},
"rules": {
"indent": [
"error",
4
],
"linebreak-style": [
"error",
"unix"
Expand All @@ -30,4 +26,4 @@
"no-console": 0,
"no-unused-vars": 0
}
}
}
14 changes: 7 additions & 7 deletions auth/get_service_account_tokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ const serviceAccount = require('path/to/serviceAccountKey.json');
const credential = admin.credential.cert(serviceAccount);

credential.getAccessToken().then((accessTokenInfo) => {
const accessToken = accessTokenInfo.access_token;
const expirationTime = accessTokenInfo.expires_in;
const accessToken = accessTokenInfo.access_token;
const expirationTime = accessTokenInfo.expires_in;

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

262 changes: 131 additions & 131 deletions firestore/extend-with-functions/functions/index.js
Original file line number Diff line number Diff line change
@@ -1,162 +1,162 @@
const functions = require('firebase-functions');

function triggerSpecificDocument() {
// [START trigger_specific_document]
// Listen for any change on document `marie` in collection `users`
exports.myFunctionName = functions.firestore
.document('users/marie').onWrite((event) => {
// ... Your code here
});
// [END trigger_specific_document]
// [START trigger_specific_document]
// Listen for any change on document `marie` in collection `users`
exports.myFunctionName = functions.firestore
.document('users/marie').onWrite((change, context) => {
// ... Your code here
});
// [END trigger_specific_document]
}

function triggerNewDocument() {
// [START trigger_new_document]
exports.createUser = functions.firestore
.document('users/{userId}')
.onCreate(event => {
// Get an object representing the document
// e.g. {'name': 'Marie', 'age': 66}
const newValue = event.data.data();

// access a particular field as you would any JS property
const name = newValue.name;

// perform desired operations ...
});
// [END trigger_new_document]
// [START trigger_new_document]
exports.createUser = functions.firestore
.document('users/{userId}')
.onCreate((snap, context) => {
// Get an object representing the document
// e.g. {'name': 'Marie', 'age': 66}
const newValue = snap.data();

// access a particular field as you would any JS property
const name = newValue.name;

// perform desired operations ...
});
// [END trigger_new_document]
}

function triggerDocumentUpdated() {
// [START trigger_document_updated]
exports.updateUser = functions.firestore
.document('users/{userId}')
.onUpdate(event => {
// Get an object representing the document
// e.g. {'name': 'Marie', 'age': 66}
const newValue = event.data.data();

// ...or the previous value before this update
const previousValue = event.data.previous.data();

// access a particular field as you would any JS property
const name = newValue.name;

// perform desired operations ...
});
// [END trigger_document_updated]
// [START trigger_document_updated]
exports.updateUser = functions.firestore
.document('users/{userId}')
.onUpdate((change, context) => {
// Get an object representing the document
// e.g. {'name': 'Marie', 'age': 66}
const newValue = change.after.data();

// ...or the previous value before this update
const previousValue = change.before.data();

// access a particular field as you would any JS property
const name = newValue.name;

// perform desired operations ...
});
// [END trigger_document_updated]
}

function triggerDocumentDeleted() {
// [START trigger_document_deleted]
exports.deleteUser = functions.firestore
.document('users/{userID}')
.onDelete(event => {
// Get an object representing the document prior to deletion
// e.g. {'name': 'Marie', 'age': 66}
const deletedValue = event.data.previous.data();

// perform desired operations ...
});
// [END trigger_document_deleted]
// [START trigger_document_deleted]
exports.deleteUser = functions.firestore
.document('users/{userID}')
.onDelete((snap, context) => {
// Get an object representing the document prior to deletion
// e.g. {'name': 'Marie', 'age': 66}
const deletedValue = snap.data();

// perform desired operations ...
});
// [END trigger_document_deleted]
}

function triggerDocumentAnyChange() {
// [START trigger_document_any_change]
exports.modifyUser = functions.firestore
.document('users/{userID}')
.onWrite(event => {
// Get an object with the current document value.
// If the document does not exist, it has been deleted.
const document = event.data.exists ? event.data.data() : null;

// Get an object with the previous document value (for update or delete)
const oldDocument = event.data.previous.data();

// perform desired operations ...
});
// [END trigger_document_any_change]
// [START trigger_document_any_change]
exports.modifyUser = functions.firestore
.document('users/{userID}')
.onWrite((change, context) => {
// Get an object with the current document value.
// If the document does not exist, it has been deleted.
const document = change.after.exists ? change.after.data() : null;

// Get an object with the previous document value (for update or delete)
const oldDocument = change.before.data();

// perform desired operations ...
});
// [END trigger_document_any_change]
}

function readingData() {
// [START reading_data]
exports.updateUser = functions.firestore
.document('users/{userId}')
.onUpdate(event => {
// Get an object representing the current document
const newValue = event.data.data();

// ...or the previous value before this update
const previousValue = event.data.previous.data();
});
// [END reading_data]
// [START reading_data]
exports.updateUser = functions.firestore
.document('users/{userId}')
.onUpdate((change, context) => {
// Get an object representing the current document
const newValue = change.after.data();

// ...or the previous value before this update
const previousValue = change.before.data();
});
// [END reading_data]
}

function readingDataWithGet(event) {
// [START reading_data_with_get]
// Fetch data using standard accessors
const age = event.data.data().age;
const name = event.data.data()['name'];
function readingDataWithGet(snap) {
// [START reading_data_with_get]
// Fetch data using standard accessors
const age = snap.data().age;
const name = snap.data()['name'];

// Fetch data using built in accessor
const experience = event.data.get('experience');
// [END reading_data_with_get]
// Fetch data using built in accessor
const experience = snap.get('experience');
// [END reading_data_with_get]
}

function writingData() {
// [START writing_data]
// Listen for updates to any `user` document.
exports.countNameChanges = functions.firestore
.document('users/{userId}')
.onUpdate((event) => {
// Retrieve the current and previous value
const data = event.data.data();
const previousData = event.data.previous.data();

// We'll only update if the name has changed.
// This is crucial to prevent infinite loops.
if (data.name == previousData.name) return;

// Retrieve the current count of name changes
let count = data.name_change_count;
if (!count) {
count = 0;
}

// Then return a promise of a set operation to update the count
return event.data.ref.set({
name_change_count: count + 1
}, {merge: true});
});
// [END writing_data]
// [START writing_data]
// Listen for updates to any `user` document.
exports.countNameChanges = functions.firestore
.document('users/{userId}')
.onUpdate((change, context) => {
// Retrieve the current and previous value
const data = change.after.data();
const previousData = change.before.data();

// We'll only update if the name has changed.
// This is crucial to prevent infinite loops.
if (data.name == previousData.name) return;

// Retrieve the current count of name changes
let count = data.name_change_count;
if (!count) {
count = 0;
}

// Then return a promise of a set operation to update the count
return change.after.ref.set({
name_change_count: count + 1
}, {merge: true});
});
// [END writing_data]
}

function basicWildcard() {
// [START basic_wildcard]
// Listen for changes in all documents and all sub-collections
exports.useWildcard = functions.firestore
.document('users/{userId}')
.onWrite((event) => {
// If we set `/users/marie` to {name: "marie"} then
// event.params.userId == "marie"
// ... and ...
// event.data.data() == {name: "Marie"}
});
// [END basic_wildcard]
// [START basic_wildcard]
// Listen for changes in all documents and all sub-collections
exports.useWildcard = functions.firestore
.document('users/{userId}')
.onWrite((change, context) => {
// If we set `/users/marie` to {name: "marie"} then
// context.params.userId == "marie"
// ... and ...
// change.after.data() == {name: "Marie"}
});
// [END basic_wildcard]
}

function multiWildcard() {
// [START multi_wildcard]
// Listen for changes in all documents and all subcollections
exports.useMultipleWildcards = functions.firestore
.document('users/{userId}/{messageCollectionId}/{messageId}')
.onWrite((event) => {
// If we set `/users/marie/incoming_messages/134` to {body: "Hello"} then
// event.params.userId == "marie";
// event.params.messageCollectionId == "incoming_messages";
// event.params.messageId == "134";
// ... and ...
// event.data.data() == {body: "Hello"}
});
// [END multi_wildcard]
// [START multi_wildcard]
// Listen for changes in all documents and all subcollections
exports.useMultipleWildcards = functions.firestore
.document('users/{userId}/{messageCollectionId}/{messageId}')
.onWrite((change, context) => {
// If we set `/users/marie/incoming_messages/134` to {body: "Hello"} then
// context.params.userId == "marie";
// context.params.messageCollectionId == "incoming_messages";
// context.params.messageId == "134";
// ... and ...
// change.after.data() == {body: "Hello"}
});
// [END multi_wildcard]
}
4 changes: 2 additions & 2 deletions firestore/extend-with-functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"description": "Cloud Functions and Firestore",
"main": "index.js",
"dependencies": {
"firebase-functions": "^0.8.1",
"firebase-admin": "^5.4.0",
"firebase-functions": "^1.0.0",
"firebase-admin": "~5.11.0",
"@google-cloud/firestore": "^0.8.0"
}
}
11 changes: 0 additions & 11 deletions firestore/full-text-search/README.md

This file was deleted.

1 change: 0 additions & 1 deletion firestore/full-text-search/functions/.gitignore

This file was deleted.

Loading