⚡ Sangsakala Proxy 🏠 Home
Skip to content

feat(knex): allow enabling the RETURNING clause via the knex config - #3728

Open
nm3xyz wants to merge 1 commit into
feathersjs:dovefrom
nm3xyz:feat/knex-returning-config
Open

nm3xyz wants to merge 1 commit into
feathersjs:dovefrom
nm3xyz:feat/knex-returning-config

Conversation

@nm3xyz

@nm3xyz nm3xyz commented Oct 31, 2026 •

Copy link
Copy Markdown

Allow enabling the RETURNING clause via the knex configuration

Branch: feat/knex-returning-config (head: nm3xyz/feathers, commit 173ad8f)
Repo: feathersjs/feathers — base dove @ 8c007bc (v5.0.53, the repository default branch)


Summary

  • Tell us about the problem your pull request is solving.

KnexAdapter._create decides whether to use the RETURNING clause in the insert statement with a hard-coded list of clients:

const RETURNING_CLIENTS = ['postgresql', 'pg', 'oracledb', 'mssql', 'sqlite3']
...
const returning = RETURNING_CLIENTS.includes(client.driverName) ? [this.id] : []

Any database that does support RETURNING but is not in that list — Firebird via knex-firebird-dialect is a concrete example — can not get the id back from the insert. The adapter then falls back to using the returned row value / an additional _get, which either does not work or costs an extra query.

This PR lets the knex configuration opt in (or out of the default) with a returning boolean:

const db = knex({
  client: knexFirebirdDialect,
  connection: { /* ... */ },
  returning: true
})

Implementation

export const supportsReturning = (client: Knex.Client) => {
  const { returning } = (client.config || {}) as ReturningConfig

  return Boolean(returning) || RETURNING_CLIENTS.includes(client.driverName)
}
  • A truthy client.config.returning enables the clause.
  • If it is not set or falsy, the existing RETURNING_CLIENTS check is used, so behaviour for every currently supported database is unchanged.

_create now calls supportsReturning(client) instead of the inline check.

Tests

Added a returning clause support suite in packages/knex/test/index.test.ts (no database required, the client and the insert are stubbed):

  • default list is used when the option is not set (postgresql, oracledb, sqlite3 → true; node-firebird-driver-native → false)
  • the option enables it for a client outside the default list
  • a falsy option falls back to the default list (false + Firebird → false, false + PostgreSQL → true)
  • _create actually passes [id] to insert when the option is enabled, and [] when it is not

Full package suite: 336 passing, 20 pending (sqlite), plus eslint and prettier clean on the changed files.

Docs

Added a short "RETURNING clause support" section to docs/api/databases/knex.md, documenting the returning knex option and the Firebird example.

Other Information

  • Are there any open issues that are related to this? — None found; this came up while porting a Feathers service to Firebird.
  • Is this PR dependent on PRs in other repos? — No.

Files changed: packages/knex/src/adapter.ts, packages/knex/test/index.test.ts, docs/api/databases/knex.md (99 insertions, 2 deletions).

The insert in `_create` only uses the `RETURNING` clause for a hard-coded
list of clients. Databases that support it but are not in that list, like
Firebird via knex-firebird-dialect, can not return the id of created
records and fall back to a second query.

The `returning` option of the knex configuration (available as
`client.config.returning`) now takes precedence. If it is not set or
falsy, the existing list of clients is still used so the behaviour for
all currently supported databases is unchanged.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant