Skip to content

Commit b02f1fa

Browse files
committed
Add Docker Compose service connection support for MongoDB Atlas
1 parent 906b6d2 commit b02f1fa

File tree

6 files changed

+101
-0
lines changed

6 files changed

+101
-0
lines changed

spring-ai-docs/src/main/antora/modules/ROOT/pages/api/docker-compose.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ The following service connection factories are provided in the `spring-ai-spring
3434
| `ChromaConnectionDetails`
3535
| Containers named `chromadb/chroma`, `ghcr.io/chroma-core/chroma`
3636

37+
| `MongoConnectionDetails`
38+
| Containers named `mongodb/mongodb-atlas-local`
39+
3740
| `OllamaConnectionDetails`
3841
| Containers named `ollama/ollama`
3942

spring-ai-spring-boot-docker-compose/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,13 @@
123123
<optional>true</optional>
124124
</dependency>
125125

126+
<dependency>
127+
<groupId>org.springframework.ai</groupId>
128+
<artifactId>spring-ai-mongodb-atlas-store</artifactId>
129+
<version>${project.parent.version}</version>
130+
<optional>true</optional>
131+
</dependency>
132+
126133
<!-- test dependencies -->
127134

128135
<dependency>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2023 - 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.ai.docker.compose.service.connection.mongo;
17+
18+
import com.mongodb.ConnectionString;
19+
import org.springframework.boot.autoconfigure.mongo.MongoConnectionDetails;
20+
import org.springframework.boot.docker.compose.core.RunningService;
21+
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionDetailsFactory;
22+
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionSource;
23+
24+
/**
25+
* @author Eddú Meléndez
26+
*/
27+
class MongoDbAtlasLocalDockerComposeConnectionDetailsFactory
28+
extends DockerComposeConnectionDetailsFactory<MongoConnectionDetails> {
29+
30+
private static final int MONGODB_PORT = 27017;
31+
32+
protected MongoDbAtlasLocalDockerComposeConnectionDetailsFactory() {
33+
super("mongodb/mongodb-atlas-local");
34+
}
35+
36+
@Override
37+
protected MongoConnectionDetails getDockerComposeConnectionDetails(DockerComposeConnectionSource source) {
38+
return new MongoDbAtlasLocalContainerConnectionDetails(source.getRunningService());
39+
}
40+
41+
/**
42+
* {@link MongoConnectionDetails} backed by a {@code MongoDB Atlas}
43+
* {@link RunningService}.
44+
*/
45+
static class MongoDbAtlasLocalContainerConnectionDetails extends DockerComposeConnectionDetails
46+
implements MongoConnectionDetails {
47+
48+
private final String connectionString;
49+
50+
MongoDbAtlasLocalContainerConnectionDetails(RunningService service) {
51+
super(service);
52+
this.connectionString = String.format("mongodb://%s:%d/?directConnection=true", service.host(),
53+
service.ports().get(MONGODB_PORT));
54+
}
55+
56+
@Override
57+
public ConnectionString getConnectionString() {
58+
return new ConnectionString(this.connectionString);
59+
}
60+
61+
}
62+
63+
}

spring-ai-spring-boot-docker-compose/src/main/resources/META-INF/spring.factories

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
org.springframework.boot.autoconfigure.service.connection.ConnectionDetailsFactory=\
22
org.springframework.ai.docker.compose.service.connection.chroma.ChromaDockerComposeConnectionDetailsFactory,\
3+
org.springframework.ai.docker.compose.service.connection.mongo.MongoDbAtlasLocalDockerComposeConnectionDetailsFactory,\
34
org.springframework.ai.docker.compose.service.connection.ollama.OllamaDockerComposeConnectionDetailsFactory,\
45
org.springframework.ai.docker.compose.service.connection.opensearch.OpenSearchDockerComposeConnectionDetailsFactory,\
56
org.springframework.ai.docker.compose.service.connection.qdrant.QdrantDockerComposeConnectionDetailsFactory,\
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.springframework.ai.docker.compose.service.connection.mongo;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.springframework.boot.autoconfigure.mongo.MongoConnectionDetails;
5+
import org.springframework.boot.docker.compose.service.connection.test.AbstractDockerComposeIntegrationTests;
6+
import org.testcontainers.utility.DockerImageName;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
10+
class MongoDbAtlasLocalDockerComposeConnectionDetailsFactoryTests extends AbstractDockerComposeIntegrationTests {
11+
12+
protected MongoDbAtlasLocalDockerComposeConnectionDetailsFactoryTests() {
13+
super("mongo-compose.yaml", DockerImageName.parse("mongodb/mongodb-atlas-local"));
14+
}
15+
16+
@Test
17+
void runCreatesConnectionDetails() {
18+
MongoConnectionDetails connectionDetails = run(MongoConnectionDetails.class);
19+
assertThat(connectionDetails.getConnectionString()).isNotNull();
20+
}
21+
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
services:
2+
mongo:
3+
image: '{imageName}'
4+
ports:
5+
- '27017'

0 commit comments

Comments
 (0)