UNPKG

@tmlmobilidade/interfaces

Version:

This package provides SDK-style connectors for interacting with databases (e.g., stops, plans, rides, alerts) and external providers (e.g., authentication, storage). It simplifies data access and integration across projects.

138 lines (137 loc) 5.99 kB
import { MongoConnector } from '@tmlmobilidade/connectors'; import { type UnixTimestamp } from '@tmlmobilidade/types'; import { Collection, DeleteOptions, DeleteResult, Document, Filter, FindOptions, IndexDescription, InsertOneOptions, InsertOneResult, MongoClientOptions, Sort, UpdateOptions, UpdateResult, WithId } from 'mongodb'; import { z } from 'zod'; export declare abstract class MongoCollectionClass<T extends Document, TCreate, TUpdate> { protected createSchema: null | z.ZodSchema; protected mongoCollection: Collection<T>; protected mongoConnector: MongoConnector; protected updateSchema: null | z.ZodSchema; /** * Gets all documents in the collection. * * @returns A promise that resolves to an array of all documents */ all(): Promise<WithId<T>[]>; /** * Establishes a connection to the Mongo database and initializes the collection. * @param options Optional Mongo client connection options * @throws {Error} If connection fails */ connect(options?: MongoClientOptions): Promise<void>; /** * Counts documents matching the filter criteria. * * @param filter - The filter criteria to match documents * @returns A promise that resolves to the count of matching documents */ count(filter?: Filter<T>): Promise<number>; /** * Deletes a single document by its ID. * * @param id - The ID of the document to delete * @returns A promise that resolves to the result of the delete operation */ deleteById(id: string, options?: DeleteOptions): Promise<DeleteResult>; /** * Deletes multiple documents matching the filter criteria. * * @param filter - The filter criteria to match documents to delete * @returns A promise that resolves to the result of the delete operation */ deleteMany(filter: Filter<T>): Promise<DeleteResult>; /** * Deletes a single document matching the filter criteria. * * @param filter - The filter criteria to match the document to delete * @returns A promise that resolves to the result of the delete operation */ deleteOne(filter: Filter<T>): Promise<DeleteResult>; /** * Disconnects from the MongoDB database. */ disconnect(): Promise<void>; /** * Finds all distinct values for a key in the collection. * * @param key - The key to find distinct values for * @returns A promise that resolves to an array of distinct values for the given key */ distinct<K extends keyof T>(key: K): Promise<T[K][]>; /** * Finds a document by its ID. * * @param id - The ID of the document to find * @returns A promise that resolves to the matching document or null if not found */ findById(id: string, options?: FindOptions): Promise<null | WithId<T>>; /** * Finds multiple documents matching the filter criteria with optional pagination and sorting. * * @param filter - (Optional) filter criteria to match documents * @param perPage - (Optional) number of documents per page for pagination * @param page - (Optional) page number for pagination * @param sort - (Optional) sort specification * @returns A promise that resolves to an array of matching documents */ findMany(filter?: Filter<T>, perPage?: number, page?: number, sort?: Sort): Promise<WithId<T>[]>; /** * Finds a single document matching the filter criteria. * * @param filter - The filter criteria to match the document * @returns A promise that resolves to the matching document or null if not found */ findOne(filter: Filter<T>): Promise<null | WithId<T>>; /** * Gets the MongoDB collection instance. * * @returns The MongoDB collection instance */ getCollection(): Promise<Collection<T>>; getMongoConnector(): MongoConnector; /** * Inserts a single document into the collection. * * @param doc - The document to insert * @param options - The options for the insert operation * @returns A promise that resolves to the result of the insert operation */ insertOne(doc: TCreate & { _id?: string; created_at?: UnixTimestamp; updated_at?: UnixTimestamp; }, { options, unsafe }?: { options?: InsertOneOptions; unsafe?: boolean; }): Promise<InsertOneResult<T>>; /** * Updates a single document matching the filter criteria. * * @param filter - The filter criteria to match the document to update * @param updateFields - The fields to update in the document * @param options - The options for the update operation * @returns A promise that resolves to the result of the update operation */ updateById(id: string, updateFields: TUpdate, options?: UpdateOptions): Promise<UpdateResult>; /** * Updates multiple documents matching the filter criteria. * * @param filter - The filter criteria to match documents to update * @param updateFields - The fields to update in the documents * @param options - The options for the update operation * @returns A promise that resolves to the result of the update operation */ updateMany(filter: Filter<T>, updateFields: TUpdate, options?: UpdateOptions): Promise<UpdateResult<T>>; /** * Updates a single document matching the filter criteria. * * @param filter - The filter criteria to match the document to update * @param updateFields - The fields to update in the document * @param options - The options for the update operation * @returns A promise that resolves to the result of the update operation */ updateOne(filter: Filter<T>, updateFields: TUpdate, options?: UpdateOptions): Promise<UpdateResult>; protected abstract getCollectionIndexes(): IndexDescription[]; protected abstract getCollectionName(): string; protected abstract getEnvName(): string; }