diff --git a/package.json b/package.json index 6026ec05..2c0573fe 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "pg": "^7.6.1", "pg-connection-string": "^2.0.0", "pretty-hrtime": "^1.0.3", - "roarr": "^2.11.7", + "roarr": "^2.11.8", "serialize-error": "^3.0.0", "ulid": "^2.3.0" }, diff --git a/src/connectionMethods/any.js b/src/connectionMethods/any.js index afdf96be..41bcde41 100644 --- a/src/connectionMethods/any.js +++ b/src/connectionMethods/any.js @@ -1,7 +1,7 @@ // @flow import { - createUlid + createQueryId } from '../utilities'; import type { InternalQueryAnyFunctionType @@ -11,7 +11,9 @@ import query from './query'; /** * Makes a query and expects any number of results. */ -const any: InternalQueryAnyFunctionType = async (connection, clientConfiguration, rawSql, values, queryId = createUlid()) => { +const any: InternalQueryAnyFunctionType = async (connection, clientConfiguration, rawSql, values, inheritedQueryId) => { + const queryId = inheritedQueryId || createQueryId(); + const { rows } = await query(connection, clientConfiguration, rawSql, values, queryId); diff --git a/src/connectionMethods/anyFirst.js b/src/connectionMethods/anyFirst.js index d436af8c..c1c40ef9 100644 --- a/src/connectionMethods/anyFirst.js +++ b/src/connectionMethods/anyFirst.js @@ -1,7 +1,7 @@ // @flow import { - createUlid + createQueryId } from '../utilities'; import { DataIntegrityError @@ -12,7 +12,9 @@ import type { import log from '../Logger'; import any from './any'; -const anyFirst: InternalQueryAnyFirstFunctionType = async (connection, clientConfigurationType, rawSql, values, queryId = createUlid()) => { +const anyFirst: InternalQueryAnyFirstFunctionType = async (connection, clientConfigurationType, rawSql, values, inheritedQueryId) => { + const queryId = inheritedQueryId || createQueryId(); + const rows = await any(connection, clientConfigurationType, rawSql, values, queryId); if (rows.length === 0) { diff --git a/src/connectionMethods/many.js b/src/connectionMethods/many.js index 131f83c3..00d86d0b 100644 --- a/src/connectionMethods/many.js +++ b/src/connectionMethods/many.js @@ -1,7 +1,7 @@ // @flow import { - createUlid + createQueryId } from '../utilities'; import { NotFoundError @@ -17,7 +17,9 @@ import query from './query'; * * @throws NotFoundError If query returns no rows. */ -const many: InternalQueryManyFunctionType = async (connection, clientConfiguration, rawSql, values, queryId = createUlid()) => { +const many: InternalQueryManyFunctionType = async (connection, clientConfiguration, rawSql, values, inheritedQueryId) => { + const queryId = inheritedQueryId || createQueryId(); + const { rows } = await query(connection, clientConfiguration, rawSql, values, queryId); diff --git a/src/connectionMethods/manyFirst.js b/src/connectionMethods/manyFirst.js index 6faf34ae..7335fbc3 100644 --- a/src/connectionMethods/manyFirst.js +++ b/src/connectionMethods/manyFirst.js @@ -1,7 +1,7 @@ // @flow import { - createUlid + createQueryId } from '../utilities'; import { DataIntegrityError @@ -12,7 +12,9 @@ import type { import log from '../Logger'; import many from './many'; -const manyFirst: InternalQueryManyFirstFunctionType = async (connection, clientConfigurationType, rawSql, values, queryId = createUlid()) => { +const manyFirst: InternalQueryManyFirstFunctionType = async (connection, clientConfigurationType, rawSql, values, inheritedQueryId) => { + const queryId = inheritedQueryId || createQueryId(); + const rows = await many(connection, clientConfigurationType, rawSql, values, queryId); if (rows.length === 0) { diff --git a/src/connectionMethods/maybeOne.js b/src/connectionMethods/maybeOne.js index d8f86dbd..d28d6b0d 100644 --- a/src/connectionMethods/maybeOne.js +++ b/src/connectionMethods/maybeOne.js @@ -1,7 +1,7 @@ // @flow import { - createUlid + createQueryId } from '../utilities'; import { DataIntegrityError @@ -17,7 +17,9 @@ import query from './query'; * * @throws DataIntegrityError If query returns multiple rows. */ -const maybeOne: InternalQueryMaybeOneFunctionType = async (connection, clientConfiguration, rawSql, values, queryId = createUlid()) => { +const maybeOne: InternalQueryMaybeOneFunctionType = async (connection, clientConfiguration, rawSql, values, inheritedQueryId) => { + const queryId = inheritedQueryId || createQueryId(); + const { rows } = await query(connection, clientConfiguration, rawSql, values, queryId); diff --git a/src/connectionMethods/maybeOneFirst.js b/src/connectionMethods/maybeOneFirst.js index 8f38d36e..903da89c 100644 --- a/src/connectionMethods/maybeOneFirst.js +++ b/src/connectionMethods/maybeOneFirst.js @@ -1,7 +1,7 @@ // @flow import { - createUlid + createQueryId } from '../utilities'; import { DataIntegrityError @@ -18,7 +18,9 @@ import maybeOne from './maybeOne'; * * @throws DataIntegrityError If query returns multiple rows. */ -const maybeOneFirst: InternalQueryMaybeOneFirstFunctionType = async (connection, clientConfiguration, rawSql, values, queryId = createUlid()) => { +const maybeOneFirst: InternalQueryMaybeOneFirstFunctionType = async (connection, clientConfiguration, rawSql, values, inheritedQueryId) => { + const queryId = inheritedQueryId || createQueryId(); + const row = await maybeOne(connection, clientConfiguration, rawSql, values, queryId); if (!row) { diff --git a/src/connectionMethods/one.js b/src/connectionMethods/one.js index b762f1b9..f016913f 100644 --- a/src/connectionMethods/one.js +++ b/src/connectionMethods/one.js @@ -8,7 +8,7 @@ import { NotFoundError } from '../errors'; import { - createUlid + createQueryId } from '../utilities'; import log from '../Logger'; import query from './query'; @@ -19,7 +19,9 @@ import query from './query'; * @throws NotFoundError If query returns no rows. * @throws DataIntegrityError If query returns multiple rows. */ -const one: InternalQueryOneFunctionType = async (connection, clientConfiguration, rawSql, values, queryId = createUlid()) => { +const one: InternalQueryOneFunctionType = async (connection, clientConfiguration, rawSql, values, inheritedQueryId) => { + const queryId = inheritedQueryId || createQueryId(); + const { rows } = await query(connection, clientConfiguration, rawSql, values, queryId); diff --git a/src/connectionMethods/oneFirst.js b/src/connectionMethods/oneFirst.js index 876fb0ba..df49ff91 100644 --- a/src/connectionMethods/oneFirst.js +++ b/src/connectionMethods/oneFirst.js @@ -1,7 +1,7 @@ // @flow import { - createUlid + createQueryId } from '../utilities'; import { DataIntegrityError @@ -19,7 +19,9 @@ import one from './one'; * @throws NotFoundError If query returns no rows. * @throws DataIntegrityError If query returns multiple rows. */ -const oneFirst: InternalQueryOneFirstFunctionType = async (connection, clientConfiguration, rawSql, values, queryId = createUlid()) => { +const oneFirst: InternalQueryOneFirstFunctionType = async (connection, clientConfiguration, rawSql, values, inheritedQueryId) => { + const queryId = inheritedQueryId || createQueryId(); + const row = await one(connection, clientConfiguration, rawSql, values, queryId); const keys = Object.keys(row); diff --git a/src/connectionMethods/query.js b/src/connectionMethods/query.js index b024ef54..03739609 100644 --- a/src/connectionMethods/query.js +++ b/src/connectionMethods/query.js @@ -6,7 +6,7 @@ import { getStackTrace } from 'get-stack-trace'; import { - createUlid, + createQueryId, normalizeAnonymousValuePlaceholders, normalizeNamedValuePlaceholders, stripComments @@ -27,7 +27,9 @@ import type { } from '../types'; import log from '../Logger'; -const query: InternalQueryFunctionType<*> = async (connection, clientConfiguration, rawSql, values, queryId = createUlid()) => { +const query: InternalQueryFunctionType<*> = async (connection, clientConfiguration, rawSql, values, inheritedQueryId) => { + const queryId = inheritedQueryId || createQueryId(); + let stackTrace; if (SLONIK_LOG_STACK_TRACE) { diff --git a/src/types.js b/src/types.js index 73d6e24c..ec628ec4 100644 --- a/src/types.js +++ b/src/types.js @@ -2,6 +2,8 @@ /* eslint-disable no-use-before-define, import/exports-last */ +export opaque type QueryIdType = string; + type FieldType = { +columnID: number, +dataTypeID: number, @@ -116,7 +118,7 @@ export type InternalQueryAnyFunctionType = ( clientConfiguration: ClientConfigurationType, sql: string, values?: DatabaseQueryValuesType, - queryId?: string + queryId?: QueryIdType ) => Promise<$ReadOnlyArray>; export type InternalQueryAnyFirstFunctionType = ( @@ -124,7 +126,7 @@ export type InternalQueryAnyFirstFunctionType = ( clientConfiguration: ClientConfigurationType, sql: string, values?: DatabaseQueryValuesType, - queryId?: string + queryId?: QueryIdType ) => Promise<$ReadOnlyArray>; export type InternalQueryManyFunctionType = ( @@ -132,7 +134,7 @@ export type InternalQueryManyFunctionType = ( clientConfiguration: ClientConfigurationType, sql: string, values?: DatabaseQueryValuesType, - queryId?: string + queryId?: QueryIdType ) => Promise<$ReadOnlyArray>; export type InternalQueryManyFirstFunctionType = ( @@ -140,7 +142,7 @@ export type InternalQueryManyFirstFunctionType = ( clientConfiguration: ClientConfigurationType, sql: string, values?: DatabaseQueryValuesType, - queryId?: string + queryId?: QueryIdType ) => Promise<$ReadOnlyArray>; export type InternalQueryMaybeOneFirstFunctionType = ( @@ -148,7 +150,7 @@ export type InternalQueryMaybeOneFirstFunctionType = ( clientConfiguration: ClientConfigurationType, sql: string, values?: DatabaseQueryValuesType, - queryId?: string + queryId?: QueryIdType ) => Promise; export type InternalQueryMaybeOneFunctionType = ( @@ -156,7 +158,7 @@ export type InternalQueryMaybeOneFunctionType = ( clientConfiguration: ClientConfigurationType, sql: string, values?: DatabaseQueryValuesType, - queryId?: string + queryId?: QueryIdType ) => Promise; export type InternalQueryOneFirstFunctionType = ( @@ -164,7 +166,7 @@ export type InternalQueryOneFirstFunctionType = ( clientConfiguration: ClientConfigurationType, sql: string, values?: DatabaseQueryValuesType, - queryId?: string + queryId?: QueryIdType ) => Promise; export type InternalQueryOneFunctionType = ( @@ -172,14 +174,14 @@ export type InternalQueryOneFunctionType = ( clientConfiguration: ClientConfigurationType, sql: string, values?: DatabaseQueryValuesType, - queryId?: string + queryId?: QueryIdType ) => Promise; export type TransactionHandlerType = (connection: DatabaseConnectionType) => Promise<*>; export type InternalTransactionFunctionType = (connection: InternalDatabaseConnectionType, handler: TransactionHandlerType) => Promise<*>; -export type InternalQueryFunctionType = (connection: InternalDatabaseConnectionType, clientConfiguration: ClientConfigurationType, sql: string, values?: DatabaseQueryValuesType, queryId?: string) => Promise>; +export type InternalQueryFunctionType = (connection: InternalDatabaseConnectionType, clientConfiguration: ClientConfigurationType, sql: string, values?: DatabaseQueryValuesType, queryId?: QueryIdType) => Promise>; export type QueryAnyFirstFunctionType = (sql: string | TaggledTemplateLiteralInvocationType, values?: DatabaseQueryValuesType) => Promise<$ReadOnlyArray>; export type QueryAnyFunctionType = (sql: string | TaggledTemplateLiteralInvocationType, values?: DatabaseQueryValuesType) => Promise<$ReadOnlyArray>; diff --git a/src/utilities/createUlid.js b/src/utilities/createQueryId.js similarity index 58% rename from src/utilities/createUlid.js rename to src/utilities/createQueryId.js index afaf1a62..4ba9aaac 100644 --- a/src/utilities/createUlid.js +++ b/src/utilities/createQueryId.js @@ -4,7 +4,10 @@ import { factory as ulidFactory, detectPrng } from 'ulid'; +import type { + QueryIdType +} from '../types'; -export default (): string => { +export default (): QueryIdType => { return ulidFactory(detectPrng(true)); }; diff --git a/src/utilities/index.js b/src/utilities/index.js index 73dc51e5..24a31971 100644 --- a/src/utilities/index.js +++ b/src/utilities/index.js @@ -1,6 +1,6 @@ // @flow -export {default as createUlid} from './createUlid'; +export {default as createQueryId} from './createQueryId'; export {default as escapeIdentifier} from './escapeIdentifier'; export {default as mapTaggedTemplateLiteralInvocation} from './mapTaggedTemplateLiteralInvocation'; export {default as normalizeAnonymousValuePlaceholders} from './normalizeAnonymousValuePlaceholders';