jfgodoy / knex-postgis

postgis extension for knex
MIT License
183 stars 25 forks source link

How to insert batches? #42

Closed AtherBilal closed 3 years ago

AtherBilal commented 3 years ago

Hello,

Firstly wanted to say, thank you for this library, its been insanely helpful.

I have a question. I'm trying to bulk insert an array of records into postGIS. with knex I used to be able to do this await db('alerts').insert(arrayOfData).onConflict('uuid').ignore()

Here is what I tried

        const arrayOfData = filteredAlerts.map((alert) => {
            return {
              lat: alert.location.y,
              long: alert.location.x,
              geom: st.geomFromText((alert.location.y, alert.location.x), 4326)
            }
          })
          await db('alerts').insert(arrayOfData).onConflict('uuid').ignore()

However, does not work.

I'm trying to figure out a way to insert all records into the db while generating a postGIS geom from a given lat and long.

I used to be able to do this using a generated column in postgres however due to our some AWS issues I'm restricted to Postgres 11.x but generated columns were created in 12.

AtherBilal commented 3 years ago

ok so I figured it out! This library does support batches! All I needed to do was change this line

geom: st.geomFromText(`Point(${alert.location.y} ${alert.location.x})`, 4326)

Please close this issue

jfgodoy commented 3 years ago

hi Bilal, I'm glad to hear it's been useful to you.

Since you figured it out, I'll close this issue.