Skip to content

Commit b113203

Browse files
committed
feat: Operation model constructor overloads
1 parent 820fbb5 commit b113203

1 file changed

Lines changed: 81 additions & 3 deletions

File tree

module-mongokt/src/commonMain/kotlin/Declarations.kt

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
*/
1616
package org.cufy.mongodb
1717

18-
import org.cufy.bson.BsonArray
19-
import org.cufy.bson.BsonDocument
20-
import org.cufy.bson.BsonElement
18+
import org.cufy.bson.*
2119
import kotlin.time.Duration
2220

2321
/* ============= ------------------ ============= */
@@ -205,6 +203,14 @@ data class MongoNamespace(
205203
"$database.$collection"
206204
}
207205

206+
fun CreateIndexModel(
207+
keys: BsonDocumentBlock,
208+
options: CreateIndexOptions.() -> Unit = {},
209+
) = CreateIndexModel(
210+
keys = BsonDocument(keys),
211+
options = CreateIndexOptions(options),
212+
)
213+
208214
/**
209215
* A model describing the creation of a single index.
210216
*
@@ -934,6 +940,14 @@ data class TimeSeriesOptions(
934940
*/
935941
sealed interface WriteModel
936942

943+
fun DeleteOneModel(
944+
filter: BsonDocumentBlock,
945+
options: DeleteOptions.() -> Unit = {},
946+
) = DeleteOneModel(
947+
filter = BsonDocument(filter),
948+
options = DeleteOptions(options),
949+
)
950+
937951
/**
938952
* A model describing the removal of at most one
939953
* document matching the query filter.
@@ -958,6 +972,14 @@ data class DeleteOneModel(
958972
val options: DeleteOptions = DeleteOptions(),
959973
) : WriteModel
960974

975+
fun DeleteManyModel(
976+
filter: BsonDocumentBlock,
977+
options: DeleteOptions.() -> Unit = {},
978+
) = DeleteManyModel(
979+
filter = BsonDocument(filter),
980+
options = DeleteOptions(options),
981+
)
982+
961983
/**
962984
* A model describing the removal of all documents
963985
* matching the query filter.
@@ -982,6 +1004,12 @@ data class DeleteManyModel(
9821004
val options: DeleteOptions = DeleteOptions(),
9831005
) : WriteModel
9841006

1007+
fun InsertOneModel(
1008+
document: BsonDocumentBlock,
1009+
) = InsertOneModel(
1010+
document = BsonDocument(document),
1011+
)
1012+
9851013
/**
9861014
* A model describing an insert of a single document.
9871015
*
@@ -998,6 +1026,16 @@ data class InsertOneModel(
9981026
val document: BsonDocument,
9991027
) : WriteModel
10001028

1029+
fun ReplaceOneModel(
1030+
filter: BsonDocumentBlock,
1031+
replacement: BsonDocumentBlock,
1032+
options: ReplaceOptions.() -> Unit = {},
1033+
) = ReplaceOneModel(
1034+
filter = BsonDocument(filter),
1035+
replacement = BsonDocument(replacement),
1036+
options = ReplaceOptions(options),
1037+
)
1038+
10011039
/**
10021040
* A model describing the replacement of at most
10031041
* one document that matches the query filter.
@@ -1030,6 +1068,26 @@ data class ReplaceOneModel(
10301068
val options: ReplaceOptions = ReplaceOptions(),
10311069
) : WriteModel
10321070

1071+
fun UpdateOneModel(
1072+
filter: BsonDocumentBlock,
1073+
update: BsonDocumentBlock,
1074+
options: UpdateOptions.() -> Unit = {},
1075+
) = UpdateOneModel(
1076+
filter = BsonDocument(filter),
1077+
update = BsonDocument(update),
1078+
options = UpdateOptions(options),
1079+
)
1080+
1081+
fun UpdateOneModel(
1082+
filter: BsonDocumentBlock,
1083+
update: List<BsonDocumentBlock>,
1084+
options: UpdateOptions.() -> Unit = {},
1085+
) = UpdateOneModel(
1086+
filter = BsonDocument(filter),
1087+
update = BsonArray { update.forEach { by(it) } },
1088+
options = UpdateOptions(options),
1089+
)
1090+
10331091
/**
10341092
* A model describing an update to at most one
10351093
* document that matches the query filter.
@@ -1072,6 +1130,26 @@ data class UpdateOneModel(
10721130
val options: UpdateOptions = UpdateOptions(),
10731131
) : WriteModel
10741132

1133+
fun UpdateManyModel(
1134+
filter: BsonDocumentBlock,
1135+
update: BsonDocumentBlock,
1136+
options: UpdateOptions.() -> Unit = {},
1137+
) = UpdateManyModel(
1138+
filter = BsonDocument(filter),
1139+
update = BsonDocument(update),
1140+
options = UpdateOptions(options),
1141+
)
1142+
1143+
fun UpdateManyModel(
1144+
filter: BsonDocumentBlock,
1145+
update: List<BsonDocumentBlock>,
1146+
options: UpdateOptions.() -> Unit = {},
1147+
) = UpdateManyModel(
1148+
filter = BsonDocument(filter),
1149+
update = BsonArray { update.forEach { by(it) } },
1150+
options = UpdateOptions(options),
1151+
)
1152+
10751153
/**
10761154
* A model describing an update to all documents
10771155
* that matches the query filter. The update to

0 commit comments

Comments
 (0)