Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: gajus/slonik
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v33.1.4
Choose a base ref
...
head repository: gajus/slonik
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v33.2.0
Choose a head ref
  • 3 commits
  • 12 files changed
  • 1 contributor

Commits on Apr 14, 2023

  1. feat: add ignoreCommit

    gajus committed Apr 14, 2023
    Copy the full SHA
    ffb7307 View commit details
  2. feat: add ignoreCommit

    gajus committed Apr 14, 2023
    Copy the full SHA
    294c8c2 View commit details
  3. feat: add ignoreCommit

    gajus committed Apr 14, 2023
    Copy the full SHA
    1e3005e View commit details
5 changes: 4 additions & 1 deletion src/connectionMethods/transaction.ts
Original file line number Diff line number Diff line change
@@ -38,7 +38,10 @@ const execTransaction: InternalTransactionFunction = async (
throw new BackendTerminatedError(poolClientState.terminated);
}

if (poolClientState.mock === false) {
if (
poolClientState.mock === false &&
poolClientState.ignoreCommit === false
) {
await connection.query('COMMIT');
}

1 change: 1 addition & 0 deletions src/factories/createClientConfiguration.ts
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@ export const createClientConfiguration = (
connectionTimeout: 5_000,
idleInTransactionSessionTimeout: 60_000,
idleTimeout: 5_000,
ignoreCommit: false,
interceptors: [],
maximumPoolSize: 10,
queryRetryLimit: 5,
1 change: 1 addition & 0 deletions src/factories/createConnection.ts
Original file line number Diff line number Diff line change
@@ -97,6 +97,7 @@ export const createConnection = async (

poolClientStateMap.set(connection, {
connectionId: createUid(),
ignoreCommit: poolState.ignoreCommit,
mock: poolState.mock,
poolId: poolState.poolId,
terminated: null,
1 change: 1 addition & 0 deletions src/factories/createMockPool.ts
Original file line number Diff line number Diff line change
@@ -40,6 +40,7 @@ export const createMockPool = (

poolStateMap.set(pool, {
ended: false,
ignoreCommit: true,
mock: true,
poolId,
typeOverrides: null,
1 change: 1 addition & 0 deletions src/factories/createPool.ts
Original file line number Diff line number Diff line change
@@ -72,6 +72,7 @@ export const createPool = async (

poolStateMap.set(pool, {
ended: false,
ignoreCommit: clientConfiguration.ignoreCommit,
mock: false,
poolId,
typeOverrides: null,
2 changes: 2 additions & 0 deletions src/state.ts
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ import { type Pool as PgPool, type PoolClient as PgClientPool } from 'pg';

type PoolState = {
ended: boolean;
ignoreCommit: boolean;
mock: boolean;
poolId: string;
typeOverrides: Promise<TypeOverrides> | null;
@@ -13,6 +14,7 @@ type PoolState = {
type PoolClientState = {
activeQuery?: DeferredPromise<unknown>;
connectionId: string;
ignoreCommit: boolean;
mock: boolean;
poolId: string;
terminated: Error | null;
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -99,6 +99,10 @@ export type ClientConfiguration = {
* Timeout (in milliseconds) after which idle clients are closed. Use 'DISABLE_TIMEOUT' constant to disable the timeout. (Default: 5000)
*/
readonly idleTimeout: number | 'DISABLE_TIMEOUT';
/**
* Do not send COMMIT after successful transaction. (Default: false)
*/
readonly ignoreCommit: boolean;
/**
* An array of [Slonik interceptors](https://github.com/gajus/slonik#slonik-interceptors).
*/
1 change: 1 addition & 0 deletions test/helpers/createClientConfiguration.ts
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ export const createClientConfiguration = (): ClientConfiguration => {
connectionTimeout: 5_000,
idleInTransactionSessionTimeout: 60_000,
idleTimeout: 5_000,
ignoreCommit: false,
interceptors: [],
maximumPoolSize: 10,
queryRetryLimit: 5,
2 changes: 2 additions & 0 deletions test/helpers/createPool.ts
Original file line number Diff line number Diff line change
@@ -44,6 +44,7 @@ export const createPool = async (

poolStateMap.set(internalPool, {
ended: false,
ignoreCommit: false,
mock: false,
poolId: '1',
typeOverrides: null,
@@ -61,6 +62,7 @@ export const createPool = async (
connectionTimeout: 5_000,
idleInTransactionSessionTimeout: 5_000,
idleTimeout: 5_000,
ignoreCommit: false,
interceptors: [],
maximumPoolSize: 1,
queryRetryLimit: 1,
1 change: 1 addition & 0 deletions test/slonik/factories/createClientConfiguration.ts
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ const defaultConfiguration = {
connectionTimeout: 5_000,
idleInTransactionSessionTimeout: 60_000,
idleTimeout: 5_000,
ignoreCommit: false,
interceptors: [],
maximumPoolSize: 10,
queryRetryLimit: 5,
23 changes: 23 additions & 0 deletions test/slonik/integration/pg.ts
Original file line number Diff line number Diff line change
@@ -9,6 +9,29 @@ const { test } = createTestRunner(PgPool, 'pg');

createIntegrationTests(test, PgPool);

test('does not break out of a transaction', async (t) => {
const pool = await createPool(t.context.dsn, {
ignoreCommit: true,
PgPool,
});

await t.notThrowsAsync(
pool.connect(async (connection) => {
await connection.query(sql.unsafe`BEGIN`);
await connection.query(sql.unsafe`SAVEPOINT foo`);

await connection.transaction(async (transaction) => {
await transaction.query(sql.unsafe`SELECT 1`);
});

await connection.query(sql.unsafe`ROLLBACK TO SAVEPOINT foo`);
await connection.query(sql.unsafe`ROLLBACK`);
}),
);

await pool.end();
});

test('returns expected query result object (NOTICE)', async (t) => {
const pool = await createPool(t.context.dsn, {
PgPool,
2 changes: 2 additions & 0 deletions test/slonik/routines/executeQuery.ts
Original file line number Diff line number Diff line change
@@ -30,6 +30,7 @@ beforeEach((t) => {

poolClientStateMap.set(t.context.connection, {
connectionId: '1',
ignoreCommit: false,
mock: true,
poolId: '1',
terminated: null,
@@ -162,6 +163,7 @@ test('transaction errors are not handled if the function was called by a transac

poolClientStateMap.set(connection, {
connectionId: '1',
ignoreCommit: false,
mock: true,
poolId: '1',
terminated: null,