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

fix types: define func expected to have only one argument #86

Merged
merged 2 commits into from
Oct 2, 2023
Merged

fix types: define func expected to have only one argument #86

merged 2 commits into from
Oct 2, 2023

Conversation

ahmetuysal
Copy link
Contributor

@ahmetuysal ahmetuysal commented Sep 23, 2023

Hi @mcollina, thanks for the amazing library. I was getting wrong results and weird cache keys and noticed I was using the library wrong since TypeScript types are incorrect.

Looking at the source code, it seems like func is expected to have a single argument called args. However, TS types are defined as if it is supporting multiple arguments and references option is getting an array of func parameters. Therefore, only the first argument is actually called and used in key calculations.

PS: I am really glad that you embraced TS :)

Here is my code for your reference:

@Injectable()
export class OrganizationMemberService {
  private readonly logger: Logger;
  private readonly cache;

  constructor(
    private prisma: PrismaService,
    @InjectRedis() private readonly redis: Redis,
  ) {
    this.logger = new Logger(OrganizationMemberService.name);
    this.cache = createCache({
      ttl: 60 * 60 * 24,
      storage: {
        type: 'redis',
        options: {
          client: redis,
          invalidation: { referencesTTL: 60 * 60 * 24 },
        },
      },
    }).define(
      'fetchOrganizationMember',
      {
        references: (args) => {
          this.logger.log(`references args: %o`, args);
          return [`organization-member~${args[0]}:${args[1]}`];
        },
      },
      async (
        organizationId: string,
        memberId: string,
      ): Promise<OrganizationMemberDto | null> => {
        const organizationMember =
          await this.prisma.organizationMember.findUnique({
            where: {
              organizationId_userId: {
                organizationId,
                userId: memberId,
              },
            },
          });

        this.logger.log(
          'Fetched organization member for organizationId: %s, memberId: %s: %o',
          organizationId,
          memberId,
          organizationMember,
        );

        return organizationMember
          ? new OrganizationMemberDto(organizationMember)
          : null;
      },
    );
  }

  async getOrganizationMember(
    organizationId: string,
    memberId: string,
  ): Promise<OrganizationMemberDto | null> {
    const organizationMember = await this.cache.fetchOrganizationMember(
      organizationId,
      memberId,
    );

    if (!organizationMember) {
      this.logger.log(
        'Organization member not found for organizationId: %s, memberId: %s',
        organizationId,
        memberId,
      );
    }

    return organizationMember;
  }
}

image

Copy link
Owner

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test for this type? We use tsd.

Copy link
Owner

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@simone-sanfratello simone-sanfratello merged commit f621991 into mcollina:main Oct 2, 2023
62 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants