来自 Fireship 网站:https://fireship.io/snippets/delete-firestore-collection/,在 Firestore 中删除集合有两种选项:
选项 1:您可以手动从 Firebase 控制台或使用 CLI 删除集合或子集合:
firebase firestore:delete path-to-delete
选项 2:
可以从云函数与 Firebase 工具进行交互。与可调用函数相结合效果尤佳,因为您肯定想要强制执行某种形式的用户授权。
首先,获取 CI 令牌以验证 firebase 工具。
cd functions
npm i firebase-tools -D
firebase login:ci
firebase functions:config:set ci_token='your_token'
该函数应验证用户是否有权限运行操作。如果允许,则在集合及其嵌套的子集合上递归运行CLI命令。
const project = process.env.GCLOUD_PROJECT;
const token = functions.config().ci_token;
exports.deleteCollection = functions.runWith({ timeoutSeconds: 540})
.https.onCall((data, context) => {
const path = data.path;
const allowed = context.auth.uid === path.split('/')[0];
if (!allowed) {
throw new functions.https.HttpsError(
'permission-denied',
'Hey, that is not cool buddy!'
);
}
return firebase_tools.firestore
.delete(path, {
project,
token,
recursive: true,
yes: true,
})
.then(() => ({ result: 'all done!' }));
});
recursiveDelete
有任何限制吗? - Sohel Islam Imran