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

Transforming the result of a query promise? #6042

Open
Linksku opened this issue Mar 23, 2024 · 0 comments
Open

Transforming the result of a query promise? #6042

Linksku opened this issue Mar 23, 2024 · 0 comments

Comments

@Linksku
Copy link

Linksku commented Mar 23, 2024

Environment

Knex version: 3.1.0
Database + version: Postgres 16.1
OS: Ubuntu

Feature discussion / request

  1. Explain what is your use case
    I'd like to always transform Knex results using a custom function. I.e. instead of needing to write resultsToEntities(await query), I'd like Knex to pass the results of queries through resultsToEntities automatically. I don't think it's possible using the start or query-response events.

  2. Explain what kind of feature would support this
    This should be a common use-case, I'd assume a lot of people want to do some custom processing with Knex's results.

  3. Give some API proposal, how the feature should work
    Have a hook like "transformResults", then it would be easier to write a model class like:

class Model {
  query() {
    return knex
      .from(this.tableName)
      .transformResults(resultsToEntities);
  }
}

Then I can do await Model.query().select(...); instead of resultsToEntities(await Model.query().select(...));.

"Objection" automatically transforms query results, but I think it does so by creating its own query builder with the same interface as Knex (https://github.com/Vincit/objection.js/blob/main/lib/queryBuilder/QueryBuilderBase.js). A transformResults method would make this much easier to do.

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

No branches or pull requests

1 participant