SyedNaqiRizvi/tedious-async

View on GitHub
src/extension-functions/connection-operations/execSqlAsync.ts

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
import { Request } from 'tedious';
import Connection from '../../index';
import { DbRow, Dictionary } from '../../types';
import { mapSQLRows } from '../../utils/sqlToJsonUtils';
import { execSqlAsyncOptions } from '../types';

const execSqlAsync = (connection: Connection) => (
  sqlString: string,
  options?: execSqlAsyncOptions,
): Promise<Dictionary[] | Error> =>
  new Promise((resolve, reject) => {
    const request = new Request(
      sqlString,
      (error: Error, rowCount: number, rows: DbRow[]) => {
        if (error) {
          reject(error);
        }
        resolve(mapSQLRows(rows, options));
      },
    );
    connection.execSql(request);
  });

export default execSqlAsync;