Provides a generic MongoDB connector implementation for managing collections and documents. This class handles CRUD operations, transforms data between application and MongoDB formats, and establishes a connection to the MongoDB instance.

Type Parameters

  • T extends MongoEntity

    The type of the entities managed by the connector. Should extend MongoEntity.

Implements

Constructors

Properties

db: Db

Methods

  • Creates a new document in the specified collection with the provided data.

    Parameters

    • collectionName: string

      The name of the collection where the data will be inserted.

    • data: T

      The data object to be added to the collection.

    Returns Promise<T>

    A promise that resolves to the inserted data including the generated ID and timestamps.

  • Deletes a document from the specified collection by its identifier.

    Parameters

    • collectionName: string

      The name of the collection from which the document will be deleted.

    • id: string

      The identifier of the document to be deleted.

    Returns Promise<boolean>

    A promise resolving to true if the document was successfully deleted, false otherwise.

  • Retrieves all documents from a given collection that match the specified query and parameters.

    Parameters

    • collectionName: string

      The name of the database collection to search.

    • query: string

      A string defining the filter conditions, separated by "AND" clauses and using "=" for key-value matching.

    • params: any[]

      An array of values to be matched against the keys defined in the query.

    • single: boolean = false

    Returns Promise<T[]>

    A promise that resolves to an array of documents matching the query conditions.

  • Fetches a single record from the specified collection that matches the given query and parameters.

    Parameters

    • collectionName: string

      The name of the collection to query.

    • query: string

      The query string to execute against the collection.

    • params: any[]

      The parameters to bind to the query.

    Returns Promise<T>

    A promise that resolves to the first matching record or null if no match is found.

  • Retrieves all documents from a specified collection.

    Parameters

    • collectionName: string

      The name of the collection to retrieve documents from.

    Returns Promise<T[]>

    A promise that resolves to an array of documents of type T.

  • Retrieves a single document from the specified collection based on its unique identifier.

    Parameters

    • collectionName: string

      The name of the collection to query.

    • id: number

      The unique identifier of the document to retrieve.

    Returns Promise<T>

    A promise that resolves to the document matching the given identifier.

  • Initializes a MongoDB connection using environment variables or default values. The connection is established with authentication and logged on successful connection.

    Returns Promise<void>

    A promise that resolves when the MongoDB connection is successfully established.

  • This method is called when the component or object is being destroyed. It is typically used for cleanup operations, such as unsubscribing from observables, detaching event listeners, or releasing resources.

    Returns void

    Does not return any value.

  • Updates a document in the specified collection with the provided data. Converts the data into a document format, applies a modified timestamp, and updates the corresponding record in the database.

    Parameters

    • collectionName: string

      The name of the collection where the update should occur.

    • data: T

      The data object to update, which will be converted into a document.

    Returns Promise<T>

    Returns the updated document if the update is successful, or null if no document is modified. In case of an error, returns the error object.