Skip to content

Commit d87763e

Browse files
committed
add firestore 2nd gen sample
1 parent d33970f commit d87763e

File tree

9 files changed

+291
-0
lines changed

9 files changed

+291
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
firebase-debug.log*
8+
9+
# Firebase cache
10+
.firebase/
11+
12+
# Firebase config
13+
14+
# Uncomment this if you'd like others to create their own Firebase project.
15+
# For a team working on the same Firebase project(s), it is recommended to leave
16+
# it commented so all members can deploy to the same project(s) in .firebaserc.
17+
# .firebaserc
18+
19+
# Runtime data
20+
pids
21+
*.pid
22+
*.seed
23+
*.pid.lock
24+
25+
# Directory for instrumented libs generated by jscoverage/JSCover
26+
lib-cov
27+
28+
# Coverage directory used by tools like istanbul
29+
coverage
30+
31+
# nyc test coverage
32+
.nyc_output
33+
34+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
35+
.grunt
36+
37+
# Bower dependency directory (https://bower.io/)
38+
bower_components
39+
40+
# node-waf configuration
41+
.lock-wscript
42+
43+
# Compiled binary addons (http://nodejs.org/api/addons.html)
44+
build/Release
45+
46+
# Dependency directories
47+
node_modules/
48+
49+
# Optional npm cache directory
50+
.npm
51+
52+
# Optional eslint cache
53+
.eslintcache
54+
55+
# Optional REPL history
56+
.node_repl_history
57+
58+
# Output of 'npm pack'
59+
*.tgz
60+
61+
# Yarn Integrity file
62+
.yarn-integrity
63+
64+
# dotenv environment variables file
65+
.env

