admin管理员组文章数量:1332873
I’m trying to intercept and modify an SQL query in the beforeQuery event of a Subscriber in TypeORM to add custom subqueries based on the selected fields.
For example, I want to replace a totalPrice field with a custom SQL subquery:
@EventSubscriber()
export class BookingSubscriber implements EntitySubscriberInterface<Booking> {
listenTo() {
return Booking;
}
beforeQuery(event: BeforeQueryEvent<Booking>) {
let newQuery = event.query;
if (newQuery.includes('totalPrice')) {
// Example replacement
newQuery = newQuery.replace(
'totalPrice',
'(SELECT SUM(price) FROM booking_item WHERE booking_item.bookingId = booking.id) AS totalPrice',
);
}
event.query = newQuery;
}
}
However, I’ve noticed that the event object passed as a parameter seems to be a copy of the original one since the changes I make to event.query are not reflected in the final query executed by TypeORM. This results in an error indicating that the totalPrice field doesn’t exist in the database (because it wasn’t replaced in the final SQL).
本文标签:
版权声明:本文标题:typescript - Is it possible to modify the SQL query in the beforeQuery event of a Subscriber in TypeORM? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742312248a2451104.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论