mercredi 30 mars 2016

Sqlite Many to Many for Restful Response

I have a books, authors, book_author_link table (since the book to author relationship is many to many), and reviews table:

book_table  
id name isbn   
1  book1 12345  
2  book2 12346  

author_table  
id name  
1  john  
2  joe  

book_author_link  
id author_fk book_fk  
1   1          1    
2   1          2  
3   2          1  
4   2          2  

reviews_table  
id  book_fk url  
1   1       'url1'   
2   1       'url2'  
3   2       'url3'  

I want to prepare a response, such that when someone wants /books/1 they get something like:
{
id: 1,
name: book1,
isbn: 12345,
authors: [{id: 1, name:john},{id: 2, name: joe}],
reviews: [{id: 1, url:'url1'}, {id: 2, url:'url2'}]
}

I'm wondering what the best way to query this is. Is it best to just do multiple queries? I've seen some answers related, but the queries were more like "how do I find what books were written by a certain author". I want to get ALL the information pertaining to one book.

Am I better off usig a nosql database? Thanks!

Aucun commentaire:

Enregistrer un commentaire