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?
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!
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.
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:
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.
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'
}
};
}
}
}
});
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'} }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With