Nick Halase
04/04/2022, 9:04 PMorg.hibernate.context.spi.CurrentTenantIdentifierResolver
and org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean
to set the database schema using SecurityContextHolder.getContext()
to determine the schema
• The Ktor service is attempting to do the same schema-based tenant isolation, but instead is doing this with `Exposed`:
import org.jetbrains.exposed.sql.Table
fun getDynamicSchemaName(): String {
TODO("How can I get access to the request-scoped JWTPrincipal to determine this?")
}
open class SchemaTable(private val name: String) : Table() {
override val tableName: String
get() = "${getDynamicSchemaName()}.$name"
}
Here is an example `Route`:
@ExperimentalCoroutinesApi
fun Route.fooFindAllRoute(fooService: FooService) {
authenticate("auth0") {
get("/foo") {
val principal = call.principal<JWTPrincipal>() ?: error("missing JWTPrincipal on authenticated request")
call.respond(fooService.findAll(principal).map { it.toResource() })
}
}
}
I don't like this because it poops up my service API by requiring the principal
.
---
Is there a better way to do what I'm trying to do here with Ktor and Exposed?suspend fun <T> dbQuery(
principal: JWTPrincipal,
block: suspend () -> T
): T = newSuspendedTransaction {
SchemaUtils.setSchema(Schema(principal.getSchemaName()))
block()
}