mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-27 16:24:53 +00:00
Add PowerClamp module with controller and service for energy data retrieval
This commit is contained in:
28
libs/common/src/helper/services/sql-loader.service.ts
Normal file
28
libs/common/src/helper/services/sql-loader.service.ts
Normal file
@ -0,0 +1,28 @@
|
||||
import { SQL_QUERIES_PATH } from '@app/common/constants/sql-query-path';
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { readFileSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
|
||||
@Injectable()
|
||||
export class SqlLoaderService {
|
||||
private readonly logger = new Logger(SqlLoaderService.name);
|
||||
private readonly sqlRootPath = join(__dirname, '../sql/queries');
|
||||
|
||||
loadQuery(module: string, queryName: string): string {
|
||||
const filePath = join(
|
||||
process.cwd(),
|
||||
SQL_QUERIES_PATH,
|
||||
module,
|
||||
`${queryName}.sql`,
|
||||
);
|
||||
try {
|
||||
return readFileSync(filePath, 'utf8');
|
||||
} catch (error) {
|
||||
this.logger.error(
|
||||
`Failed to load SQL query: ${module}/${queryName}`,
|
||||
error.stack,
|
||||
);
|
||||
throw new Error(`SQL query not found: ${module}/${queryName}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user