Overview
The KyselyPlugin interface is the foundation for all Kysely plugins. Plugins allow you to intercept and modify queries before execution and transform results after execution.
Interface Definition
Methods
This method is called for each query before it is executed. You can modify the query by transforming its OperationNode tree.
Parameters:
args.queryId - Unique identifier for the query
args.node - The root operation node representing the query
Returns: The transformed root operation node
Usage:
You typically use an OperationNodeTransformer to transform the query tree:
This method is called for each query after it has been executed. You can modify the result and return the modified result.
Parameters:
args.queryId - Unique identifier for the query (same as in transformQuery)
args.result - The query result containing rows and metadata
Returns: A promise resolving to the transformed query result
Example:
Sharing Data Between Methods
If you need to pass query-related data between transformQuery and transformResult, use a WeakMap with args.queryId as the key:
Use a WeakMap instead of a Map because transformQuery is not always matched by a call to transformResult, which would leave orphaned items in the map and cause a memory leak.
Using Plugins
Global Installation
Add plugins when creating a Kysely instance:
Per-Query Installation
Apply a plugin to a single query:
Built-in Plugins
Kysely provides several built-in plugins:
See Also