DynamicModule is designed for cases where column or table names come from user input or are not otherwise known at compile time.
Accessing the dynamic module
The dynamic module is available on thedb instance:
Dynamic column references
Useref() to create references to columns not known at compile time.
Filter by dynamic column
Order by dynamic column
Dynamic selections
You can add selections dynamically with proper typing:The resulting type contains all
PossibleColumns as optional fields because we cannot know which field was actually selected before running the code.Dynamic table references
Usetable() to reference tables that aren’t fully known at compile time. The type parameter can be a union of multiple tables.
Generic find function
Here’s a type-safe helper for finding a row by any column value:Best practices
Validate input
Always validate column and table names before passing them to dynamic methods
Use type unions
Define explicit type unions for possible column values to maintain type safety
Prefer static
Only use dynamic queries when truly necessary — static queries are safer
Use allowlists
Maintain allowlists of valid column/table names rather than accepting arbitrary input