-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix up scripts with batched iterator, add toggleAdmin (#170)
- Loading branch information
Showing
7 changed files
with
124 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { getDatabase } from "firebase-admin/database"; | ||
import inquirer from "inquirer"; | ||
|
||
import { databaseIterator } from "./utils.js"; | ||
|
||
function displayUser(user) { | ||
const name = user.child("name").val(); | ||
const lastOnline = new Date( | ||
user.child("lastOnline").val(), | ||
).toLocaleDateString(); | ||
return `${name} (last online: ${lastOnline})`; | ||
} | ||
|
||
export async function listAdmins() { | ||
for await (const [userId, user] of databaseIterator("users")) { | ||
if (user.child("admin").val()) { | ||
console.log(userId, displayUser(user)); | ||
} | ||
} | ||
} | ||
|
||
export async function listPatrons() { | ||
for await (const [userId, user] of databaseIterator("users")) { | ||
if (user.child("patron").val()) { | ||
console.log(userId, displayUser(user)); | ||
} | ||
} | ||
} | ||
|
||
export async function toggleAdmin() { | ||
const { userId } = await inquirer.prompt([ | ||
{ type: "input", name: "userId", message: "Enter the user ID:" }, | ||
]); | ||
|
||
const user = await getDatabase().ref(`users/${userId}`).get(); | ||
console.log(displayUser(user)); | ||
|
||
if (user.child("admin").val()) { | ||
const { confirm } = await inquirer.prompt([ | ||
{ | ||
type: "confirm", | ||
name: "confirm", | ||
message: "User is admin, do you want to remove admin status?", | ||
}, | ||
]); | ||
if (confirm) { | ||
await getDatabase().ref(`users/${userId}/admin`).remove(); | ||
} | ||
} else { | ||
const { confirm } = await inquirer.prompt([ | ||
{ | ||
type: "confirm", | ||
name: "confirm", | ||
message: "User is not admin, do you want to grant admin status?", | ||
}, | ||
]); | ||
if (confirm) { | ||
await getDatabase().ref(`users/${userId}/admin`).set(true); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { getDatabase } from "firebase-admin/database"; | ||
|
||
/** | ||
* Iterate through a reference in the database, returning the children ordered | ||
* by key in batches. | ||
* | ||
* @param {string} path The path to the reference to iterate through. | ||
* @param {number} batchSize The number of children to fetch in each batch. | ||
* @returns {AsyncGenerator<[string, import("firebase-admin/database").DataSnapshot]>} | ||
*/ | ||
export async function* databaseIterator(path, batchSize = 1000) { | ||
let lastKey = null; | ||
while (true) { | ||
const snap = lastKey | ||
? await getDatabase() | ||
.ref(path) | ||
.orderByKey() | ||
.startAfter(lastKey) | ||
.limitToFirst(batchSize) | ||
.get() | ||
: await getDatabase() | ||
.ref(path) | ||
.orderByKey() | ||
.limitToFirst(batchSize) | ||
.get(); | ||
if (!snap.exists()) return; | ||
|
||
const childKeys = []; | ||
snap.forEach((child) => { | ||
childKeys.push(child.key); | ||
lastKey = child.key; | ||
}); | ||
for (const key of childKeys) { | ||
yield [key, snap.child(key)]; | ||
} | ||
} | ||
} |