<commit_before>'use strict';

const Writable = require('stream').Writable;
const pg = require('pg');

class LogStream extends Writable {
	constructor(options) {
		super(options);

		if (options.connection === undefined || options.tableName === undefined) {
			throw new Error('Invalid bunyan-postgres stream configuration');
		}

		if (typeof options.connection.__knex__ === 'string') {
			this.knex = options.connection;
			this._write = this._writeKnex;
		}

		if (typeof options.connection === 'object') {
			this.connection = options.connection;
			this._write = this._writePgPool;
			this.pool = new pg.Pool(this.connection);
		}

		this.tableName = options.tableName;
	}

	_writeKnex(chunk, env, cb) {
		const content = JSON.parse(chunk.toString());
		this.knex
			.insert({
				name: content.name,
				level: content.level,
				hostname: content.hostname,
				msg: content.msg,
				pid: content.pid,
				time: content.time,
				content: JSON.stringify(content)
			})
			.into(this.tableName)
			.asCallback(cb);
	}

	writePgPool(client, content) {
		return client.query(`
			insert into ${this.tableName}
				(name, level, hostname, msg, pid, time, content)
			values (
				'${content.name}',
				'${content.level}',
				'${content.hostname}',
				'${content.msg}',
				'${content.pid}',
				'${content.time}',
				'${JSON.stringify(content)}'
			)`);
	}

	_writePgPool(chunk, env, cb) {
		const content = JSON.parse(chunk.toString());
		this.pool.connect().then(client => {
			return this.writePgPool(client, content).then(result => {
				cb(null, result.rows);
				client.release();
			});
		})
		.catch(err => cb(err));
	}

	end(cb) {
		if (this.pool) {
			return this.pool.end(cb);
		}
		cb();
	}
}

module.exports = (options = {}) => {
	return new LogStream(options);
};
<commit_msg>Stop using deprecated __knex__ property<commit_after>'use strict';

const Writable = require('stream').Writable;
const pg = require('pg');

class LogStream extends Writable {
	constructor(options) {
		super(options);

		if (options.connection === undefined || options.tableName === undefined) {
			throw new Error('Invalid bunyan-postgres stream configuration');
		}

		if (options.connection.client && options.connection.client.makeKnex) {
			this.knex = options.connection;
			this._write = this._writeKnex;
		}

		if (typeof options.connection === 'object') {
			this.connection = options.connection;
			this._write = this._writePgPool;
			this.pool = new pg.Pool(this.connection);
		}

		this.tableName = options.tableName;
	}

	_writeKnex(chunk, env, cb) {
		const content = JSON.parse(chunk.toString());
		this.knex
			.insert({
				name: content.name,
				level: content.level,
				hostname: content.hostname,
				msg: content.msg,
				pid: content.pid,
				time: content.time,
				content: JSON.stringify(content)
			})
			.into(this.tableName)
			.asCallback(cb);
	}

	writePgPool(client, content) {
		return client.query(`
			insert into ${this.tableName}
				(name, level, hostname, msg, pid, time, content)
			values (
				'${content.name}',
				'${content.level}',
				'${content.hostname}',
				'${content.msg}',
				'${content.pid}',
				'${content.time}',
				'${JSON.stringify(content)}'
			)`);
	}

	_writePgPool(chunk, env, cb) {
		const content = JSON.parse(chunk.toString());
		this.pool.connect().then(client => {
			return this.writePgPool(client, content).then(result => {
				cb(null, result.rows);
				client.release();
			});
		})
		.catch(err => cb(err));
	}

	end(cb) {
		if (this.pool) {
			return this.pool.end(cb);
		}
		cb();
	}
}

module.exports = (options = {}) => {
	return new LogStream(options);
};

{'fraction_non_alphanumeric': 0.16905286343612336, 'fraction_numerical': 0.0, 'mean_word_length': 3.5018587360594795, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3445267', 'n_tokens_mistral': 1429, 'n_tokens_neox': 1229, 'n_words': 239}