Typeorm: Remap Raw Query

Created on 13 Nov 2017  路  3Comments  路  Source: typeorm/typeorm

How I remap raw query in typeorm?

TypeScript

class UserEntity {
   @PrimaryColumn({ name: "ID" })
   id:number;

   @Column({ name: "NAME" })
   name:string;
}

getManager().getRepository(UserEntity).query('SELECT ID, NAME FROM USER').then(retJSON => {
     console.log(retJSON[0]['id']);
});

getManager().getRepository(UserEntity).query('SELECT ID, NAME FROM USER').then((user:UserEntity ) => {
     console.log(user.id);
});

JAVA

Query query = entityManager.createNativeQuery("SELECT ID, NAME FROM USER", UserEntity.class);
UserEntity user = (UserEntity)query.getSingleResult();
question

Most helpful comment

import { plainToClass } from 'class-transformer';

export class Client{
    @Expose({ name: 'ID' })
    id: number;
}

export class User {
    @Expose({ name: 'ID' })
    id: number;
    @Expose({ name: 'NAME' })
    name: string;
    @Type(() => Client)
        client: Client;
}

let ret = await this.connection.query("SELECT ID, NAME, CLIENT.ID AS 'client.ID' FROM USER JOIN CLIENT ON CLIENT.ID = USER.ID;");
ret.forEach((e: any) => {
    Object.keys(e).forEach(key => {
        if (key.indexOf('.') != -1) {
            let nameStart = key.substring(0, key.indexOf('.'));
            let nameLast = key.substring(key.indexOf('.') + 1, key.length);
            if (!e[nameStart]) {
                e[nameStart] = {};
            }
            e[nameStart][nameLast] = e[key];
            delete e[key]
        }
    });
});

console.log(plainToClass(User, ret as Object[]));

/* JSON
[ 
    User {
    id: 1,
    name: 'test1',
    client:
     Client {
       id: 1,
     } 
    }
]

All 3 comments

If we are talking about remaping raw results into entity then its not possible. are you executing raw query and raw results are expected. You can map them into real entity by yourself. Raw results are rows which database returns you when you are executing sql query.

If we are talking about just write a plain sql query to get entities, its kinda not possible because query's method purpose is to return you raw results of your executed query. So, you have to use query builder functionality. TBH I don't see big value in adding "raw and limited sql to real entity support" when you can do same via query builder methods in a much more flexible and beautiful way.

Let me know if you have something to add...

import { plainToClass } from 'class-transformer';

export class Client{
    @Expose({ name: 'ID' })
    id: number;
}

export class User {
    @Expose({ name: 'ID' })
    id: number;
    @Expose({ name: 'NAME' })
    name: string;
    @Type(() => Client)
        client: Client;
}

let ret = await this.connection.query("SELECT ID, NAME, CLIENT.ID AS 'client.ID' FROM USER JOIN CLIENT ON CLIENT.ID = USER.ID;");
ret.forEach((e: any) => {
    Object.keys(e).forEach(key => {
        if (key.indexOf('.') != -1) {
            let nameStart = key.substring(0, key.indexOf('.'));
            let nameLast = key.substring(key.indexOf('.') + 1, key.length);
            if (!e[nameStart]) {
                e[nameStart] = {};
            }
            e[nameStart][nameLast] = e[key];
            delete e[key]
        }
    });
});

console.log(plainToClass(User, ret as Object[]));

/* JSON
[ 
    User {
    id: 1,
    name: 'test1',
    client:
     Client {
       id: 1,
     } 
    }
]
Was this page helpful?
0 / 5 - 0 ratings

Related issues

banduk picture banduk  路  3Comments

SPAHI4 picture SPAHI4  路  3Comments

shotor picture shotor  路  3Comments

Diluka picture Diluka  路  3Comments

idchlife picture idchlife  路  3Comments