Write effective, type-safe Kysely queries for PostgreSQL. This skill should be used when working in Node.js/TypeScript backends with Kysely installed, covering query patterns, migrations, type generation, and common pitfalls to avoid.
View on GitHubgallop-systems/claude-skills
all
skills/kysely-postgres/SKILL.md
January 20, 2026
Select agents to install to:
npx add-skill https://github.com/gallop-systems/claude-skills/blob/main/skills/kysely-postgres/SKILL.md -a claude-code --skill kysely-postgresInstallation paths:
.claude/skills/kysely-postgres/# Kysely for PostgreSQL
Kysely is a type-safe TypeScript SQL query builder. This skill provides patterns for writing effective queries, managing migrations, and avoiding common pitfalls.
## When to Use This Skill
Use this skill when:
- Working in a Node.js/TypeScript project with Kysely installed
- Writing database queries for PostgreSQL
- Creating or modifying database migrations
- Debugging type inference issues in Kysely queries
## Reference Files
For detailed examples, see these topic-focused reference files:
- [select-where.ts](references/select-where.ts) - Basic SELECT patterns, WHERE clauses, AND/OR conditions
- [joins.ts](references/joins.ts) - Simple joins, callback joins, subquery joins, cross joins
- [aggregations.ts](references/aggregations.ts) - COUNT, SUM, AVG, GROUP BY, HAVING
- [orderby-pagination.ts](references/orderby-pagination.ts) - ORDER BY, NULLS handling, DISTINCT, pagination
- [ctes.ts](references/ctes.ts) - Common Table Expressions, multiple CTEs, recursive CTEs
- [json-arrays.ts](references/json-arrays.ts) - JSONB handling, array columns, jsonBuildObject, jsonAgg
- [relations.ts](references/relations.ts) - jsonArrayFrom, jsonObjectFrom for nested data
- [mutations.ts](references/mutations.ts) - INSERT, UPDATE, DELETE, UPSERT, INSERT FROM SELECT
- [expressions.ts](references/expressions.ts) - CASE, $if, subqueries, eb.val/lit/not, standalone expressionBuilder
## Core Principles
1. **Prefer Kysely methods over raw SQL**: Almost everything you can do in SQL, you can do in Kysely without `sql``
2. **Use the ExpressionBuilder (eb)**: The `eb` parameter in callbacks is the foundation of type-safe query building
3. **Let TypeScript guide you**: If it compiles, it's likely correct SQL
## ExpressionBuilder (eb) - The Foundation
The `eb` parameter in select/where callbacks provides all expression methods:
```typescript
.select((eb) => [
eb.ref("column").as("alias"), // Column reference
eb.fn<string>("upper", [eb.ref(