UNPKG

@imigueldiaz/mongodb-labeler

Version:

A MongoDB-based labeling system for content moderation with cryptographic signing

79 lines (78 loc) 3.12 kB
import { Filter, FindOptions, ObjectId } from "mongodb"; import type { SavedLabel, UnsignedLabel } from "./util/types.js"; /** * Client for interacting with the MongoDB database where labels are stored. * * This class handles connecting to the database, creating necessary indexes, * and performing basic CRUD operations on labels. * * @param uri The URI to connect to the MongoDB instance. * @param databaseName The name of the database to use. Defaults to 'labeler'. * @param collectionName The name of the collection to use. Defaults to 'labels'. */ export declare class MongoDBClient { private _db?; private _client?; private _labels?; private readonly _url; /** * Create a new MongoDBClient instance. * @param uri The URI to connect to the MongoDB instance. */ constructor(uri: string); /** * Connect to the MongoDB instance and initialize the collection. * If the collection doesn't exist, it will be created with the necessary indexes. * * This method should be called before any other method in this class. */ connect(): Promise<void>; /** * Close the connection to MongoDB. */ close(): Promise<void>; /** * Save a label to the MongoDB collection. * * @param label - The label to save, including a signature as an ArrayBuffer. * @returns A promise that resolves to the saved label. */ saveLabel(label: UnsignedLabel & { sig: ArrayBuffer; }): Promise<SavedLabel>; /** * Find labels in the MongoDB collection matching the given query. * * @param query - The query object to filter labels. * @param options - Optional settings for the query, such as sort and limit. * @returns A promise that resolves to an array of labels matching the query. */ findLabels(query?: Filter<SavedLabel> & { allowExpired?: boolean; }, options?: FindOptions<SavedLabel>): Promise<SavedLabel[]>; /** * Find a single label in the MongoDB collection matching the given query. * * @param query - The query object to filter labels. * @returns A promise that resolves to the matching label or null if not found. */ findOne(query: Filter<SavedLabel>): Promise<SavedLabel | null>; /** * Retrieve a limited number of labels from the database that have an ID greater than the specified cursor. * * @param cursor - The ID after which labels should be retrieved. * @param limit - The maximum number of labels to return. * @returns A promise that resolves to an array of labels with IDs greater than the cursor. */ getLabelsAfterCursor(cursor: ObjectId, limit: number): Promise<SavedLabel[]>; /** * Update an existing label in the MongoDB collection. * * @param id - The ID of the label to update. * @param label - The new label data. * @returns A promise that resolves to true if the update was successful, false if the label wasn't found. */ updateLabel(id: ObjectId, label: UnsignedLabel & { sig: ArrayBuffer; }): Promise<boolean>; }