下面是一个示例代码:
battle.heroes = [{ id: hero.id, name: hero.name }]; //This is my array I want to insert
await db.one('INSERT INTO battles(mode, params, heroes) VALUES(${mode}, ${params}, ${heroes}) RETURNING id', {
mode: battle.mode,
params: battle.params,
heroes: battle.heroes,
});信息类型‘PostgreSQL _info’:
id int4
name varchar发布于 2017-05-01 20:29:21
通过Custom Type Formatting呈现每个数组元素,方法是使用rawType和toPostgres扩展现有对象,或者使用您自己的自定义类型,如下所示:
const hero = (id, name) => ({
rawType: true,
toPostgres: () => pgp.as.format('($1, $2)::hero_info', [id, name])
});使用示例:
const heroes = [hero(1, 'first'), hero(2, 'second')];
await db.one('INSERT INTO battles(mode, params, heroes) VALUES(${mode}, ${params}, ${heroes}) RETURNING id', {
mode: battle.mode,
params: battle.params,
heroes
});对于上面的代码,您的heros数组将正确地格式化为:
array[(1, 'first')::hero_info, (2, 'second')::hero_info]
https://stackoverflow.com/questions/43715976
复制相似问题