Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
await firebaseTools.deploy({
project,
token,
cwd: resolvePath(__dirname, '../../config')
});
// Firestore calls grpc.load() which has been deprecated and we
// get an ugly warning on screen. This mutes it temporarily.
const unmute = muteDeprecationWarning();
const firestore = firebase.initializeApp(config).firestore();
firestore.settings({ timestampsInSnapshots: true });
const rootRef = firestore.doc('/');
task = showTask('Deleting "shops" collection');
await firebaseTools.firestore.delete('/shops', {
project,
yes: true,
recursive: true
});
task = showTask('Loading test data into "shops" collection');
await loadTestDataset(rootRef, loadJSONFile(
'./data.json'
) as TestCollection[]);
unmute();
task.done();
return;
})
.then((result?: boolean) => {
if (!(context.auth && context.auth.token && context.auth.token.admin)) {
throw new functions.https.HttpsError(
'permission-denied',
'Must be an administrative user to initiate delete.'
);
}
const path = data.path;
console.log(
`User ${context.auth.uid} has requested to delete path ${path}`
);
// Run a recursive delete on the given document or collection path.
// The 'token' must be set in the functions config, and can be generated
// at the command line by running 'firebase login:ci'.
return firebase_tools.firestore
.delete(path, {
project: process.env.GCLOUD_PROJECT,
recursive: true,
yes: true,
token: functions.config().fb.token
})
.then(() => {
return {
path: path
};
});
});
// [END recursive_delete_function]
try {
const isRecursive = config.firestoreDeleteMode === 'recursive';
if (!isRecursive) {
const firestore = admin.firestore();
logs.firestorePathDeleting(path, false);
// Wrapping in transaction to allow for automatic retries (#48)
await firestore.runTransaction((transaction => {
transaction.delete(firestore.doc(path));
return Promise.resolve();
}));
logs.firestorePathDeleted(path, false);
} else {
logs.firestorePathDeleting(path, true);
await firebase_tools.firestore.delete(path, {
project: process.env.PROJECT_ID,
recursive: true,
yes: true, // auto-confirmation
});
logs.firestorePathDeleted(path, true);
}
} catch (err) {
logs.firestorePathError(path, err);
}
});
if (!data.path) {
throw new functions.https.HttpsError(
"invalid-argument",
"Must specify a 'path' argument."
);
}
console.log(
`[fsdelete] User ${context.auth.uid} has requested to delete path ${
data.path
}`
);
const deletePath = data.path;
try {
await tools.firestore.delete(deletePath, {
project: process.env.PROJECT_ID,
recursive: true,
yes: true,
});
console.log("[fsdelete] Delete success");
return {
path: deletePath,
};
} catch (e) {
console.warn("[fsdelete] Delete failure", e);
throw new functions.https.HttpsError("unknown", JSON.stringify(e));
}
});
const promises = paths.map((path) => __awaiter(void 0, void 0, void 0, function* () {
try {
const isRecursive = config_1.default.firestoreDeleteMode === 'recursive';
if (!isRecursive) {
logs.firestorePathDeleting(path, false);
yield admin
.firestore()
.doc(path)
.delete();
logs.firestorePathDeleted(path, false);
}
else {
logs.firestorePathDeleting(path, true);
yield firebase_tools.firestore.delete(path, {
project: process.env.PROJECT_ID,
recursive: true,
yes: true,
});
logs.firestorePathDeleted(path, true);
}
}
catch (err) {
logs.firestorePathError(path, err);
}
}));
yield Promise.all(promises);
exports.fsdelete = functions.https.onCall(async (data, context) => {
logs.start();
const { auth } = context;
assertions.userIsAuthenticated(auth);
const { token, uid } = auth;
assertions.userHasClaim(token);
const { path: deletePath } = data;
assertions.pathExists(deletePath);
logs.deletePath(uid, deletePath);
try {
await tools.firestore.delete(deletePath, {
project: process.env.PROJECT_ID,
recursive: true,
yes: true,
});
logs.success(deletePath);
return {
path: deletePath,
};
} catch (err) {
logs.error(deletePath, err);
throw httpErrors.unknown(err);
}
});