cachegoose icon indicating copy to clipboard operation
cachegoose copied to clipboard

Add TypeScript definition

Open she11sh0cked opened this issue 6 years ago • 5 comments

See #38

she11sh0cked avatar Jan 20 '20 11:01 she11sh0cked

Just ran into a typescript issue with cachegoose:

item = await SomeModel.findOne({_id: id}).cache(60)

generating:

TS2551: Property 'cache' does not exist on type 'DocumentQuery<...>'. Did you mean 'catch'?

don't we miss the cache declaration on DocumentQuery/Aggregate here ? Probably something like this:

declare module 'mongoose' {
    interface DocumentQuery<T, DocType extends Document, QueryHelpers = {}>  {
        cache(ttl?: number, customKey?: string): this
    }
    interface Aggregate<T> {
        cache(ttl?: number, customKey?: string): this
    }
}

syffs avatar Apr 01 '20 09:04 syffs

@boblauer anything missing here ?

syffs avatar Apr 10 '20 13:04 syffs

I couldn't use {} had to use Record<never,never>>, I tried Record<string,never>> but conflicts with mongoose. If your happy with this controversial solution ( https://github.com/typescript-eslint/typescript-eslint/issues/2063 ) I can submit an updated typedef so we don't have to override locally anymore.

declare module 'mongoose' {
    interface DocumentQuery<T, DocType extends Document, QueryHelpers = Record<never,never>>  {
        cache(ttl?: number, customKey?: string): this
    }
    interface Aggregate<T> {
        cache(ttl?: number, customKey?: string): this
    }
}

declare module 'cachegoose' {
  import { DocumentQuery, Document, Mongoose } from 'mongoose'

  function cachegoose(mongoose: Mongoose, cacheOptions?: cachegoose.Types.IOptions): void

  namespace cachegoose {
      namespace Types {
          interface IOptions {
              engine?: string
              port?: number
              host?: string
          }
      }

      function clearCache(customKey: string, cb?: () => void): void
  }

  export = cachegoose
}

TellonUK avatar Jul 11 '20 17:07 TellonUK

Closing in favor of #63

she11sh0cked avatar Nov 04 '20 14:11 she11sh0cked

Actually no, I will reopen. There are still some problems with my typedef, but I think it's better. I will push some more commits later.

she11sh0cked avatar Nov 05 '20 13:11 she11sh0cked