Skip to content

PersistentAI API Documentation / @persistentai/fireflow-vfs / server / withAdvisoryLock

Function: withAdvisoryLock()

withAdvisoryLock<T>(db, lockKey, callback): Promise<T>

Defined in: packages/fireflow-vfs/src/db/locks.ts:40

Execute callback with PostgreSQL advisory lock. Lock is automatically released when transaction commits/rolls back.

Uses pg_advisory_xact_lock() which is transaction-scoped:

  • Lock is held until transaction ends (COMMIT or ROLLBACK)
  • No explicit unlock needed
  • Blocks until lock is available

Type Parameters

T

T

Parameters

db

DB

Drizzle database instance

lockKey

string

Unique string identifier for the lock

callback

() => Promise<T>

Function to execute while holding the lock

Returns

Promise<T>

Result of the callback

Example

typescript
// Ensure only one request creates a personal workspace
const workspace = await withAdvisoryLock(
  db,
  `personal-workspace:${userId}`,
  async () => {
    const existing = await findPersonalWorkspace(userId)
    if (existing) return existing
    return createPersonalWorkspace(userId)
  }
)

Licensed under BUSL-1.1