import { DataSource, QueryRunner } from 'typeorm';

export async function withQueryRunner<T>(
  dataSource: DataSource,
  fn: (runner: QueryRunner) => Promise<T>,
): Promise<T> {
  const runner = dataSource.createQueryRunner();
  await runner.connect();
  try {
    return await fn(runner);
  } finally {
    await runner.release();
  }
}
