diff --git a/services/src/jvmMain/kotlin/dev/stashy/mongoservices/MongoServiceBase.kt b/services/src/jvmMain/kotlin/dev/stashy/mongoservices/MongoServiceBase.kt index c6c3e3a..4a54ae7 100644 --- a/services/src/jvmMain/kotlin/dev/stashy/mongoservices/MongoServiceBase.kt +++ b/services/src/jvmMain/kotlin/dev/stashy/mongoservices/MongoServiceBase.kt @@ -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 { @@ -19,3 +16,5 @@ inline fun MongoServiceBase.index(fn: IndexBuilder.() -> Bson): inline fun MongoServiceBase.update(fn: UpdateBuilder.() -> Bson): Bson = fn(UpdateBuilder()) inline fun MongoServiceBase.filter(fn: FilterBuilder.() -> Bson): Bson = fn(FilterBuilder()) + +inline fun MongoServiceBase.projection(fn: ProjectionBuilder.() -> Bson): Bson = fn(ProjectionBuilder()) \ No newline at end of file diff --git a/services/src/jvmMain/kotlin/dev/stashy/mongoservices/builders/ProjectionBuilder.kt b/services/src/jvmMain/kotlin/dev/stashy/mongoservices/builders/ProjectionBuilder.kt new file mode 100644 index 0000000..34641e6 --- /dev/null +++ b/services/src/jvmMain/kotlin/dev/stashy/mongoservices/builders/ProjectionBuilder.kt @@ -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 { + fun include(vararg fields: KProperty1): Bson = + Projections.include(fields.map { it.serialName() }) + + fun include(fields: Iterable>): Bson = + Projections.include(fields.map { it.serialName() }) + + fun exclude(vararg fields: KProperty1): Bson = + Projections.exclude(fields.map { it.serialName() }) + + fun exclude(fields: Iterable>): Bson = + Projections.exclude(fields.map { it.serialName() }) + + fun excludeId(): Bson = Projections.excludeId() + + fun elemMatch(field: KProperty1): Bson = + Projections.elemMatch(field.serialName()) + + fun elemMatch(field: KProperty1, filter: FilterBuilder.() -> Bson): Bson = + Projections.elemMatch(field.serialName(), filter(FilterBuilder())) + + fun meta(field: KProperty1, metaFieldName: String): Bson = + Projections.meta(field.serialName(), metaFieldName) + + fun metaTextScore(field: KProperty1): Bson = + Projections.metaTextScore(field.serialName()) + + fun metaSearchScore(field: KProperty1): Bson = + Projections.metaSearchScore(field.serialName()) + + fun metaVectorSearchScore(field: KProperty1): Bson = + Projections.metaVectorSearchScore(field.serialName()) + + fun metaSearchHighlights(field: KProperty1): Bson = + Projections.metaSearchHighlights(field.serialName()) + + fun slice(field: KProperty1, 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 = + Projections.fields(projections) + + fun computed(field: KProperty1, expression: TExpression & Any) = + Projections.computed(field.serialName(), expression) + + fun computedSearchMeta(field: KProperty1): Bson = + Projections.computedSearchMeta(field.serialName()) +} \ No newline at end of file