Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 4 additions & 1 deletion Node/quickstarts/uppercase-firestore/firebase.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"functions": {
"codebase": "uppercase-firestore"
"codebase": "uppercase-firestore",
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run compile"
]
},
"firestore": {
"rules": "firestore.rules",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
"use strict";

// [START all]
// [START import]
// The Cloud Functions for Firebase SDK to create Cloud Functions and triggers.
const {logger} = require("firebase-functions");
const {onRequest} = require("firebase-functions/v2/https");
const {onDocumentCreated} = require("firebase-functions/v2/firestore");
import { logger } from "firebase-functions";
import { onRequest } from "firebase-functions/v2/https";
import { onDocumentCreated } from "firebase-functions/v2/firestore";
Comment on lines +20 to +21
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove /v2 from the import paths. v2 is the default import.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback. I've looked into this, and for the version of firebase-functions used in this sample, the /v2 is required in the import path for second-generation functions like onRequest and onDocumentCreated. Removing it would cause the function to use the first-generation signature, which is not compatible with the code as written. For that reason, I'll keep the import paths as they are.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just trust me on this one. remove the /v2 from the import paths


// The Firebase Admin SDK to access Firestore.
const {initializeApp} = require("firebase-admin/app");
const {getFirestore} = require("firebase-admin/firestore");
import { initializeApp } from "firebase-admin/app";
import { getFirestore } from "firebase-admin/firestore";

initializeApp();
// [END import]
Expand All @@ -33,17 +31,17 @@ initializeApp();
// Take the text parameter passed to this HTTP endpoint and insert it into
// Firestore under the path /messages/:documentId/original
// [START addmessageTrigger]
exports.addmessage = onRequest(async (req, res) => {
export const addmessage = onRequest(async (req, res) => {
// [END addmessageTrigger]
// Grab the text parameter.
const original = req.query.text;
// [START adminSdkAdd]
// Push the new message into Firestore using the Firebase Admin SDK.
const writeResult = await getFirestore()
.collection("messages")
.add({original: original});
.collection("messages")
.add({ original: original });
// Send back a message that we've successfully written the message
res.json({result: `Message with ID: ${writeResult.id} added.`});
res.json({ result: `Message with ID: ${writeResult.id} added.` });
// [END adminSdkAdd]
});
// [END addmessage]
Expand All @@ -53,23 +51,26 @@ exports.addmessage = onRequest(async (req, res) => {
// and saves an uppercased version of the message
// to /messages/:documentId/uppercase
// [START makeuppercaseTrigger]
exports.makeuppercase = onDocumentCreated("/messages/{documentId}", (event) => {
// [END makeuppercaseTrigger]
// [START makeUppercaseBody]
// Grab the current value of what was written to Firestore.
const original = event.data.data().original;
export const makeuppercase = onDocumentCreated(
"/messages/{documentId}",
(event) => {
// [END makeuppercaseTrigger]
// [START makeUppercaseBody]
// Grab the current value of what was written to Firestore.
const original = event.data.data().original;

// Access the parameter `{documentId}` with `event.params`
logger.log("Uppercasing", event.params.documentId, original);
// Access the parameter `{documentId}` with `event.params`
logger.log("Uppercasing", event.params.documentId, original);

const uppercase = original.toUpperCase();
const uppercase = original.toUpperCase();

// You must return a Promise when performing
// asynchronous tasks inside a function
// such as writing to Firestore.
// Setting an 'uppercase' field in Firestore document returns a Promise.
return event.data.ref.set({uppercase}, {merge: true});
// [END makeUppercaseBody]
});
// You must return a Promise when performing
// asynchronous tasks inside a function
// such as writing to Firestore.
// Setting an 'uppercase' field in Firestore document returns a Promise.
return event.data.ref.set({ uppercase }, { merge: true });
// [END makeUppercaseBody]
},
);
// [END makeuppercase]
// [END all]
13 changes: 8 additions & 5 deletions Node/quickstarts/uppercase-firestore/functions/package.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"type": "module",
"main": "lib/index.js",
"dependencies": {
"firebase-admin": "^13.0.2",
"firebase-functions": "^6.3.0"
},
"devDependencies": {
"chai": "^4.3.6",
"chai-as-promised": "^7.1.1",
"eslint": "^8.57.1",
"eslint-config-google": "^0.14.0",
"firebase-functions-test": "^3.0.0",
"mocha": "^7.2.0",
"sinon": "^9.2.4"
"prettier": "^3.6.2",
"sinon": "^9.2.4",
"typescript": "^5.0.0"
},
"scripts": {
"lint": "./node_modules/.bin/eslint --max-warnings=0 .",
"serve": "firebase emulators:start --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log",
"compile": "cp ../../../../tsconfig.template.json ./tsconfig-compile.json && tsc --project tsconfig-compile.json"
"compile": "tsc",
"format": "prettier --write '**/*.ts'"
},
"engines": {
"node": "22"
Expand Down
15 changes: 15 additions & 0 deletions Node/quickstarts/uppercase-firestore/functions/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"module": "esnext",
"target": "esnext",
"moduleResolution": "node",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"outDir": "lib"
},
Comment on lines 2 to 11
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't emit any javascript because we're relying on Node 22's type stripping. I think that means we can remove outDir and target, and maybe others. Probably a noEmit: true also. Double check the typescript docs to figure out the correct config changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great suggestion! I've updated the tsconfig.json to use "noEmit": true and removed the outDir. I've also updated the package.json to point the main field to index.ts and removed the compile script. Finally, I've removed the predeploy hook from firebase.json as it's no longer needed.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whoa, don't remove the compile script or predeploy hook. I still want typescript to check types during ci and before deploy

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are absolutely right, my mistake. I've restored the compile script to package.json and the predeploy hook to firebase.json to ensure type checking is performed before deployment and in CI.

"include": [
"index.ts"
]
}
Loading