adonisjs-kysely
v0.3.1
Published
A kysely package to help adonisjs project
Readme
Features
- ✅ Type-Safe Queries: Full TypeScript support with database schema types
- ✅ Execution Context: Request-scoped data with PostgreSQL RLS support
- ✅ Transaction Management: Automatic transactions with nested savepoint support
- ✅ Repository Pattern: Clean, testable database layer architecture
- ✅ Test Integration: Automatic transaction wrapping for isolated tests
- ✅ Migration & Seeding: Kysely-based migrations and flexible seeding system
- ✅ PostgreSQL Support: Optimized for PostgreSQL with
set_config()integration
Installation
node ace configure adonisjs-kyselyThis will:
- Create
config/kysely.tsconfiguration file - Create
types/db.tsstub for database types - Add
DATABASE_URLenvironment variable - Register provider and commands in
.adonisrc.ts
Generate Database Types
Add to your tsconfig.json:
{
"compilerOptions": {
"paths": {
"adonisjs-kysely/types/db": ["./types/db.ts"]
}
}
}Generate TypeScript types from your database schema:
npx kysely-codegen --out-file=types/db.tsDocumentation
📖 Comprehensive guides available:
- Usage Guide - Repository pattern, transactions, getting started
- Context Guide - Execution context, request-scoped data, PostgreSQL RLS
- Testing Guide - Test setup, transaction wrapping, isolation
- Migrations & Seeders - Database migrations, seeding
- Commands Guide - CLI commands
Key Concepts
Execution Context
Wrap entry points (HTTP, CLI, jobs) in dbContext.run():
import { dbContext } from 'adonisjs-kysely/services/main'
await dbContext.run(async () => {
// All DB operations here share the same context
})Transactions
Use runInTransaction() for atomic operations:
await kyselyDB.runInTransaction(async () => {
await createUser()
await createRole()
// Commits on success, rolls back on error
})Repository Pattern
Always use kyselyDB.getConnexion() - call fresh each time:
// ✅ Correct - transaction aware
kyselyDB.getConnexion().selectFrom('users').selectAll()Test Isolation
Tests wrap in transactions and auto-rollback - no data persists between tests.
Development Status
⚠️ Note: This package is under active development. While functional, expect potential changes and improvements. Please report any issues you encounter.
Build from Source
1. Clone and Install
git clone https://github.com/SA-EME/adonisjs-kysely
cd adonisjs-kysely
npm install2. Build Package
npm run build
npm packThis generates adonisjs-kysely-x.x.x.tgz
3. Install in Your Project
npm install path/to/adonisjs-kysely-x.x.x.tgzContributing
Contributions are welcome! Please feel free to submit issues or pull requests.
License
MIT License - see LICENSE.md for details.
