简体   繁体   English

将 SqlDelight 与 Flow 结合使用并将实体映射到包装器 object

[英]Using SqlDelight with Flow and also mapping Entities to a wrapper object

I am using SqlDelight and Kotlin Multiplatform and am wondering if there is a way to map the returned record entities to a helper class before returning the Flow.我正在使用 SqlDelight 和 Kotlin Multiplatform,我想知道在返回 Flow 之前是否有办法将返回的记录实体 map 返回给助手 class。

Here is normal usage:这是正常用法:

val players: Flow<List<HockeyPlayer>> = 
  playerQueries.selectAll()
    .asFlow()
    .mapToList()

I want to do something like this:我想做这样的事情:

val players: Flow<List<HockeyPlayer>> = 
  playerQueries.selectAll()
    .map { it.toOtherClass() }
    .asFlow()
    .mapToList()

Is there a way to do this?有没有办法做到这一点? I'm a Kotlin n00b so I feel like I'm missing something obvious我是 Kotlin n00b,所以我觉得我错过了一些明显的东西

selectAll() returns a Query , so any mapping of result entities needs to happen after the asFlow() extension, and the query needs to be executed, ie executeAsList() for fetching the updated result as list. selectAll()返回一个Query ,因此结果实体的任何映射都需要在asFlow()扩展之后发生,并且需要执行查询,即executeAsList()用于获取更新结果作为列表。

An example could look like this:一个例子可能是这样的:

val players: Flow<List<HockeyPlayer>> = 
  playerQueries.selectAll()
    .map { it.toOtherClass() }
    .asFlow()
    .map { query ->
      query.executeAsList().map { it.toOtherClass() } 
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM