egon-orm-workspace / egon-orm / subquery
Function: subquery() ​
ts
function subquery<Q>(query): Source<SourceBindingOf<Q>, SourceRowOf<Q>, QueryParams<Q>>;Defined in: core/src/query/advanced/subquery.ts:46
A query as a source, for from or a join: (SELECT …) AS "alias". The binding exposes what the query selects: its keys when it selects an object of expressions, the schema's columns when it selects whole rows of one.
Type Parameters ​
Q ​
Q extends QueryLike
Parameters ​
query ​
Q & SourceCheck<Q>
Returns ​
Source<SourceBindingOf<Q>, SourceRowOf<Q>, QueryParams<Q>>
Example ​
ts
const postCounts = from(Post, "p").groupBy(({ p }) => p.authorId).select(({ p }) => ({ authorId: p.authorId, n: count() }));
from(subquery(postCounts), "c").where(({ c }) => c.n.gt(5));
from(User, "u").join(subquery(postCounts), "c", ({ u, c }) => c.authorId.eq(u.id));Throws ​
QueryError when the argument is not a query, or the query selects anything but an object of expressions or whole rows, or has preloads or set(…).

