简体   繁体   English

使用 MySQL 和 Node.js 从特定类别中获取每个项目的最佳实践是什么?

[英]What is the best practice in getting each item from a specific category with MySQL and Node.js?

For example, I have a table called albums例如,我有一个名为专辑的表

album_id专辑编号 name名称 description描述 price价格 release_date发布日期
1 1 album1专辑1 description1说明1 10.00 10.00 current date当前的日期
2 2 album2专辑2 description2说明2 10.00 10.00 current date当前的日期
... ... ... ... ... ... ... ... ... ...

and now I have a table called songs现在我有一张桌子叫做歌曲

song_id歌曲编号 name名称 description描述 duration期间 album_id专辑编号
1 1 song1歌曲1 song description 1歌曲说明 1 180 180 1 1
1 1 song2歌曲2 song description 2歌曲说明2 180 180 2 2
1 1 song3歌曲3 song description 3歌曲描述 3 180 180 2 2
1 1 song4歌曲4 song description 4歌曲描述 4 180 180 2 2

Now, I'm trying to build an API endpoint where I return a JSON of all albums which includes a list of the songs that are included in each album.现在,我正在尝试构建一个 API 端点,我在其中返回所有专辑的 JSON,其中包括每个专辑中包含的歌曲列表。

Currently, I can do it by first getting all the albums through this:目前,我可以先通过以下方式获取所有专辑:

let albums;
db.query('SELECT * FROM albums', (error, results) => {
    albums = results;
} 

This returns a JSON with the details of all albums.这将返回一个包含所有专辑详细信息的 JSON。 Now, I want each entry in the JSON to have a songs property which contains an array of songs that belong to that album.现在,我希望 JSON 中的每个条目都有一个歌曲属性,其中包含属于该专辑的歌曲数组。

To do that, I thought about doing it this way为此,我想这样做

albums = albums.forEach(album => {
    let songs;
    db.query('SELECT * from songs WHERE album_id = ?', [album.album_id], (error, results) => {
        album.songs = results;
    }
}

My expected output is a JSON of containing all the properties from the first query with an additional "songs" property that contains an array of the details of each song that is in that album.我的预期输出是一个包含第一个查询中所有属性的 JSON,还有一个附加的“歌曲”属性,该属性包含该专辑中每首歌曲的详细信息数组。

I think this would accomplish what I am trying to do but is there a better way of doing it?我认为这会完成我想要做的事情,但有没有更好的方法呢? I have a feeling this is not the most efficient way of doing this.我有一种感觉,这不是最有效的方法。 I am new to relational databases so I am not sure how to do this purely off of SQL.我是关系数据库的新手,所以我不确定如何完全脱离 SQL。

Based on your stated goal:根据您的既定目标:

I'm trying to build an API endpoint where I return a JSON of all albums which includes a list of the songs that are included in each album.我正在尝试构建一个 API 端点,在其中返回所有专辑的 JSON,其中包括每个专辑中包含的歌曲列表。

The best way to do this probably with a JOIN query, a VIEW created on the DB side, or a STORED PROCEDURE created on the DB side.最好的方法可能是使用JOIN查询、在 DB 端创建的VIEW或在 DB 端创建的STORED PROCEDURE You can read more about the when/why to use each here .您可以在此处阅读有关何时/为什么使用每个的更多信息

All 3 of these options can potentially return 100% of the albums and their associated song data based on how you structure the queries.根据您构建查询的方式,所有这 3 个选项都可能返回 100% 的专辑及其相关歌曲数据。 Depending on the size of your datasets it might be prudent to stream the results .根据数据集的大小,流式传输结果可能是谨慎的做法。

Beyond your stated goal:超出您的既定目标:

It is not generally best practice to simply pull all the data (especially when adding nested data) and return it to the front-end or consumer of an API endpoint.简单地提取所有数据(尤其是在添加嵌套数据时)并将其返回给 API 端点的前端或使用者通常不是最佳实践。 Large datasets may cause undesirable performance on both sides of the process.大型数据集可能会导致过程双方出现不良性能。

The best approach is to model your API endpoints based on how they will be used and consumed.最好的方法是根据 API 端点的使用和消费方式对其进行建模。 In the generic example you provide we might expect the following endpoints:在您提供的通用示例中,我们可能需要以下端点:

GET /api/list_albums
GET /api/get_album_by_id/1001
GET /api/list_songs_by_album_id/1001
GET /api/get_song_by_id/2001

As a user of your API works their way through these endpoints they get increasingly more specific results.当您的 API 用户通过这些端点工作时,他们会获得越来越具体的结果。 The results of each query contain data that can be used to inform followup queries.每个查询的结果都包含可用于通知后续查询的数据。

If I have a DB with 10,000 albums and I only need to see the song list for one of them I can query for the list, locate the album I care about and use its ID to query for its songs.如果我有一个包含 10,000 张专辑的数据库并且我只需要查看其中一张专辑的歌曲列表,我可以查询列表,找到我关心的专辑并使用其 ID 查询其歌曲。

Two queries are required but an enormous amount of data aggregation, transmission, and parsing is saved.需要两次查询,但节省了大量的数据聚合、传输和解析。

You can read more about the best practices of API design at the links below.您可以在下面的链接中阅读有关 API 设计最佳实践的更多信息。 These can help you think about how to best structure data for any type of solution.这些可以帮助您思考如何为任何类型的解决方案最好地构建数据。

https://swagger.io/resources/articles/best-practices-in-api-design/ https://swagger.io/resources/articles/best-practices-in-api-design/

https://jsonapi.org/recommendations/ https://jsonapi.org/recommendations/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM