TypeORM return plain object TypeORM return plain object database database

TypeORM return plain object


You can easily use the class-transformer library to produce plain objects from entity instances:

https://github.com/typestack/class-transformer

const user = repository.findOne({ where: { id: 1 } });const plain = classToPlain(user);

Another way is to use queryBuilder API and returns raw results from the database

const rawResult = repository.createQueryBuilder("user")    .select("user")    .where("user.id = :id", {id: 1})    .getRawOne();

The point is why would you want to do so? Entity is first citizen in TypeOrm so you want that any change on its structure should generate compile errors if something is not back compatible. Using plain objects would hide contract changes, and you would discover errors only at runtime, not sure if it is an optimal way to work with TypeOrm to my mind.