在 NodeJS 中使用 PostgreSQL JSON 类型的最佳方法是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22358082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the best way to use PostgreSQL JSON types with NodeJS
提问by Andre Garzia
I am facing some analysis paralysis here. There are so many options for programming databases with NodeJS that I am a bit lost.
我在这里面临一些分析瘫痪。使用 NodeJS 编程数据库有很多选择,我有点迷茫。
I am building an API server using Express that will talk to a HTML5 app on mobile. I decided to use PostgreSQL because my data is "very relational" and PostgreSQL new JSON data type will make my life a lot easier.
我正在使用 Express 构建一个 API 服务器,它将与移动设备上的 HTML5 应用程序通信。我决定使用 PostgreSQL,因为我的数据“非常有关系”,而 PostgreSQL 新的 JSON 数据类型将使我的生活更轻松。
Unfortunately, I can't find any library for PostgreSQL taking advantage of the new JSON datatype or exposing it. I thought about using Sequelizeand having a nice ORM or rolling my own stuff by using the raw pgsql module.
不幸的是,我找不到任何利用新 JSON 数据类型或公开它的 PostgreSQL 库。我想过使用Sequelize并拥有一个不错的 ORM 或使用原始 pgsql 模块滚动我自己的东西。
Can someone shed a clue? I'd ask this on some NodeJS stackexchange but I don't think we have one as specific as this.
有人可以提供线索吗?我会在一些 NodeJS stackexchange 上问这个问题,但我认为我们没有像这样具体的。
回答by Tim Brown
I like https://github.com/brianc/node-postgres. It's actively developed, and just a nice thin layer.
我喜欢https://github.com/brianc/node-postgres。它正在积极开发,只是一个很好的薄层。
To use the json types in a prepared query, just JSON.stringify
whatever you are trying to store as json (that's how postgres wants it anyway).
要在准备好的查询中使用 json 类型,只需JSON.stringify
要将您尝试存储为 json 的任何内容(无论如何 postgres 想要它)。
回答by Mikael Lepist?
Objection.jshas really good support for relationaldata as well as for JSONBdata.
Objection.js对关系数据和JSONB数据都有很好的支持。
You don't have to do any tricks to parse / stringify json data. It all is done automatically. You can declare schemas to allow validating data you are going to put DB etc.
您不必做任何技巧来解析/字符串化 json 数据。这一切都是自动完成的。您可以声明模式以允许验证要放入 DB 等的数据。
One can insert nested relational object hierarchies to DB and rows will be generated to correct tables and you have javascript API to query data inside JSON columns so no need to write RAW SQL for that either.
可以将嵌套的关系对象层次结构插入到数据库中,并且将生成行以更正表,并且您有 javascript API 来查询 JSON 列内的数据,因此也无需为此编写 RAW SQL。
EDIT:
编辑:
No idea why the down votes here (its -2 currently), Objection.js
still has the best support for Postgresql's JSONB operations in node world (and the only choice in current answers, which has any special support for postgresql jsonb handling).
不知道为什么这里的反对票(当前为 -2)Objection.js
仍然对节点世界中 Postgresql 的 JSONB 操作有最好的支持(并且是当前答案中的唯一选择,它对 postgresql jsonb 处理有任何特殊支持)。
Latest addition was support for patching only parts of data inside JSONB column, where objection.js automatically constructs jsonb_set()
calls for you.
最新添加的是支持仅修补 JSONB 列中的部分数据,其中 objection.js 会自动jsonb_set()
为您构造调用。
for example:
例如:
ModelWithJsonColumn.query().update({
'jsonColumn:attribute' : 'new value',
otherColum: ref('jsonColumn:extractThisAttribute')
}).where('id', 1).returning('*')
will create update query like this:
将创建这样的更新查询:
update "ModelWithJsonColumn" set
"jsonColumn" = jsonb_set("jsonColumn", '{attribute}', to_jsonb('new value'), true),
"otherColumn" = "jsonColumn"#>'{extractThisAttribute}'
where "id" = 1 returning *
Also one can use ref()
syntax in pretty much every query builder method like in
也可以ref()
在几乎每个查询构建器方法中使用语法,例如
.select(['id', ref('jsonArrayColumn:[0]')])
or
或者
.where('name', ref('jsonColumn:middleName'))
or even with joins
甚至连接
.join('PetTable',
ref('PetTable.jsonColumn:details.name'),
'=',
ref('ThisTable.someOtherJsonbColumn:dogName'))
回答by vitaly-t
pg-promiseis the easiest way to use PostgreSQL with Node JS, which extends node-postgreswith promises, for automated connections and transactions.
pg-promise是将 PostgreSQL 与 Node JS 结合使用的最简单方法,它使用 promise扩展了node-postgres,用于自动连接和事务。
回答by Vlad Ankudinov
I've also searched on answer to this question and found solution like that in related question.
我还搜索了这个问题的答案,并在相关问题中找到了类似的解决方案。
var pg = require("pg");
var Promise = require("bluebird");
Object.keys(pg).forEach(function(key) {
var Class = pg[key];
if (typeof Class === "function") {
Promise.promisifyAll(Class.prototype);
Promise.promisifyAll(Class);
}
})
Promise.promisifyAll(pg);
This allows you to use pg
with Promise
s. Details here Manually promisifying pg.connect with Bluebird
这允许您pg
与Promise
s一起使用。此处的详细信息使用 Bluebird 手动承诺 pg.connect