Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/invalid-input-error-cause.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@slonik/pg-driver": minor
---

fix: `InvalidInputError` raised for SQLSTATE 22P02 (invalid text representation) includes the original error as `cause`, like the other errors that the driver maps.
5 changes: 5 additions & 0 deletions .changeset/one-first-data-integrity-error.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"slonik": minor
---

fix: `oneFirst` throws `DataIntegrityError` (instead of `UnexpectedStateError`) when the query returns multiple columns, like `maybeOneFirst`, `anyFirst` and `manyFirst`.
5 changes: 5 additions & 0 deletions .changeset/remove-query-method-values-parameter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"slonik": minor
---

remove the `values` parameter from the types of the query methods (e.g. `pool.query(query, values)`). The parameter was accepted, but ignored; values are bound using the `sql` tag.
5 changes: 5 additions & 0 deletions .changeset/reuse-connection-after-result-errors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"slonik": minor
---

fix: a query that fails with `NotFoundError`, `DataIntegrityError` or `SchemaValidationError` (e.g. `pool.one()` that finds no rows) no longer destroys its connection; the connection is returned to the pool. This applies to queries and transactions on the pool. Connections used by `pool.connect()` are still destroyed when the routine fails.
5 changes: 5 additions & 0 deletions .changeset/routed-transaction-retry-limit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"slonik": minor
---

fix: `pool.transaction(handler, transactionRetryLimit)` passes `transactionRetryLimit` to the pool returned by a `beforePoolConnection` interceptor.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid literal configuration values. Pass instead {retryLimit?: number} object.

25 changes: 10 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -389,9 +389,7 @@ To begin with, Slonik does not allow running plain-text queries.
connection.query("SELECT 1");
```

The above invocation would produce an error:

> TypeError: Query must be constructed using `sql` tagged template literal.
Query methods only accept queries constructed using the `sql` tagged template literal, so TypeScript rejects the above invocation.

This means that the only way to run a query is by constructing it using [`sql` tagged template literal](https://github.com/gajus/slonik#value-placeholders-tagged-template-literals), e.g.

Expand Down Expand Up @@ -808,7 +806,7 @@ Read: [Protecting against unsafe connection handling](#protecting-against-unsafe

The `DatabasePool` extends `DatabasePoolEventEmitter` and exposes the following events:

- `error`: `(error: SlonikError) => void` – emitted for all errors that happen within the pool.
- `error`: `(error: SlonikError) => void` – emitted when a query (including `stream`) fails with a `SlonikError`. Database errors that Slonik does not map to its own [error classes](#error-handling) are not emitted.

```ts
import { createPool, type DatabasePoolEventEmitter } from "slonik";
Expand Down Expand Up @@ -1069,18 +1067,22 @@ pool.connect();

Executed after query has been executed and before rows were transformed using `transformRow`.

Note: When query is executed using `stream`, then `afterQuery` is called with empty result set.
Note: Not executed for queries executed using `stream`.

#### <code>beforeQueryExecution</code>

This function can optionally return a direct result of the query which will cause the actual query never to be executed.

Note: Not executed for queries executed using `stream`.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use special blockquotes for all notes, e.g.

Note

Useful information that users should know, even when skimming content.

Update all similar notes in README to use the same format instead of plain note:


#### <code>beforeQueryResult</code>

Executed just before the result is returned to the client.

Use this method to capture the result that will be returned to the client.

Note: Not executed for queries executed using `stream`.

#### <code>beforeTransformQuery</code>

Executed before `transformQuery`. Use this interceptor to capture the original query (e.g. for logging purposes).
Expand Down Expand Up @@ -1573,25 +1575,18 @@ query with 'baz' value binding.

### Manually constructing the query

Manually constructing queries is not allowed.

There is an internal mechanism that checks to see if query was created using `sql` tagged template literal, i.e.
Query methods only accept queries constructed using the `sql` tagged template literal, so TypeScript rejects a manually constructed query, e.g.

```ts
const query = {
sql: "SELECT 1 AS id FROM foo WHERE bar = $1",
type: "SQL",
values: ["baz"],
};

connection.query(query);
```

Will result in an error:

> Query must be constructed using `sql` tagged template literal.

This is a security measure designed to prevent unsafe query execution.
The `sql` tag functions must be called as tagged template literals. Calling one as a regular function, e.g. `sql.unsafe("SELECT 1")`, throws `InvalidInputError` ("Function must be called as a template literal."). This prevents passing a plain string (e.g. user input) as the query text.

Furthermore, a query object constructed using `sql` tagged template literal is [frozen](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze) to prevent further manipulation.

