Kest

Search

Search the documentation

Data

Overview

Connect to one or several databases and bind entities to them.

Kest has its own ORM (object-relational mapper): you describe your data as Kotlin classes (entities), and repositories give you methods to save and find them, without writing SQL for the common cases. The same entity model works on PostgreSQL, MySQL and MongoDB.

Loading diagram…

Connect

Name each connection once in the module that sets up the ORM, then bind entities and repositories to it:

import(OrmModule.forRoot {
    postgres("main", pgUrl, user, password)                    // "main" is just a name you choose
    mysql("reports", myUrl, user, password)
    mongo("events", mongoUri, database = "events")
})
 
@Entity("orders", connection = "main")   data class Order(...)
@Entity("daily_totals", connection = "reports") data class DailyTotal(...)
@Entity("audit_log", connection = "events") data class AuditEntry(...)
 
@Repository interface OrderRepository : CrudRepository<Order, String>                       // uses the entity's connection: main
@Repository("reports") interface ReportRepository : CrudRepository<Order, String>           // the same entity in another database

Most applications have one database and never write connection = at all (the connection called default is used). Several connections are for real needs: a read replica, a reporting database, an event store.

Or inject by entity and data source, with no interface (see ORM basics):

class Reports(@InjectRepository(Order::class, "reports") val orders: Repository<Order>, @InjectRepository(AuditEntry::class, "events") val audit: MongoRepository<AuditEntry>)

Which connection does a repository use? In this order: @Repository("name"), else the entity's connection, else default. A relation uses its target entity's connection unless it names another (@OneToMany(Event::class, "orderId", connection = "events")).

Where to go from here

You wantRead
define entities, save, find, derived queriesTypeORM (ORM basics)
link entities (one-to-many, many-to-many)Relations
several changes that succeed or fail togetherTransactions
change the schema safely over timeMigrations
lists in pages, cursorsPagination
a typed query builderDrizzle
collect changes and flushMikroORM
a document databaseMongoDB
cache expensive readsCaching

How it works (briefly)

One generated mapper serves SQL and MongoDB, and the same contract test runs against all three databases. The mapping is generated at build time by the processor, so there is no runtime reflection over your entities. Relations across databases load with batched queries and filters resolve in steps; cascades and EXISTS filters use one database when both sides share it.

health-orm adds a health indicator per connection, so readiness reflects database availability.

Good practice

  • Keep database settings (URL, user, password) in configuration and secrets in the environment.
  • Close connections on shutdown (the module does) and avoid creating the ORM in tests without module.close(): leaked pools make later tests fail.
  • Use migrations, not automatic schema changes, in production.

Read more