yxx4c / prisma-extension-redis

Extensive Prisma extension designed for efficient caching and cache invalidation using Redis and Dragonfly Databases
https://npmjs.com/package/prisma-extension-redis
MIT License
34 stars 2 forks source link

EXECABORT Transaction discarded because of previous errors. #20

Open moshOntong-IT opened 2 hours ago

moshOntong-IT commented 2 hours ago
error: EXECABORT Transaction discarded because of previous errors. {"command":{"args":[],"name":"exec"},"previousErrors":[{"command":{"args":["emerald_api*user*op*find_unique*key*66947619"],"name":"JSON.GET"}}]}

Code:

 .$extends(
    PrismaExtensionRedis({
      config: {
        type: "JSON",
        ttl: 60,
        stale: 30,
        logger,
        cacheKey: {
          case: CacheCase.SNAKE_CASE,
          delimiter: "*",
          prefix: "emerald_api",
        },
        auto: {},
      },
      client: {
        host: "redis",
        port: parseInt(Bun.env.APP_REDIS_PORT!),
        password: Bun.env.REDIS_PASSWORD!,
      },
    })
  );
yxx4c commented 1 hour ago

Hii @moshOntong-IT,

Could you please share the query that triggered this error? Thank you!

kalebheitzman commented 4 minutes ago

I'm getting the same error when I use autocaching on the findMany method. I've noticed my model returns undefined instead of null on a cache miss and have had to implement a retry method into my model to get around this at the moment:

export async function getAssessmentsForUser(
  userId: number,
  maxRetries = 3
): Promise<AssessmentWithScenario[]> {
  const customKey = prisma.getAutoKey({
    args: { where: { userId: userId } },
    model: "Assessment",
    operation: "findMany",
  });

  let retries = 0;
  while (retries < maxRetries) {
    try {
      let assessments: AssessmentWithScenario[] =
        await prisma.assessment.findMany({
          where: { userId: userId },
          orderBy: { id: "asc" },
          cache: { ttl: 5, key: customKey },
        });

      assessments = await Promise.all(
        assessments.map(async (assessment) => {
          assessment.scenario = await getScenarioByKey(
            assessment.scenarioKey || ""
          );
          return assessment;
        })
      );

      return assessments;
    } catch (error) {
      retries++;
    }
  }

  throw new Error("Failed to fetch assessments after multiple retries");
}