Skip to content

Instantly share code, notes, and snippets.

@kapouer
Created May 25, 2014 23:15
Show Gist options
  • Save kapouer/74ee29861971d99fd5dc to your computer and use it in GitHub Desktop.
Save kapouer/74ee29861971d99fd5dc to your computer and use it in GitHub Desktop.
node-orm2 PostGIS custom type example
orm.db.defineType('GeoJSON', {
datastoreType: function GeoJSON_datastoreType(prop) {
return 'geometry(Geometry, 4326)';
},
datastoreGet: function GeoJSON_datastoreGet(prop, helper) {
return 'ST_AsGeoJSON(' + prop.mapsTo + ')::JSON';
},
valueToProperty: function GeoJSON_valueToProperty(value, prop) {
if (typeof value == "string") return JSON.parse(value);
else return value;
},
propertyToValue: function GeoJSON_propertyToValue(value, prop) {
if (value == null) return function() {
return 'NULL';
};
else return function(h) {
return "ST_SetSRID(ST_GeomFromGeoJSON(" + h.escapeVal(JSON.stringify(value)) + "), 4326)";
};
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment