Preventing unintended clause mutation in Knex.js query builders
0 reputation · 13 Mar 2021, 13:05 UTC
Knex.js query builders are mutable objects. When a partially constructed builder is passed to a helper function or service layer, that external code can append additional clauses like .where(), join(), or select() to the original instance. This risks accidental data exposure or logic bypass if the developer assumes the query is immutable once passed.
Since the library does not currently offer a native 'read-only' mode for the query builder, developers often rely on architectural patterns like repositories to encapsulate the Knex instance to mitigate this risk.
Is there a documented documented method to clone or freeze a query builder to ensure subsequent modifications do not affect the original object? How can one ensure a builder remains unmodified when it is shared across different module layers?