Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert/Update PostGis Geometry with Sequelize ORM

I have extracted models of some PostGis layers with sequelize-auto, giving:

module.exports = function(sequelize, DataTypes) {
return sequelize.define('table', {
  id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    primaryKey: true,
    autoIncrement: true
  },
  geom: {
    type: DataTypes.GEOMETRY('POINT', 4326),
    allowNull: true,
  },
...

On GET sequelize sends the geom to the client as GeoJSON:

{
  "type":"Point",
  "coordinates":[11.92164103734465,57.67219297300486]
}

When I try to save this back PosGis errors with:

ERROR:  Geometry SRID (0) does not match column SRID (4326)

This answer gives a god indication of how to add the SRID (How to insert a PostGIS GEOMETRY Point in Sequelize ORM?),

var point = { 
  type: 'Point', 
  coordinates: [39.807222,-76.984722],
  crs: { type: 'name', properties: { name: 'EPSG:4326'} }
};

User.create({username: 'username', geometry: point }).then(function(newUser) {
...
});

I understand that SRID used to be a feature that was removed from sequelize (https://github.com/sequelize/sequelize/issues/4054).

Does anyone know a way to hook into Sequelize so that the srid is added to the GeoJson sent to PostGis? Where to put it? In a setter on the model?

like image 418
minisaurus Avatar asked Dec 13 '17 14:12

minisaurus


People also ask

How to test Sequelize Orm in PostgreSQL?

We will use this to test out the Sequelize ORM. To begin with, visit the official site of PostgreSQL and download it. It supports all Major operating system. Once you have installed it, open up the PostgreSQL admin screen. It will look something like this. Awesome!

How to stringify a geometry object in Sequelize?

Basically, it appears that sequelize will stringify the provided geometry object assuming that it is valid GeoJSON and pipe that into the PostGIS function ST_GeomFromGeoJSON. Therefore, one can just follow the GeoJSON spec for geometry objects.

What is Sequelize NodeJS?

Sequelize is the NodeJS module which provides common functionalities and utilities to manipulate SQL based databases. Technically, it is an ORM ( Object relational mapping ) module for NodeJS. It supports the following databases:

What is Sequelize interfacing with PostgreSQL?

In this tutorial, we will learn about NodeJS ORM called Sequelize interfacing with PostgreSQL Database. Sequelize is the NodeJS module which provides common functionalities and utilities to manipulate SQL based databases.


2 Answers

It seems that Sequelize does not save the SRID when saving a GEOMETRY field (note, that it correctly saves the SRID on a GEOGRAPHY field). Then when you update that model in the future, the update will fail because it does not have a SRID set on the GEOMETRY field as expected in the model definition.

This isn't a desirable solution, but you can use Sequelize's beforeSave hook to always speciify the coordinates system to use.

myDatabase.define('user', {
  name: {
    type: Sequelize.STRING
  },
  geometry: {
    type: Sequelize.GEOMETRY('POINT', 4326)
  }
}, {
  hooks: {
    beforeSave: function(instance) {
      if (instance.geometry && !instance.geometry.crs) {
        instance.geometry.crs = {
          type: 'name',
          properties: {
            name: 'EPSG:4326'
          }
        };
      }
    }
  }
});
like image 75
mcranston18 Avatar answered Oct 20 '22 16:10

mcranston18


Declare the column type as: DataTypes.GEOMETRY('Point'),

Set the model attribute as:

{
  type: 'Point',
  coordinates: [ lat, long ],
  crs: { type: 'name', properties: { name: 'EPSG:4326'} }
}
like image 3
Jerome Anthony Avatar answered Oct 20 '22 14:10

Jerome Anthony