Expand Down Expand Up @@ -2853,7 +2848,7 @@ await connection.transaction(async (t1) => {
await t1.transaction(async (t2) => {
await t2.query(sql.typeAlias("void")`INSERT INTO qux (quux) VALUES ('corge')`);

await t1.transaction(async (t3) => {
await t2.transaction(async (t3) => {
await t3.query(sql.typeAlias("void")`INSERT INTO uier (grault) VALUES ('garply')`);

return Promise.reject(new Error("foo"));
Expand Down
2 changes: 1 addition & 1 deletion packages/pg-driver/src/factories/createPgDriverFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ const wrapError = (error: Error, query: null | Query) => {
}

if (error.code === "22P02") {
return new InvalidInputError(error.message);
return new InvalidInputError(error.message, { cause: error });
}

if (error.code === "25P03") {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createPoolWithSpy } from "../../../helpers.test/createPoolWithSpy.js";
import { createTestRunner } from "../../../helpers.test/createTestRunner.js";
import type { DatabasePool } from "../../../types.js";
import { createPgDriverFactory } from "@slonik/pg-driver";
import { createSqlTag } from "@slonik/sql-tag";
import * as sinon from "sinon";
Expand Down Expand Up @@ -29,6 +30,31 @@ test("`beforePoolConnection` is called before `connect`", async (t) => {
t.true(beforePoolConnection.calledBefore(spy.acquire));
});

test("`beforePoolConnection` that returns a pool runs the transaction using that pool, with the same retry limit", async (t) => {
const transaction = sinon.stub().resolves("foo");

const { pool } = await createPoolWithSpy(t.context.dsn, {
driverFactory,
interceptors: [
{
beforePoolConnection: () => {
return {
transaction,
} as unknown as DatabasePool;
},
name: "foo",
},
],
});

const handler = async () => {
return "bar";
};

t.is(await pool.transaction(handler, 3), "foo");
t.deepEqual(transaction.firstCall.args, [handler, 3]);
});

test("pool connection lifecycle reuses the same context object", async (t) => {
const connectionContexts = new WeakMap<object, string>();
const beforePoolConnection = sinon.stub().callsFake((connectionContext) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/slonik/src/binders/bindPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ export const bindPool = (
);
},
(newPool) => {
return newPool.transaction(transactionHandler);
return newPool.transaction(transactionHandler, transactionRetryLimit);
},
);
},
Expand Down
5 changes: 3 additions & 2 deletions packages/slonik/src/connectionMethods/oneFirst.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createPool } from "../factories/createPool.js";
import { createTestRunner } from "../helpers.test/createTestRunner.js";
import { DataIntegrityError, NotFoundError, UnexpectedStateError } from "@slonik/errors";
import { DataIntegrityError, NotFoundError } from "@slonik/errors";
import { createPgDriverFactory } from "@slonik/pg-driver";
import { createSqlTag } from "@slonik/sql-tag";
import { expectTypeOf } from "expect-type";
Expand Down Expand Up @@ -68,7 +68,8 @@ test("throws an error if more than one column is returned", async (t) => {
`),
);

t.true(error instanceof UnexpectedStateError);
t.true(error instanceof DataIntegrityError);
t.is(error?.message, "Query returned rows with multiple columns.");
});

test("describes zod object associated with the query", async (t) => {
Expand Down
26 changes: 5 additions & 21 deletions packages/slonik/src/connectionMethods/oneFirst.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,25 @@
import type { InternalQueryMethod } from "../types.js";
import { one } from "./one.js";
import { UnexpectedStateError } from "@slonik/errors";
import { query } from "./query.js";
import { generateUid } from "@slonik/utilities";

/**
* Makes a query and expects exactly one result.
* Returns value of the first column.
* @throws NotFoundError If query returns no rows.
* @throws DataIntegrityError If query returns multiple rows.
* @throws DataIntegrityError If query returns multiple rows or multiple columns.
*/
export const oneFirst: InternalQueryMethod = async (
log,
connection,
clientConfiguration,
slonikSql,
slonikQuery,
inheritedQueryId,
) => {
const queryId = inheritedQueryId ?? generateUid();

const row = await one(log, connection, clientConfiguration, slonikSql, queryId, {
const { rows } = await query(log, connection, clientConfiguration, slonikQuery, queryId, {
validationType: "ONE_COLUMN",
});

const keys = Object.keys(row);

if (keys.length > 1) {
log.error(
{
queryId,
},
"DataIntegrityError",
);

throw new UnexpectedStateError(
"Expected query to return one result, but received multiple results.",
);
}

return row[keys[0]];
return rows[0][Object.keys(rows[0])[0]];
};
41 changes: 25 additions & 16 deletions packages/slonik/src/factories/createConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type {
} from "../types.js";
import type { ConnectionPool, ConnectionPoolClient } from "./createConnectionPool.js";
import { SpanStatusCode, trace } from "@opentelemetry/api";
import { UnexpectedStateError } from "@slonik/errors";
import { DataIntegrityError, SchemaValidationError, UnexpectedStateError } from "@slonik/errors";
import type { QuerySqlToken } from "@slonik/sql-tag";

const tracer = trace.getTracer("slonik.interceptors");
Expand Down Expand Up @@ -195,21 +195,6 @@ export const createConnection = async (
throw error;
}

let result;

try {
result = await connectionHandler(
connectionLog,
connection,
boundConnection,
clientConfiguration,
);
} catch (error) {
await connection.destroy();

throw error;
}

const releaseConnection = async () => {
try {
for (const interceptor of clientConfiguration.interceptors) {
Expand All @@ -235,6 +220,30 @@ export const createConnection = async (
}
};

let result;

try {
result = await connectionHandler(
connectionLog,
connection,
boundConnection,
clientConfiguration,
);
} catch (error) {
// These errors describe the result of a query that succeeded, i.e. the connection is
// healthy. A routine passed to `connect()` can leave the connection in any state, though.
if (
connectionType !== "EXPLICIT" &&
(error instanceof DataIntegrityError || error instanceof SchemaValidationError)
) {
releaseConnection().catch(() => {});
} else {
await connection.destroy();
}

throw error;
}

releaseConnection().catch(() => {});

return result;
Expand Down
1 change: 1 addition & 0 deletions packages/slonik/src/helpers.test/createIntegrationTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1382,6 +1382,7 @@ export const createIntegrationTests = (
);

t.true(error instanceof InvalidInputError);
t.is((error?.cause as undefined | { code?: string })?.code, "22P02");
});

test("terminates transaction if any of the queries fails", async (t) => {
Expand Down
Loading
Loading