node-postgres/lib/utils.js
2013-01-24 22:03:02 +01:00

50 lines
1.3 KiB
JavaScript

var url = require('url');
var defaults = require(__dirname + "/defaults");
var events = require('events');
//compatibility for old nodes
if(typeof events.EventEmitter.prototype.once !== 'function') {
events.EventEmitter.prototype.once = function (type, listener) {
var self = this;
self.on(type, function g () {
self.removeListener(type, g);
listener.apply(this, arguments);
});
};
}
//converts values from javascript types
//to their 'raw' counterparts for use as a postgres parameter
//note: you can override this function to provide your own conversion mechanism
//for complex types, etc...
var prepareValue = function(val) {
if(val instanceof Date) {
return JSON.stringify(val);
}
if(typeof val === 'undefined') {
return null;
}
return val === null ? null : val.toString();
};
function normalizeQueryConfig (config, values, callback) {
//can take in strings or config objects
config = (typeof(config) == 'string') ? { text: config } : config;
if(values) {
if(typeof values === 'function') {
config.callback = values;
} else {
config.values = values;
}
}
if (callback) {
config.callback = callback;
}
return config;
}
module.exports = {
prepareValue: prepareValue,
normalizeQueryConfig: normalizeQueryConfig
};