TypeOrm query builder
14,361
Try to use getRawMany() instead of getMany() if you want to select some specified fields of your entity:
//I suppose you have a *grpId* variable in your function
const ids = await getRepository(Reg)
.createQueryBuilder('reg')
.select('reg.grpId', 'id')
.where('reg.grpId = :grpId', { grpId } )
.getRawMany();
console.log(ids);
return ids
}
Related videos on Youtube
Author by
A B
Updated on June 04, 2022Comments
-
A B almost 2 years
I can't figure out how to write the following MySql statement using TypeOrm Query Builder
SELECT reg.id FROM farm.reg WHERE grpId = 'ABC';
this select is returning just the id's but my query builder is returning the entire objects.
In this moment I have this function in my NestJs service but I need to use 'map' and I don't want to..
async getGrupIds(entity: string, grpId: string) { console.log(entity, grpId); const ids = await getRepository(reg) .createQueryBuilder(entity) .where('reg.grpId = :grpId', {grpId: grpId}) .getMany(); console.log(ids); return ids.map(o => o.id); }
Thank you
-
A B about 4 yearsasync getGroupIds(entity: string, grpId: string) { const ids = await getRepository(reg) .createQueryBuilder(entity) .select('reg.id') .where('reg.grpId = :grpId', {grpId: grpId}) .getRawMany(); console.log(ids); // return ids.map(o => o.id); return ids } works like a charm, many thanks!