A Ktor plugin that provides a concise DSL-style API for building RESTful web services based on Ktor and Jimmer
go to the ktor-jimmer-rest-sample
Add it in your settings.gradle(.kts)
dependencyResolutionManagement {
repositories {
maven { url = uri("https://jitpack.io") }
}
}Add ktor-jimmer-rest to your project
implementation("com.github.eimsound:ktor-jimmer-rest")Use the JimmerRest plugin in your project
install(JimmerRest) {
jimmerSqlClientFactory {
inject<KSqlClient>() // koin inject
}
}Provides routes (create | remove | edit | id | list | api). For detailed usage, refer to
the documentation.
api<Book> {
// use specification dto or filter dsl
// filter(BookSpec::class)
filter {
where(
`ilike?`(table::name),
`ilike?`(table.store::name),
`between?`(table::price),
table.edition.`between?`(call["price", "ge"], call["price", "le"])
)
orderBy(table.id.desc())
}
// use view dto or fetcher dsl
// fetcher(BookView::class)
fetcher {
fetch.by {
allScalarFields()
name()
store {
name()
website()
}
authors {
name()
firstName()
lastName()
}
}
}
// use input dto or entity dsl
// input(BookInput::class) {}
input {
validator {
with(it) {
::name.notBlank { "名称不能为空" }
::price.range(0.toBigDecimal()..100.toBigDecimal()) { range ->
"价格必须在${range.start}和${range.endInclusive}之间"
}
}
}
transformer {
it.copy { name = it.name.uppercase() }
}
}
}- Inside
api<T>{},Tis a jimmer entity class, used to mark the context type - The filter conditions inside
filterare the functions provided by jimmer, and we have added extensions to these functions, for example,`between?`(table::price)is nullable and will be mapped to theprice__ge | price__lequery parameter, and for__ge | __le, it is a special extension of`between?`,`ilike?`can be used with the suffixes__anywhere | __exact | __start | __end, which correspond to different filtering functions, see the documentation for details - The fetcher then continues to use the functionality of jimmer, jimmer is indeed a very powerful orm framework, and writing it is very elegant, please refer to jimmer's documentation for details
- The input includes validator and transformer, which can be used to validate and transform objects