Open3
Firebaseを使って快適に開発する為のTips

Firebaseをバックエンドとして使う際に、導入しているnpmとscripts
{
"dependencies": {
"@google-cloud/bigquery": "^5.6.0", // Firestoreのみだとどうしても取得しづらい集計データなど、BigQueryから取得しています。
"@google-cloud/pubsub": "^2.19.4", // 非同期で動作してほしい処理の為
"firebase-admin": "^10.0.2",
"firebase-dynamic-links": "^1.1.0", // URLを発行したい
"firebase-functions": "^3.16.0",
"google-auth-library": "^6.1.6", // Cloud Run上に作成された機能を呼び出す為
"lodash": "^4.17.21", // 主にchunkを使用しています。Firestoreのbatchは制限があるので必須
"node-fetch": "^2.6.7" // HTTPリクエスト
},
"devDependencies": {
"@types/node-fetch": "^2.5.8",
"@typescript-eslint/eslint-plugin": "^3.9.1",
"@typescript-eslint/parser": "^4.15.1",
"dotenv-cli": "^4.0.0", // .envファイルで変数を切り替えています
"eslint": "^7.6.0",
"eslint-config-google": "^0.14.0",
"eslint-plugin-import": "^2.22.1",
"firebase-functions-test": "^0.3.3",
"fireway": "^1.0.2", // Firestore用につくられたmigrationツール
"ts-node": "^9.1.1", // 一括登録などのTypescriptで書かれたファイルを実行する為
"typescript": "^3.8.0"
},
"engines": {
"node": "12"
},
"main": "lib/index.js",
"name": "functions",
"private": true,
"scripts": {
"build": "tsc",
"deploy": "firebase deploy --only functions,firestore:rules",
"emulators": "firebase emulators:start --import=./testdata --export-on-exit", // データ永続化して起動しています。
"fireway": "npx fireway migrate --require='ts-node/register' --path='src/migrations' --forceWait", // async await も考慮してmigrationを実行できます
"lint": "eslint --ext .js,.ts .",
"migrate": "dotenv -e .env npm run fireway",
"shell": "dotenv -e .env -- firebase functions:shell --inspect-functions", // function text用
"watch": "tsc --watch --preserveWatchOutput" // 差分検知
}
}

functions/src/index.ts
は、役割を分けてバージョン管理すると良いです
import * as admin from "firebase-admin";
import * as callable from "./modules/callable"; // https関連
import * as firestore from "./modules/firestore"; // Firestoreのトリガー処理
import * as schedule from "./modules/schedule"; // pubsub関連
admin.initializeApp();
module.exports = {
v1: {
callable: {
}
},
v2: {
callable: {
},
},
firestore: firestore,
schedule: schedule,
};

Firebase Functionを VS Code上にブレイクポイントをつけてDebugする方法
①プロジェクト直下に .vscode/launch.json
を作成します
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug Functions",
"type": "node",
"request": "attach",
"port": 9229,
"outFiles": [
"${workspaceRoot}/functions/lib/**/*.js"
]
}
]
}
② --inspect-functions
のオプション付きでfunctionを起動します。
あとは、RAN AND DEBUG
のセレクトボックスから↑のnameを選択するのみ