UNPKG

syncguard

Version:

Functional TypeScript library for distributed locking across microservices. Prevents race conditions with Redis, PostgreSQL, Firestore, and custom backends. Features automatic lock management, timeout handling, and extensible architecture.

46 lines (45 loc) 1.59 kB
import type { Sql } from "postgres"; import type { AcquisitionOptions, LockConfig } from "../common/types.js"; import type { PostgresBackendOptions } from "./types.js"; /** * PostgreSQL backend for SyncGuard distributed locking. * * Uses postgres.js (porsager/postgres) for transaction-based locking * with table storage, server-side time authority, and fence tokens. * * @module syncguard/postgres */ /** * Creates distributed lock with PostgreSQL backend via postgres.js client. * * IMPORTANT: Call setupSchema() once before creating locks to set up schema. * * @param sql - postgres.js SQL instance * @param options - Table names and cleanup config * @returns Auto-managed lock function (see: common/auto-lock.ts) * * @example * ```typescript * import postgres from 'postgres'; * import { createLock, setupSchema } from 'syncguard/postgres'; * * const sql = postgres('postgresql://localhost:5432/myapp'); * * // Setup schema (once, during initialization) * await setupSchema(sql); * * // Create lock function (synchronous) * const lock = createLock(sql); * * // Use lock * await lock(async () => { * // Your code here * }, { key: 'my-lock', ttlMs: 30_000 }); * ``` */ export declare function createLock(sql: Sql, options?: PostgresBackendOptions): <T>(fn: () => Promise<T> | T, config: LockConfig & { acquisition?: AcquisitionOptions; }) => Promise<T>; export { createPostgresBackend } from "./backend.js"; export { setupSchema } from "./schema.js"; export type { PostgresBackendOptions, PostgresCapabilities, PostgresConfig, } from "./types.js";