Skip to content
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

fix(knex): Add tableOptions parameter for inheritance on knex adapter options to pass on knex builder #3539

Merged
merged 3 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
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)
Copy link

Choose a reason for hiding this comment

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

Will it work if the tableOptions has not been provided, I would prefer we check it before passing the tableOptions as parameter on the knex adapter options like this:-

let queryBuilder: Knex.QueryBuilder;
  if (params?.transaction?.trx) {
    const { trx } = params.transaction;
    queryBuilder = schema ? trx.withSchema(schema).table(name) : trx.table(name);
  } else {
    queryBuilder = schema ? Model.withSchema(schema).table(name) : Model.table(name);
  }

  // Apply tableOptions if provided
  if (tableOptions) {
    queryBuilder = queryBuilder.withOptions(tableOptions); // Adjust this if .withOptions() isn't the exact method for tableOptions
  }
  return queryBuilder;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the
Model(name, tableOptions)

has already the tableOptions that can undefined. (its tha case now)
i am just proposign to pass them and then allow to have this property reach the knex mode.
no the ONLY postgres query is impossible to have though feathers.

}

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