Directly use the database for sources in graphql

This commit is contained in:
Syer10
2023-03-31 20:29:55 -04:00
parent 007d20d417
commit 37f41ade43
4 changed files with 67 additions and 31 deletions

View File

@@ -10,52 +10,53 @@ package suwayomi.tachidesk.graphql.dataLoaders
import com.expediagroup.graphql.dataloader.KotlinDataLoader
import org.dataloader.DataLoader
import org.dataloader.DataLoaderFactory
import org.jetbrains.exposed.sql.SqlExpressionBuilder.inList
import org.jetbrains.exposed.sql.StdOutSqlLogger
import org.jetbrains.exposed.sql.addLogger
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.transactions.transaction
import suwayomi.tachidesk.graphql.types.SourceType
import suwayomi.tachidesk.manga.impl.Source
import suwayomi.tachidesk.manga.model.table.MangaTable
import suwayomi.tachidesk.manga.model.table.SourceTable
import suwayomi.tachidesk.server.JavalinSetup.future
class SourceDataLoader : KotlinDataLoader<String, SourceType> {
class SourceDataLoader : KotlinDataLoader<Long, SourceType?> {
override val dataLoaderName = "SourceDataLoader"
override fun getDataLoader(): DataLoader<String, SourceType> = DataLoaderFactory.newDataLoader<String, SourceType> { ids ->
override fun getDataLoader(): DataLoader<Long, SourceType?> = DataLoaderFactory.newDataLoader { ids ->
future {
Source.getSourceList().filter { it.id in ids }
.map { SourceType(it) }
transaction {
SourceTable.select { SourceTable.id inList ids }.map {
SourceType(it)
}
}
}
}
}
class SourceForMangaDataLoader : KotlinDataLoader<Int, SourceType?> {
override val dataLoaderName = "SourceForMangaDataLoader"
override fun getDataLoader(): DataLoader<Int, SourceType?> = DataLoaderFactory.newDataLoader<Int, SourceType?> { ids ->
override fun getDataLoader(): DataLoader<Int, SourceType?> = DataLoaderFactory.newDataLoader { ids ->
future {
transaction {
addLogger(StdOutSqlLogger)
val mangaSourceMap = MangaTable
val itemsByRef = MangaTable.innerJoin(SourceTable)
.select { MangaTable.id inList ids }
.associate { it[MangaTable.id].value to it[MangaTable.sourceReference] }
.map { Triple(it[MangaTable.id].value, it[MangaTable.sourceReference], it) }
.let { triples ->
val sources = buildMap {
triples.forEach {
if (!containsKey(it.second)) {
put(it.second, SourceType(it.third))
}
}
}
triples.associate {
it.first to sources[it.second]
}
}
val sourceIds = mangaSourceMap
.values
.distinct()
.map { it.toString() }
val sources = Source.getSourceList()
.filter { it.id in sourceIds }
.map { SourceType(it) }
.associateBy { it.id }
val mangaSourceTypeMap = mangaSourceMap.mapValues {
sources[it.value]
}
ids.map {
mangaSourceTypeMap[it]
}
ids.map { itemsByRef[it] }
}
}
}