Insert/Update PostGis Geometry with Sequelize ORM
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'} }
}