Mapping raw query to entities
See original GitHub issueIssue type:
[ ] bug report [x] feature request [ ] documentation issue
Database system/driver:
[ ] cordova
[ ] mongodb
[ ] mssql
[ ] mysql
/ mariadb
[ ] oracle
[x] postgres
[ ] cockroachdb
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo
TypeORM version:
[ ] latest
[x] @next
[ ] 0.x.x
(or put your version here)
Steps to reproduce or a small repository showing the problem:
Hello, I’ve been using typeorm for a while and I like it a lot, working with entities adds a lot of convenience.
I use a transformer on my entity classes to conveniently map values when reading/writing from the DB, however this only works if entities are obtained from the query builder or entity manager.
Right now there is no way to simply map a raw query to entities, however sometimes raw queries are a valuable escape hatch that will always be required in some cases, some being more common than others.
Right now I need the UNION
operator which is fairly commonly used in SQL but not supported by typeorm (#2992).
I can use Object.assign
or similar ways to manually map rows (like #1175), but this won’t apply my transformers (and possibly other typeorm specific stuff declared by annotations).
I suggest exposing some API which would at least allow for mapping a single row to an entity so that there is some way to keep using typeorm annotations while still being able to use unsupported SQL.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:15
- Comments:16 (2 by maintainers)
Top GitHub Comments
I think this is a necessary feature. There isn’t any manner to query a huge quantity of rows. The single way to perform that is to use stream but if I haven’t mapped Entity what is the advantage to use ORM? It’s a standard feature in many ORMs for instance Prisma. Please think to implement it.
TypeORM has
RawSqlResultsToEntityTransformer
which can help with this mapping. The catch is that you need to pass aQueryExpressionMap
instance to it which might be difficult to build manually.However if you are able to define all your selected columns and joins etc using
QueryBuilder
and then modify the resulting query with your custom SQL requirements, then this will provide theQueryExpressionMap
for you. I would imagine aUNION
would be an applicable use case here if you can build at least one of the component queries usingQueryBuilder
and use string manipulation to combine them into aUNION
.For example in my use case I needed to use a locking mode in SQL Server that is not currently supported by TypeORM. I was able to create my query using
QueryBuilder
and then modify the lock expression in the SQL before executing it as a raw query. Then transform that into entities usingRawSqlResultsToEntityTransformer
.Here is the code: