Comparing version 5.0.0 to 5.1.0
@@ -26,2 +26,4 @@ 'use strict'; | ||
var _config = require('./config'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -55,40 +57,21 @@ | ||
let globalQueryId = 0; | ||
const query = exports.query = async (connection, rawSql, values) => { | ||
const strippedSql = (0, _utilities.stripComments)(rawSql); | ||
const queryId = globalQueryId++; | ||
let rowCount = null; | ||
let normalized; | ||
log.debug({ | ||
queryId, | ||
sql: strippedSql | ||
}, 'input query'); | ||
const start = process.hrtime(); | ||
if (values) { | ||
log.trace({ | ||
queryId, | ||
values | ||
}, 'query variables'); | ||
} | ||
try { | ||
const start = process.hrtime(); | ||
let result; | ||
if (Array.isArray(values)) { | ||
const { | ||
sql: normalizedSql, | ||
values: normalizedValues | ||
} = (0, _utilities.normalizeAnonymousValuePlaceholders)(strippedSql, values); | ||
result = await connection.query(normalizedSql, normalizedValues); | ||
normalized = (0, _utilities.normalizeAnonymousValuePlaceholders)(strippedSql, values); | ||
} else if (values) { | ||
const { | ||
sql: normalizedSql, | ||
values: normalizedValues | ||
} = (0, _utilities.normalizeNamedValuePlaceholders)(strippedSql, values); | ||
normalized = (0, _utilities.normalizeNamedValuePlaceholders)(strippedSql, values); | ||
} | ||
result = await connection.query(normalizedSql, normalizedValues); | ||
if (normalized) { | ||
result = await connection.query(normalized.sql, normalized.values); | ||
} else { | ||
@@ -98,6 +81,2 @@ result = await connection.query(strippedSql); | ||
const end = process.hrtime(start); | ||
let rowCount = null; | ||
if (result.rowCount) { | ||
@@ -109,8 +88,2 @@ rowCount = result.rowCount; | ||
log.trace({ | ||
executionTime: (0, _prettyHrtime2.default)(end), | ||
queryId, | ||
rowCount | ||
}, 'query completed'); | ||
return result; | ||
@@ -123,2 +96,21 @@ } catch (error) { | ||
throw error; | ||
} finally { | ||
const end = process.hrtime(start); | ||
// eslint-disable-next-line flowtype/no-weak-types | ||
const payload = { | ||
executionTime: (0, _prettyHrtime2.default)(end), | ||
rowCount, | ||
sql: strippedSql | ||
}; | ||
if (_config.SLONIK_LOG_VALUES) { | ||
payload.values = values; | ||
} | ||
if (_config.SLONIK_LOG_NORMALISED) { | ||
payload.normalized = normalized; | ||
} | ||
log.debug(payload, 'query'); | ||
} | ||
@@ -125,0 +117,0 @@ }; |
@@ -11,12 +11,4 @@ 'use strict'; | ||
var _Logger = require('../Logger'); | ||
var _Logger2 = _interopRequireDefault(_Logger); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
const log = _Logger2.default.child({ | ||
namespace: 'normalizeAnonymousValuePlaceholders' | ||
}); | ||
const anonymousePlaceholdersRegex = /\?/g; | ||
@@ -29,2 +21,3 @@ | ||
exports.default = (sql, values = []) => { | ||
@@ -107,7 +100,2 @@ if (!anonymousePlaceholdersRegex.test(sql)) { | ||
log.trace({ | ||
sql: result, | ||
values | ||
}, 'normalized SQL'); | ||
return { | ||
@@ -114,0 +102,0 @@ sql: result, |
@@ -7,12 +7,3 @@ 'use strict'; | ||
var _Logger = require('../Logger'); | ||
var _Logger2 = _interopRequireDefault(_Logger); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
const log = _Logger2.default.child({ | ||
namespace: 'normalizeNamedValuePlaceholders' | ||
}); | ||
/** | ||
@@ -27,2 +18,3 @@ * @see https://regex101.com/r/KrEe8i/2 | ||
exports.default = (sql, values = {}) => { | ||
@@ -73,7 +65,2 @@ let chunkIndex = 0; | ||
log.trace({ | ||
sql: result, | ||
values | ||
}, 'normalized SQL'); | ||
return { | ||
@@ -80,0 +67,0 @@ sql: result, |
@@ -1,76 +0,1 @@ | ||
{ | ||
"author": { | ||
"email": "gajus@gajus.com", | ||
"name": "Gajus Kuizinas", | ||
"url": "http://gajus.com" | ||
}, | ||
"ava": { | ||
"babel": "inherit", | ||
"require": [ | ||
"babel-register" | ||
] | ||
}, | ||
"dependencies": { | ||
"ajv": "^5.3.0", | ||
"array-flatten": "^2.1.1", | ||
"es6-error": "^4.0.2", | ||
"pg": "^7.4.0", | ||
"pg-connection-string": "^2.0.0", | ||
"pretty-hrtime": "^1.0.3", | ||
"roarr": "^2.0.2" | ||
}, | ||
"description": "A PostgreSQL client with strict types and assertions.", | ||
"devDependencies": { | ||
"ava": "^0.23.0", | ||
"babel-cli": "^6.26.0", | ||
"babel-plugin-istanbul": "^4.1.5", | ||
"babel-plugin-transform-flow-strip-types": "^6.22.0", | ||
"babel-plugin-transform-object-rest-spread": "^6.26.0", | ||
"babel-preset-env": "^1.6.1", | ||
"babel-register": "^6.26.0", | ||
"coveralls": "^3.0.0", | ||
"eslint": "^4.10.0", | ||
"eslint-config-canonical": "^9.3.1", | ||
"flow-bin": "^0.58.0", | ||
"flow-copy-source": "^1.2.1", | ||
"husky": "^0.14.3", | ||
"nyc": "^11.3.0", | ||
"semantic-release": "^8.2.0", | ||
"sinon": "^4.1.2" | ||
}, | ||
"engines": { | ||
"node": ">=8.0" | ||
}, | ||
"keywords": [ | ||
"postgresql", | ||
"promise", | ||
"types" | ||
], | ||
"license": "BSD-3-Clause", | ||
"main": "./dist/index.js", | ||
"name": "slonik", | ||
"nyc": { | ||
"include": [ | ||
"src/**/*.js" | ||
], | ||
"instrument": false, | ||
"reporter": [ | ||
"text-lcov" | ||
], | ||
"require": [ | ||
"babel-register" | ||
], | ||
"sourceMap": false | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/gajus/slonik" | ||
}, | ||
"scripts": { | ||
"build": "rm -fr ./dist && NODE_ENV=production babel ./src --out-dir ./dist --copy-files --source-maps && flow-copy-source src dist", | ||
"lint": "eslint ./src ./test && flow", | ||
"precommit": "npm run lint && npm run test", | ||
"test": "ava --verbose" | ||
}, | ||
"version": "5.0.0" | ||
} | ||
{"author":{"email":"gajus@gajus.com","name":"Gajus Kuizinas","url":"http://gajus.com"},"ava":{"babel":"inherit","require":["babel-register"]},"dependencies":{"ajv":"^5.3.0","array-flatten":"^2.1.1","boolean":"^0.1.3","es6-error":"^4.0.2","pg":"^7.4.0","pg-connection-string":"^2.0.0","pretty-hrtime":"^1.0.3","roarr":"^2.0.2"},"description":"A PostgreSQL client with strict types and assertions.","devDependencies":{"ava":"^0.23.0","babel-cli":"^6.26.0","babel-plugin-istanbul":"^4.1.5","babel-plugin-transform-flow-strip-types":"^6.22.0","babel-plugin-transform-object-rest-spread":"^6.26.0","babel-preset-env":"^1.6.1","babel-register":"^6.26.0","coveralls":"^3.0.0","eslint":"^4.10.0","eslint-config-canonical":"^9.3.1","flow-bin":"^0.58.0","flow-copy-source":"^1.2.1","husky":"^0.14.3","nyc":"^11.3.0","semantic-release":"^8.2.0","sinon":"^4.1.2"},"engines":{"node":">=8.0"},"keywords":["postgresql","promise","types"],"license":"BSD-3-Clause","main":"./dist/index.js","name":"slonik","nyc":{"include":["src/**/*.js"],"instrument":false,"reporter":["text-lcov"],"require":["babel-register"],"sourceMap":false},"repository":{"type":"git","url":"https://github.com/gajus/slonik"},"scripts":{"build":"rm -fr ./dist && NODE_ENV=production babel ./src --out-dir ./dist --copy-files --source-maps && flow-copy-source src dist","lint":"eslint ./src ./test && flow","precommit":"npm run lint && npm run test","test":"ava --verbose"},"version":"5.1.0"} |
@@ -499,29 +499,18 @@ # Slonik | ||
Define `DEBUG=slonik*` environment variable to enable logging. | ||
Slonik uses [roarr](https://github.com/gajus/roarr) to log queries. | ||
Logging includes information about: | ||
To enable logging, define `ROARR_LOG=true` environment variable. | ||
* the query thats about to be executed | ||
* placeholder values | ||
* the execution time | ||
* the number of result rows | ||
By default, Slonik logs the input query, query execution time and affected row count. | ||
Here is the output example: | ||
You can enable additional logging details by configuring the following environment variables. | ||
```bash | ||
# Logs query parameter values | ||
export SLONIK_LOG_VALUES=true | ||
# Logs normalised query and input values | ||
export SLONIK_LOG_NORMALISED=true | ||
``` | ||
slonik query execution time 196 ms +199ms | ||
slonik query returned 4 row(s) +0ms | ||
slonik query SELECT * FROM `movie` WHERE id IN (1000223) +3ms | ||
slonik values [ 'movie', [ 1000223 ] ] +0ms | ||
slonik query execution time 28 ms +29ms | ||
slonik query returned 1 row(s) +0ms | ||
slonik query SELECT * FROM `movie` WHERE id IN (1000292) +3ms | ||
slonik values [ 'movie', [ 1000292 ] ] +0ms | ||
slonik query execution time 24 ms +25ms | ||
slonik query returned 1 row(s) +0ms | ||
slonik query SELECT * FROM `movie` WHERE id IN (1000220) +1ms | ||
slonik values [ 'movie', [ 1000220 ] ] +0ms | ||
slonik query execution time 26 ms +27ms | ||
slonik query returned 1 row(s) +0ms | ||
``` | ||
@@ -528,0 +517,0 @@ ## Syntax highlighting |
@@ -40,2 +40,6 @@ // @flow | ||
import Logger from './Logger'; | ||
import { | ||
SLONIK_LOG_VALUES, | ||
SLONIK_LOG_NORMALISED | ||
} from './config'; | ||
@@ -76,40 +80,21 @@ export type { | ||
let globalQueryId = 0; | ||
export const query: InternalQueryType<*> = async (connection, rawSql, values) => { | ||
const strippedSql = stripComments(rawSql); | ||
const queryId = globalQueryId++; | ||
let rowCount: number | null = null; | ||
let normalized; | ||
log.debug({ | ||
queryId, | ||
sql: strippedSql | ||
}, 'input query'); | ||
const start = process.hrtime(); | ||
if (values) { | ||
log.trace({ | ||
queryId, | ||
values | ||
}, 'query variables'); | ||
} | ||
try { | ||
const start = process.hrtime(); | ||
let result; | ||
if (Array.isArray(values)) { | ||
const { | ||
sql: normalizedSql, | ||
values: normalizedValues | ||
} = normalizeAnonymousValuePlaceholders(strippedSql, values); | ||
result = await connection.query(normalizedSql, normalizedValues); | ||
normalized = normalizeAnonymousValuePlaceholders(strippedSql, values); | ||
} else if (values) { | ||
const { | ||
sql: normalizedSql, | ||
values: normalizedValues | ||
} = normalizeNamedValuePlaceholders(strippedSql, values); | ||
normalized = normalizeNamedValuePlaceholders(strippedSql, values); | ||
} | ||
result = await connection.query(normalizedSql, normalizedValues); | ||
if (normalized) { | ||
result = await connection.query(normalized.sql, normalized.values); | ||
} else { | ||
@@ -119,6 +104,2 @@ result = await connection.query(strippedSql); | ||
const end = process.hrtime(start); | ||
let rowCount: number | null = null; | ||
if (result.rowCount) { | ||
@@ -130,8 +111,2 @@ rowCount = result.rowCount; | ||
log.trace({ | ||
executionTime: prettyHrtime(end), | ||
queryId, | ||
rowCount | ||
}, 'query completed'); | ||
return result; | ||
@@ -144,2 +119,21 @@ } catch (error) { | ||
throw error; | ||
} finally { | ||
const end = process.hrtime(start); | ||
// eslint-disable-next-line flowtype/no-weak-types | ||
const payload: Object = { | ||
executionTime: prettyHrtime(end), | ||
rowCount, | ||
sql: strippedSql | ||
}; | ||
if (SLONIK_LOG_VALUES) { | ||
payload.values = values; | ||
} | ||
if (SLONIK_LOG_NORMALISED) { | ||
payload.normalized = normalized; | ||
} | ||
log.debug(payload, 'query'); | ||
} | ||
@@ -146,0 +140,0 @@ }; |
@@ -8,8 +8,3 @@ // @flow | ||
} from '../types'; | ||
import Logger from '../Logger'; | ||
const log = Logger.child({ | ||
namespace: 'normalizeAnonymousValuePlaceholders' | ||
}); | ||
const anonymousePlaceholdersRegex = /\?/g; | ||
@@ -101,7 +96,2 @@ | ||
log.trace({ | ||
sql: result, | ||
values | ||
}, 'normalized SQL'); | ||
return { | ||
@@ -108,0 +98,0 @@ sql: result, |
@@ -7,8 +7,3 @@ // @flow | ||
} from '../types'; | ||
import Logger from '../Logger'; | ||
const log = Logger.child({ | ||
namespace: 'normalizeNamedValuePlaceholders' | ||
}); | ||
/** | ||
@@ -70,7 +65,2 @@ * @see https://regex101.com/r/KrEe8i/2 | ||
log.trace({ | ||
sql: result, | ||
values | ||
}, 'normalized SQL'); | ||
return { | ||
@@ -77,0 +67,0 @@ sql: result, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
43
118744
8
1192
529
4
+ Addedboolean@^0.1.3
+ Addedboolean@0.1.3(transitive)