index.js 1.2 KB

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