tusken icon indicating copy to clipboard operation
tusken copied to clipboard

Nested JSON objects/arrays from subqueries

Open aleclarson opened this issue 3 years ago • 1 comments

db.select(
  t.tweet(tweet => ({
    // Many-to-one relation
    author: db.get(t.user, tweet.author),
    // Many-to-many relation
    hashtags: db.select(t.hashtag).where(hashtag => hashtag.id.in(tweet.hashtags)),
    // One-to-many relation
    likes: db.select(t.like).where(like => like.tweet.eq(tweet.id)),
  }))
)

That query would produce an array of objects with the following shape:

const result = {
  author: { ... },
  hashtags: [ { ... }, ... ],
  likes: [ { ... }, ... ],
}

aleclarson avatar Sep 05 '22 15:09 aleclarson

Marking this as low priority, since #14 covers the most poignant use cases.

aleclarson avatar Sep 09 '22 14:09 aleclarson