@yarlisai/idempotency
Idempotent-operation primitives — atomic key claiming, result replay, TTL expiry, and cleanup.
Idempotent-operation primitives for webhooks, pollers, and any operation that must run exactly once — atomic key claiming, result storage and replay, retry release, TTL expiry, and expired-key cleanup.
Install
npm install @yarlisai/idempotencySource: packages/idempotency ·
npm ·
CHANGELOG
Why
@yarlisai/idempotency follows the port/adapter contract: consumers depend on the IdempotencyProvider port and instantiate an adapter at runtime. Two adapters ship at v1:
postgres— drizzle-backed, with an optional Redis-compatible cache fast path. The caller injects its owndb,idempotency_keytable reference, andand/eq/ltoperators, so the package never bundles a second copy ofdrizzle-ormorioredis.memory— in-process Map with TTL, for tests and single-instance deployments.
Usage
import { createIdempotencyClient } from '@yarlisai/idempotency'
import { and, eq, lt } from 'drizzle-orm'
const webhookIdempotency = createIdempotencyClient({
adapter: 'postgres',
db,
table: idempotencyKey,
operators: { and, eq, lt },
namespace: 'webhook',
ttlSeconds: 60 * 60 * 24 * 7,
})
const claim = await webhookIdempotency.atomicallyClaim('stripe', event.id)
if (!claim.claimed) return // duplicate deliveryThe package's README ships a complete quickstart. mybotbox-platform itself is the reference consumer — apps/sat/lib/idempotency/ shows how every callsite uses the port instead of reaching the database directly.