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

NestJS v9 incompatible with version 5 #210

Closed
rurruur opened this issue Sep 30, 2022 · 35 comments
Closed

NestJS v9 incompatible with version 5 #210

rurruur opened this issue Sep 30, 2022 · 35 comments
Labels

Comments

@rurruur
Copy link

rurruur commented Sep 30, 2022

i get this error

TypeError: store.get is not a function
    at Object.get (.../node_modules/cache-manager/src/caching.ts:88:36)
    at AppController.getHello

Im implementing caching following the NestJS docs.

this is my posting on stackoverflow (https://stackoverflow.com/questions/73908197/typeerror-store-get-is-not-a-function-nestjs-cache-manager)

but when im switch to version4 then its working.. but i dont know why.

i wonder this is just my fault or version issue

-> i try to find difference but i think get method's logic is same(using lru-cache package). so maybe type issue?

if this is my fault, then can you tell me how to solve it?

@gustav1105
Copy link

I have the same issue

@Notalifeform
Copy link

same here

@denernun
Copy link

same here...back to version @4.1.0 and wait

@davidzwa
Copy link

The cache construction is now async. Thats why you need to await it.

I also have issues with this: dependency injection with both Awilix and NestJS is breaking. Constructors should be sync.

Why is this Promise based now? Can this be avoided or made sync as fallback for users who don't have the luxury of awaiting?

@udisun
Copy link

udisun commented Oct 2, 2022

+1 for a fix for this

@jmcdo29
Copy link

jmcdo29 commented Oct 3, 2022

After some investigation, it appears the caching method's signature has changed to now accept a factory as the first parameter and options as the second vs the old single options object that included the store. Nest will need to update some code to support v5, but I make no guarantees if we'll concurrently support v4 and v5 or just v4 until Nest v10 is released.

This change was rather hard to find as the old lib/caching and the new src/caching have different git histories and there's no changelog explicitly mentioning this change

@not-Ryan
Copy link

not-Ryan commented Oct 3, 2022

My fix for NestJS was to downgrade to "cache-manager": "^4.0.0",.

This is also forced by @micalevisk 's PR here.

@WaylonOKC
Copy link

+1 for a fix.

I believe this is the commit that broke everything. A "Breaking Changes" should have been issued.

@zzau13
Copy link
Contributor

zzau13 commented Oct 3, 2022

Sorry for the delay. It is that there are breaking changes that have not been introduced in the NestJS documentation because the version is very early.

Now I put an example of how to implement it with the new version while it is being updated in NestJS.

@BryanDonovan
Copy link
Contributor

I'm no longer the maintainer of this project, but:

  1. The major version was revved from 4 to 5, which implies that there is (or could be) a breaking change.
  2. Users should specify a version (or at least a version range) in their projects to prevent breaking changes from causing problems.

But I also agree that the constructors should not be async.

@jmcdo29
Copy link

jmcdo29 commented Oct 3, 2022

@WaylonOKC as this was over a semver major version, a breaking change was fine. It's just that until now, Nest's peerDeps were okay with any version of node-cache-manager. Therefore, no "fix" is necessary from this library. I'll be working on an update for Nest to figure out if support ing v4 and v5 simultaneously is possible

@zzau13
Copy link
Contributor

zzau13 commented Oct 3, 2022

The problem is:

https://github.com/nestjs/nest/blob/746533214ea1f3ad1aff1d13fd72d293a30d1fa9/packages/common/cache/cache.providers.ts#L30-L33

I have changed the definition of the function caching and multicaching. It can't work with both versions.

Specify in the NestJS documentation and its dependencies.

I already have the commit to update NestJS but it won't take long until they release a version.

I'll leave this open if anyone has the same problem:

npm i cache-manager@^4

@zzau13 zzau13 changed the title i get type error with version 5 NestJS v9 incompatible with version 5 Oct 3, 2022
@zzau13 zzau13 added the question label Oct 3, 2022
@riboher
Copy link

riboher commented Oct 4, 2022

Any chance of adding a bit of documentation on how to achieve the same results with the new version? I too am trying to update my code to v5 but following just the commit messages seems a bit confusing and I'm getting similar type errors when using the caching function.

Not complaining, just would like to know if we could have a thorough guide on how to update from one version to another.

Thanks!

@zzau13
Copy link
Contributor

zzau13 commented Oct 4, 2022

Any chance of adding a bit of documentation on how to achieve the same results with the new version? I too am trying to update my code to v5 but following just the commit messages seems a bit confusing and I'm getting similar type errors when using the caching function.

Not complaining, just would like to know if we could have a thorough guide on how to update from one version to another.

Thanks!

Ok, I'll make a guide. But this is what I did to migrate this Store. Other Store implementations may not have updated types

https://github.com/node-cache-manager/node-cache-manager-redis-yet/commit/3e25560765759af00005f12203ce2b594800c208#diff-a2a171449d862fe29692ce031981047d7ab755ae7f84c707aef80701b3ea0c80

@WaylonOKC
Copy link

@WaylonOKC as this was over a semver major version, a breaking change was fine. It's just that until now, Nest's peerDeps were okay with any version of node-cache-manager. Therefore, no "fix" is necessary from this library. I'll be working on an update for Nest to figure out if support ing v4 and v5 simultaneously is possible

Breaking changes are often expected by a major version, but they definitely should be called out and not just left to be discovered after people upgrade.

FWIW, this thread started out as a "TypeError" and not specifically a NestJS incompatibility.

@srinivasan-getstan
Copy link

Hi Team,
Any update?

@jmcdo29
Copy link

jmcdo29 commented Oct 10, 2022

@srinivasan-getstan there's nothing really to update. botika was kind enough to provide an example of how to use cache-manager@^5 with Nest if you want to use that until a stable fix gets merged in with Nest's code base. Otherwise, v5 s a breaking change and you should use v4 for the old functionality. ThisPR should fix any usage issues between v4 and v5, at least in the tests I've done when creating it, but we'll have to wait for it to get merged in and published, which may take a bit. Until then, as I said, there's nothing cache-manager needs to do about this, they correctly released a major change, which can include breaking changes, and that's that.

@srinivasan-getstan
Copy link

Thanks, @jmcdo29

@zzau13
Copy link
Contributor

zzau13 commented Oct 10, 2022

Hi Team, Any update?

Basically, I did 12020d8 and remove it.
I would use version 4 until nest is updated or you can use that module. There are also not many updated Stores yet.

@HappyHappy1996
Copy link

node-cache-manager version 5 does not work with Nest version 8 either

@vincentwinkel
Copy link

For the ones using cache-manager-redis-store, you also need to downgrade it to v2.0.0 (instead of 3.0.1).

@techleaddev
Copy link

I'm getting the same error and it looks like it hasn't been updated yet !

@eso32
Copy link

eso32 commented Dec 30, 2022

If you are coming here from the NestJS caching Redis tutorial, use these dependencies:

"cache-manager": "4.1.0",
"cache-manager-redis-store": "2.0.0",

@vincentwinkel thanks

@adworacz
Copy link

adworacz commented Jan 4, 2023

I can confirm that using the latest NestJS version (9.2.1) solves this issue for me. Looks like anything north of 9.1.3 should address the issue, which is when this was merged: nestjs/nest@4eb5fb0

@zzau13
Copy link
Contributor

zzau13 commented Jan 4, 2023

Fixed in nest@9.2.0

Thanks all!!

@zzau13 zzau13 closed this as completed Jan 4, 2023
EmileCalixte added a commit to EmileCalixte/nestjs-openfoodfacts that referenced this issue Jan 23, 2023
@andarocks
Copy link

Hi. This still does not work for me even after updating it to 9.2.0.
Below is the code for initiating CacheModule in app.module.ts:

CacheModule.register<RedisClientOptions>({
     isGlobal: true,
     store: redisStore,
     host: 'localhost',
     port: 6379,
   }),

Versions:
cache-manager: 5.1.6
redis:3.1.2
cache-manager-redis-store:3.0.1

@Adherentman
Copy link

Adherentman commented Feb 23, 2023

This is work for me.
redis: "^3.1.2"
cache-manager: "^5.1.3"
cache-manager-redis-store: "^3.0.1"

 CacheModule.registerAsync<any>({
      isGlobal: true,
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: async (configService: ConfigService) => {
        const redisConfig = configService.get<RedisConfig>('redis');
        const storeConfig: CacheModuleOptions<RedisClientOptions<RedisModules, RedisFunctions, RedisScripts>> = {
          url: `redis://${redisConfig.host}:6379`,
        };
        if (redisConfig.auth) {
          storeConfig.password = redisConfig.auth;
        }
        const store = await redisStore(storeConfig);
        return { store: () => store };
      },
    }),

It should be noted that
this.cacheManager.set(key, value, { ttl } as any);

@zzau13
Copy link
Contributor

zzau13 commented Feb 23, 2023

cache-manager-redis-store: "^3.0.1"

Is not updated. Use https://github.com/node-cache-manager/node-cache-manager#official-and-updated-to-last-version instead

@negreanucalin
Copy link

negreanucalin commented Feb 27, 2023

Hello, I'm having the original issue of @rurruur

TypeError: store.get is not a function

My setup:

import * as redisStore from 'cache-manager-redis-store';

CacheModule.registerAsync({
    imports: [ConfigModule],
    inject: [ConfigService],
    isGlobal: true,
    useFactory: async (configService: ConfigService) => {
        console.log({...configService.get('redis')}); //  Output: { host: 'localhost', port: '6379', password: undefined }
        return {
            store: redisStore as unknown as CacheStore, // Hotfix
            ...configService.get('redis'),
        };
    },
}),

import { CACHE_MANAGER, Inject, Injectable } from '@nestjs/common';
import { Cache } from 'cache-manager';
@Injectable()
export class UsersService {
    constructor(
        private userRepository: UserRepository,
        @Inject(CACHE_MANAGER) private cacheManager: Cache,
    ) {}
    async findAll(): Promise<User[]> {
        console.log(this.cacheManager.get);                                                           // Output: [Function: get]
        const value: User[] | undefined = await this.cacheManager.get('find_all'); // Here I get : TypeError: store.get is not a function
        if (value) {
            console.log('From cache');
            return value;
        }
        const dbList: User[] = await this.userRepository.find({
            relations: { oncall_request: true },
            order: { first_name: 'ASC', last_name: 'ASC' },
        });
        await this.cacheManager.set('find_all', dbList, 3000);
    }

Backend:
cache-manager: 5.1.7,
cache-manager-redis-store: 3.0.1
nest: 9.3.9

Other:
platform: Windows
redis: 7.0.8
Node: v16.19.1

Update

  1. Using these version everything works (I can see data in Redis-cli) :
"cache-manager": "4.1.0",
"cache-manager-redis-store": "2.0.0"

@zzau13
Copy link
Contributor

zzau13 commented Feb 28, 2023

cache-manager-redis-store: "^3.0.1"

Is not updated. Use https://github.com/node-cache-manager/node-cache-manager#official-and-updated-to-last-version instead

The awser is in the last post. And install Linux, archlinux, https://wiki.archlinux.org/title/installation_guide

@devMarcos24
Copy link

devMarcos24 commented Mar 28, 2023

to resolve that i need to change the "cache-manager": "^4.0.0", and if you use redis "cache-manager-redis-store": "2.0.0", this resolved from me

@suoncha
Copy link

suoncha commented Apr 23, 2023

I managed to solve this with:

"@nestjs/common": "^9.4.0",
"cache-manager": "^5.2.0",
"cache-manager-redis-yet": "^4.1.1",

My module file:

import { redisStore } from 'cache-manager-redis-yet';
import { Module, Global } from '@nestjs/common';
import { CacheModule } from '@nestjs/cache-manager';
import { ConfigModule, ConfigService } from '@nestjs/config';

import { RedisService } from './redis.service';

@Global()
@Module({
  imports: [
    CacheModule.registerAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: async (configService: ConfigService) => ({
        store: await redisStore({
          url: configService.get('REDIS_URL'),
          database: 0,
        }),
      }),
    }),
  ],
  providers: [RedisService],
  exports: [RedisService],
})
export class RedisModule {}

