Skip to content

Commit

Permalink
feat: replace debug with Roarr
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Oct 2, 2017
1 parent ad6a8c5 commit 775a19d
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 42 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
"dependencies": {
"ajv": "^5.2.2",
"array-flatten": "^2.1.1",
"debug": "^3.0.1",
"es6-error": "^4.0.2",
"pg": "^7.3.0",
"pg-connection-string": "^2.0.0",
"pretty-hrtime": "^1.0.3"
"pretty-hrtime": "^1.0.3",
"roarr": "^1.7.1"
},
"description": "A PostgreSQL client with strict types and assertions.",
"devDependencies": {
Expand Down
7 changes: 7 additions & 0 deletions src/Logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// @flow

import Logger from 'roarr';

export default Logger.child({
package: 'mightyql'
});
7 changes: 0 additions & 7 deletions src/errors.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
// @flow

import ExtendableError from 'es6-error';
import {
createDebug
} from './factories';

const debug = createDebug('errors');

export class NotFoundError extends ExtendableError {
constructor (message: string = 'Resource not found.') {
debug(message);

super(message);
}
}
Expand Down
7 changes: 0 additions & 7 deletions src/factories/createDebug.js

This file was deleted.

3 changes: 0 additions & 3 deletions src/factories/index.js

This file was deleted.

21 changes: 12 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import pg, {
import {
parse as parseConnectionString
} from 'pg-connection-string';
import createDebug from 'debug';
import prettyHrtime from 'pretty-hrtime';
import {
DataIntegrityError,
Expand Down Expand Up @@ -35,6 +34,7 @@ import type {
InternalTransactionType,
TaggledTemplateLiteralInvocationType
} from './types';
import Logger from './Logger';

export type {
DatabaseConnectionType,
Expand All @@ -53,14 +53,17 @@ types.setTypeParser(20, (value) => {
return parseInt(value, 10);
});

const debug = createDebug('mightyql');
const log = Logger.child({
namespace: 'mightyql'
});

export const query: InternalQueryType<*> = async (connection, rawSql, values) => {
const strippedSql = stripComments(rawSql);

debug('input query', strippedSql, {
log.debug({
sql: strippedSql,
values
});
}, 'input query');

try {
const start = process.hrtime();
Expand All @@ -87,12 +90,12 @@ export const query: InternalQueryType<*> = async (connection, rawSql, values) =>

const end = process.hrtime(start);

debug('query execution time', prettyHrtime(end));
log.debug('query execution time %s', prettyHrtime(end));

if (result.rowCount) {
debug('query returned %d row(s)', result.rowCount);
log.debug('query returned %d row(s)', result.rowCount);
} else if (Array.isArray(result)) {
debug('query returned %d row(s)', result.length);
log.debug('query returned %d row(s)', result.length);
}

return result;
Expand Down Expand Up @@ -315,7 +318,7 @@ const createPool = (
oneFirst: mapTaggedTemplateLiteralInvocation(oneFirst.bind(null, pool, clientConfiguration)),
query: mapTaggedTemplateLiteralInvocation(query.bind(null, pool)),
transaction: async (handler) => {
debug('allocating a new connection to execute the transaction');
log.debug('allocating a new connection to execute the transaction');

const connection = await connect();

Expand All @@ -324,7 +327,7 @@ const createPool = (
try {
result = await connection.transaction(handler);
} finally {
debug('releasing the connection that was earlier secured to execute the transaction');
log.debug('releasing the connection that was earlier secured to execute the transaction');

await connection.release();
}
Expand Down
16 changes: 9 additions & 7 deletions src/utilities/normalizeAnonymousValuePlaceholders.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
// @flow

import arrayFlatten from 'array-flatten';
import {
createDebug
} from '../factories';
import type {
AnonymouseValuePlaceholderValuesType,
NormalizedQueryType
} from '../types';
import Logger from '../Logger';

const anonymousePlaceholdersRegex = /\?/g;
const log = Logger.child({
namespace: 'normalizeAnonymousValuePlaceholders'
});

const debug = createDebug('normalizeAnonymousValuePlaceholders');
const anonymousePlaceholdersRegex = /\?/g;

/**
* @see https://github.com/mysqljs/sqlstring/blob/f946198800a8d7f198fcf98d8bb80620595d01ec/lib/SqlString.js#L73
*/
// eslint-disable-next-line complexity
export default (
sql: string,
values: AnonymouseValuePlaceholderValuesType = []
Expand Down Expand Up @@ -96,9 +97,10 @@ export default (
result += sql.slice(chunkIndex);
}

debug('normalized SQL', result, {
log.debug({
sql: result,
values
});
}, 'normalized SQL');

return {
sql: result,
Expand Down
15 changes: 8 additions & 7 deletions src/utilities/normalizeNamedValuePlaceholders.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
// @flow

import {
createDebug
} from '../factories';
import type {
NamedValuePlaceholderValuesType,
NormalizedQueryType
} from '../types';
import Logger from '../Logger';

const log = Logger.child({
namespace: 'normalizeNamedValuePlaceholders'
});

/**
* @see https://regex101.com/r/KrEe8i/2
*/
const namedPlaceholderRegex = /[\s,(]:([a-zA-Z]+)/g;

const debug = createDebug('normalizeNamedValuePlaceholders');

/**
* @see https://github.com/mysqljs/sqlstring/blob/f946198800a8d7f198fcf98d8bb80620595d01ec/lib/SqlString.js#L73
*/
Expand Down Expand Up @@ -66,9 +66,10 @@ export default (
result += sql.slice(chunkIndex);
}

debug('normalized SQL', result, {
log.debug({
sql: result,
values
});
}, 'normalized SQL');

return {
sql: result,
Expand Down

0 comments on commit 775a19d

Please sign in to comment.