import { MongoClient, MongoClientOptions } from 'mongodb'; import { WorkflowLock, StoreSaveInput, StoreLoadResult, ResumeState } from 'awaitly/durable'; export { SnapshotStore, StoreLoadResult, StoreSaveInput, WorkflowLock, WorkflowSnapshot } from 'awaitly/durable'; /** * MongoDB workflow lock (lease) for cross-process concurrency control. * Uses a lease (TTL) + owner token; release verifies the token. */ interface MongoLockOptions { /** * Collection name for workflow locks. * @default 'workflow_lock' */ lockCollectionName?: string; } /** * awaitly-mongo * * MongoDB persistence adapter for awaitly workflows. * Provides ready-to-use SnapshotStore backed by MongoDB. * Supports both WorkflowSnapshot and ResumeState (serialized via serializeResumeState). */ /** * Options for the mongo() shorthand function. */ interface MongoOptions { /** MongoDB connection URL. */ url: string; /** Database name. @default 'awaitly' */ database?: string; /** Collection name for snapshots. @default 'awaitly_snapshots' */ collection?: string; /** Key prefix for IDs. @default '' */ prefix?: string; /** Bring your own client. */ client?: MongoClient; /** MongoDB client options. */ clientOptions?: MongoClientOptions; /** Cross-process lock options. When set, the store implements WorkflowLock. */ lock?: MongoLockOptions; } /** Mongo store with widened save/load for WorkflowSnapshot and ResumeState. Compatible with SnapshotStore for snapshot-only usage. */ interface MongoStore extends Partial { save(id: string, state: StoreSaveInput): Promise; load(id: string): Promise; loadResumeState(id: string): Promise; delete(id: string): Promise; list(options?: { prefix?: string; limit?: number; }): Promise>; close(): Promise; } /** * Create a snapshot store backed by MongoDB. * Save accepts WorkflowSnapshot or ResumeState; load returns whichever was stored. * Use loadResumeState(id) for type-safe restore, or toResumeState(await store.load(id)). * * @example * ```typescript * import { mongo } from 'awaitly-mongo'; * import { createWorkflow } from 'awaitly'; * * const store = mongo('mongodb://localhost:27017/mydb'); * const workflow = createWorkflow(deps); * * // Run and persist resume state * const { result, resumeState } = await workflow.runWithState(fn); * await store.save('wf-123', resumeState); * * // Restore * const resumeState = await store.loadResumeState('wf-123'); * if (resumeState) await workflow.run(fn, { resumeState }); * ``` * * @example * ```typescript * // With options including cross-process locking * const store = mongo({ * url: 'mongodb://localhost:27017', * database: 'myapp', * collection: 'my_workflow_snapshots', * prefix: 'orders:', * lock: { lockCollectionName: 'my_workflow_locks' }, * }); * ``` */ declare function mongo(urlOrOptions: string | MongoOptions): MongoStore; export { type MongoLockOptions, type MongoOptions, type MongoStore, mongo };