index.js
1 var textParsers = require('./lib/textParsers'); 2 var binaryParsers = require('./lib/binaryParsers'); 3 var arrayParser = require('./lib/arrayParser'); 4 var builtinTypes = require('./lib/builtins'); 5 6 exports.getTypeParser = getTypeParser; 7 exports.setTypeParser = setTypeParser; 8 exports.arrayParser = arrayParser; 9 exports.builtins = builtinTypes; 10 11 var typeParsers = { 12 text: {}, 13 binary: {} 14 }; 15 16 //the empty parse function 17 function noParse (val) { 18 return String(val); 19 }; 20 21 //returns a function used to convert a specific type (specified by 22 //oid) into a result javascript type 23 //note: the oid can be obtained via the following sql query: 24 //SELECT oid FROM pg_type WHERE typname = 'TYPE_NAME_HERE'; 25 function getTypeParser (oid, format) { 26 format = format || 'text'; 27 if (!typeParsers[format]) { 28 return noParse; 29 } 30 return typeParsers[format][oid] || noParse; 31 }; 32 33 function setTypeParser (oid, format, parseFn) { 34 if(typeof format == 'function') { 35 parseFn = format; 36 format = 'text'; 37 } 38 typeParsers[format][oid] = parseFn; 39 }; 40 41 textParsers.init(function(oid, converter) { 42 typeParsers.text[oid] = converter; 43 }); 44 45 binaryParsers.init(function(oid, converter) { 46 typeParsers.binary[oid] = converter; 47 });