Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add .paginate on Query Helpers #217

Open
SkyaTura opened this issue Jul 21, 2024 · 1 comment
Open

Add .paginate on Query Helpers #217

SkyaTura opened this issue Jul 21, 2024 · 1 comment
Labels
solved This issue has been resolved/answered.

Comments

@SkyaTura
Copy link
Contributor

Is your feature request related to a problem? Please describe.
Currently is not possible to use chainable methods to compose a query with a cursor.

Describe the solution you'd like

UserModel.find()
  .where({ active: true })
  .where({ deletedAt: {$ne: null })
  .paginate()

Describe alternatives you've considered
While it is possible to rewrite the query like

UserModel.paginate({ active: true, deletedAt: { $ne: null } })

If I have QueryHelpers it wouldn't work:

UserModel.find().whereActive().paginate(...)

Additional context
I think there is none.

@SkyaTura SkyaTura changed the title Add .paginate on Query class Add .paginate on Query Helpers Jul 21, 2024
@SkyaTura
Copy link
Contributor Author

SkyaTura commented Jul 22, 2024

I was able to meet this requirement by creating a new local plugin with the following code, but I still think this would be a nice feature for the lib.

mongoose.plugin((schema) => {
  function paginate(this: Query<any, any, any, any, any, any>, options?: PaginateOptions) {
    return this.model.paginate(this.getQuery(), options)
  }
  Object.assign(schema.query, { paginate })
})

export interface Query<
    ResultType,
    DocType,
    THelpers = NonNullable<unknown>,
    RawDocType = DocType,
    QueryOp = 'find',
    TInstanceMethods = Record<string, never>,
  > {
    paginate<O extends PaginateOptions>(
      options?: O,
      callback?: (
        err: any,
        result: PaginateResult<PaginateDocument<RawDocType, TInstanceMethods, O>>
      ) => void
    ): Promise<PaginateResult<PaginateDocument<RawDocType, TInstanceMethods, O>>>
    paginate<UserType = ResultType, O extends PaginateOptions = PaginateOptions>(
      options?: O,
      callback?: (
        err: any,
        result: PaginateResult<PaginateDocument<UserType, TInstanceMethods, O>>
      ) => void
    ): Promise<PaginateResult<PaginateDocument<UserType, TInstanceMethods, O>>>
    paginate<UserType = ResultType>(
      options?: PaginateOptions,
      callback?: (
        err: any,
        result: PaginateResult<PaginateDocument<UserType, TInstanceMethods, PaginateOptions>>
      ) => void
    ): Promise<PaginateResult<PaginateDocument<UserType, TInstanceMethods, PaginateOptions>>>
}

@aravindnc aravindnc added the solved This issue has been resolved/answered. label Jul 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
solved This issue has been resolved/answered.
Projects
None yet
Development

No branches or pull requests

2 participants