Skip to content

SWC builder causes cacheFactory is always create a new instance of KeyV #756

Open
@yawhide

Description

@yawhide

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

Using swc as the compiler causes @nestjs/cache-manager's cache.provider.js' cachingFactory to always use the else case

function createCacheManager() {
    return {
        provide: cache_constants_1.CACHE_MANAGER,
        useFactory: async (options) => {
            const cachingFactory = async (store, options) => {
                if (store instanceof keyv_1.default) { // <--------- this line is always false even if i am passing in an instance of KeyV as the store
                    return store;
                }
                return new keyv_1.default({
                    store,
                    ttl: options.ttl,
                    namespace: options.namespace,
                });
            };
...

I am not sure why this is the case 🤔

What is unfortunate about this behaviour is I cannot follow the examples in the nestjs documentation. I have to find out the hard way on how to get @nestjs/cache-manager to use @keyv/redis properly.
What ends up happening is, even if your store is an instance of KeyV, it'll get wrapped by another instance KeyV and the options wont be passed in properly and wont use @keyv/redis as the underlying cache store

This is the nestjs example i am talking about:


import { Module } from '@nestjs/common';
import { CacheModule } from '@nestjs/cache-manager';
import { AppController } from './app.controller';
import { createKeyv } from '@keyv/redis';
import { Keyv } from 'keyv';
import { CacheableMemory } from 'cacheable';

@Module({
  imports: [
    CacheModule.registerAsync({
      useFactory: async () => {
        return {
          stores: [
            new Keyv({
              store: new CacheableMemory({ ttl: 60000, lruSize: 5000 }),
            }),
            createKeyv('redis://localhost:6379'),
          ],
        };
      },
    }),
  ],
  controllers: [AppController],
})
export class AppModule {}

Minimum reproduction code

https://github.com/yawhide/nestjs-cache-manager

Steps to reproduce

clone my repo
npm install

store !== keyv_1.default

npm run start:debug
put a breakpoint on line 21 in node_modules/@nestjs/cache-manager/dist/cache.providers.js (if (store instanceof keyv_1.default) {)

observe that this line will always be false.

store === keyv_1.default

delete "builder": "swc" in nest-cli.json

npm run start:debug
put a breakpoint on line 21 in node_modules/@nestjs/cache-manager/dist/cache.providers.js (if (store instanceof keyv_1.default) {)

observe that this line will be true

Expected behavior

i can use swc as the builder and when I pass in an instance of Keyv, @nestjs/manager wont wrap it in another instance of KeyV

Package version

3.0.1

NestJS version

11.1.0

Node.js version

20.19.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions