Comparing version 6.0.0 to 6.1.0
@@ -48,3 +48,3 @@ "use strict"; | ||
}); | ||
exports.createPool = exports.createConnection = exports.sql = exports.transaction = exports.anyFirst = exports.any = exports.manyFirst = exports.many = exports.maybeOneFirst = exports.oneFirst = exports.maybeOne = exports.one = exports.query = void 0; | ||
exports.createPool = exports.createConnection = exports.transaction = exports.anyFirst = exports.any = exports.manyFirst = exports.many = exports.maybeOneFirst = exports.oneFirst = exports.maybeOne = exports.one = exports.query = exports.sql = void 0; | ||
@@ -100,2 +100,49 @@ var _pg = _interopRequireWildcard(require("pg")); | ||
const sql = (parts, ...values) => { | ||
let raw = ''; | ||
const bindings = []; | ||
let index = 0; | ||
for (const part of parts) { | ||
const value = values[index++]; | ||
raw += part; | ||
if (index >= parts.length) { | ||
// eslint-disable-next-line no-continue | ||
continue; | ||
} | ||
if (value && Array.isArray(value.names) && value.type === 'IDENTIFIER') { | ||
raw += value.names.map(identifierName => { | ||
if (typeof identifierName !== 'string') { | ||
throw new TypeError('Identifier name must be a string.'); | ||
} | ||
return (0, _utilities.escapeIdentifier)(identifierName); | ||
}).join('.'); // eslint-disable-next-line no-continue | ||
continue; | ||
} else { | ||
raw += '?'; | ||
bindings.push(value); | ||
} | ||
} | ||
return { | ||
sql: raw, | ||
values: bindings | ||
}; | ||
}; | ||
exports.sql = sql; | ||
sql.identifier = names => { | ||
// @todo Replace `type` with a symbol once Flow adds symbol support | ||
// @see https://github.com/facebook/flow/issues/810 | ||
return { | ||
names, | ||
type: 'IDENTIFIER' | ||
}; | ||
}; | ||
// eslint-disable-next-line complexity | ||
@@ -448,11 +495,2 @@ const query = async (connection, clientConfiguration, rawSql, values, queryId = ulid()) => { | ||
const sql = (parts, ...values) => { | ||
return { | ||
sql: parts.join('?'), | ||
values | ||
}; | ||
}; | ||
exports.sql = sql; | ||
const createConnection = async (connectionConfiguration, clientConfiguration = defaultClientConfiguration) => { | ||
@@ -459,0 +497,0 @@ const pool = new _pg.default.Pool(typeof connectionConfiguration === 'string' ? (0, _pgConnectionString.parse)(connectionConfiguration) : connectionConfiguration); |
@@ -6,2 +6,8 @@ "use strict"; | ||
}); | ||
Object.defineProperty(exports, "escapeIdentifier", { | ||
enumerable: true, | ||
get: function () { | ||
return _escapeIdentifier.default; | ||
} | ||
}); | ||
Object.defineProperty(exports, "mapTaggedTemplateLiteralInvocation", { | ||
@@ -32,2 +38,4 @@ enumerable: true, | ||
var _escapeIdentifier = _interopRequireDefault(require("./escapeIdentifier")); | ||
var _mapTaggedTemplateLiteralInvocation = _interopRequireDefault(require("./mapTaggedTemplateLiteralInvocation")); | ||
@@ -34,0 +42,0 @@ |
@@ -20,3 +20,3 @@ { | ||
"pretty-hrtime": "^1.0.3", | ||
"roarr": "^2.3.0", | ||
"roarr": "^2.5.0", | ||
"serialize-error": "^2.1.0", | ||
@@ -28,19 +28,19 @@ "stack-trace": "0.0.10", | ||
"devDependencies": { | ||
"@babel/cli": "^7.0.0-beta.51", | ||
"@babel/core": "^7.0.0-beta.51", | ||
"@babel/plugin-transform-flow-strip-types": "^7.0.0-beta.51", | ||
"@babel/preset-env": "^7.0.0-beta.51", | ||
"@babel/register": "^7.0.0-beta.51", | ||
"@babel/cli": "^7.0.0-beta.55", | ||
"@babel/core": "^7.0.0-beta.55", | ||
"@babel/plugin-transform-flow-strip-types": "^7.0.0-beta.55", | ||
"@babel/preset-env": "^7.0.0-beta.55", | ||
"@babel/register": "^7.0.0-beta.55", | ||
"ava": "^1.0.0-beta.6", | ||
"babel-plugin-istanbul": "^5.0.0", | ||
"babel-plugin-istanbul": "^5.0.1", | ||
"coveralls": "^3.0.2", | ||
"eslint": "^5.0.1", | ||
"eslint-config-canonical": "^10.3.2", | ||
"flow-bin": "^0.75.0", | ||
"flow-copy-source": "^2.0.1", | ||
"eslint": "^5.2.0", | ||
"eslint-config-canonical": "^11.0.0", | ||
"flow-bin": "^0.77.0", | ||
"flow-copy-source": "^2.0.2", | ||
"gitdown": "^2.5.2", | ||
"husky": "^1.0.0-rc.9", | ||
"nyc": "^13.0.0", | ||
"semantic-release": "^15.6.1", | ||
"sinon": "^6.0.1" | ||
"husky": "^1.0.0-rc.13", | ||
"nyc": "^13.0.1", | ||
"semantic-release": "^15.8.1", | ||
"sinon": "^6.1.4" | ||
}, | ||
@@ -93,3 +93,3 @@ "engines": { | ||
}, | ||
"version": "6.0.0" | ||
"version": "6.1.0" | ||
} |
@@ -309,2 +309,19 @@ <a name="slonik"></a> | ||
<a name="slonik-value-placeholders-tagged-template-literals-creating-dynamic-delimited-identifiers"></a> | ||
#### Creating dynamic delimited identifiers | ||
[Delimited identifiers](https://www.postgresql.org/docs/current/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS) are created by enclosing an arbitrary sequence of characters in double-quotes ("). To create create a delimited identifier, create an `sql` tag function placeholder value using `sql.identifier`, e.g. | ||
```js | ||
sql`SELECT ${'foo'} FROM ${sql.identifier(['bar', 'baz'])}`; | ||
// { | ||
// sql: 'SELECT ? FROM "bar"."baz"', | ||
// values: [ | ||
// 'foo' | ||
// ] | ||
// } | ||
``` | ||
<a name="slonik-value-placeholders-tagged-template-literals-guarding-against-accidental-unescaped-input"></a> | ||
@@ -311,0 +328,0 @@ #### Guarding against accidental unescaped input |
@@ -28,2 +28,3 @@ // @flow | ||
import { | ||
escapeIdentifier, | ||
mapTaggedTemplateLiteralInvocation, | ||
@@ -35,3 +36,3 @@ normalizeAnonymousValuePlaceholders, | ||
import type { | ||
AnonymouseValuePlaceholderValuesType, | ||
AnonymouseValuePlaceholderValueType, | ||
ClientConfigurationType, | ||
@@ -51,2 +52,3 @@ DatabaseConfigurationType, | ||
InternalTransactionFunctionType, | ||
QueryIdentifierType, | ||
TaggledTemplateLiteralInvocationType | ||
@@ -87,2 +89,54 @@ } from './types'; | ||
const sql = (parts: $ReadOnlyArray<string>, ...values: $ReadOnlyArray<AnonymouseValuePlaceholderValueType>): TaggledTemplateLiteralInvocationType => { | ||
let raw = ''; | ||
const bindings = []; | ||
let index = 0; | ||
for (const part of parts) { | ||
const value = values[index++]; | ||
raw += part; | ||
if (index >= parts.length) { | ||
// eslint-disable-next-line no-continue | ||
continue; | ||
} | ||
if (value && Array.isArray(value.names) && value.type === 'IDENTIFIER') { | ||
raw += value.names | ||
.map((identifierName) => { | ||
if (typeof identifierName !== 'string') { | ||
throw new TypeError('Identifier name must be a string.'); | ||
} | ||
return escapeIdentifier(identifierName); | ||
}) | ||
.join('.'); | ||
// eslint-disable-next-line no-continue | ||
continue; | ||
} else { | ||
raw += '?'; | ||
bindings.push(value); | ||
} | ||
} | ||
return { | ||
sql: raw, | ||
values: bindings | ||
}; | ||
}; | ||
sql.identifier = (names: $ReadOnlyArray<string>): QueryIdentifierType => { | ||
// @todo Replace `type` with a symbol once Flow adds symbol support | ||
// @see https://github.com/facebook/flow/issues/810 | ||
return { | ||
names, | ||
type: 'IDENTIFIER' | ||
}; | ||
}; | ||
export type { | ||
@@ -103,2 +157,3 @@ DatabaseConnectionType, | ||
SlonikError, | ||
sql, | ||
UniqueIntegrityConstraintViolationError | ||
@@ -452,9 +507,2 @@ }; | ||
export const sql = (parts: $ReadOnlyArray<string>, ...values: AnonymouseValuePlaceholderValuesType): TaggledTemplateLiteralInvocationType => { | ||
return { | ||
sql: parts.join('?'), | ||
values | ||
}; | ||
}; | ||
export const createConnection = async ( | ||
@@ -461,0 +509,0 @@ connectionConfiguration: DatabaseConfigurationType, |
@@ -91,2 +91,7 @@ // @flow | ||
export type QueryIdentifierType = {| | ||
names: $ReadOnlyArray<string>, | ||
type: 'IDENTIFIER' | ||
|}; | ||
type QueryPrimitiveValueType = string | number | null; | ||
@@ -99,6 +104,5 @@ | ||
$ReadOnlyArray<QueryPrimitiveValueType | $ReadOnlyArray<QueryPrimitiveValueType>> | | ||
QueryPrimitiveValueType; | ||
QueryPrimitiveValueType | | ||
QueryIdentifierType; | ||
export type AnonymouseValuePlaceholderValuesType = $ReadOnlyArray<AnonymouseValuePlaceholderValueType>; | ||
export type NamedValuePlaceholderValuesType = { | ||
@@ -109,3 +113,3 @@ +[key: string]: string | number | null | ||
export type DatabaseQueryValuesType = | ||
AnonymouseValuePlaceholderValuesType | | ||
$ReadOnlyArray<AnonymouseValuePlaceholderValueType> | | ||
NamedValuePlaceholderValuesType; | ||
@@ -112,0 +116,0 @@ |
// @flow | ||
export {default as escapeIdentifier} from './escapeIdentifier'; | ||
export {default as mapTaggedTemplateLiteralInvocation} from './mapTaggedTemplateLiteralInvocation'; | ||
@@ -4,0 +5,0 @@ export {default as normalizeAnonymousValuePlaceholders} from './normalizeAnonymousValuePlaceholders'; |
@@ -5,3 +5,3 @@ // @flow | ||
import type { | ||
AnonymouseValuePlaceholderValuesType, | ||
AnonymouseValuePlaceholderValueType, | ||
NormalizedQueryType | ||
@@ -18,3 +18,3 @@ } from '../types'; | ||
sql: string, | ||
values: AnonymouseValuePlaceholderValuesType = [] | ||
values: $ReadOnlyArray<AnonymouseValuePlaceholderValueType> = [] | ||
): NormalizedQueryType => { | ||
@@ -21,0 +21,0 @@ if (!anonymousePlaceholdersRegex.test(sql)) { |
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
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
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
164123
47
1624
744
Updatedroarr@^2.5.0