Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 将HTTP响应的嵌套JSON数据传输到SQL数据库的最佳方式是什么?_Node.js_Json_Postgresql_Http_Fastify - Fatal编程技术网

Node.js 将HTTP响应的嵌套JSON数据传输到SQL数据库的最佳方式是什么?

Node.js 将HTTP响应的嵌套JSON数据传输到SQL数据库的最佳方式是什么?,node.js,json,postgresql,http,fastify,Node.js,Json,Postgresql,Http,Fastify,我正在使用Node.js、fastfy和Postgresql创建一个API,这是为了从一个尚未开发的React客户端获取表单数据,我设想最终将到达我的API的数据结构如下: { "quote": { "body": "Of our qualities, none can be ascribed to the divine being without tainting it with its imperfection.", "a

我正在使用Node.js、fastfy和Postgresql创建一个API,这是为了从一个尚未开发的React客户端获取表单数据,我设想最终将到达我的API的数据结构如下:

{
"quote": {
"body": "Of our qualities, none can be ascribed to the divine being without tainting it with its imperfection.",
"author": "michel de montaigne",
"page": "55",
"book": {
"title": "Diary of the Journey to Italy",
"publisher": "Eichborn Verlag",
"year": "2009"
        }
    },
"hashtag": "#God",
"user": {
"name": "betonhead",
"email": "bier@wurst.de"
    }
}
现在我想将这些值存储在我的Postgresql数据库中,为此我需要将数据拆分为相互关联的不同表(作者、引用、书籍、用户等)

在研究如何实现我想用作API文件输入的JSON数据时,我发现了两个概念:

1. creating a view in Postgres, splitting the data via Node.js middleware.

2. store the raw JSON files directly in Postgres and from there distribute them to the tables using a function.
我现在的问题是哪种方法更可取,或者可能还有其他更好的方法

感谢您的帮助。

“最佳方式”取决于特定场景的特定约束/要求。但是,;a) PostgreSQL用于存储“规范化[1]”数据,b)JSON负载中表示的数据与多个实体相关,c)您预计数据的形状/结构不会经常变化,d)当前的API调用可能会导致将JSON反序列化为适当的中间对象/实体以持久化它们的开销,那么我建议在应用层执行这项工作——将实体相应地保存在它们的规范化表示中。但是,如果较低的延迟比避免最终的一致性[2]问题更可取,那么您可以选择一种方法,将负载转储到某种中间持久存储(如消息队列),以便将来处理到PostgreSQL中

另一种选择是使用像MongoDB这样的文档数据库

NB
jsonb
[3]是一个选项,如果您希望有效负载经常更改,并且/或者特定字段的形状是动态/不确定的
jsonb
以二进制格式索引和存储数据,并将相关元数据与每条记录一起存储。不要认为这是这种情况下的最佳方法,但值得一提

更多阅读: