如何使用pg promise将jsonb[]数据插入列中



给定一个具有jsonb[]类型列的表,如何在该列中插入json数组?

使用提供的格式化程序:array:json在这种情况下不起作用——除非我缺少正确的组合或其他什么。

const links = [
{
title: 'IMDB',
url: 'https://www.imdb.com/title/tt0076759'
},
{
title: 'Rotten Tomatoes',
url: 'https://www.rottentomatoes.com/m/star_wars'
}
];
const result = await db.none(`INSERT INTO tests (links) VALUES ($1:json)`, [links]);

在这种情况下,您不需要库的:json过滤器,因为您需要一个JSON对象数组,而不是一个带有JSON对象数组的JSON。

默认情况下,前者的格式是正确的,然后只需要::json[]类型的强制转换:

await db.none(`INSERT INTO tests(links) VALUES($1::json[])`, [links]);

其他备注

  • 使用pg监视器或事件查询输出正在执行的查询,以便于诊断
  • 方法none只能用null来解析,无需将结果存储在变量中
  • pg-promise没有任何:array筛选器,请参阅支持的筛选器

最新更新