mongoloquent
    Preparing search index...

    Class DB<T>

    DB class represents a database collection wrapper that extends QueryBuilder to provide MongoDB-like query operations.

    Type Parameters

    • T

      The type of documents stored in the collection

    Hierarchy (View Summary)

    Indexable

    • [key: string]: any

      Allow dynamic property access

    Index

    Constructors

    Properties

    $alias: string = ""

    Alias for relationship

    $changes: Partial<Record<keyof T, any>> = {}

    Changes made to the document

    $collection: string = "mongoloquent"

    Collection name

    $connection: string = ""

    MongoDB connection string

    $databaseName: string = ""

    Database name

    $deletedAt: string = "deletedAt"

    Field name for the deletedAt timestamp

    $id: null | string | ObjectId = null

    Document ID

    $isDeleted: string = "isDeleted"

    Field name for the isDeleted flag

    $limit: number = 0

    Number of documents to limit in query

    $lookups: Document[] = []

    Lookup stages for aggregation pipelines

    $options: IRelationOptions = {}

    Relationship options

    $original: Partial<T> = {}

    Original document data

    $useSoftDelete: boolean = false

    Flag to enable soft delete functionality

    $useTimestamps: boolean = true

    Flag to enable timestamps

    $collection: string = ""

    Collection name

    $connection: string = MONGOLOQUENT_DATABASE_URI

    Default MongoDB connection string

    $databaseName: string = MONGOLOQUENT_DATABASE_NAME

    Default database name

    $isDeleted: string = "isDeleted"

    Field name for the isDeleted flag

    $schema: any

    Schema definition for the document

    $timezone: string = TIMEZONE

    Field name for the timezone

    $useSoftDelete: boolean = false

    Flag to enable soft delete functionality

    $useTimestamps: boolean = true

    Flag to enable timestamps

    Methods

    • Sets the collection name for operations on the current instance

      Parameters

      • collection: string

        The name of the collection to operate on

      Returns DB<T>

      The current DB instance for method chaining

    • Alias for insertMany - creates multiple documents in the collection

      Parameters

      • docs: FormSchema<T>[]

        Array of documents to create

      • Optionaloptions: BulkWriteOptions

        MongoDB bulk write options

      Returns Promise<ObjectId[]>

      Array of created document IDs

    • Sets the database name for operations on the current instance

      Parameters

      • database: string

        The name of the database to use

      Returns DB<T>

      The current DB instance for method chaining

    • Deletes documents by IDs (soft delete if enabled)

      Parameters

      • ...ids: (string | ObjectId | (string | ObjectId)[])[]

        IDs of documents to delete

      Returns Promise<number>

      Number of documents deleted or soft-deleted

    • Returns the first document matching the query as an enhanced instance

      Type Parameters

      • K extends string | number | symbol

        Keys of document type T

      Parameters

      • ...fields: (K | K[])[]

        Optional fields to select

      Returns Promise<null | DB<T> & T>

      First matching document or null if none found

    • Returns first matching document or creates a new one

      Parameters

      • filter: Partial<FormSchema<T>>

        Filter to find existing document

      • Optionaldoc: Partial<FormSchema<T>>

        Data to use for creation if no match is found

      Returns Promise<T>

      Existing or newly created document

    • Returns the first document matching the query or throws exception if none found

      Type Parameters

      • K extends string | number | symbol

        Keys of document type T

      Parameters

      • ...columns: (K | K[])[]

        Optional fields to select

      Returns Promise<DB<T> & T>

      First matching document

      If no document found

    • Alias for firstOrCreate - returns existing document or creates a new model instance

      Parameters

      • filter: Partial<FormSchema<T>>

        Filter to find existing document

      • Optionaldoc: Partial<FormSchema<T>>

        Data to use for the new instance if no match is found

      Returns Promise<T>

      Existing or newly created document

    • Permanently deletes soft-deleted documents by IDs

      Parameters

      • ...ids: (string | ObjectId | (string | ObjectId)[])[]

        IDs of documents to permanently delete

      Returns Promise<number>

      Number of documents permanently deleted

      If force deletion fails

    • Inserts multiple documents into the collection

      Parameters

      • docs: FormSchema<T>[]

        Array of documents to insert

      • Optionaloptions: BulkWriteOptions

        MongoDB bulk write options

      Returns Promise<ObjectId[]>

      Array of inserted document IDs

      If bulk insertion fails

    • Checks if all of the specified fields are unchanged

      Type Parameters

      • K extends string | number | symbol

        Keys of document type T

      Parameters

      • ...fields: (K | K[])[]

        Fields to check

      Returns boolean

      True if all fields are clean, false otherwise

    • Checks if any of the specified fields have been changed

      Type Parameters

      • K extends string | number | symbol

        Keys of document type T

      Parameters

      • ...fields: (K | K[])[]

        Fields to check

      Returns boolean

      True if any field is dirty, false otherwise

    • Performs a MongoDB $lookup aggregation to join documents from another collection

      Parameters

      • document: IDBLookup

        The lookup configuration containing from, localField, foreignField, and as properties

      Returns DB<T>

      The current DB instance for method chaining

    • Sets the order for the query results

      Type Parameters

      • K extends string | number | symbol

        Keys of document type T

      Parameters

      • column: K

        Column to order by

      • Optionaldirection: "asc" | "desc" = "asc"

        Sort direction (asc or desc)

      • OptionalcaseSensitive: boolean = false

        Whether sorting should be case sensitive

      Returns this

      Current query builder instance

    • Adds an OR where condition to the query

      Type Parameters

      • K extends string | number | symbol

        Keys of document type T

      Parameters

      • column: K

        Column name

      • operator: any

        Operator or value if comparing equality

      • Optionalvalue: any = null

        Value to compare against (optional if operator is the value)

      Returns QueryBuilder<T>

      Current query builder instance

    • Returns paginated results with metadata

      Parameters

      • Optionalpage: number = 1

        Page number (starting from 1)

      • Optionallimit: number = 15

        Number of items per page

      Returns Promise<IModelPaginate>

      Object containing data and pagination metadata

      If pagination fails

    • Adds raw MongoDB aggregation pipeline stages to the query

      Parameters

      • documents: Document | Document[]

        One or more MongoDB aggregation pipeline stages

      Returns DB<T>

      The current DB instance for method chaining

    • Updates a document in the collection based on the current query

      Parameters

      • doc: Partial<FormSchema<T>>

        Document fields to update

      • Optionaloptions: FindOneAndUpdateOptions = {}

        MongoDB findOneAndUpdate options

      Returns Promise<null | WithId<FormSchema<T>>>

      Updated document

      If update fails

    • Updates multiple documents in the collection matching the current query

      Parameters

      • doc: Partial<FormSchema<T>>

        Document fields to update

      • Optionaloptions: UpdateOptions

        MongoDB updateMany options

      Returns Promise<number>

      Number of documents modified

      If bulk update fails

    • Updates a document if it exists, otherwise creates it

      Parameters

      • filter: Partial<FormSchema<T>>

        Filter to find the document

      • Optionaldoc: Partial<FormSchema<T>>

        Document fields to update or insert

      Returns Promise<T | WithId<FormSchema<T>>>

      Updated or created document

    • Alias for updateOrCreate

      Parameters

      • filter: Partial<FormSchema<T>>

        Filter to find the document

      • Optionaldoc: Partial<FormSchema<T>>

        Document fields to update or insert

      Returns Promise<T | WithId<FormSchema<T>>>

      Updated or created document

    • Adds a where condition to the query

      Type Parameters

      • K extends string | number | symbol

        Keys of document type T

      Parameters

      • column: K

        Column name

      • operator: any

        Operator or value if comparing equality

      • Optionalvalue: any = null

        Value to compare against (optional if operator is the value)

      Returns QueryBuilder<T>

      Current query builder instance

    • Creates a new DB instance for the specified collection

      Type Parameters

      • T

        The type of documents stored in the collection

      Parameters

      • this: new () => DB<T>
      • collection: string

        The name of the collection to operate on

      Returns DB<T>

      A new DB instance configured for the specified collection

    • Sets the connection URI for database operations

      Type Parameters

      • T

        The type of documents stored in the collection

      Parameters

      • this: new () => DB<T>
      • connection: string

      Returns DB<T>

      A new DB instance with the specified connection

    • Sets the database name for subsequent operations

      Type Parameters

      • T

        The type of documents stored in the collection

      Parameters

      • this: new () => DB<T>
      • database: string

        The name of the database to use

      Returns DB<T>

      A new DB instance with the specified database