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

Types not working for 7.1.0 #17

Open
mikaelmattsson opened this issue Jun 18, 2024 · 2 comments
Open

Types not working for 7.1.0 #17

mikaelmattsson opened this issue Jun 18, 2024 · 2 comments
Labels
help wanted Extra attention is needed question Further information is requested

Comments

@mikaelmattsson
Copy link

The type didn't want to resolve for me in typescript.

I had to add this in my project

typescript

declare module 'mongoose' {
  interface Query<ResultType, DocType, THelpers, RawDocType> {
    cache(ttl: number, customKey: string): this
    cache(customKey: string): this
    cache(ttl: number): this
  }
}

I think the issue is that the interface in the library is DocumentQuery while in mongoose I'm using it's Query

I'm using mongoose 7.1.0
recachegoose 11.0.0
pnpm 8.15.8
Node v18.18.0

@aalfiann
Copy link
Owner

aalfiann commented Jul 3, 2024

I'm keep thinking that if I remove the DocumentQuery, it might be not backwards compatible.

So if you add another interface then it works to you?

I've modified this typescript, but I just can't test it, can you help me to make sure this below index.d.ts works?

Here is the full modified index.d.ts

import { Document, Mongoose } from 'mongoose'

// Declare the 'recachegoose' module
declare module 'recachegoose' {
  function cachegoose(mongoose: Mongoose, cacheOptions: cachegoose.IOptions): void

  namespace cachegoose {
    interface IOptions {
      engine?: string
      count?: number
      port?: number
      host?: string
      password?: string
      client?: any
    }

    function clearCache(customKey: any, cb: (err: any) => void): void
  }

  export = cachegoose
}

// Augment the 'mongoose' module
declare module 'mongoose' {
  interface DocumentQuery<T, DocType extends Document, QueryHelpers = {}> {
    // Not cachegoose related fix, but useful. Thanks to https://github.com/DefinitelyTyped/DefinitelyTyped/issues/34205#issuecomment-621976826
    orFail(err?: Error | (() => Error)): DocumentQuery<NonNullable<T>, DocType, QueryHelpers>
    cache(ttl?: number, customKey?: string): this
  }

  interface Query<ResultType, DocType extends Document, THelpers = {}, RawDocType = {}> {
    cache(ttl?: number, customKey?: string): this
  }
}
``

If you or anyone reading this issue, saying this is works, then I'll deliver the new update.

Thank you.

@aalfiann aalfiann added help wanted Extra attention is needed question Further information is requested labels Jul 3, 2024
@rrigoni
Copy link

rrigoni commented Jul 10, 2024

Not working here as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants