4 ms·
Postgres 9.3+ is fairly straight-forward too. Here is go + github.com/mgutz/dat // one trip to database using subqueries and Postgres' JSON functions c
by mmgutz 12y ago
Postgres 9.3+ is fairly straight-forward too. Here is go + github.com/mgutz/dat
// one trip to database using subqueries and Postgres' JSON functions
con.SelectDoc("id", "user_name", "avatar").
HasMany("recent_comments", `SELECT id, title FROM comments WHERE id = users.id LIMIT 10`).
HasMany("recent_posts", `SELECT id, title FROM posts WHERE author_id = users.id LIMIT 10`).
HasOne("account", `SELECT balance FROM accounts WHERE user_id = users.id`).
From("users").
Where("id = $1", 4).
QueryStruct(&obj) // obj must be agreeable with json.Unmarshal()
results in
{
"id": 4,
"user_name": "mario",
"avatar": "https://imgur.com/a23x.jpg",
"recent_comments": [{"id": 1, "title": "..."}],
"recent_posts": [{"id": 1, "title": "..."}],
"account": {
"balance": 42.00
}
}