1
0
Fork 0
panel/lib/db.ts

51 lines
1.2 KiB
TypeScript

import { Filter, MongoClient } from "mongodb";
import type { Report, SnapshotContent, User } from "revolt-api";
let client: MongoClient;
function mongo() {
if (!client) {
client = new MongoClient(process.env.MONGODB!);
}
return client;
}
export default mongo;
export function fetchUserById(id: string) {
return mongo().db("revolt").collection<User>("users").findOne({ _id: id });
}
export function fetchUsersById(ids: string[]) {
return mongo()
.db("revolt")
.collection<User>("users")
.find({ _id: { $in: ids } })
.toArray();
}
export function fetchReports(query: Filter<Report> = { status: "Created" }) {
return mongo()
.db("revolt")
.collection<Report>("safety_reports")
.find(query as never)
.toArray();
}
export function fetchReportById(id: string) {
return mongo()
.db("revolt")
.collection<Report>("safety_reports")
.findOne({ _id: id });
}
export function fetchSnapshotsByReport(reportId: string) {
return mongo()
.db("revolt")
.collection<{ content: SnapshotContent }>("safety_snapshots")
.find({ report_id: reportId })
.toArray()
.then((snapshots) => snapshots.map((snapshot) => snapshot!.content));
}