Comparing version 34.3.0 to 35.0.1
@@ -21,3 +21,3 @@ "use strict"; | ||
}, 'DataIntegrityError'); | ||
throw new errors_1.UnexpectedStateError(); | ||
throw new errors_1.UnexpectedStateError('Expected query to return one result, but received multiple results.'); | ||
} | ||
@@ -24,0 +24,0 @@ return row[keys[0]]; |
@@ -5,7 +5,7 @@ import { type PrimitiveValueExpression, type Query, type QueryResultRow } from './types'; | ||
export declare class SlonikError extends ExtendableError { | ||
} | ||
declare class WrappedPGError extends SlonikError { | ||
readonly message: string; | ||
readonly originalError: Error; | ||
constructor(originalError: Error, message: string); | ||
readonly cause?: Error; | ||
constructor(message: string, options?: { | ||
cause?: Error; | ||
}); | ||
} | ||
@@ -16,2 +16,6 @@ export declare class InvalidConfigurationError extends SlonikError { | ||
} | ||
export declare class InputSyntaxError extends SlonikError { | ||
sql: string; | ||
constructor(error: Error, query: Query); | ||
} | ||
export declare class UnexpectedStateError extends SlonikError { | ||
@@ -21,13 +25,13 @@ } | ||
} | ||
export declare class StatementCancelledError extends WrappedPGError { | ||
constructor(error: Error, message?: string); | ||
export declare class StatementCancelledError extends SlonikError { | ||
constructor(error: Error); | ||
} | ||
export declare class StatementTimeoutError extends StatementCancelledError { | ||
export declare class StatementTimeoutError extends SlonikError { | ||
constructor(error: Error); | ||
} | ||
export declare class BackendTerminatedError extends WrappedPGError { | ||
export declare class BackendTerminatedError extends SlonikError { | ||
constructor(error: Error); | ||
} | ||
export declare class TupleMovedToAnotherPartitionError extends WrappedPGError { | ||
constructor(error: Error, message?: string); | ||
export declare class TupleMovedToAnotherPartitionError extends SlonikError { | ||
constructor(error: Error); | ||
} | ||
@@ -51,19 +55,23 @@ export declare class NotFoundError extends SlonikError { | ||
} | ||
export declare class IntegrityConstraintViolationError extends WrappedPGError { | ||
type IntegrityConstraintViolationErrorCause = Error & { | ||
constraint: string; | ||
constructor(error: Error, constraint: string, message?: string); | ||
}; | ||
export declare class IntegrityConstraintViolationError extends SlonikError { | ||
constraint: string; | ||
cause?: Error; | ||
constructor(message: string, error: IntegrityConstraintViolationErrorCause); | ||
} | ||
export declare class NotNullIntegrityConstraintViolationError extends IntegrityConstraintViolationError { | ||
constructor(error: Error, constraint: string); | ||
constructor(error: IntegrityConstraintViolationErrorCause); | ||
} | ||
export declare class ForeignKeyIntegrityConstraintViolationError extends IntegrityConstraintViolationError { | ||
constructor(error: Error, constraint: string); | ||
constructor(error: IntegrityConstraintViolationErrorCause); | ||
} | ||
export declare class UniqueIntegrityConstraintViolationError extends IntegrityConstraintViolationError { | ||
constructor(error: Error, constraint: string); | ||
constructor(error: IntegrityConstraintViolationErrorCause); | ||
} | ||
export declare class CheckIntegrityConstraintViolationError extends IntegrityConstraintViolationError { | ||
constructor(error: Error, constraint: string); | ||
constructor(error: IntegrityConstraintViolationErrorCause); | ||
} | ||
export {}; | ||
//# sourceMappingURL=errors.d.ts.map |
@@ -6,13 +6,12 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.CheckIntegrityConstraintViolationError = exports.UniqueIntegrityConstraintViolationError = exports.ForeignKeyIntegrityConstraintViolationError = exports.NotNullIntegrityConstraintViolationError = exports.IntegrityConstraintViolationError = exports.SchemaValidationError = exports.DataIntegrityError = exports.NotFoundError = exports.TupleMovedToAnotherPartitionError = exports.BackendTerminatedError = exports.StatementTimeoutError = exports.StatementCancelledError = exports.ConnectionError = exports.UnexpectedStateError = exports.InvalidInputError = exports.InvalidConfigurationError = exports.SlonikError = void 0; | ||
exports.CheckIntegrityConstraintViolationError = exports.UniqueIntegrityConstraintViolationError = exports.ForeignKeyIntegrityConstraintViolationError = exports.NotNullIntegrityConstraintViolationError = exports.IntegrityConstraintViolationError = exports.SchemaValidationError = exports.DataIntegrityError = exports.NotFoundError = exports.TupleMovedToAnotherPartitionError = exports.BackendTerminatedError = exports.StatementTimeoutError = exports.StatementCancelledError = exports.ConnectionError = exports.UnexpectedStateError = exports.InputSyntaxError = exports.InvalidInputError = exports.InvalidConfigurationError = exports.SlonikError = void 0; | ||
const es6_error_1 = __importDefault(require("es6-error")); | ||
class SlonikError extends es6_error_1.default { | ||
constructor(message, options) { | ||
super(message); | ||
this.message = message || this.constructor.name; | ||
this.cause = options === null || options === void 0 ? void 0 : options.cause; | ||
} | ||
} | ||
exports.SlonikError = SlonikError; | ||
class WrappedPGError extends SlonikError { | ||
constructor(originalError, message) { | ||
super(`${message} ${originalError.message}`); | ||
this.originalError = originalError; | ||
} | ||
} | ||
class InvalidConfigurationError extends SlonikError { | ||
@@ -24,2 +23,11 @@ } | ||
exports.InvalidInputError = InvalidInputError; | ||
class InputSyntaxError extends SlonikError { | ||
constructor(error, query) { | ||
super(error.message, { | ||
cause: error, | ||
}); | ||
this.sql = query.sql; | ||
} | ||
} | ||
exports.InputSyntaxError = InputSyntaxError; | ||
class UnexpectedStateError extends SlonikError { | ||
@@ -31,23 +39,27 @@ } | ||
exports.ConnectionError = ConnectionError; | ||
class StatementCancelledError extends WrappedPGError { | ||
constructor(error, message = 'Statement has been cancelled.') { | ||
super(error, message); | ||
class StatementCancelledError extends SlonikError { | ||
constructor(error) { | ||
super('Statement has been cancelled.', { cause: error }); | ||
} | ||
} | ||
exports.StatementCancelledError = StatementCancelledError; | ||
class StatementTimeoutError extends StatementCancelledError { | ||
class StatementTimeoutError extends SlonikError { | ||
constructor(error) { | ||
super(error, 'Statement has been cancelled due to a statement_timeout.'); | ||
super('Statement has been cancelled due to a statement_timeout.', { | ||
cause: error, | ||
}); | ||
} | ||
} | ||
exports.StatementTimeoutError = StatementTimeoutError; | ||
class BackendTerminatedError extends WrappedPGError { | ||
class BackendTerminatedError extends SlonikError { | ||
constructor(error) { | ||
super(error, 'Backend has been terminated.'); | ||
super('Backend has been terminated.', { cause: error }); | ||
} | ||
} | ||
exports.BackendTerminatedError = BackendTerminatedError; | ||
class TupleMovedToAnotherPartitionError extends WrappedPGError { | ||
constructor(error, message = 'Tuple moved to another partition due to concurrent update.') { | ||
super(error, message); | ||
class TupleMovedToAnotherPartitionError extends SlonikError { | ||
constructor(error) { | ||
super('Tuple moved to another partition due to concurrent update.', { | ||
cause: error, | ||
}); | ||
} | ||
@@ -66,3 +78,3 @@ } | ||
constructor(query) { | ||
super('Query returns an unexpected result.'); | ||
super('Query returned an unexpected result.'); | ||
this.sql = query.sql; | ||
@@ -83,6 +95,9 @@ this.values = query.values; | ||
exports.SchemaValidationError = SchemaValidationError; | ||
class IntegrityConstraintViolationError extends WrappedPGError { | ||
constructor(error, constraint, message = 'Query violates an integrity constraint.') { | ||
super(error, message); | ||
this.constraint = constraint; | ||
class IntegrityConstraintViolationError extends SlonikError { | ||
constructor(message, error) { | ||
super(message, { cause: error }); | ||
if (!error.constraint) { | ||
throw new Error('IntegrityConstraintViolationError requires constraint name.'); | ||
} | ||
this.constraint = error.constraint; | ||
} | ||
@@ -94,4 +109,4 @@ } | ||
class NotNullIntegrityConstraintViolationError extends IntegrityConstraintViolationError { | ||
constructor(error, constraint) { | ||
super(error, constraint, 'Query violates a not NULL integrity constraint.'); | ||
constructor(error) { | ||
super('Query violates a not NULL integrity constraint.', error); | ||
} | ||
@@ -101,4 +116,4 @@ } | ||
class ForeignKeyIntegrityConstraintViolationError extends IntegrityConstraintViolationError { | ||
constructor(error, constraint) { | ||
super(error, constraint, 'Query violates a foreign key integrity constraint.'); | ||
constructor(error) { | ||
super('Query violates a foreign key integrity constraint.', error); | ||
} | ||
@@ -108,4 +123,4 @@ } | ||
class UniqueIntegrityConstraintViolationError extends IntegrityConstraintViolationError { | ||
constructor(error, constraint) { | ||
super(error, constraint, 'Query violates a unique integrity constraint.'); | ||
constructor(error) { | ||
super('Query violates a unique integrity constraint.', error); | ||
} | ||
@@ -115,4 +130,4 @@ } | ||
class CheckIntegrityConstraintViolationError extends IntegrityConstraintViolationError { | ||
constructor(error, constraint) { | ||
super(error, constraint, 'Query violates a check integrity constraint.'); | ||
constructor(error) { | ||
super('Query violates a check integrity constraint.', error); | ||
} | ||
@@ -119,0 +134,0 @@ } |
import { type ClientConfiguration, type Connection, type DatabasePool, type DatabasePoolConnection, type Logger, type MaybePromise, type QuerySqlToken } from '../types'; | ||
import { type Pool as PgPool, type PoolClient as PgPoolClient } from 'pg'; | ||
type ConnectionHandlerType = (connectionLog: Logger, connection: PgPoolClient, boundConnection: DatabasePoolConnection, clientConfiguration: ClientConfiguration) => MaybePromise<any>; | ||
type ConnectionHandlerType = (connectionLog: Logger, connection: PgPoolClient, boundConnection: DatabasePoolConnection, clientConfiguration: ClientConfiguration) => MaybePromise<unknown>; | ||
type PoolHandlerType = (pool: DatabasePool) => Promise<unknown>; | ||
@@ -5,0 +5,0 @@ export declare const createConnection: (parentLog: Logger, pool: PgPool, clientConfiguration: ClientConfiguration, connectionType: Connection, connectionHandler: ConnectionHandlerType, poolHandler: PoolHandlerType, query?: QuerySqlToken | null) => Promise<any>; |
@@ -34,21 +34,6 @@ "use strict"; | ||
}; | ||
const createConnection = async (parentLog, pool, clientConfiguration, connectionType, connectionHandler, poolHandler, query = null) => { | ||
const establishConnection = async (parentLog, pool, connectionRetryLimit) => { | ||
const poolState = (0, state_1.getPoolState)(pool); | ||
if (poolState.ended) { | ||
throw new errors_1.UnexpectedStateError('Connection pool shutdown has been already initiated. Cannot create a new connection.'); | ||
} | ||
for (const interceptor of clientConfiguration.interceptors) { | ||
if (interceptor.beforePoolConnection) { | ||
const maybeNewPool = await interceptor.beforePoolConnection({ | ||
log: parentLog, | ||
poolId: poolState.poolId, | ||
query, | ||
}); | ||
if (maybeNewPool) { | ||
return await poolHandler(maybeNewPool); | ||
} | ||
} | ||
} | ||
let connection; | ||
let remainingConnectionRetryLimit = clientConfiguration.connectionRetryLimit; | ||
let remainingConnectionRetryLimit = connectionRetryLimit; | ||
// eslint-disable-next-line no-constant-condition | ||
@@ -86,4 +71,23 @@ while (true) { | ||
} | ||
const poolClientState = (0, state_1.getPoolClientState)(connection); | ||
const { connectionId } = poolClientState; | ||
return connection; | ||
}; | ||
const createConnection = async (parentLog, pool, clientConfiguration, connectionType, connectionHandler, poolHandler, query = null) => { | ||
const { ended, poolId } = (0, state_1.getPoolState)(pool); | ||
if (ended) { | ||
throw new errors_1.UnexpectedStateError('Connection pool shutdown has been already initiated. Cannot create a new connection.'); | ||
} | ||
for (const interceptor of clientConfiguration.interceptors) { | ||
if (interceptor.beforePoolConnection) { | ||
const maybeNewPool = await interceptor.beforePoolConnection({ | ||
log: parentLog, | ||
poolId, | ||
query, | ||
}); | ||
if (maybeNewPool) { | ||
return await poolHandler(maybeNewPool); | ||
} | ||
} | ||
} | ||
const connection = await establishConnection(parentLog, pool, clientConfiguration.connectionRetryLimit); | ||
const { connectionId } = (0, state_1.getPoolClientState)(connection); | ||
const connectionLog = parentLog.child({ | ||
@@ -96,3 +100,3 @@ connectionId, | ||
log: connectionLog, | ||
poolId: poolState.poolId, | ||
poolId, | ||
}; | ||
@@ -99,0 +103,0 @@ const boundConnection = (0, bindPoolConnection_1.bindPoolConnection)(connectionLog, connection, clientConfiguration); |
@@ -54,5 +54,5 @@ "use strict"; | ||
} | ||
throw new errors_1.UnexpectedStateError(); | ||
throw new errors_1.UnexpectedStateError('Unexpected token type.'); | ||
}; | ||
exports.createSqlTokenSqlFragment = createSqlTokenSqlFragment; | ||
//# sourceMappingURL=createSqlTokenSqlFragment.js.map |
@@ -38,3 +38,3 @@ /// <reference types="node" /> | ||
}; | ||
export { BackendTerminatedError, CheckIntegrityConstraintViolationError, ConnectionError, DataIntegrityError, ForeignKeyIntegrityConstraintViolationError, IntegrityConstraintViolationError, InvalidConfigurationError, InvalidInputError, NotFoundError, NotNullIntegrityConstraintViolationError, SchemaValidationError, SlonikError, StatementCancelledError, StatementTimeoutError, TupleMovedToAnotherPartitionError, UnexpectedStateError, UniqueIntegrityConstraintViolationError, } from './errors'; | ||
export { BackendTerminatedError, CheckIntegrityConstraintViolationError, ConnectionError, DataIntegrityError, ForeignKeyIntegrityConstraintViolationError, InputSyntaxError, IntegrityConstraintViolationError, InvalidConfigurationError, InvalidInputError, NotFoundError, NotNullIntegrityConstraintViolationError, SchemaValidationError, SlonikError, StatementCancelledError, StatementTimeoutError, TupleMovedToAnotherPartitionError, UnexpectedStateError, UniqueIntegrityConstraintViolationError, } from './errors'; | ||
export { createMockPool } from './factories/createMockPool'; | ||
@@ -41,0 +41,0 @@ export { createMockQueryResult } from './factories/createMockQueryResult'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.stringifyDsn = exports.parseDsn = exports.isSqlToken = exports.createTimestampWithTimeZoneTypeParser = exports.createTimestampTypeParser = exports.createNumericTypeParser = exports.createIntervalTypeParser = exports.createDateTypeParser = exports.createBigintTypeParser = exports.createTypeParserPreset = exports.createSqlTokenSqlFragment = exports.createSqlTag = exports.createPool = exports.createMockQueryResult = exports.createMockPool = exports.UniqueIntegrityConstraintViolationError = exports.UnexpectedStateError = exports.TupleMovedToAnotherPartitionError = exports.StatementTimeoutError = exports.StatementCancelledError = exports.SlonikError = exports.SchemaValidationError = exports.NotNullIntegrityConstraintViolationError = exports.NotFoundError = exports.InvalidInputError = exports.InvalidConfigurationError = exports.IntegrityConstraintViolationError = exports.ForeignKeyIntegrityConstraintViolationError = exports.DataIntegrityError = exports.ConnectionError = exports.CheckIntegrityConstraintViolationError = exports.BackendTerminatedError = exports.sql = void 0; | ||
exports.stringifyDsn = exports.parseDsn = exports.isSqlToken = exports.createTimestampWithTimeZoneTypeParser = exports.createTimestampTypeParser = exports.createNumericTypeParser = exports.createIntervalTypeParser = exports.createDateTypeParser = exports.createBigintTypeParser = exports.createTypeParserPreset = exports.createSqlTokenSqlFragment = exports.createSqlTag = exports.createPool = exports.createMockQueryResult = exports.createMockPool = exports.UniqueIntegrityConstraintViolationError = exports.UnexpectedStateError = exports.TupleMovedToAnotherPartitionError = exports.StatementTimeoutError = exports.StatementCancelledError = exports.SlonikError = exports.SchemaValidationError = exports.NotNullIntegrityConstraintViolationError = exports.NotFoundError = exports.InvalidInputError = exports.InvalidConfigurationError = exports.IntegrityConstraintViolationError = exports.InputSyntaxError = exports.ForeignKeyIntegrityConstraintViolationError = exports.DataIntegrityError = exports.ConnectionError = exports.CheckIntegrityConstraintViolationError = exports.BackendTerminatedError = exports.sql = void 0; | ||
const createSqlTag_1 = require("./factories/createSqlTag"); | ||
@@ -12,2 +12,3 @@ exports.sql = (0, createSqlTag_1.createSqlTag)(); | ||
Object.defineProperty(exports, "ForeignKeyIntegrityConstraintViolationError", { enumerable: true, get: function () { return errors_1.ForeignKeyIntegrityConstraintViolationError; } }); | ||
Object.defineProperty(exports, "InputSyntaxError", { enumerable: true, get: function () { return errors_1.InputSyntaxError; } }); | ||
Object.defineProperty(exports, "IntegrityConstraintViolationError", { enumerable: true, get: function () { return errors_1.IntegrityConstraintViolationError; } }); | ||
@@ -14,0 +15,0 @@ Object.defineProperty(exports, "InvalidConfigurationError", { enumerable: true, get: function () { return errors_1.InvalidConfigurationError; } }); |
@@ -9,3 +9,3 @@ "use strict"; | ||
const defer_1 = require("../utilities/defer"); | ||
const getStackTrace_1 = require("../utilities/getStackTrace"); | ||
const get_stack_trace_1 = require("get-stack-trace"); | ||
const serialize_error_1 = require("serialize-error"); | ||
@@ -64,3 +64,3 @@ const retryQuery = async (connectionLogger, connection, query, retryLimit) => { | ||
if (clientConfiguration.captureStackTrace) { | ||
stackTrace = (0, getStackTrace_1.getStackTrace)(); | ||
stackTrace = (0, get_stack_trace_1.getStackTrace)(); | ||
} | ||
@@ -168,13 +168,16 @@ const queryId = inheritedQueryId !== null && inheritedQueryId !== void 0 ? inheritedQueryId : (0, createQueryId_1.createQueryId)(); | ||
if (error.code === '23502') { | ||
throw new errors_1.NotNullIntegrityConstraintViolationError(error, error.constraint); | ||
throw new errors_1.NotNullIntegrityConstraintViolationError(error); | ||
} | ||
if (error.code === '23503') { | ||
throw new errors_1.ForeignKeyIntegrityConstraintViolationError(error, error.constraint); | ||
throw new errors_1.ForeignKeyIntegrityConstraintViolationError(error); | ||
} | ||
if (error.code === '23505') { | ||
throw new errors_1.UniqueIntegrityConstraintViolationError(error, error.constraint); | ||
throw new errors_1.UniqueIntegrityConstraintViolationError(error); | ||
} | ||
if (error.code === '23514') { | ||
throw new errors_1.CheckIntegrityConstraintViolationError(error, error.constraint); | ||
throw new errors_1.CheckIntegrityConstraintViolationError(error); | ||
} | ||
if (error.code === '42601') { | ||
throw new errors_1.InputSyntaxError(error, actualQuery); | ||
} | ||
error.notices = notices; | ||
@@ -198,3 +201,3 @@ throw error; | ||
if (!result) { | ||
throw new errors_1.UnexpectedStateError(); | ||
throw new errors_1.UnexpectedStateError('Expected query result to be returned.'); | ||
} | ||
@@ -201,0 +204,0 @@ // @ts-expect-error -- We want to keep notices as readonly for consumer, but write to it here. |
@@ -10,2 +10,3 @@ { | ||
"es6-error": "^4.1.1", | ||
"get-stack-trace": "^3.1.1", | ||
"iso8601-duration": "^1.3.0", | ||
@@ -102,3 +103,3 @@ "pg": "^8.11.3", | ||
"types": "./dist/index.d.ts", | ||
"version": "34.3.0" | ||
"version": "35.0.1" | ||
} |
@@ -39,3 +39,5 @@ import { UnexpectedStateError } from '../errors'; | ||
throw new UnexpectedStateError(); | ||
throw new UnexpectedStateError( | ||
'Expected query to return one result, but received multiple results.', | ||
); | ||
} | ||
@@ -42,0 +44,0 @@ |
@@ -9,13 +9,12 @@ import { | ||
export class SlonikError extends ExtendableError {} | ||
export class SlonikError extends ExtendableError { | ||
public readonly message: string; | ||
class WrappedPGError extends SlonikError { | ||
public readonly message!: string; | ||
public readonly cause?: Error; | ||
public readonly originalError: Error; | ||
public constructor(message: string, options?: { cause?: Error }) { | ||
super(message); | ||
public constructor(originalError: Error, message: string) { | ||
super(`${message} ${originalError.message}`); | ||
this.originalError = originalError; | ||
this.message = message || this.constructor.name; | ||
this.cause = options?.cause; | ||
} | ||
@@ -28,2 +27,14 @@ } | ||
export class InputSyntaxError extends SlonikError { | ||
public sql: string; | ||
public constructor(error: Error, query: Query) { | ||
super(error.message, { | ||
cause: error, | ||
}); | ||
this.sql = query.sql; | ||
} | ||
} | ||
export class UnexpectedStateError extends SlonikError {} | ||
@@ -33,26 +44,27 @@ | ||
export class StatementCancelledError extends WrappedPGError { | ||
public constructor(error: Error, message = 'Statement has been cancelled.') { | ||
super(error, message); | ||
export class StatementCancelledError extends SlonikError { | ||
public constructor(error: Error) { | ||
super('Statement has been cancelled.', { cause: error }); | ||
} | ||
} | ||
export class StatementTimeoutError extends StatementCancelledError { | ||
export class StatementTimeoutError extends SlonikError { | ||
public constructor(error: Error) { | ||
super(error, 'Statement has been cancelled due to a statement_timeout.'); | ||
super('Statement has been cancelled due to a statement_timeout.', { | ||
cause: error, | ||
}); | ||
} | ||
} | ||
export class BackendTerminatedError extends WrappedPGError { | ||
export class BackendTerminatedError extends SlonikError { | ||
public constructor(error: Error) { | ||
super(error, 'Backend has been terminated.'); | ||
super('Backend has been terminated.', { cause: error }); | ||
} | ||
} | ||
export class TupleMovedToAnotherPartitionError extends WrappedPGError { | ||
public constructor( | ||
error: Error, | ||
message = 'Tuple moved to another partition due to concurrent update.', | ||
) { | ||
super(error, message); | ||
export class TupleMovedToAnotherPartitionError extends SlonikError { | ||
public constructor(error: Error) { | ||
super('Tuple moved to another partition due to concurrent update.', { | ||
cause: error, | ||
}); | ||
} | ||
@@ -80,3 +92,3 @@ } | ||
public constructor(query: Query) { | ||
super('Query returns an unexpected result.'); | ||
super('Query returned an unexpected result.'); | ||
@@ -107,13 +119,24 @@ this.sql = query.sql; | ||
export class IntegrityConstraintViolationError extends WrappedPGError { | ||
type IntegrityConstraintViolationErrorCause = Error & { | ||
constraint: string; | ||
}; | ||
export class IntegrityConstraintViolationError extends SlonikError { | ||
public constraint: string; | ||
public cause?: Error; | ||
public constructor( | ||
error: Error, | ||
constraint: string, | ||
message = 'Query violates an integrity constraint.', | ||
message: string, | ||
error: IntegrityConstraintViolationErrorCause, | ||
) { | ||
super(error, message); | ||
super(message, { cause: error }); | ||
this.constraint = constraint; | ||
if (!error.constraint) { | ||
throw new Error( | ||
'IntegrityConstraintViolationError requires constraint name.', | ||
); | ||
} | ||
this.constraint = error.constraint; | ||
} | ||
@@ -126,4 +149,4 @@ } | ||
export class NotNullIntegrityConstraintViolationError extends IntegrityConstraintViolationError { | ||
public constructor(error: Error, constraint: string) { | ||
super(error, constraint, 'Query violates a not NULL integrity constraint.'); | ||
public constructor(error: IntegrityConstraintViolationErrorCause) { | ||
super('Query violates a not NULL integrity constraint.', error); | ||
} | ||
@@ -133,8 +156,4 @@ } | ||
export class ForeignKeyIntegrityConstraintViolationError extends IntegrityConstraintViolationError { | ||
public constructor(error: Error, constraint: string) { | ||
super( | ||
error, | ||
constraint, | ||
'Query violates a foreign key integrity constraint.', | ||
); | ||
public constructor(error: IntegrityConstraintViolationErrorCause) { | ||
super('Query violates a foreign key integrity constraint.', error); | ||
} | ||
@@ -144,4 +163,4 @@ } | ||
export class UniqueIntegrityConstraintViolationError extends IntegrityConstraintViolationError { | ||
public constructor(error: Error, constraint: string) { | ||
super(error, constraint, 'Query violates a unique integrity constraint.'); | ||
public constructor(error: IntegrityConstraintViolationErrorCause) { | ||
super('Query violates a unique integrity constraint.', error); | ||
} | ||
@@ -151,5 +170,5 @@ } | ||
export class CheckIntegrityConstraintViolationError extends IntegrityConstraintViolationError { | ||
public constructor(error: Error, constraint: string) { | ||
super(error, constraint, 'Query violates a check integrity constraint.'); | ||
public constructor(error: IntegrityConstraintViolationErrorCause) { | ||
super('Query violates a check integrity constraint.', error); | ||
} | ||
} |
@@ -22,4 +22,3 @@ import { bindPoolConnection } from '../binders/bindPoolConnection'; | ||
clientConfiguration: ClientConfiguration, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
) => MaybePromise<any>; | ||
) => MaybePromise<unknown>; | ||
@@ -56,36 +55,12 @@ type PoolHandlerType = (pool: DatabasePool) => Promise<unknown>; | ||
export const createConnection = async ( | ||
const establishConnection = async ( | ||
parentLog: Logger, | ||
pool: PgPool, | ||
clientConfiguration: ClientConfiguration, | ||
connectionType: Connection, | ||
connectionHandler: ConnectionHandlerType, | ||
poolHandler: PoolHandlerType, | ||
query: QuerySqlToken | null = null, | ||
connectionRetryLimit: number, | ||
) => { | ||
const poolState = getPoolState(pool); | ||
if (poolState.ended) { | ||
throw new UnexpectedStateError( | ||
'Connection pool shutdown has been already initiated. Cannot create a new connection.', | ||
); | ||
} | ||
for (const interceptor of clientConfiguration.interceptors) { | ||
if (interceptor.beforePoolConnection) { | ||
const maybeNewPool = await interceptor.beforePoolConnection({ | ||
log: parentLog, | ||
poolId: poolState.poolId, | ||
query, | ||
}); | ||
if (maybeNewPool) { | ||
return await poolHandler(maybeNewPool); | ||
} | ||
} | ||
} | ||
let connection: PgPoolClient; | ||
let remainingConnectionRetryLimit = clientConfiguration.connectionRetryLimit; | ||
let remainingConnectionRetryLimit = connectionRetryLimit; | ||
@@ -132,6 +107,44 @@ // eslint-disable-next-line no-constant-condition | ||
const poolClientState = getPoolClientState(connection); | ||
return connection; | ||
}; | ||
const { connectionId } = poolClientState; | ||
export const createConnection = async ( | ||
parentLog: Logger, | ||
pool: PgPool, | ||
clientConfiguration: ClientConfiguration, | ||
connectionType: Connection, | ||
connectionHandler: ConnectionHandlerType, | ||
poolHandler: PoolHandlerType, | ||
query: QuerySqlToken | null = null, | ||
) => { | ||
const { ended, poolId } = getPoolState(pool); | ||
if (ended) { | ||
throw new UnexpectedStateError( | ||
'Connection pool shutdown has been already initiated. Cannot create a new connection.', | ||
); | ||
} | ||
for (const interceptor of clientConfiguration.interceptors) { | ||
if (interceptor.beforePoolConnection) { | ||
const maybeNewPool = await interceptor.beforePoolConnection({ | ||
log: parentLog, | ||
poolId, | ||
query, | ||
}); | ||
if (maybeNewPool) { | ||
return await poolHandler(maybeNewPool); | ||
} | ||
} | ||
} | ||
const connection = await establishConnection( | ||
parentLog, | ||
pool, | ||
clientConfiguration.connectionRetryLimit, | ||
); | ||
const { connectionId } = getPoolClientState(connection); | ||
const connectionLog = parentLog.child({ | ||
@@ -145,3 +158,3 @@ connectionId, | ||
log: connectionLog, | ||
poolId: poolState.poolId, | ||
poolId, | ||
}; | ||
@@ -148,0 +161,0 @@ |
@@ -56,4 +56,6 @@ import { bindPool } from '../binders/bindPool'; | ||
let getTypeParser: typeof pgTypes.getTypeParser; | ||
try { | ||
await setupClient.connect(); | ||
getTypeParser = await createTypeOverrides( | ||
@@ -60,0 +62,0 @@ setupClient, |
@@ -59,3 +59,3 @@ import { UnexpectedStateError } from '../errors'; | ||
throw new UnexpectedStateError(); | ||
throw new UnexpectedStateError('Unexpected token type.'); | ||
}; |
@@ -11,2 +11,3 @@ import { createSqlTag } from './factories/createSqlTag'; | ||
ForeignKeyIntegrityConstraintViolationError, | ||
InputSyntaxError, | ||
IntegrityConstraintViolationError, | ||
@@ -13,0 +14,0 @@ InvalidConfigurationError, |
@@ -6,2 +6,3 @@ import { TRANSACTION_ROLLBACK_ERROR_PREFIX } from '../constants'; | ||
ForeignKeyIntegrityConstraintViolationError, | ||
InputSyntaxError, | ||
InvalidInputError, | ||
@@ -31,3 +32,3 @@ NotNullIntegrityConstraintViolationError, | ||
import { defer } from '../utilities/defer'; | ||
import { getStackTrace } from '../utilities/getStackTrace'; | ||
import { getStackTrace } from 'get-stack-trace'; | ||
import { type PoolClient as PgPoolClient } from 'pg'; | ||
@@ -300,29 +301,21 @@ import { serializeError } from 'serialize-error'; | ||
if (error.code === '23502') { | ||
throw new NotNullIntegrityConstraintViolationError( | ||
error, | ||
error.constraint, | ||
); | ||
throw new NotNullIntegrityConstraintViolationError(error); | ||
} | ||
if (error.code === '23503') { | ||
throw new ForeignKeyIntegrityConstraintViolationError( | ||
error, | ||
error.constraint, | ||
); | ||
throw new ForeignKeyIntegrityConstraintViolationError(error); | ||
} | ||
if (error.code === '23505') { | ||
throw new UniqueIntegrityConstraintViolationError( | ||
error, | ||
error.constraint, | ||
); | ||
throw new UniqueIntegrityConstraintViolationError(error); | ||
} | ||
if (error.code === '23514') { | ||
throw new CheckIntegrityConstraintViolationError( | ||
error, | ||
error.constraint, | ||
); | ||
throw new CheckIntegrityConstraintViolationError(error); | ||
} | ||
if (error.code === '42601') { | ||
throw new InputSyntaxError(error, actualQuery); | ||
} | ||
error.notices = notices; | ||
@@ -353,3 +346,3 @@ | ||
if (!result) { | ||
throw new UnexpectedStateError(); | ||
throw new UnexpectedStateError('Expected query result to be returned.'); | ||
} | ||
@@ -356,0 +349,0 @@ |
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
Sorry, the diff of this file is too big to display
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
512918
15
339
7535
+ Addedget-stack-trace@^3.1.1
+ Addedget-stack-trace@3.1.1(transitive)
+ Addedstacktrace-parser@0.1.10(transitive)
+ Addedtype-fest@0.7.1(transitive)