Skip to content

Commit

Permalink
0.0.4 - adding alternative schema name
Browse files Browse the repository at this point in the history
  • Loading branch information
jimexist committed Jun 5, 2016
1 parent 89149c7 commit b84993d
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 10 deletions.
9 changes: 6 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ var PgLogger = function (_winston$Transport) {

var opts = _extends({
name: 'PgLogger',
level: 'info'
level: 'info',
schemaName: 'public'
}, options);

var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(PgLogger).call(this, opts));

var connString = opts.connString;
var tableName = opts.tableName;
var schemaName = opts.schemaName;
var initTable = opts.initTable;

if (!connString) {
Expand All @@ -51,6 +53,7 @@ var PgLogger = function (_winston$Transport) {
if (!tableName) {
throw new Error('empty table name');
}
_this.schemaName = schemaName;
_this.tableName = tableName;
_this.connString = connString;
if (initTable) {
Expand All @@ -68,7 +71,7 @@ var PgLogger = function (_winston$Transport) {
if (err) {
callback(err);
} else {
client.query('CREATE TABLE IF NOT EXISTS "' + _this2.tableName + '" (\n id serial primary key,\n ts timestamp default current_timestamp,\n level varchar(10) not null,\n message varchar(1024) not null,\n meta json\n )', [], function (err, result) {
client.query('CREATE TABLE IF NOT EXISTS "' + _this2.schemaName + '"."' + _this2.tableName + '" (\n id serial primary key,\n ts timestamp default current_timestamp,\n level varchar(10) not null,\n message varchar(1024) not null,\n meta json\n )', [], function (err, result) {
pgDone();
if (err) {
callback(err);
Expand All @@ -91,7 +94,7 @@ var PgLogger = function (_winston$Transport) {
logger.emit('error', err);
callback(err);
} else {
client.query('INSERT INTO "' + _this3.tableName + '" (level, message, meta) VALUES ($1, $2, $3)', [level, msg, meta instanceof Array ? JSON.stringify(meta) : meta], function (err, result) {
client.query('INSERT INTO "' + _this3.schemaName + '"."' + _this3.tableName + '" (level, message, meta) ' + 'VALUES ($1, $2, $3)', [level, msg, meta instanceof Array ? JSON.stringify(meta) : meta], function (err, result) {
pgDone();
if (err) {
logger.emit('error', err);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "winston-pg",
"version": "0.0.3",
"version": "0.0.4",
"description": "winston postgres transport",
"main": "lib/index.js",
"scripts": {
Expand Down
9 changes: 6 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ export default class PgLogger extends winston.Transport {
let opts = {
name: 'PgLogger',
level: 'info',
schemaName: 'public',
...options
};
super(opts);
const { connString, tableName, initTable } = opts;
const { connString, tableName, schemaName, initTable } = opts;
if (!connString) {
throw new Error('empty connString');
}
if (!tableName) {
throw new Error('empty table name');
}
this.schemaName = schemaName;
this.tableName = tableName;
this.connString = connString;
if (initTable) {
Expand All @@ -30,7 +32,7 @@ export default class PgLogger extends winston.Transport {
if (err) {
callback(err);
} else {
client.query(`CREATE TABLE IF NOT EXISTS "${this.tableName}" (
client.query(`CREATE TABLE IF NOT EXISTS "${this.schemaName}"."${this.tableName}" (
id serial primary key,
ts timestamp default current_timestamp,
level varchar(10) not null,
Expand All @@ -56,7 +58,8 @@ export default class PgLogger extends winston.Transport {
logger.emit('error', err);
callback(err);
} else {
client.query(`INSERT INTO "${this.tableName}" (level, message, meta) VALUES ($1, $2, $3)`,
client.query(`INSERT INTO "${this.schemaName}"."${this.tableName}" (level, message, meta) ` +
`VALUES ($1, $2, $3)`,
[level, msg, meta instanceof Array ? JSON.stringify(meta) : meta],
(err, result) => {
pgDone();
Expand Down
31 changes: 31 additions & 0 deletions test/alternative_schema.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import PgLogger from '../src';
import winston from 'winston';

describe('logging with alternative schema', () => {
let logger;
before(done => {
const pgLogger = new PgLogger({
name: 'test-logger',
level: 'debug',
schemaName: 'winston',
connString: 'postgres://ubuntu@localhost:5432/circle_test', // this is setup by circle ci
tableName: 'winston_logs',
});
logger = new winston.Logger({
transports: [
new winston.transports.Console({
color: true,
timestamp: true,
}),
pgLogger,
]
});
pgLogger.initTable(done);
});

it('works', done => {
logger.on('logged', () => done());
logger.on('error', done);
logger.log('info', 'it works', { funniness: "👌" });
});
});
4 changes: 1 addition & 3 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ import PgLogger from '../src';
import winston from 'winston';

describe('logging', () => {

let logger;

before(done => {
const pgLogger = new PgLogger({
name: 'test-logger',
level: 'debug',
connString: 'postgres://ubuntu@localhost:5432/circle_test',
connString: 'postgres://ubuntu@localhost:5432/circle_test', // this is setup by circle ci
tableName: 'winston_logs',
});
logger = new winston.Logger({
Expand Down

0 comments on commit b84993d

Please sign in to comment.