https://kotlinlang.org logo
#exposed
Title
# exposed
n

Nick Halase

04/04/2022, 9:04 PM
Greetings! I'm evaluating switching my Spring Boot project over to Ktor. Here's some relevant information: • The service is a resource server with Auth0 as the provider. • The original Spring Boot service is using schema-based tenant isolation. ◦ Using
org.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`:
Copy code
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`:
Copy code
@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?
Alternatively, I think this could work:
Copy code
suspend fun <T> dbQuery(
    principal: JWTPrincipal,
    block: suspend () -> T
): T = newSuspendedTransaction {
    SchemaUtils.setSchema(Schema(principal.getSchemaName()))
    block()
}
3 Views