2nd-gen/uppercase-firestore/README.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Firebase SDK for Cloud Functions Quickstart - Firestore
2+
3+
This quickstart demonstrates using the **Firebase SDK for Cloud Functions** with
4+
**Firestore**.
5+
6+
## Introduction
7+
8+
This sample app does two things:
9+
10+
- Creates messages in Firestore using a simple HTTPS request which is
11+
handled by an HTTP function. Writing to Firestore is done using the
12+
Firebase Admin SDK.
13+
- When a message gets added in Firestore, a function triggers and
14+
automatically makes these messages all uppercase.
15+
16+
## Set up the sample
17+
18+
Before you can test the functions locally or deploy to a Firebase project,
19+
you'll need to run `npm install` in the `functions` directory.
20+
21+
## Run locally with the Firebase Emulator suite
22+
23+
The
24+
[Firebase Local Emulator Suite](https://firebase.google.com/docs/emulator-suite)
25+
allows you to build and test apps on your local machine instead of deploying to
26+
a Firebase project.
27+
28+
1. Create a Firebase project in the
29+
[Firebase Console](https://console.firebase.google.com)
30+
> _Wondering why this step is needed?_ Even though the emulator will run this
31+
> sample on your local machine, it needs to interact with a Firebase project
32+
> to retrieve some configuration values.
33+
1. [Set up or update the Firebase CLI](https://firebase.google.com/docs/cli#setup_update_cli)
34+
1. Run `firebase emulators:start`
35+
1. Open the Emulator Suite UI
36+
1. Look in the output of the `firebase emulators:start` command for the URL
37+
of the Emulator Suite UI. It defaults to
38+
[localhost:4000](http://localhost:4000), but may be hosted on a different
39+
port on your machine.
40+
1. Enter that URL in your browser to open the UI.
41+
1. Trigger the functions
42+
1. Look in the output of the `firebase emulators:start` command for the URL
43+
of the http function "addmessage". It will look similar to:
44+
`http://localhost:5001/MY_PROJECT/us-central1/addmessage`
45+
1. `MY_PROJECT` will be replaced with your project ID
46+
1. The port may be different on your local machine
47+
1. Add the query string `?text=uppercaseme` to the end of the function's URL.
48+
It should now look something like:
49+
`http://localhost:5001/MY_PROJECT/us-central1/addmessage?text=uppercaseme`
50+
1. Optionally, you can change the message "uppercaseme" to a custom
51+
message
52+
1. Create a new message by opening the URL in a new tab in your browser
53+
1. View the effects of the functions in the Emulator Suite UI
54+
55+
1. In the "Logs" tab, you should see new logs indicating that the functions
56+
"addmessage" and "makeuppercase" ran:
57+
58+
> `functions: Beginning execution of "addmessage"`
59+
60+
> `functions: Beginning execution of "makeuppercase"`
61+
62+
1. In the "Firestore" tab, you should see a document containing your original
63+
message as well as the uppercased version of your message (if it was
64+
originally "uppercaseme", you'll see "UPPERCASEME")
65+
66+
## Deploy and test on a live Firebase project
67+
68+
To deploy and test the sample:
69+
70+
1. Create a Firebase project on the
71+
[Firebase Console](https://console.firebase.google.com)
72+
1. Deploy your project's code using `firebase deploy`
73+
1. Create a message by opening the function URL in your browser.
74+
75+
You should see your text value displayed in the console and uppercase.
76+
77+
## Contributing
78+
79+
We'd love that you contribute to the project. Before doing so please read our
80+
[Contributor guide](../../CONTRIBUTING.md).
81+
82+
## License
83+
84+
© Google, 2023. Licensed under an [Apache-2](../../LICENSE) license.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"firestore": {
3+
"rules": "firestore.rules",
4+
"indexes": "firestore.indexes.json"
5+
},
6+
"emulators": {
7+
"functions": {
8+
"port": 5001
9+
},
10+
"firestore": {
11+
"port": 8080
12+
},
13+
"ui": {
14+
"enabled": true
15+
}
16+
}
17+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"indexes": [],
3+
"fieldOverrides": []
4+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
service cloud.firestore {
2+
match /databases/{database}/documents {
3+
match /messages/{message} {
4+
// Allow authenticated users to read/write the messages collection
5+
allow read, write: if request.auth != null;
6+
}
7+
}
8+
}
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+
es2020: true,
5+
node: true,
6+
},
7+
extends: [
8+
"eslint:recommended",
9+
"google",
10+
],
11+
rules: {
12+
quotes: ["error", "double"],
13+
},
14+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* Copyright 2023 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 Cloud Functions and set up triggers.
21+
const {logger} = require('firebase-functions');
22+
const {onRequest} = require('firebase-functions/v2/https');
23+
const {onDocumentCreated} = require('firebase-functions/v2/firestore');
24+
25+
// The Firebase Admin SDK to access Firestore.
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
32+
// Firestore under the path /messages/:documentId/original
33+
// [START addmessageTrigger]
34+
exports.addmessage = onRequest(async (req, res) => {
35+
// [END addmessageTrigger]
36+
// Grab the text parameter.
37+
const original = req.query.text;
38+
// [START adminSdkAdd]
39+
// Push the new message into Firestore using the Firebase Admin SDK.
40+
const writeResult = await admin.firestore().collection('messages').add({original: original});
41+
// Send back a message that we've successfully written the message
42+
res.json({result: `Message with ID: ${writeResult.id} added.`});
43+
// [END adminSdkAdd]
44+
});
45+
// [END addmessage]
46+
47+
// [START makeuppercase]
48+
// Listens for new messages added to /messages/:documentId/original and creates an
49+
// uppercase version of the message to /messages/:documentId/uppercase
50+
// [START makeuppercaseTrigger]
51+
exports.makeuppercase = onDocumentCreated('/messages/{documentId}', (event) => {
52+
// [END makeuppercaseTrigger]
53+
// [START makeUppercaseBody]
54+
// Grab the current value of what was written to Firestore.
55+
const original = event.data.data().original;
56+
57+
// Access the parameter `{documentId}` with `context.params`
58+
logger.log('Uppercasing', context.params.documentId, original);
59+
60+
const uppercase = original.toUpperCase();
61+
62+
// You must return a Promise when performing asynchronous tasks inside a Functions such as
63+
// writing to Firestore.
64+
// Setting an 'uppercase' field in Firestore document returns a Promise.
65+
return event.data.ref.set({uppercase}, {merge: true});
66+
// [END makeUppercaseBody]
67+
});
68+
// [END makeuppercase]
69+
// [END all]
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "functions",
3+
"description": "Cloud Functions for Firebase",
4+
"dependencies": {
5+
"firebase-admin": "^11.5.0",
6+
"firebase-functions": "^4.3.0"
7+
},
8+
"devDependencies": {
9+
"chai": "^4.3.6",
10+
"chai-as-promised": "^7.1.1",
11+
"eslint": "^6.8.0",
12+
"eslint-plugin-promise": "^4.3.1",
13+
"mocha": "^7.2.0",
14+
"sinon": "^9.2.4"
15+
},
16+
"scripts": {
17+
"lint": "./node_modules/.bin/eslint --max-warnings=0 .",
18+
"serve": "firebase emulators:start --only functions",
19+
"shell": "firebase functions:shell",
20+
"start": "npm run shell",
21+
"deploy": "firebase deploy --only functions",
22+
"logs": "firebase functions:log",
23+
"compile": "cp ../../../tsconfig.template.json ./tsconfig-compile.json && tsc --project tsconfig-compile.json"
24+
},
25+
"engines": {
26+
"node": "16"
27+
},
28+
"private": true
29+
}

0 commit comments

Comments
 (0)