ids in crud repos

This commit is contained in:
2022-12-02 13:46:06 +06:00
parent 04c301d1ac
commit 217e977f0d
14 changed files with 88 additions and 0 deletions

View File

@@ -22,6 +22,17 @@ abstract class AbstractExposedReadCRUDRepo<ObjectType, IdType>(
)
}
}
override suspend fun getIdsByPagination(pagination: Pagination): PaginationResult<IdType> {
return transaction(db = database) {
selectAll().paginate(pagination).map {
it.asId
}.createPaginationResult(
pagination,
selectAll().count()
)
}
}
override suspend fun getById(id: IdType): ObjectType? {
return transaction(db = database) {
select {

View File

@@ -4,6 +4,7 @@ import org.jetbrains.exposed.sql.*
interface CommonExposedRepo<IdType, ObjectType> : ExposedRepo {
val ResultRow.asObject: ObjectType
val ResultRow.asId: IdType
val selectById: ISqlExpressionBuilder.(IdType) -> Op<Boolean>
val selectByIds: ISqlExpressionBuilder.(List<IdType>) -> Op<Boolean>
get() = { list ->

View File

@@ -15,7 +15,13 @@ abstract class AbstractExposedReadKeyValueRepo<Key, Value>(
CommonExposedRepo<Key, Value>,
Table(tableName ?: "") {
abstract val keyColumn: Column<*>
/**
* Same as [asId] in context of KeyValue repo
*/
abstract val ResultRow.asKey: Key
override val ResultRow.asId: Key
get() = asKey
abstract val selectByValue: ISqlExpressionBuilder.(Value) -> Op<Boolean>
override suspend fun get(k: Key): Value? = transaction(database) {

View File

@@ -17,6 +17,8 @@ abstract class AbstractExposedReadKeyValuesRepo<Key, Value>(
Table(tableName ?: "") {
abstract val keyColumn: Column<*>
abstract val ResultRow.asKey: Key
override val ResultRow.asId: Key
get() = asKey
abstract val selectByValue: ISqlExpressionBuilder.(Value) -> Op<Boolean>
override suspend fun count(k: Key): Long = transaction(database) { select { selectById(k) }.count() }