Skip to main content
Kysely’s plugin system allows you to intercept and modify queries before execution and transform results after execution. Plugins enable features like automatic column name transformation, query logging, and custom data parsing.

The KyselyPlugin Interface

All plugins implement the KyselyPlugin interface:

Installing Plugins

Plugins are registered when creating a Kysely instance:
Or add them to an existing instance:

Built-in Plugins

CamelCasePlugin

Converts snake_case database identifiers to camelCase in JavaScript:
Generated SQL:
Options:

DeduplicateJoinsPlugin

Automatically deduplicates identical JOIN clauses:

ParseJSONResultsPlugin

Automatically parses JSON columns:

Creating Custom Plugins

Basic Example: Query Logger

Advanced Example: Timing Plugin

Sharing Data Between transformQuery and transformResult

Use a WeakMap with queryId as the key:
Always use WeakMap instead of Map for storing query data. transformQuery is not always matched by transformResult, which could cause memory leaks with strong references.

Transforming the Query AST

Use an OperationNodeTransformer to modify query structure:

Plugin Execution Order

Plugins execute in the order they’re registered:
For transformQuery, plugins run in order:
For transformResult, plugins run in reverse:

Dynamic Plugin Management

Real-World Plugin Examples

Soft Delete Plugin

Row-Level Security Plugin

Best Practices

Keep plugins focused on a single responsibility. Create multiple small plugins rather than one large plugin.
Always return a value from transformQuery and transformResult. Returning undefined will cause errors.
Plugins that modify the query structure can break type safety. Test thoroughly when creating AST-transforming plugins.
Use WeakMap with queryId to share data between transformQuery and transformResult to prevent memory leaks.