#TypeORM

1 messages · Page 1 of 1 (latest)

slender shard
#

a query in sql brings me a result, which is correct, but I use the same query in typeorm with nest and it only brings me one of the three columns that I select, is it a bug? I have country.name, city.name and profile.name and it only returns the country.name but nevertheless the query in the same database brings me the result with the 3 columns

#

I can't call three columns in different tables with the same name "name" in typeorm?

prime finch
slender shard
#

this is the query

prime finch
#

Okay, but what does the typeorm return look like?

slender shard
#

async findUser(id: number) { try { this.logger.info('searching user by req.user.id', { repository: UserRepository.name, }); const row = await this.dataSource.query( 'SELECT profile.name, city.name, country.name FROM user INNER JOIN profile ON user.id = profile.userId INNER JOIN address ON profile.addressId = address.id INNER JOIN city ON address.cityId = city.id INNER JOIN country ON city.countryId = country.id where user.id = ?', [id], );

prime finch
#

Okay. That would most likely be because it doesn't know how to serialize name from each table. SQL is able to jsut say "Hey, this column is "name"" and you can refer back to the query. JSON doesn't have that ability, so TypeORM does

const obj = {};
obj.name = 'zazaza'
obj.name = 'Seattle'
obj.name = 'USA'

And the final output because of that is obj.name = 'USA'

#

Just a difference of lanagues.

#

Side note: you really should be aliasing those columns. For readability in the query result, and in the TypeO result

slender shard
#

Thank you very much for the explanation, I did not know that, so with a

#

SELECT column_name AS alias_name FROM table_name;

#

should i fix that mapping?

prime finch
#

I believe that will do it

slender shard
#

yep 😄 It is working!

slender shard
#

to format the response from the controller, the best solution is the interceptors?

prime finch
#

Depends. Do you just need it once? In the service or controller is fine. Do you want to everywhere and consistent with the rest of the application? Do it in an interceptor.

slender shard
#

Hi! I create a Interceptor but i know not how to map this object nested

#

I would like to map with the commented object in the first image but I don't know how to do it