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.

34 lines (33 loc) 1.09 kB
import type { Sql } from "postgres"; /** * Sets up required schema (tables and indexes) for PostgreSQL lock backend. * * Creates (if not exist): * - Lock table with primary key on 'key' and indexes on 'lock_id' and 'expires_at_ms' * - Fence counter table with primary key on 'fence_key' * * This is an idempotent operation and safe to call multiple times. * Call this once during application initialization, before creating lock backends. * * @param sql - postgres.js SQL instance * @param options - Optional configuration for table names * @returns Promise that resolves when schema is created * * @example * ```typescript * import postgres from 'postgres'; * import { setupSchema, createLock } from 'syncguard/postgres'; * * const sql = postgres('postgresql://localhost:5432/myapp'); * * // Setup phase (once, during initialization) * await setupSchema(sql); * * // Usage phase (synchronous) * const lock = createLock(sql); * ``` */ export declare function setupSchema(sql: Sql, options?: { tableName?: string; fenceTableName?: string; }): Promise<void>;