I'm diving into the world of typeORM and Typescript. I've created a service class to handle a specific resource:
import { Injectable} from '@nestjs/common';
import { getManager } from 'typeorm';
class MyServiceClass {
static readonly sqlStr = `SELECT MAINTABLE.* WITH LEFT OUTER JOIN WITH TWO MORE TABLES`;
async findAll(limit?: number) {
const mgr = getManager();
const retRows = await mgr.query(
`
${MyServiceClass.sqlStr}
${limit ? `FETCH NEXT ${limit} ROWS ONLY` : ''}
`,
);
return retRows;
}
async findOne(id: number) {
const mgr = getManager();
const retRow = await mgr.query(
`
${MyServiceClass.sqlStr}
${id ? ` WHERE MAINTABLE.MAIN_ID = ${id}` : ''}
`,
);
return retRow;
}
}
I'm looking to streamline my code by removing the SQL statement from each function and utilizing the MyServiceClass.sqlStr property instead. Can anyone provide guidance on how to achieve this?