BE/src/mssql/mssql.service.ts

30 lines
862 B
TypeScript
Raw Normal View History

2025-04-07 12:28:39 +05:30
import { Injectable } from '@nestjs/common';
2025-04-07 15:39:23 +05:30
import { Connection, Request } from 'mssql';
2025-04-07 12:28:39 +05:30
import { MssqlDBService } from 'src/db/db.service';
@Injectable()
export class MssqlService {
constructor(private readonly mssqlDBService: MssqlDBService) { }
async checkConnection() {
2025-04-07 17:10:59 +05:30
let connection : Connection;
2025-04-07 12:28:39 +05:30
try {
connection = await this.mssqlDBService.getConnection();
const request = new Request(connection);
const result = await request.query('SELECT GETDATE() AS CurrentDateTime');
return { data: result.recordset[0].CurrentDateTime }
} catch (error) {
console.error('Error checking connection:', error);
2025-04-07 17:10:59 +05:30
return {error:error.message}
2025-04-07 15:39:23 +05:30
} finally {
if (connection) {
connection.close();
}
}
}
2025-04-07 12:28:39 +05:30
}