interface BaseRepo<T extends BaseModel> {
    modelName: string;
    countDocuments(query?: FilterQuery<T>): Promise<number>;
    create(entity: T): Promise<Saved<T>>;
    createMany(entity: T[], options?: CreateManyOptions): Promise<string[]>;
    deleteById(id: string): Promise<null | Saved<T>>;
    destroyById(id: string): Promise<null | Saved<T>>;
    findById(
        id: string,
        fields?: SelectQuery<T>[],
        ListQueryOptions?: ListQueryOptions<T>,
    ): Promise<null | Saved<T>>;
    findOne(
        query?: FilterQuery<T>,
        fields?: SelectQuery<T>[],
        ListQueryOptions?: ListQueryOptions<T>,
    ): Promise<null | Saved<T>>;
    list(
        query?: FilterQuery<T>,
        fields?: SelectQuery<T>[],
        ListQueryOptions?: ListQueryOptions<T>,
    ): Promise<Saved<T>[]>;
    paginate(
        options?: PaginateQueryOptions<T>,
    ): Promise<PaginationResponse<Saved<T>>>;
    updateById(
        id: string,
        entity: PartialQuery<T>,
        options?: UpdateOptions,
    ): Promise<null | Saved<T>>;
    updateMany(
        filter: FilterQuery<T>,
        update: PartialQuery<T>,
    ): Promise<number>;
    updateOne(
        filter: FilterQuery<T>,
        update: PartialQuery<T>,
        options?: UpdateOneOptions,
    ): Promise<null | Saved<T>>;
    upsertOne(
        filter: FilterQuery<T>,
        entity: PartialQuery<T>,
    ): Promise<null | Saved<T>["_id"]>;
}

Type Parameters

  • T extends BaseModel

Hierarchy (View Summary)

Implemented by

Properties

modelName: string

The name of the model.

Methods

  • Create multiple entities.

    Parameters

    • entity: T[]

      An array of entities to create.

    • Optionaloptions: CreateManyOptions

    Returns Promise<string[]>

    • A promise that resolves to an array of created entity IDs.
  • Soft delete an entity by its ID. This operation marks the entity as deleted without permanently destroying the record.

    Parameters

    • id: string

      The ID of the entity to delete. @returns- A promise that resolves to the deleted entity or null if not found.

    Returns Promise<null | Saved<T>>

  • Find an entity by its ID.

    Parameters

    • id: string

      The ID of the entity to find.

    • Optionalfields: SelectQuery<T>[]

      Fields to select in the returned entity.

    • OptionalListQueryOptions: ListQueryOptions<T>

      Options for listing queries.

    Returns Promise<null | Saved<T>>

    • A promise that resolves to the found entity or null if not found.
  • List entities matching the query.

    Parameters

    • Optionalquery: FilterQuery<T>

      The query to filter documents.

    • Optionalfields: SelectQuery<T>[]

      Fields to select in the returned entities.

    • OptionalListQueryOptions: ListQueryOptions<T>

      Options for listing queries.

    Returns Promise<Saved<T>[]>

    • A promise that resolves to an array of found entities.
  • Retrieve paginated results. Only intended for use with the admin portal for performance reasons.

    Parameters

    • Optionaloptions: PaginateQueryOptions<T>

      The pagination options

    Returns Promise<PaginationResponse<Saved<T>>>

  • Update an entity by its ID.

    Parameters

    • id: string

      The ID of the entity to update.

    • entity: PartialQuery<T>

      The partial entity to update.

    • Optionaloptions: UpdateOptions

    Returns Promise<null | Saved<T>>

    • A promise that resolves to the updated entity or null if not found.
  • Update multiple entities matching the filter.

    Parameters

    • filter: FilterQuery<T>

      The filter to match documents.

    • update: PartialQuery<T>

      The partial update to apply.

    Returns Promise<number>

    • A promise that resolves to the number of documents updated.
  • Update a single entity matching the filter.

    Parameters

    • filter: FilterQuery<T>

      The filter to match documents.

    • update: PartialQuery<T>

      The partial update to apply.

    • Optionaloptions: UpdateOneOptions

      Options for updating a single document.

    Returns Promise<null | Saved<T>>

    | null>} - A promise that resolves to the updated entity or null if not found.

  • Upsert a single record based on certain criteria.

    Parameters

    • filter: FilterQuery<T>

      The filter to find the record to update or insert.

    • entity: PartialQuery<T>

      The entity to upsert.

    Returns Promise<null | Saved<T>["_id"]>

    The return types of this method align with the MongoDB UpdateResult interface

    • Returns models.baseModel.Saved<T>['_id'] if the document did not exist (an upsert took place).
    • Returns null if the document did exist before the upsert operation.