So I'm starting a journey down the road of microservices. I've spent some hours online trying immerse myself into this topic.
One concept I'm not quite grasping yet is the idea of not using SQL joins and therefore having a small independent database for authors and the same for books.
So I understand the following SQL:
BooksTable - id, name, authorid
AuthorsTable - id, name
select book.name, author.name from book
join author on book.authorId = author.id
In Node.js world
index.js
app.get('/api/books' bookDomain.get());
bookDomain.js
exports.get = () => {
const books = bookService.get();
const authors = authorService.get();
/*
This is where I'm lost: how do you achieve the simple SQL
above? I'm assuming in the domain is where this information is
"joined"? am I correct?
*/
};
Services
Database1
**bookService.js**
database context
Database2
**authorService.js**
database context
expected data (something like it, basically i'm saying JSON should be the return type)
[{
book {
"name": "Book 1",
"author": "Author Name 1"
}
},
{
book {
"name": "Book 2",
"author": "Author Name 2"
}
}]
There are at least three ways that I can think of to solve this problem.
Books
and Authors
and an AuthorBooks
table that are all joined on a pair of foreign keys - you can use a document NoSQL DB like Mongo where your books are stored as document type, in a BSON format that looks almost identical to the JSON in your question. A nice feature of document DBs like Mongo is that you can set up an index on JSON inside the Book
document on Author
, thus improving your query time. This is discussed quite well in NoSQL Distilled by Martin Fowler (Sec. 2.2 and Chp. 9).
book_id | book_name | book_author
=====================================================
1 | NoSQL Distilled | Pramod J. Sadalage
-----------------------------------------------------
1 | NoSQL Distilled | Martin Fowler
-----------------------------------------------------
2 | Building Microservices | Sam Newman
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With