Rod*_*Rod 11 amazon-web-services node.js microservices aws-lambda
所以我开始了微服务的旅程.我花了几个小时在网上尝试沉浸在这个话题中.
我还没有完全理解的一个概念是不使用SQL连接的想法,因此为作者提供了一个小的独立数据库,对于书籍也有相同的独立数据库.
所以我理解以下SQL:
BooksTable - id, name, authorid
AuthorsTable - id, name
select book.name, author.name from book
join author on book.authorId = author.id
Run Code Online (Sandbox Code Playgroud)
在Node.js世界中
index.js
app.get('/api/books' bookDomain.get());
Run Code Online (Sandbox Code Playgroud)
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?
*/
};
Run Code Online (Sandbox Code Playgroud)
服务
Database1
**bookService.js**
database context
Database2
**authorService.js**
database context
Run Code Online (Sandbox Code Playgroud)
预期的数据(类似的东西,基本上我说JSON应该是返回类型)
[{
book {
"name": "Book 1",
"author": "Author Name 1"
}
},
{
book {
"name": "Book 2",
"author": "Author Name 2"
}
}]
Run Code Online (Sandbox Code Playgroud)
我至少有三种方法可以解决这个问题.
Books和Authors和AuthorBooks表都加入了一对外国键-这看起来几乎可以使用文档的NoSQL数据库一样蒙戈您的书籍存储为文档类型,以BSON格式与您问题中的JSON相同.像Mongo这样的文档数据库的一个很好的功能是你可以在Book文档中设置JSON索引Author,从而缩短你的查询时间.这在Martin Fowler推出的NoSQL Distilled中得到了很好的讨论(Sec.2.2和Chp.9).
book_id | book_name | book_author
=====================================================
1 | NoSQL Distilled | Pramod J. Sadalage
-----------------------------------------------------
1 | NoSQL Distilled | Martin Fowler
-----------------------------------------------------
2 | Building Microservices | Sam Newman
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
222 次 |
| 最近记录: |