All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jvmMain.dev.inmo.micro_utils.repos.exposed.AbstractExposedReadCRUDRepo.kt Maven / Gradle / Ivy

There is a newer version: 0.22.2
Show newest version
package dev.inmo.micro_utils.repos.exposed

import dev.inmo.micro_utils.pagination.*
import dev.inmo.micro_utils.repos.ReadCRUDRepo
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.transaction

abstract class AbstractExposedReadCRUDRepo(
    tableName: String
) :
    ReadCRUDRepo,
    ExposedCRUDRepo,
    Table(tableName)
{
    override suspend fun getByPagination(pagination: Pagination): PaginationResult {
        return transaction(db = database) {
            selectAll().paginate(pagination).map {
                it.asObject
            }.createPaginationResult(
                pagination,
                selectAll().count()
            )
        }
    }
    override suspend fun getById(id: IdType): ObjectType? {
        return transaction(db = database) {
            select {
                selectById(id)
            }.limit(1).firstOrNull() ?.asObject
        }
    }

    override suspend fun contains(id: IdType): Boolean = transaction(db = database) {
        select { selectById(id) }.limit(1).any()
    }

    override suspend fun count(): Long = transaction(db = database) { selectAll().count() }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy