Closed8
2nd gen firebase functions 移行メモ
いつの間にか 2nd gen firebase functions が GA になっていたので、1st gen との書き方の違い見ていく。
Upgrade 1st gen Node.js functions to 2nd gen
Make sure you're using at least Firebase CLI version 12.00 and firebase-functions version 4.3.0. Any newer version will support 2nd gen as well as 1st gen.
- Firebase CLI version: v12.00 以上
- firebase-functions version v4.3.0 以上
Update imports
Before
const functions = require("firebase-functions");
After
const {onRequest} = require("firebase-functions/v2/https");
const {onDocumentCreated} = require("firebase-functions/v2/firestore");
firebase-functions/v2/* から個別に import
Update trigger definitions
Before
const functions = require("firebase-functions");
exports.date = functions.https.onRequest((req, res) => {
// ...
});
exports.uppercase = functions.firestore
.document("my-collection/{docId}")
.onCreate((change, context) => {
// ...
});
After
const {onRequest} = require("firebase-functions/v2/https");
const {onDocumentCreated} = require("firebase-functions/v2/firestore");
exports.date = onRequest({cors: true}, (req, res) => {
// ...
});
exports.uppercase = onDocumentCreated("my-collection/{docId}", (event) => {
/* ... */
});
Set runtime options
Before
const functions = require("firebase-functions");
exports.date = functions
.runWith({
// Keep 5 instances warm for this latency-critical function
minInstances: 5,
})
// locate function closest to users
.region("asia-northeast1")
.https.onRequest((req, res) => {
// ...
});
exports.uppercase = functions
// locate function closest to users and database
.region("asia-northeast1")
.firestore.document("my-collection/{docId}")
.onCreate((change, context) => {
// ...
});
After
const {onRequest} = require("firebase-functions/v2/https");
const {onDocumentCreated} = require("firebase-functions/v2/firestore");
const {setGlobalOptions} = require("firebase-functions/v2");
// locate all functions closest to users
setGlobalOptions({ region: "asia-northeast1" });
exports.date = onRequest({
// Keep 5 instances warm for this latency-critical function
minInstances: 5,
}, (req, res) => {
// ...
});
exports.uppercase = onDocumentCreated("my-collection/{docId}", (event) => {
/* ... */
});
region の指定を一括でできるの嬉しい↓
setGlobalOptions({ region: "asia-northeast1" });
firestore trigger functions で、第一引数の document path 内の params に型ついてる!

firetore trigger functions で memory 等の options 渡す場合は、第一引数を object にしてあげればOK
onDocumentCreated({
document: 'users/{userId}/posts/{postId}',
memory: '2GiB'
// ...other options
}, (event) => {
// ...
})
ただ、これだと型補完がいい感じにつかない...
String 判定されちゃってる感じする...

型を見にいく。
function overload してるのか↓
firebase-functions/lib/v2/providers/firestore.d.ts
/**
* Event handler which triggers when a document is created in Firestore.
*
* @param document - The Firestore document path to trigger on.
* @param handler - Event handler which is run every time a Firestore create occurs.
*/
export declare function onDocumentCreated<Document extends string>(document: Document, handler: (event: FirestoreEvent<QueryDocumentSnapshot | undefined, ParamsOf<Document>>) => any | Promise<any>): CloudFunction<FirestoreEvent<QueryDocumentSnapshot | undefined, ParamsOf<Document>>>;
/**
* Event handler which triggers when a document is created in Firestore.
*
* @param opts - Options that can be set on an individual event-handling function.
* @param handler - Event handler which is run every time a Firestore create occurs.
*/
export declare function onDocumentCreated<Document extends string>(opts: DocumentOptions<Document>, handler: (event: FirestoreEvent<QueryDocumentSnapshot | undefined, ParamsOf<Document>>) => any | Promise<any>): CloudFunction<FirestoreEvent<QueryDocumentSnapshot | undefined, ParamsOf<Document>>>;
/**
* Event handler which triggers when a document is updated in Firestore.
*
* @param document - The Firestore document path to trigger on.
* @param handler - Event handler which is run every time a Firestore update occurs.
*/
opts: DocumentOptions<Document> の型を参照してほしいので、satisfies してあげるといい感じになる
import {
DocumentOptions,
onDocumentCreated
} from "firebase-functions/v2/firestore";
onDocumentCreated({
document: 'users/{userId}/posts/{postId}',
memory: '2GiB'
} satisfies DocumentOptions, (event) => {
// ...
})

このスクラップは2023/06/03にクローズされました