使用 geom 和 json 列的 pg-promise 多行更新

pg-promise multi-row update with geom and json columns

我喜欢 pg-promise,但很难将多行插入到同时具有 geom 和 json 列的 table 中。我不确定语法。

我希望能够传入一个对象数组,例如:

[{pt_title: 'title1', 
       lat: -42.360061, 
       lng: -71.065567, 
 waypoints:[[42.360061,-71.065567],[42.360061,-71.06549]]},
{pt_title: 'title2', 
       lat: -43.360061, 
       lng: -71.065567, 
 waypoints:[[43.360061,-71.065567],[42.360061,-71.06549]]}]

并更新我的 table 积分:

pt_title varchar,
pt_geom geom,
waypoints json

因此我必须像这样转换纬度和经度值:

ST_GeomFromText(POINT(${pt.lng} ${pt.lat}), 4326)

并在更新之前将 waypoints 数组转换为 json。

CAST(pt.waypoints AS json)

我阅读了有关使用更新助手的信息,但找不到任何 gis 列的示例。我不知道如何设置列以便它知道转换?

此外,是否有辅助实用程序,以便我可以看到生成的 SQL 字符串 使用 db.none(sql, values) 语法?我想看看最终的 SQL 是什么以及插入的值,这样我就可以看到发生了什么。

I can't figure how to do the column set so that it knows about the conversion?

属性 init 在每个 Column 中是您的转化回调。

这是您的完整 ColumnSet 对象:

const cs = new pgp.helpers.ColumnSet([
    'pt_title',
    {
        name: 'pt_geom',
        mod: ':raw',
        init: c => pgp.as.format('ST_GeomFromText(POINT(${lng}, ${lat}), 4326)', c.source)
    },
    'waypoints:json'
], {table: 'my-table'});

Also, is there a helper utility so I can see the resulting SQL string...?

参见 pg-monitor, or just handle event query