Created
May 25, 2014 23:15
-
-
Save kapouer/74ee29861971d99fd5dc to your computer and use it in GitHub Desktop.
node-orm2 PostGIS custom type example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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