My provider file:

import { Inject, Injectable } from '@nestjs/common';
import { CACHE_MANAGER } from '@nestjs/cache-manager';
import { Cache } from 'cache-manager';

@Injectable()
export class RedisService {
  constructor(@Inject(CACHE_MANAGER) private readonly cache: Cache) {}

  async get(key: any) {
    return await this.cache.get(key);
  }

  async set(key: any, value: any, ttl: number) {
    await this.cache.set(key, value, ttl * 1000); // from cache-manager v5 they use milliseconds instead of seconds
  }

  async del(key: any) {
    await this.cache.del(key);
  }
}

You are welcome.

@zzau13
Copy link
Contributor

zzau13 commented Apr 23, 2023

My cache.module.ts

import { Module } from '@nestjs/common';
import { CacheModule as _CacheModule } from '@nestjs/cache-manager';
import { ConfigModule, ConfigService } from '@nestjs/config';

import { RedisClientType } from 'redis';
import { redisInsStore } from 'cache-manager-redis-yet';

import { REDIS, RedisModule } from '@app/redis';
import { CacheService } from './cache.service';

@Module({
  imports: [
    _CacheModule.registerAsync({
      imports: [ConfigModule, RedisModule],
      inject: [ConfigService, REDIS],
      useFactory: (config: ConfigService, redis: RedisClientType) => {
        const conf = {
          ttl: config.getOrThrow<number>('REDIS_CACHE_TTL'),
        };
        return {
          store: redisInsStore(redis, conf),
          ...conf,
        };
      },
    }),
  ],
  providers: [CacheService],
  exports: [_CacheModule, CacheService],
})
export class CacheModule {}

My cache.service.ts:

import { Inject, Injectable } from '@nestjs/common';
import { CACHE_MANAGER } from '@nestjs/cache-manager';

import { RedisCache } from 'cache-manager-redis-yet';
@Injectable()
export class CacheService {
  constructor(@Inject(CACHE_MANAGER) public readonly cache: RedisCache) {}

  async clean(pattern: string) {
    for await (const key of this.cache.store.client.scanIterator({
      MATCH: pattern,
    })) {
      await this.cache.store.client.unlink(key);
    }
  }
}

Thanks all for use it!

@oooredooo-bit
Copy link

@botika may I know if there's a repo of that code you sent above? I would like to check it. Thanks.

bertyhell added a commit to viaacode/react-admin-core-module that referenced this issue Jul 18, 2023
@huboh
Copy link

huboh commented Jul 27, 2023

wrote a fix for newer version of cache-manager(v5 and above) on stackoverflow, it should help https://stackoverflow.com/a/76777854/16362460

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests