-
-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question on composing queries together #21
Comments
Not supported yet, but I'm taking this to the development plan. |
This feature is ready now, update the packages to check it out. Added method With the tables defined in the related issues, this works: const accounts = db.accounts
.select({
cover: q => q.cover.selectAll(),
num_posts: q => q.account_posts.count().whereNotExists(db.commentsOnPosts, "comment_id", "=", "id"),
num_comments: q => q.account_posts.count().whereExists(db.commentsOnPosts, "comment_id", "=", "id"),
num_posts_liked: q => q.account_likes.count().whereNotExists(db.commentsOnPosts, "comment_id", "=", "post_id"),
num_comments_liked: q => q.account_likes.count().whereExists(db.commentsOnPosts, "comment_id", "=", "post_id"),
})
.limit(10);
console.log(
await db.$from(accounts).where({ num_posts_liked: { gt: 0 } })
)
await db.$close() |
Is there any way to provide an alias to $from as well? I notice that $from by default aliases the subquery by the table it references from. Also, does {left,right,inner,outer}[j|J]oin() support subqueries as well? |
Yes, there is a separate method await db.$from(accounts).as('t').where(...) Here it's aliased as
Do you mean to get something like this? SELECT * FROM table
JOIN ( SELECT * FROM other ) t ON true No, currently not. |
Got it 👍 should I make a separate issue for having subqueries supported for joins? |
Yeah, make it please |
Seem to run into an error about type instantiation being too deep here: error TS2589: Type instantiation is excessively deep and possibly infinite.
12 const results2 = await db.$from(results).as("accounts_view").selectAll();
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ const results = db.accounts.select("id", "publicKey", "name", "alias", "bio", "createdAt", "updatedAt", "lastSeenAt", "isVerified")
.select({
numPosts: q => db.posts.count().whereNotExists(db.commentsOnPosts, { commentId: "id" }).where(q.raw("posts.author_id = accounts.id")),
numComments: q => db.posts.count().whereExists(db.commentsOnPosts, { commentId: "id" }).where(q.raw("posts.author_id = accounts.id")),
});
const results2 = await db.$from(results).as("accounts_view").selectAll();
console.log(results2); |
Suppose I have the following base query:
I am attempting to represent the following query:
My attempt at building this query is as so, though is incorrect:
The text was updated successfully, but these errors were encountered: