|
| 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. |
0 commit comments