Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/stashymane/mongodb-services
A collection of utilities for the MongoDB Kotlin driver
https://github.com/stashymane/mongodb-services
kotlin mongodb
Last synced: 4 days ago
JSON representation
A collection of utilities for the MongoDB Kotlin driver
- Host: GitHub
- URL: https://github.com/stashymane/mongodb-services
- Owner: stashymane
- Created: 2024-03-19T11:44:42.000Z (8 months ago)
- Default Branch: master
- Last Pushed: 2024-04-12T11:49:05.000Z (7 months ago)
- Last Synced: 2024-04-12T18:48:36.862Z (7 months ago)
- Topics: kotlin, mongodb
- Language: Kotlin
- Homepage:
- Size: 90.8 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# MongoDB Kotlin driver services
A collection of utilities to improve the usability of the Kotlin MongoDB driver.
## Usage
Currently only available via my personal repo, will be published to Maven central on first stable release.
```kotlin
repositories {
maven("https://repo.stashy.dev/releases")
}dependencies {
implementation("dev.stashy.mongoservices:mongodb-services:")//model has no dependency on MongoDB libs and is included in the library above
implementation("dev.stashy.mongoservices:model:")
}
```## Features
* Builder/DSL pattern for many operations (insert, update, index, etc.)
* Automatic SerialName fetching with operation builder pattern (via Kotlin reflection, not possible to do this otherwise
currently)
* Service class for better ergonomics
* ObjectId replacement (`DocumentId`) for sharing your data model across projects without including the MongoDB driver
itself.## Examples
### Filter builder
```kotlin
data class Foo(@SerialName("_id") val id: @Contextual DocumentId, val bar: String)filter {
(Foo::bar equals "bar") or (Foo::bar equals "baz")
}// the above is equivalent to:
Filters.or(
Filters.eq(Foo::bar.name, "bar"),
Filters.eq(Foo::bar.name, "baz")
)// ...with the added benefit of automatically
// using names from the SerialName annotation.
```### Service class
```kotlin
class DataService(db: MongoDatabase) : MongoService("foo", db, Foo::class) {
override suspend fun init() {
super.init() // base function creates the collection
collection.createIndex { Foo::bar.text() } // creates a text index for the `bar` property
}fun search(query: String): Foo? = collection.find { text(query) }.singleOrNull()
}
```## TODO
* Aggregate builder
* Projection builder