Repository navigation
fix: reuse connections after result errors and fix oneFirst, routed retry limit, 22P02 cause and docs #829
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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. | ||
|
|
||
|
|
@@ -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"; | ||
|
|
@@ -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`. | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
|
||
| #### <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). | ||
|
|
@@ -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. | ||
|
|
||
|
|
@@ -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")); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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]]; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.