firebase-toolsのバージョンアップに伴うforceオプションの追加
firebaseのアクションをきっかけにfirestore内のデータを操作したい場合があります。
ex.
アカウントが削除されたときにFirestoreデータベース内のユーザーのコレクションを削除するFirebase関数。
const firebase_tools = require("firebase-tools");
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
exports.deleteUser = functions.auth.user().onDelete((user) => {
return firebase_tools.firestore
.delete(`users/${user.uid}`, {
project: process.env.GCLOUD_PROJECT,
token: functions.config().fb.token,
recursive: true,
yes: true
}).catch((error) => {
console.log(error);
throw new functions.https.HttpsError(
"unknown",
"Error deleting user's data"
);
});
});
"firebase-tools": "9.18.0"まではこれで動作していましたが、"firebase-tools": "^10.1.*"以降で確認が求められるように。
そのため、force: trueが必要になりました。
{
project: process.env.GCLOUD_PROJECT,
token: functions.config().fb.token,
recursive: true,
yes: true,
force: true // add this
}
firebase-tools
firebase firestore:delete -hで最新のコマンドを確認することもできます。
Options:
-r, --recursive Recursive. Delete all documents and subcollections at and under the specified level. May not be passed along with --shallow.
--shallow Shallow. Delete only documents at the specified level and ignore documents in subcollections. This action can potentially orphan documents nested in subcollections. May not be passed along with -r.
--all-collections Delete all. Deletes the entire Firestore database, including all collections and documents. Any other flags or arguments will be ignored.
-f, --force No confirmation. Otherwise, a confirmation prompt will appear.
-h, --help output usage information
参考