UNPKG

@point-hub/papi

Version:

Point API Framework

79 lines 3.24 kB
import { CreateIndexesOptions, IndexSpecification, ObjectId } from 'mongodb'; import { IDatabase } from '../../index'; type ObjectIdToString<T> = T extends ObjectId ? string : T extends (infer U)[] ? ObjectIdToString<U>[] : T extends Date ? T : T extends object ? { [K in keyof T]: ObjectIdToString<T[K]>; } : T; /** * https://www.mongodb.com/docs/drivers/node/current/fundamentals/indexes/ * https://www.mongodb.com/docs/manual/reference/collation/ * https://www.mongodb.com/docs/manual/core/index-sparse/ * https://www.mongodb.com/docs/manual/core/index-partial/ */ export declare class MongoDBHelper { private db; constructor(db: IDatabase); /** * Create unique column * * @example * create unique attribute "name" * createUnique(collection, { * name: -1, * }) * * @example * create unique attribute for multiple column "first_name" and "last_name" * createUnique(collection, { * firstName: -1, * lastName: -1, * }) */ createUnique(collection: string, properties: object): Promise<void>; /** * Create unique if column is exists */ createUniqueIfNotNull(collection: string, properties: object): Promise<void>; createIndex(collection: string, spec: IndexSpecification, options?: CreateIndexesOptions): Promise<void>; isExists(name: string): Promise<boolean>; static stringToObjectId(ids: string[]): ObjectId[]; static stringToObjectId<T>(input: T): T; static expandDottedObject(obj: Record<string, unknown>): Record<string, unknown>; static convertToObjectId(input: unknown): unknown; /** * Recursively converts all ObjectId instances within a structure to strings. * @param value The input data structure. * @returns The data structure with ObjectId instances converted to strings. */ static objectIdToString<T>(value: T): ObjectIdToString<T>; /** * Deeply cleans an object by removing properties that are: * 1. Undefined * 2. Null * 3. Empty objects ({}) * 4. Empty arrays ([]) * * @param rawInput The input data object. * @returns The deeply filtered object, or an empty object if all content was filtered. */ static buildPostData(rawInput: Record<string, any>): Record<string, any>; /** * Builds a clean array of documents for a MongoDB insertMany operation. * It iterates over the array, applying deep filtering to each document. * Documents that become empty after filtering are removed from the array. */ static buildPostManyData(rawInput: Array<Record<string, any>>): Array<Record<string, any>>; /** * Builds safe MongoDB $set and $unset operations for a PATCH request. * It flattens nested objects into dot notation and handles explicit null values for $unset. * Undefined values are ignored. */ static buildPatchData: (rawInput: Record<string, any>) => { $set?: Record<string, any>; $unset?: Record<string, true>; $push?: Record<string, any>; $pull?: Record<string, any>; $inc?: Record<string, number>; $addToSet?: Record<string, any>; }; } export {}; //# sourceMappingURL=mongodb-helper.d.ts.map