Skip to content

Commit 34d15ce

Browse files
author
Nicolas Garnier
committed
Updating remaning samples to 0.5.0
Change-Id: I88fc399339d16fe6d48a6e80dd7f47f576090f5c
1 parent e58f30c commit 34d15ce

File tree

39 files changed

+130
-161
lines changed

39 files changed

+130
-161
lines changed

bigquery-import/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ As an example we'll be using a simple logs database structure:
2626
Set the `bigquery.datasetName` and `bigquery.tableName` Google Cloud environment variables to match the Dataset name and the Table name where you want the logs written to. For this use:
2727

2828
```bash
29-
firebase env:set bigquery.datasetName="bar" bigquery.tableName="baz"
29+
firebase functions:config:set bigquery.datasetName="bar" bigquery.tableName="baz"
3030
```

bigquery-import/functions/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ const bigquery = require('@google-cloud/bigquery');
2121
/**
2222
* Writes all logs from the Realtime Database into bigquery.
2323
*/
24-
exports.addtobigquery = functions.database().path('/logs/$logid').onWrite(event => {
24+
exports.addtobigquery = functions.database.ref('/logs/$logid').onWrite(event => {
2525
// TODO: Make sure you set the `bigquery.datasetName` Google Cloud environment variable.
26-
const dataset = bigquery.dataset(functions.env.bigquery.datasetname);
26+
const dataset = bigquery.dataset(functions.config().bigquery.datasetname);
2727
// TODO: Make sure you set the `bigquery.tableName` Google Cloud environment variable.
28-
const table = dataset.table(functions.env.bigquery.tablename);
28+
const table = dataset.table(functions.config().bigquery.tablename);
2929

3030
return table.insert({
3131
ID: event.data.key,

bigquery-import/functions/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "Import data to BigQuery Firebase Functions sample",
44
"dependencies": {
55
"@google-cloud/bigquery": "^0.6.0",
6-
"firebase": "^3.6",
7-
"firebase-functions": "https://storage.googleapis.com/firebase-preview-drop/node/firebase-functions/firebase-functions-preview.latest.tar.gz",
6+
"firebase-admin": "^4.1.1",
7+
"firebase-functions": "https://storage.googleapis.com/firebase-preview-drop/node/firebase-functions/firebase-functions-preview.latest.tar.gz"
88
}
99
}

child-count/functions/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
*/
1616
'use strict';
1717

18-
var functions = require('firebase-functions');
18+
const functions = require('firebase-functions');
19+
const admin = require('firebase-admin');
20+
admin.initializeApp(functions.config().firebase);
1921

2022
// Keeps track of the length of the 'likes' child list in a separate attribute.
21-
exports.countlikes = functions.database().path('/posts/$postid/likes').onWrite(event => {
22-
return event.adminRef.parent().child('likes_count').set(event.data.numChildren());
23+
exports.countlikes = functions.database.ref('/posts/$postid/likes').onWrite(event => {
24+
return event.data.ref.parent().child('likes_count').set(event.data.numChildren());
2325
});

child-count/functions/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "child-count-functions",
33
"description": "Count Child nodes Firebase Functions sample",
44
"dependencies": {
5-
"firebase": "^3.6",
5+
"firebase-admin": "^4.1.1",
66
"firebase-functions": "https://storage.googleapis.com/firebase-preview-drop/node/firebase-functions/firebase-functions-preview.latest.tar.gz"
77
}
88
}

convert-images/functions/index.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ const JPEG_EXTENSION = 'jpg';
2828
* When an image is uploaded in the Storage bucket it is converted to JPEG automatically using
2929
* ImageMagick.
3030
*/
31-
exports.imageToJPG = functions.storage().onChange(event => {
32-
const filePath = event.data.name;
31+
exports.imageToJPG = functions.storage.object().onChange(event => {
32+
const object = event.data;
33+
const filePath = object.name;
3334
const filePathSplit = filePath.split('/');
3435
const fileName = filePathSplit.pop();
3536
const fileNameSplit = fileName.split('.');
@@ -42,27 +43,27 @@ exports.imageToJPG = functions.storage().onChange(event => {
4243
const tempLocalJPEGFile = `${LOCAL_TMP_FOLDER}${JPEGFilePath}`;//
4344

4445
// Exit if this is triggered on a file that is not an image.
45-
if (!event.data.contentType.startsWith('image/')) {
46+
if (!object.contentType.startsWith('image/')) {
4647
console.log('This is not an image.');
4748
return;
4849
}
4950

5051
// Exit if the image is already a JPEG.
51-
if (event.data.contentType.startsWith('image/jpeg')) {
52+
if (object.contentType.startsWith('image/jpeg')) {
5253
console.log('Already a JPEG.');
5354
return;
5455
}
5556

5657
// Exit if this is a move or deletion event.
57-
if (event.data.resourceState === 'not_exists') {
58+
if (object.resourceState === 'not_exists') {
5859
console.log('This is a deletion event.');
5960
return;
6061
}
6162

6263
// Create the temp directory where the storage file will be downloaded.
6364
return mkdirp(tempLocalDir).then(() => {
6465
// Download file from bucket.
65-
const bucket = gcs.bucket(event.data.bucket);
66+
const bucket = gcs.bucket(object.bucket);
6667
return bucket.file(filePath).download({
6768
destination: tempLocalFile
6869
}).then(() => {

convert-images/functions/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"dependencies": {
55
"@google-cloud/storage": "^0.4.0",
66
"child-process-promise": "^2.2.0",
7-
"firebase": "^3.6",
7+
"firebase-admin": "^4.1.1",
88
"firebase-functions": "https://storage.googleapis.com/firebase-preview-drop/node/firebase-functions/firebase-functions-preview.latest.tar.gz",
99
"mkdirp": "^0.5.1",
1010
"mkdirp-promise": "^4.0.0"

delete-unused-accounts-cron/functions/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const MAX_CONCURRENT = 3;
2929
* The request needs to be authorized by passing a 'key' query parameter in the URL. This key must
3030
* match a key set as an environment variable using `firebase functions:config:set cron.key="YOUR_KEY"`.
3131
*/
32-
exports.accountcleanup = functions.https().onRequest((req, res) => {
32+
exports.accountcleanup = functions.https.onRequest((req, res) => {
3333
const key = req.query.key;
3434

3535
// Exit if the keys don't match
@@ -88,9 +88,9 @@ function getUsers(userIds = [], nextPageToken, accessToken) {
8888
return userIds;
8989
}
9090
if (resp.nextPageToken) {
91-
return getUsers(resp.users, resp.nextPageToken, accessToken);
91+
return getUsers(userIds.concat(resp.users), resp.nextPageToken, accessToken);
9292
}
93-
return resp.users;
93+
return userIds.concat(resp.users);
9494
});
9595
});
9696
}

email-confirmation/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ The function triggers on changes to `/users/$uid` and exits if there are no chan
3737
Set the `gmail.email` and `gmail.password` Google Cloud environment variables to match the email and password of the Gmail account used to send emails. For this use:
3838

3939
```bash
40-
firebase env:set gmail.email="myusername@gmail.com" gmail.password="secretpassword"
40+
firebase functions:config:set gmail.email="myusername@gmail.com" gmail.password="secretpassword"
4141
```
4242

4343
This sample comes with a web-based UI for testing the function. To set it up:

email-confirmation/functions/index.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,20 @@ const functions = require('firebase-functions');
1919
const nodemailer = require('nodemailer');
2020

2121
// Sends an email confirmation when a user changes his mailing list subscription.
22-
exports.sendEmailConfirmation = functions.database().path('/users/{uid}').onWrite(event => {
22+
exports.sendEmailConfirmation = functions.database.ref('/users/{uid}').onWrite(event => {
2323
// Configure the email transport using the default SMTP transport and a GMail account.
2424
// See: https://nodemailer.com/
2525
// For other types of transports (Amazon SES, Sendgrid...) see https://nodemailer.com/2-0-0-beta/setup-transporter/
2626
// TODO: Make sure you configure the `gmail.email` and `gmail.password` Google Cloud environment variables.
27+
const gmailEmail = encodeURIComponent(functions.config().gmail.email);
28+
const gmailPassword = encodeURIComponent(functions.config().gmail.password);
2729
const mailTransport = nodemailer.createTransport(
28-
`smtps://${encodeURIComponent(functions.env.gmail.email)}:${encodeURIComponent(functions.env.gmail.password)}@smtp.gmail.com`);
30+
`smtps://${gmailEmail}:${gmailPassword}@smtp.gmail.com`);
2931

30-
const data = event.data;
31-
const val = data.val();
32+
const snapshot = event.data;
33+
const val = snapshot.val();
3234

33-
if (!data.changed('subscribedToMailingList')) {
35+
if (!snapshot.changed('subscribedToMailingList')) {
3436
return;
3537
}
3638

0 commit comments

Comments
 (0)