Skip to content

Commit 381df9e

Browse files
committed
initial
0 parents  commit 381df9e

File tree

8 files changed

+2418
-0
lines changed

8 files changed

+2418
-0
lines changed

.babelrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"presets": [
3+
["env", {
4+
"targets": {
5+
"node": 6.0
6+
}
7+
}]
8+
]
9+
}

.eslintrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": ["standard", "plugin:import/errors"]
3+
}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
lib/

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Maxim Khvatalin <khmm12@gmail.com> (http://github.com/khmm12)
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# knex-tiny-logger
2+
3+
[![Github All Releases](https://img.shields.io/github/downloads/knex-tiny-logger/knex-tiny-logger/total.svg?style=flat-square)]()
4+
[![](https://img.shields.io/npm/v/knex-tiny-logger.svg?style=flat-square)](https://npmjs.com/package/knex-tiny-logger)
5+
[![](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://standardjs.com)
6+
7+
> Zero config queries logger for knex
8+
9+
## Usage
10+
11+
Install the package:
12+
13+
```bash
14+
$ yarn add knex-tiny-logger
15+
```
16+
17+
Decorate knex with `knex-tiny-logger`:
18+
19+
```
20+
import createKnex from 'knex'
21+
import knexTinyLogger from 'knex-tiny-logger'
22+
23+
const knexOptions = {} // Your knex config
24+
const knex = knexTinyLogger(createKnex(knexOptions))
25+
26+
// or
27+
const knex = createKnex(knexOptions)
28+
knexTinyLogger(knex)
29+
```
30+
31+
## Advanced usage
32+
33+
By default `knex-tiny-logger` uses `console.log`, but you can specify any logger which your prefer:
34+
```
35+
import createKnex from 'knex'
36+
import knexTinyLogger from 'knex-tiny-logger'
37+
import initDebug from 'debug'
38+
39+
const awesomeLogger = initDebug('my-project:knex')
40+
const knexOptions = {} // Your knex config
41+
const knex = createKnex(knexOptions)
42+
knexTinyLogger(knex, { logger })
43+
```
44+
45+
## License
46+
47+
[MIT]('./LICENSE')

package.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "knex-tiny-logger",
3+
"version": "1.0.2",
4+
"description": "Tiny logger for knex",
5+
"repository": {
6+
"type": "git",
7+
"url": "git://github.com/khmm12/knex-tiny-logger.git"
8+
},
9+
"author": "Maixm Khvatalin <khmm12@gmail.com> (http://github.com/khmm12)",
10+
"files": [
11+
"lib",
12+
"src"
13+
],
14+
"keywords": [
15+
"knex",
16+
"logger"
17+
],
18+
"license": "MIT",
19+
"main": "lib/index.js",
20+
"engines": {
21+
"node": ">=6"
22+
},
23+
"scripts": {
24+
"prepublish": "yarn test && yarn build",
25+
"test": "eslint src",
26+
"build": "babel src --out-dir lib"
27+
},
28+
"devDependencies": {
29+
"babel-cli": "^6.24.0",
30+
"babel-preset-env": "^1.3.2",
31+
"eslint": "^3.18.0",
32+
"eslint-config-standard": "^7.1.0",
33+
"eslint-plugin-import": "^2.2.0",
34+
"eslint-plugin-promise": "^3.5.0",
35+
"eslint-plugin-standard": "^2.1.1"
36+
},
37+
"dependencies": {
38+
"chalk": "^1.1.3"
39+
}
40+
}

src/index.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import chalk from 'chalk'
2+
3+
/**
4+
* Decorate `knex` instance with logger
5+
*
6+
* @param {Object} knex - knex instance
7+
* @param {Object} options
8+
* @param {Function} [options.logger=console.log]
9+
* @return {Object} knex - knex instance
10+
*/
11+
12+
export default function knexTinyLogger (knex, { logger = console.log } = {}) {
13+
const queries = {}
14+
knex.on('query', ({ sql, bindings, __knexQueryUid: queryId }) => {
15+
const startTime = process.hrtime()
16+
queries[queryId] = { sql, bindings, startTime }
17+
})
18+
.on('query-response', (response, { __knexQueryUid: queryId }) => {
19+
const { sql, bindings, startTime } = queries[queryId]
20+
delete queries[queryId]
21+
22+
const diff = process.hrtime(startTime)
23+
const duration = diff[0] * 1e3 + diff[1] * 1e-6
24+
const sqlRequest = sql.split('?').reduce((memo, part, index) => {
25+
const binding = bindings[index] ? bindings[index] : ''
26+
return memo + part + binding
27+
}, '')
28+
29+
logger('%s %s %s',
30+
chalk.gray('SQL'),
31+
chalk.cyan(sqlRequest),
32+
chalk.magenta(duration.toFixed(3) + 'ms')
33+
)
34+
})
35+
return knex
36+
}

0 commit comments

Comments
 (0)