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(table_name) { | |
//convert from : to _ syntax | |
return table_name.replace(/:/g, '_'); | |
}; | |
var tableExists = function (table_name, cb) { | |
table_name = convertTableName(table_name); | |
var exists = true; | |
that.query('select * from information_schema.tables where table_name=\'' + table_name + '\';', [], function(err, result) { | |
if (result.rowCount && cb) cb(true); | |
}); | |
}; | |
that.query = function(text, values, cb) { | |
pg.connect(conString, function(err, client, done) { | |
client.query(text, values, function(err, result) { | |
client.query('COMMIT', done); | |
done(); | |
if (cb) cb(err, result); | |
}); | |
}); | |
}; | |
that.insertEvent = function (table_name, event_id, message) { | |
var created = false; | |
if(typeof(message.time) === 'number') { | |
created = new Date(message.time * 1000); | |
} | |
table_name = convertTableName(table_name); | |
if(!created) { | |
return that.query('INSERT INTO ' + table_name + ' (event_id, message) VALUES($1, $2)', [event_id, message], function(err, result) { | |
if (err) console.log(err); | |
}); | |
} | |
that.query('INSERT INTO ' + table_name + ' (event_id, message, created) VALUES($1, $2, $3)', [event_id, message, created], function(err, result) { | |
if (err) console.log(err); | |
}); | |
}; | |
that.updatePerson = function (table_name, user_id, message) { | |
table_name = convertTableName(table_name); | |
that.query('UPDATE ' + table_name + ' SET message = $2 WHERE user_id = $1;', | |
[user_id, message], function(err, result) { | |
if (err) console.log(err); | |
if (result.rowCount) return; | |
that.query('INSERT INTO ' + table_name + ' (user_id, message) VALUES($1, $2);', [user_id, message], function(err, result) { | |
if (err) console.log(err); | |
}); | |
}); | |
}; | |
that.createEventTable = function(table_name, cb) { | |
table_name = convertTableName(table_name); | |
cb = cb || function() {}; | |
tableExists(table_name, function (exists) { | |
if (exists) return cb(); | |
that.query('\ | |
CREATE TABLE ' + table_name + ' ( \ | |
id serial PRIMARY KEY, \ | |
event_id character varying(100) NOT NULL,\ | |
message json NOT NULL DEFAULT \'{}\'::json,\ | |
created timestamp with time zone DEFAULT now()\ | |
)\ | |
WITH (\ | |
OIDS=FALSE\ | |
);\ | |
ALTER TABLE ' + table_name + ' \ | |
OWNER TO pg_logvent;\ | |
CREATE INDEX ' + table_name + '_event_id_idx ON ' + table_name + ' USING BTREE (event_id);', | |
[], function(err, result) { | |
if (err) console.log(err); | |
cb(err, result); // hvis altsaa du vil sende err og result med - er normalt at goere | |
}); | |
}); | |
}; | |
that.createPersonTable = function(table_name, cb) { | |
table_name = convertTableName(table_name); | |
cb = cb || function() {}; | |
tableExists(table_name, function (exists) { | |
if (exists) return cb(); | |
that.query('\ | |
CREATE TABLE ' + table_name + ' ( \ | |
id serial PRIMARY KEY, \ | |
user_id character varying(100) NOT NULL,\ | |
message json NOT NULL DEFAULT \'{}\'::json,\ | |
created timestamp with time zone DEFAULT now()\ | |
)\ | |
WITH (\ | |
OIDS=FALSE\ | |
);\ | |
ALTER TABLE ' + table_name + ' \ | |
OWNER TO pg_logvent;\ | |
CREATE INDEX ' + table_name + '_user_id_idx ON ' + table_name + ' USING BTREE (user_id);', | |
[], function(err, result) { | |
if (err) console.log(err); | |
cb(err, result); // hvis altsaa du vil sende err og result med - er normalt at goere | |
}); | |
}); | |
}; | |
return that; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment