Skip to content

Commit 87e297f

Browse files
authored
Merge pull request #1000 from firebase/colerogers.rtdb-gen2-samples
RTDB v2 Triggers
2 parents 3515b7f + cc94963 commit 87e297f

File tree

6 files changed

+152
-0
lines changed

6 files changed

+152
-0
lines changed

2nd-gen/uppercase/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Firebase SDK for Cloud Functions 2nd Gen Quickstart - Realtime Database
2+
3+
This quickstart demonstrates using **Firebase SDK for Cloud Functions** with **Firebase Realtime Database**.
4+
5+
6+
## Introduction
7+
8+
This sample app does two things:
9+
- Create messages in the Firebase Realtime Database using a simple HTTPS request which is handled by a v2 HTTPS function. Writing to the Realtime Database is done using the Firebase Admin SDK.
10+
- When a message gets added in the Realtime Database, a v2 function triggers and automatically makes these messages all uppercase.
11+
12+
## Deploy and try out
13+
14+
To deploy and try out the sample:
15+
16+
- Create a Firebase project using the [Firebase console](https://console.firebase.google.com)
17+
- Install the required dependencies by running `npm install` in the `functions` directory
18+
- Deploy your project's code using `firebase deploy`
19+
- Create a message by opening the querying the functions URL from your browser.
20+
21+
The function executes and redirects the browser to the Firebase console at the database location where the text string was stored. You should see your text value displayed in the console and in uppercase.
22+
23+
## Contributing
24+
25+
We'd love you to contribute to the project. Before doing so please read our [Contributor guide](../../CONTRIBUTING.md).
26+
27+
28+
## License
29+
30+
© Google, 2022. Licensed under an [Apache-2](../../LICENSE) license.

2nd-gen/uppercase/database.rules.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"rules": {
3+
".read": true,
4+
".write": true
5+
}
6+
}

2nd-gen/uppercase/firebase.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"rulesFile": "database.rules.json",
3+
"emulators": {
4+
"functions": {
5+
"port": 5001
6+
},
7+
"database": {
8+
"port": 9000
9+
},
10+
"ui": {
11+
"enabled": true
12+
}
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports = {
2+
root: true,
3+
env: {
4+
es6: true,
5+
node: true,
6+
},
7+
extends: [
8+
"eslint:recommended",
9+
"google",
10+
],
11+
rules: {
12+
quotes: ["error", "double"],
13+
},
14+
};

2nd-gen/uppercase/functions/index.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Copyright 2022 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
'use strict';
17+
18+
// [START all]
19+
// [START import]
20+
// The Cloud Functions for Firebase SDK to create v2 Cloud Functions and setup triggers.
21+
const { onRequest } = require('firebase-functions/v2/https');
22+
const { onValueCreated } = require('firebase-functions/v2/database');
23+
const { logger } = require('firebase-functions');
24+
25+
// The Firebase Admin SDK to access the Firebase Realtime Database.
26+
const admin = require('firebase-admin');
27+
admin.initializeApp();
28+
// [END import]
29+
30+
// [START addmessage]
31+
// Take the text parameter passed to this HTTP endpoint and insert it into the
32+
// Realtime Database under the path /messages/:pushId/original
33+
// [START addMessageTrigger]
34+
exports.addmessage = onRequest(async (req, resp) => {
35+
// [END addMessageTrigger]
36+
// Grab the text parameter.
37+
const original = req.query.text;
38+
// [START adminSdkPush]
39+
// Push the new message into the Realtime Database using the Firebase Admin SDK.
40+
const snapshot = await admin.database().ref('/messages').push({original: original});
41+
// Redirect with 303 SEE OTHER to the URL of the pushed object in the Firebase console.
42+
res.redirect(303, snapshot.ref.toString());
43+
// [END adminSdkPush]
44+
});
45+
// [END addmessage]
46+
47+
// [START makeuppercase]
48+
// Listens for new messages added to /messages/:pushId/original and creates an
49+
// uppercase version of the message to /messages/:pushId/uppercase
50+
// for all databases in 'us-central1'
51+
exports.makeuppercase = onValueCreated('/messages/{pushId}/original', (event) => {
52+
// Grab the current value of what was written to the Realtime Database.
53+
const original = event.data.val();
54+
logger.log('Uppercasing', event.params.pushId, original);
55+
const uppercase = original.toUpperCase();
56+
// You must return a Promise when performing asynchronous tasks inside a function, such as
57+
// writing to the Firebase Realtime Database.
58+
// Setting an "uppercase" sibling in the Realtime Database returns a Promise.
59+
return event.data.ref.parent.child('uppercase').set(uppercase);
60+
});
61+
// [END makeuppercase]
62+
// [END all]
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "functions",
3+
"description": "Cloud Functions for Firebase",
4+
"scripts": {
5+
"lint": "eslint .",
6+
"serve": "firebase emulators:start --only functions",
7+
"shell": "firebase functions:shell",
8+
"start": "npm run shell",
9+
"deploy": "firebase deploy --only functions",
10+
"logs": "firebase functions:log"
11+
},
12+
"engines": {
13+
"node": "16"
14+
},
15+
"main": "index.js",
16+
"dependencies": {
17+
"firebase-admin": "^10.0.2",
18+
"firebase-functions": "^3.22.0"
19+
},
20+
"devDependencies": {
21+
"eslint": "^8.9.0",
22+
"eslint-config-google": "^0.14.0",
23+
"firebase-functions-test": "^0.2.0"
24+
},
25+
"private": true
26+
}

0 commit comments

Comments
 (0)