Last active
January 4, 2016 16:28
-
-
Save freeall/8647128 to your computer and use it in GitHub Desktop.
This file contains 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
/* globals done */ | |
var pg = require('pg'); | |
var conString = 'postgres://pg_logvent:Bow3CuU7foVHbr@localhost/db_logvent'; | |
module.exports = function(options) { | |
'use strict'; | |
options = options || {}; | |
var that = {}; | |
var convertTableName = function(tableName) { | |
//convert from : to _ syntax | |
return tableName.replace(/:/g, '_'); | |
}; | |
var tableExists = function (tableName, cb) { | |
cb = cb || function() {}; | |
tableName = convertTableName(tableName); | |
var exists = false; | |
that.query('select * from information_schema.tables where table_name=\'' + tableName + '\';', [], function(err, result) { | |
cb(err, result.rowCount); | |
}); | |
}; | |
that.query = function(text, values, cb) { | |
cb = cb || function() {}; | |
pg.connect(conString, function(err, client, done) { | |
client.query(text, values, function(err, result) { | |
client.query('COMMIT', done); | |
done(); | |
cb(err, result); | |
}); | |
}); | |
}; | |
that.insertEvent = function (tableName, eventId, message) { | |
var created = false; | |
if(typeof(message.time) === 'number') { | |
created = new Date(message.time * 1000); | |
} | |
tableName = convertTableName(tableName); | |
if(!created) { | |
return that.query('INSERT INTO ' + tableName + ' (eventId, message) VALUES($1, $2)', [eventId, message], function(err, result) { | |
if (err) console.log(err); | |
}); | |
} | |
that.query('INSERT INTO ' + tableName + ' (eventId, message, created) VALUES($1, $2, $3)', [eventId, message, created], function(err, result) { | |
if (err) console.log(err); | |
}); | |
}; | |
that.updatePerson = function (tableName, userId, message) { | |
tableName = convertTableName(tableName); | |
that.query('UPDATE ' + tableName + ' SET message = $2 WHERE userId = $1;', | |
[userId, message], function(err, result) { | |
if (err) console.log(err); | |
if (result.rowCount) return; | |
that.query('INSERT INTO ' + tableName + ' (userId, message) VALUES($1, $2);', [userId, message], function(err, result) { | |
if (err) console.log(err); | |
}); | |
}); | |
}; | |
that.createEventTable = function(tableName, cb) { | |
cb = cb || function() {}; | |
tableName = convertTableName(tableName); | |
tableExists(tableName, function (err, exists) { | |
if (exists) return cb(); | |
that.query('\ | |
CREATE TABLE "' + tableName + '" ( \ | |
id serial PRIMARY KEY, \ | |
eventId character varying(100) NOT NULL,\ | |
message json NOT NULL DEFAULT \'{}\'::json,\ | |
created timestamp with time zone DEFAULT now()\ | |
)\ | |
WITH (\ | |
OIDS=FALSE\ | |
);\ | |
ALTER TABLE "' + tableName + '" \ | |
OWNER TO pg_logvent;\ | |
CREATE INDEX "' + tableName + '_eventId_idx" ON "' + tableName + '" USING BTREE (eventId);', | |
[], function(err, result) { | |
if (err) console.log(err); | |
cb(err, result); | |
}); | |
}); | |
}; | |
that.createPersonTable = function(tableName, cb) { | |
cb = cb || function() {}; | |
tableName = convertTableName(tableName); | |
tableExists(tableName, function (err, exists) { | |
if (exists) return cb(); | |
that.query('\ | |
CREATE TABLE "' + tableName + '" ( \ | |
id serial PRIMARY KEY, \ | |
userId character varying(100) NOT NULL,\ | |
message json NOT NULL DEFAULT \'{}\'::json,\ | |
created timestamp with time zone DEFAULT now()\ | |
)\ | |
WITH (\ | |
OIDS=FALSE\ | |
);\ | |
ALTER TABLE "' + tableName + '" \ | |
OWNER TO pg_logvent;\ | |
CREATE INDEX "' + tableName + '_userId_idx" ON "' + tableName + '" USING BTREE (userId);', | |
[], function(err, result) { | |
if (err) console.log(err); | |
cb(err, result); | |
}); | |
}); | |
}; | |
return that; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment