slonik
Advanced tools
Comparing version 11.0.0 to 11.1.0
@@ -18,2 +18,8 @@ "use strict"; | ||
}); | ||
Object.defineProperty(exports, "createFormatFieldNameInterceptor", { | ||
enumerable: true, | ||
get: function () { | ||
return _interceptors.createFormatFieldNameInterceptor; | ||
} | ||
}); | ||
Object.defineProperty(exports, "sql", { | ||
@@ -78,2 +84,4 @@ enumerable: true, | ||
var _interceptors = require("./interceptors"); | ||
var _templateTags = require("./templateTags"); | ||
@@ -80,0 +88,0 @@ |
@@ -16,2 +16,3 @@ { | ||
"boolean": "^0.2.0", | ||
"camelcase": "^5.0.0", | ||
"crack-json": "^1.1.0", | ||
@@ -95,3 +96,3 @@ "es6-error": "^4.1.1", | ||
}, | ||
"version": "11.0.0" | ||
"version": "11.1.0" | ||
} |
@@ -44,2 +44,4 @@ <a name="slonik"></a> | ||
* [`afterQuery`](#slonik-interceptors-afterquery) | ||
* [Built-in interceptors](#slonik-built-in-interceptors) | ||
* [Field name formatter](#slonik-built-in-interceptors-field-name-formatter) | ||
* [Recipes](#slonik-recipes) | ||
@@ -260,3 +262,77 @@ * [Logging `auto_explain`](#slonik-recipes-logging-auto_explain) | ||
<a name="slonik-built-in-interceptors"></a> | ||
## Built-in interceptors | ||
<a name="slonik-built-in-interceptors-field-name-formatter"></a> | ||
### Field name formatter | ||
`createFormatFieldNameInterceptor` creates an interceptor that formats query result field names. | ||
This interceptor removes the necessity to alias field names, e.g. | ||
```js | ||
connection.any(sql` | ||
SELECT | ||
id, | ||
full_name "fullName" | ||
FROM person | ||
`); | ||
``` | ||
Field name formatter uses `afterQuery` interceptor to format field names. | ||
<a name="slonik-built-in-interceptors-field-name-formatter-api"></a> | ||
#### API | ||
```js | ||
/** | ||
* @property format The only supported format is CAMEL_CASE. | ||
* @property test Tests whether the field should be formatted. The default behaviour is to include all fields that match ^[a-z0-9_]+$ regex. | ||
*/ | ||
type ConfigurationType = {| | ||
+format: 'CAMEL_CASE', | ||
+test: (field: FieldType) => boolean | ||
|}; | ||
(configuration: ConfigurationType) => InterceptorType; | ||
``` | ||
<a name="slonik-built-in-interceptors-field-name-formatter-example-usage"></a> | ||
#### Example usage | ||
```js | ||
import { | ||
createFormatFieldNameInterceptor, | ||
createPool | ||
} from 'slonik'; | ||
const interceptors = [ | ||
createFormatFieldNameInterceptor({ | ||
format: 'CAMEL_CASE' | ||
}) | ||
]; | ||
const connection = createPool('postgres://', { | ||
interceptors | ||
}); | ||
connection.any(sql` | ||
SELECT | ||
id, | ||
full_name | ||
FROM person | ||
`); | ||
// [ | ||
// { | ||
// id: 1, | ||
// fullName: 1 | ||
// } | ||
// ] | ||
``` | ||
<a name="slonik-recipes"></a> | ||
@@ -263,0 +339,0 @@ ## Recipes |
@@ -37,2 +37,5 @@ // @flow | ||
export { | ||
createFormatFieldNameInterceptor | ||
} from './interceptors'; | ||
export { | ||
sql | ||
@@ -39,0 +42,0 @@ } from './templateTags'; |
@@ -17,3 +17,3 @@ // @flow | ||
type FieldType = {| | ||
export type FieldType = {| | ||
+columnID: number, | ||
@@ -20,0 +20,0 @@ +dataTypeID: number, |
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
254452
159
2770
978
13
+ Addedcamelcase@^5.0.0
+ Addedcamelcase@5.3.1(transitive)