Skip to content

Commit

Permalink
Integrate MongoDB
Browse files Browse the repository at this point in the history
  • Loading branch information
Kim Hendrick committed May 2, 2024
1 parent 543dd2f commit 00e31f4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 16 deletions.
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,34 @@ flowchart LR
1. Open http://0.0.0.0:8888/

## GraphQL
[KGraphQL](https://kgraphql.io/)
1. [Playground](http://0.0.0.0:8888/graphql)
2. Example Curl
2. Query documents via curl:
```bash
curl 'http://0.0.0.0:8888/graphql' \
-H 'Content-Type: application/json' \
--data-binary '{"query":"query {\n documents {\n info\n }\n}\n"}'
```
3. Create document via curl:
```bash
curl 'http://0.0.0.0:8888/graphql' \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
--data-binary '{"query":"# query {\n# documents {\n# info\n# }\n# }\n\nmutation Document($info: String!) {\n documents(info: $info)\n}","variables":{"info":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc sed eleifend urna. Vestibulum sodales cursus tellus, sit amet posuere felis pellentesque sed. Phasellus non lacus lobortis, iaculis nunc a, vulputate neque. Duis fringilla scelerisque augue, id ornare massa mollis sed. Integer augue enim, tincidunt in nunc ac, sollicitudin tincidunt mauris. Sed enim dolor, fermentum vel tincidunt at, interdum at est. Nunc posuere libero eu urna ultricies lobortis."}}'
```

## MongoDB
1. https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-os-x/
```bash
% mongosh
test> db.documents.insertOne(
{
info: "Lorem ipsum dolor sit amet...",
}
)
test> db.documents.find()
```

That's a wrap for now.
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@ package io.initialcapacity.web

import com.apurebase.kgraphql.GraphQL
import freemarker.cache.ClassTemplateLoader
import io.initialcapacity.documents.CreateDocuments
import io.initialcapacity.documents.QueryDocuments
import io.ktor.server.application.Application
import io.ktor.server.application.ApplicationCall
import io.ktor.server.application.call
import io.ktor.server.application.install
import io.ktor.server.engine.embeddedServer
import io.ktor.server.freemarker.FreeMarker
import io.ktor.server.freemarker.FreeMarkerContent
import io.ktor.server.http.content.staticResources
import io.ktor.server.netty.Netty
import io.ktor.server.response.respond
import io.ktor.server.routing.Routing
import io.ktor.server.routing.get
import io.ktor.util.pipeline.PipelineContext
import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.server.freemarker.*
import io.ktor.server.http.content.*
import io.ktor.server.netty.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.util.pipeline.*
import java.util.*

fun Application.module() {
Expand All @@ -35,6 +31,9 @@ fun Application.module() {
query("documents") {
resolver { -> QueryDocuments().list() }
}
mutation("documents") {
resolver { info: String -> CreateDocuments().execute(info) }
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions components/data-documents/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ dependencies {
implementation(project(":support:workflow-support"))

implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
implementation("org.mongodb:mongodb-driver-kotlin-coroutine:5.1.0")
implementation("org.mongodb:bson-kotlin:5.1.0")
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package io.initialcapacity.documents

import com.mongodb.ConnectionString
import com.mongodb.kotlin.client.coroutine.MongoClient
import kotlinx.coroutines.flow.toList

class QueryDocuments() {
fun list(): List<Document> {
return listOf(Document("Lorem Ipsum..."))
suspend fun list(): List<Document> {
val connectionString = ConnectionString("mongodb://localhost:27017")
val mongoClient = MongoClient.create(connectionString)
val db = mongoClient.getDatabase("test")
val collection = db.getCollection<Document>("documents")
return collection.find().toList()
}
}

0 comments on commit 00e31f4

Please sign in to comment.