mirror of https://github.com/flutter/pinball.git
feat(functions): add leaderboard cleanup function (#376)
parent
480b08d3c2
commit
6454d166e9
@ -0,0 +1,5 @@
|
||||
{
|
||||
"projects": {
|
||||
"default": "pinball-dev"
|
||||
}
|
||||
}
|
@ -0,0 +1 @@
|
||||
node_modules/
|
@ -0,0 +1,28 @@
|
||||
const functions = require("firebase-functions");
|
||||
const admin = require("firebase-admin");
|
||||
admin.initializeApp();
|
||||
|
||||
const db = admin.firestore();
|
||||
|
||||
exports.timedLeaderboardCleanup = functions.firestore
|
||||
.document("leaderboard/{leaderboardEntry}")
|
||||
.onCreate(async (_, __) => {
|
||||
functions.logger.info(
|
||||
"Document created, getting all leaderboard documents"
|
||||
);
|
||||
const snapshot = await db
|
||||
.collection("leaderboard")
|
||||
.orderBy("score", "desc")
|
||||
.offset(10)
|
||||
.get();
|
||||
|
||||
functions.logger.info(
|
||||
`Preparing to delete ${snapshot.docs.length} documents.`
|
||||
);
|
||||
try {
|
||||
await Promise.all(snapshot.docs.map((doc) => doc.ref.delete()));
|
||||
functions.logger.info("Success");
|
||||
} catch (error) {
|
||||
functions.logger.error(`Failed to delete documents ${error}`);
|
||||
}
|
||||
});
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,23 @@
|
||||
{
|
||||
"name": "functions",
|
||||
"description": "Cloud Functions for Firebase",
|
||||
"scripts": {
|
||||
"serve": "firebase emulators:start --only functions",
|
||||
"shell": "firebase functions:shell",
|
||||
"start": "npm run shell",
|
||||
"deploy": "firebase deploy --only functions",
|
||||
"logs": "firebase functions:log"
|
||||
},
|
||||
"engines": {
|
||||
"node": "16"
|
||||
},
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
"firebase-admin": "^10.0.2",
|
||||
"firebase-functions": "^3.18.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"firebase-functions-test": "^0.2.0"
|
||||
},
|
||||
"private": true
|
||||
}
|
Loading…
Reference in new issue