AI agents: Read [llms.txt](https://kysely.dev/llms.txt) for the documentation index and links to Markdown (.md) versions of individual pages. Read [llms-full.txt](https://kysely.dev/llms-full.txt) for the full documentation in one file.

* [](https://kysely.dev/index.md)
* Recipes

# Recipes

Explore Kysely recipes for reusable helpers, expressions, nested relations, database schemas, logging, and advanced TypeScript types.

## [📄️Relations](https://kysely.dev/docs/recipes/relations.md)

[Nest related rows as JSON objects and arrays in Kysely queries using helpers for PostgreSQL, MySQL, SQL Server, and SQLite.](https://kysely.dev/docs/recipes/relations.md)

## [📄️Reusable helpers](https://kysely.dev/docs/recipes/reusable-helpers.md)

[Build reusable, type-safe Kysely query helpers with expressions, the expression builder, subqueries, and the sql template tag.](https://kysely.dev/docs/recipes/reusable-helpers.md)

## [📄️Data types](https://kysely.dev/docs/recipes/data-types.md)

[Understand compile-time and runtime data types in Kysely, and align your TypeScript column types with values returned by your database driver.](https://kysely.dev/docs/recipes/data-types.md)

## [📄️Raw SQL](https://kysely.dev/docs/recipes/raw-sql.md)

[Use the Kysely sql template tag to execute raw SQL and compose SQL snippets within type-safe queries.](https://kysely.dev/docs/recipes/raw-sql.md)

## [📄️Splitting query building and execution](https://kysely.dev/docs/recipes/splitting-query-building-and-execution.md)

[Compile Kysely queries without a database connection using DummyDriver, infer result types, and execute compiled queries separately.](https://kysely.dev/docs/recipes/splitting-query-building-and-execution.md)

## [📄️Conditional selects](https://kysely.dev/docs/recipes/conditional-selects.md)

[Select columns conditionally with the Kysely $if method while preserving type inference for optional fields in query results.](https://kysely.dev/docs/recipes/conditional-selects.md)

## [📄️Expressions](https://kysely.dev/docs/recipes/expressions.md)

[Compose type-safe SQL expressions with the Kysely expression builder, including function calls, subqueries, and conditional filters.](https://kysely.dev/docs/recipes/expressions.md)

## [📄️Working with schemas](https://kysely.dev/docs/recipes/schemas.md)

[Work with named database schemas in Kysely using qualified table names and withSchema for grouped tables and multitenant applications.](https://kysely.dev/docs/recipes/schemas.md)

## [📄️Deduplicate joins](https://kysely.dev/docs/recipes/deduplicate-joins.md)

[Prevent duplicate joins in dynamic Kysely queries by configuring DeduplicateJoinsPlugin globally or for an individual query.](https://kysely.dev/docs/recipes/deduplicate-joins.md)

## [📄️Dealing with the Type instantiation is excessively deep and possibly infinite error](https://kysely.dev/docs/recipes/excessively-deep-types.md)

[Fix the TypeScript TS2589 excessively deep type instantiation error in Kysely queries using $assertType without losing type safety.](https://kysely.dev/docs/recipes/excessively-deep-types.md)

## [📄️Extending kysely](https://kysely.dev/docs/recipes/extending-kysely.md)

[Extend Kysely with custom type-safe expressions and helpers using Expression, AliasedExpression, and the sql template tag.](https://kysely.dev/docs/recipes/extending-kysely.md)

## [📄️Introspecting relation metadata](https://kysely.dev/docs/recipes/introspecting-relation-metadata.md)

[Inspect database tables and views at runtime using the Kysely introspection API to retrieve schema metadata.](https://kysely.dev/docs/recipes/introspecting-relation-metadata.md)

## [📄️Logging](https://kysely.dev/docs/recipes/logging.md)

[Configure Kysely query and error logging with built-in log levels or a custom callback for SQL, parameters, and execution duration.](https://kysely.dev/docs/recipes/logging.md)

[Previous](https://kysely.dev/docs/examples/cte/inserts-updates-and-deletions.md)

[Inserts, updates and deletions](https://kysely.dev/docs/examples/cte/inserts-updates-and-deletions.md)

[Next](https://kysely.dev/docs/recipes/relations.md)

[Relations](https://kysely.dev/docs/recipes/relations.md)
