Skip to content

Commit

Permalink
fix(knex): Add tableOptions parameter for inheritance on knex adapter…
Browse files Browse the repository at this point in the history
… options to pass on knex builder (#3539)
  • Loading branch information
fidelio314 authored Feb 6, 2025
1 parent 73d081c commit ba5621b
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 2 deletions.
2 changes: 2 additions & 0 deletions docs/api/databases/knex.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ The Knex specific adapter options are:
- `Model {Knex}` (**required**) - The KnexJS database instance
- `name {string}` (**required**) - The name of the table
- `schema {string}` (_optional_) - The name of the schema table prefix (example: `schema.table`)
- `tableOptions {only: boolean` (_optional_) - For PostgreSQL only. Argument for passing options to knex db builder. ONLY keyword is used before the tableName to discard inheriting tables' data. (https://knexjs.org/guide/query-builder.html#common)


The [common API options](./common.md#options) are:

Expand Down
4 changes: 2 additions & 2 deletions packages/knex/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ export class KnexAdapter<
}

db(params?: ServiceParams) {
const { Model, name, schema } = this.getOptions(params)
const { Model, name, schema, tableOptions } = this.getOptions(params)

if (params && params.transaction && params.transaction.trx) {
const { trx } = params.transaction
// debug('ran %s with transaction %s', fullName, id)
return schema ? (trx.withSchema(schema).table(name) as Knex.QueryBuilder) : trx(name)
}

return schema ? (Model.withSchema(schema).table(name) as Knex.QueryBuilder) : Model(name)
return schema ? (Model.withSchema(schema).table(name) as Knex.QueryBuilder) : Model(name, tableOptions)
}

knexify(knexQuery: Knex.QueryBuilder, query: Query = {}, parentKey?: string): Knex.QueryBuilder {
Expand Down
3 changes: 3 additions & 0 deletions packages/knex/src/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ export interface KnexAdapterOptions extends AdapterServiceOptions {
Model: Knex
name: string
schema?: string
tableOptions?: {
only?: boolean
}
}

export interface KnexAdapterTransaction {
Expand Down

0 comments on commit ba5621b

Please sign in to comment.