Skip to content

Commit

Permalink
style: use opaque type to describe query ID
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Nov 14, 2018
1 parent 86e0d2c commit 7fb30ba
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 30 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -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"
},
Expand Down
6 changes: 4 additions & 2 deletions src/connectionMethods/any.js
@@ -1,7 +1,7 @@
// @flow

import {
createUlid
createQueryId
} from '../utilities';
import type {
InternalQueryAnyFunctionType
Expand All @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions src/connectionMethods/anyFirst.js
@@ -1,7 +1,7 @@
// @flow

import {
createUlid
createQueryId
} from '../utilities';
import {
DataIntegrityError
Expand All @@ -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) {
Expand Down
6 changes: 4 additions & 2 deletions src/connectionMethods/many.js
@@ -1,7 +1,7 @@
// @flow

import {
createUlid
createQueryId
} from '../utilities';
import {
NotFoundError
Expand All @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions src/connectionMethods/manyFirst.js
@@ -1,7 +1,7 @@
// @flow

import {
createUlid
createQueryId
} from '../utilities';
import {
DataIntegrityError
Expand All @@ -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) {
Expand Down
6 changes: 4 additions & 2 deletions src/connectionMethods/maybeOne.js
@@ -1,7 +1,7 @@
// @flow

import {
createUlid
createQueryId
} from '../utilities';
import {
DataIntegrityError
Expand All @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions src/connectionMethods/maybeOneFirst.js
@@ -1,7 +1,7 @@
// @flow

import {
createUlid
createQueryId
} from '../utilities';
import {
DataIntegrityError
Expand All @@ -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) {
Expand Down
6 changes: 4 additions & 2 deletions src/connectionMethods/one.js
Expand Up @@ -8,7 +8,7 @@ import {
NotFoundError
} from '../errors';
import {
createUlid
createQueryId
} from '../utilities';
import log from '../Logger';
import query from './query';
Expand All @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions src/connectionMethods/oneFirst.js
@@ -1,7 +1,7 @@
// @flow

import {
createUlid
createQueryId
} from '../utilities';
import {
DataIntegrityError
Expand All @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions src/connectionMethods/query.js
Expand Up @@ -6,7 +6,7 @@ import {
getStackTrace
} from 'get-stack-trace';
import {
createUlid,
createQueryId,
normalizeAnonymousValuePlaceholders,
normalizeNamedValuePlaceholders,
stripComments
Expand All @@ -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) {
Expand Down
20 changes: 11 additions & 9 deletions src/types.js
Expand Up @@ -2,6 +2,8 @@

/* eslint-disable no-use-before-define, import/exports-last */

export opaque type QueryIdType = string;

type FieldType = {
+columnID: number,
+dataTypeID: number,
Expand Down Expand Up @@ -116,70 +118,70 @@ export type InternalQueryAnyFunctionType = (
clientConfiguration: ClientConfigurationType,
sql: string,
values?: DatabaseQueryValuesType,
queryId?: string
queryId?: QueryIdType
) => Promise<$ReadOnlyArray<QueryResultRowType>>;

export type InternalQueryAnyFirstFunctionType = (
connection: InternalDatabaseConnectionType,
clientConfiguration: ClientConfigurationType,
sql: string,
values?: DatabaseQueryValuesType,
queryId?: string
queryId?: QueryIdType
) => Promise<$ReadOnlyArray<QueryResultRowColumnType>>;

export type InternalQueryManyFunctionType = (
connection: InternalDatabaseConnectionType,
clientConfiguration: ClientConfigurationType,
sql: string,
values?: DatabaseQueryValuesType,
queryId?: string
queryId?: QueryIdType
) => Promise<$ReadOnlyArray<QueryResultRowType>>;

export type InternalQueryManyFirstFunctionType = (
connection: InternalDatabaseConnectionType,
clientConfiguration: ClientConfigurationType,
sql: string,
values?: DatabaseQueryValuesType,
queryId?: string
queryId?: QueryIdType
) => Promise<$ReadOnlyArray<QueryResultRowColumnType>>;

export type InternalQueryMaybeOneFirstFunctionType = (
connection: InternalDatabaseConnectionType,
clientConfiguration: ClientConfigurationType,
sql: string,
values?: DatabaseQueryValuesType,
queryId?: string
queryId?: QueryIdType
) => Promise<QueryResultRowColumnType | null>;

export type InternalQueryMaybeOneFunctionType = (
connection: InternalDatabaseConnectionType,
clientConfiguration: ClientConfigurationType,
sql: string,
values?: DatabaseQueryValuesType,
queryId?: string
queryId?: QueryIdType
) => Promise<QueryResultRowType | null>;

export type InternalQueryOneFirstFunctionType = (
connection: InternalDatabaseConnectionType,
clientConfiguration: ClientConfigurationType,
sql: string,
values?: DatabaseQueryValuesType,
queryId?: string
queryId?: QueryIdType
) => Promise<QueryResultRowColumnType>;

export type InternalQueryOneFunctionType = (
connection: InternalDatabaseConnectionType,
clientConfiguration: ClientConfigurationType,
sql: string,
values?: DatabaseQueryValuesType,
queryId?: string
queryId?: QueryIdType
) => Promise<QueryResultRowType>;

export type TransactionHandlerType = (connection: DatabaseConnectionType) => Promise<*>;

export type InternalTransactionFunctionType = (connection: InternalDatabaseConnectionType, handler: TransactionHandlerType) => Promise<*>;

export type InternalQueryFunctionType<T: QueryResultRowType> = (connection: InternalDatabaseConnectionType, clientConfiguration: ClientConfigurationType, sql: string, values?: DatabaseQueryValuesType, queryId?: string) => Promise<QueryResultType<T>>;
export type InternalQueryFunctionType<T: QueryResultRowType> = (connection: InternalDatabaseConnectionType, clientConfiguration: ClientConfigurationType, sql: string, values?: DatabaseQueryValuesType, queryId?: QueryIdType) => Promise<QueryResultType<T>>;

export type QueryAnyFirstFunctionType<T: QueryResultRowColumnType> = (sql: string | TaggledTemplateLiteralInvocationType, values?: DatabaseQueryValuesType) => Promise<$ReadOnlyArray<T>>;
export type QueryAnyFunctionType<T: QueryResultRowType> = (sql: string | TaggledTemplateLiteralInvocationType, values?: DatabaseQueryValuesType) => Promise<$ReadOnlyArray<T>>;
Expand Down
Expand Up @@ -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));
};
2 changes: 1 addition & 1 deletion 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';
Expand Down

0 comments on commit 7fb30ba

Please sign in to comment.