Skip to content

Commit

Permalink
Create basic projection builder
Browse files Browse the repository at this point in the history
  • Loading branch information
stashymane committed May 25, 2024
1 parent 25731a2 commit 28ec72c
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package dev.stashy.mongoservices

import com.mongodb.kotlin.client.coroutine.MongoCollection
import dev.stashy.mongoservices.builders.FilterBuilder
import dev.stashy.mongoservices.builders.IndexBuilder
import dev.stashy.mongoservices.builders.SortBuilder
import dev.stashy.mongoservices.builders.UpdateBuilder
import dev.stashy.mongoservices.builders.*
import org.bson.conversions.Bson

interface MongoServiceBase<T : Any> {
Expand All @@ -19,3 +16,5 @@ inline fun <T : Any> MongoServiceBase<T>.index(fn: IndexBuilder<T>.() -> Bson):
inline fun <T : Any> MongoServiceBase<T>.update(fn: UpdateBuilder<T>.() -> Bson): Bson = fn(UpdateBuilder())

inline fun <T : Any> MongoServiceBase<T>.filter(fn: FilterBuilder<T>.() -> Bson): Bson = fn(FilterBuilder())

inline fun <T : Any> MongoServiceBase<T>.projection(fn: ProjectionBuilder<T>.() -> Bson): Bson = fn(ProjectionBuilder())
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package dev.stashy.mongoservices.builders

import com.mongodb.client.model.Projections
import org.bson.conversions.Bson
import kotlin.reflect.KProperty1

class ProjectionBuilder<T> {
fun include(vararg fields: KProperty1<T, *>): Bson =
Projections.include(fields.map { it.serialName() })

fun include(fields: Iterable<KProperty1<T, *>>): Bson =
Projections.include(fields.map { it.serialName() })

fun exclude(vararg fields: KProperty1<T, *>): Bson =
Projections.exclude(fields.map { it.serialName() })

fun exclude(fields: Iterable<KProperty1<T, *>>): Bson =
Projections.exclude(fields.map { it.serialName() })

fun excludeId(): Bson = Projections.excludeId()

fun elemMatch(field: KProperty1<T, *>): Bson =
Projections.elemMatch(field.serialName())

fun elemMatch(field: KProperty1<T, *>, filter: FilterBuilder<T>.() -> Bson): Bson =
Projections.elemMatch(field.serialName(), filter(FilterBuilder()))

fun meta(field: KProperty1<T, *>, metaFieldName: String): Bson =
Projections.meta(field.serialName(), metaFieldName)

fun metaTextScore(field: KProperty1<T, *>): Bson =
Projections.metaTextScore(field.serialName())

fun metaSearchScore(field: KProperty1<T, *>): Bson =
Projections.metaSearchScore(field.serialName())

fun metaVectorSearchScore(field: KProperty1<T, *>): Bson =
Projections.metaVectorSearchScore(field.serialName())

fun metaSearchHighlights(field: KProperty1<T, *>): Bson =
Projections.metaSearchHighlights(field.serialName())

fun slice(field: KProperty1<T, *>, skip: Int = 0, limit: Int): Bson =
Projections.slice(field.serialName(), skip, limit)

fun combine(vararg projection: Bson): Bson =
Projections.fields(*projection)

fun combine(projections: List<Bson>): Bson =
Projections.fields(projections)

fun <TExpression> computed(field: KProperty1<T, *>, expression: TExpression & Any) =
Projections.computed(field.serialName(), expression)

fun computedSearchMeta(field: KProperty1<T, *>): Bson =
Projections.computedSearchMeta(field.serialName())
}

0 comments on commit 28ec72c

Please sign in to comment.