Skip to content

Commit fb22242

Browse files
committed
Add Spring Boot Starter for Spring Data FalkorDB
- Created spring-boot-starter-data-falkordb module - FalkorDBAutoConfiguration for auto-configuring FalkorDB beans - FalkorDBProperties for externalized configuration - FalkorDBRepositoriesAutoConfiguration for repository support - FalkorDBHealthIndicator for actuator health checks - Comprehensive README with usage examples The starter provides auto-configuration for: - FalkorDB Driver and Client beans - FalkorDBTemplate for data access - Repository scanning and creation - Health indicator integration - Configuration properties (spring.data.falkordb.*) Note: Some API details need adjustment once tested with JFalkorDB 0.5.1
1 parent 4a4fea6 commit fb22242

File tree

9 files changed

+606
-0
lines changed

9 files changed

+606
-0
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
# Spring Boot Starter for Spring Data FalkorDB
2+
3+
This starter provides auto-configuration for Spring Data FalkorDB in Spring Boot applications.
4+
5+
## Quick Start
6+
7+
### 1. Add Dependency
8+
9+
**Maven:**
10+
```xml
11+
<dependency>
12+
<groupId>com.falkordb</groupId>
13+
<artifactId>spring-boot-starter-data-falkordb</artifactId>
14+
<version>1.0.0-SNAPSHOT</version>
15+
</dependency>
16+
```
17+
18+
**Gradle:**
19+
```gradle
20+
implementation 'com.falkordb:spring-boot-starter-data-falkordb:1.0.0-SNAPSHOT'
21+
```
22+
23+
### 2. Configure Application Properties
24+
25+
Add the following to your `application.properties` or `application.yml`:
26+
27+
**application.properties:**
28+
```properties
29+
spring.data.falkordb.uri=falkordb://localhost:6379
30+
spring.data.falkordb.database=my-graph-db
31+
```
32+
33+
**application.yml:**
34+
```yaml
35+
spring:
36+
data:
37+
falkordb:
38+
uri: falkordb://localhost:6379
39+
database: my-graph-db
40+
```
41+
42+
### 3. Create Entity
43+
44+
```java
45+
@Node
46+
public class Person {
47+
@Id
48+
private Long id;
49+
50+
private String name;
51+
private int age;
52+
53+
@Relationship(type = "KNOWS")
54+
private List<Person> friends;
55+
56+
// getters and setters
57+
}
58+
```
59+
60+
### 4. Create Repository
61+
62+
```java
63+
public interface PersonRepository extends FalkorDBRepository<Person, Long> {
64+
List<Person> findByName(String name);
65+
List<Person> findByAgeGreaterThan(int age);
66+
}
67+
```
68+
69+
### 5. Use in Your Application
70+
71+
```java
72+
@SpringBootApplication
73+
public class MyApplication {
74+
75+
public static void main(String[] args) {
76+
SpringApplication.run(MyApplication.class, args);
77+
}
78+
}
79+
80+
@RestController
81+
public class PersonController {
82+
83+
@Autowired
84+
private PersonRepository personRepository;
85+
86+
@GetMapping("/persons")
87+
public List<Person> getAllPersons() {
88+
return personRepository.findAll();
89+
}
90+
91+
@PostMapping("/persons")
92+
public Person createPerson(@RequestBody Person person) {
93+
return personRepository.save(person);
94+
}
95+
}
96+
```
97+
98+
## Configuration Properties
99+
100+
| Property | Description | Default |
101+
|----------|-------------|---------|
102+
| `spring.data.falkordb.uri` | FalkorDB server URI | `falkordb://localhost:6379` |
103+
| `spring.data.falkordb.database` | Database name (required) | - |
104+
| `spring.data.falkordb.connection-timeout` | Connection timeout in milliseconds | `2000` |
105+
| `spring.data.falkordb.socket-timeout` | Socket timeout in milliseconds | `2000` |
106+
| `spring.data.falkordb.username` | Username for authentication | - |
107+
| `spring.data.falkordb.password` | Password for authentication | - |
108+
| `spring.data.falkordb.repositories.enabled` | Enable repository auto-configuration | `true` |
109+
110+
## Health Check
111+
112+
If you have Spring Boot Actuator in your classpath, a health indicator for FalkorDB is automatically configured.
113+
114+
```xml
115+
<dependency>
116+
<groupId>org.springframework.boot</groupId>
117+
<artifactId>spring-boot-starter-actuator</artifactId>
118+
</dependency>
119+
```
120+
121+
Check health at: `http://localhost:8080/actuator/health`
122+
123+
## Advanced Usage
124+
125+
### Custom FalkorDBClient
126+
127+
You can provide your own `FalkorDBClient` bean:
128+
129+
```java
130+
@Configuration
131+
public class FalkorDBConfig {
132+
133+
@Bean
134+
public FalkorDBClient falkorDBClient() {
135+
// Custom configuration
136+
return new MyCustomFalkorDBClient();
137+
}
138+
}
139+
```
140+
141+
### Disable Repository Auto-Configuration
142+
143+
```properties
144+
spring.data.falkordb.repositories.enabled=false
145+
```
146+
147+
## Examples
148+
149+
See the [examples directory](../examples) for complete sample applications.
150+
151+
## License
152+
153+
Apache License 2.0
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
5+
https://maven.apache.org/xsd/maven-4.0.0.xsd">
6+
<modelVersion>4.0.0</modelVersion>
7+
8+
<groupId>com.falkordb</groupId>
9+
<artifactId>spring-boot-starter-data-falkordb</artifactId>
10+
<version>1.0.0-SNAPSHOT</version>
11+
<packaging>jar</packaging>
12+
13+
<name>Spring Boot Starter for Spring Data FalkorDB</name>
14+
<description>Spring Boot Starter for Spring Data FalkorDB</description>
15+
<url>https://github.com/FalkorDB/spring-data-falkordb</url>
16+
17+
<licenses>
18+
<license>
19+
<name>The Apache Software License, Version 2.0</name>
20+
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
21+
<distribution>repo</distribution>
22+
</license>
23+
</licenses>
24+
25+
<developers>
26+
<developer>
27+
<id>shaharbiron</id>
28+
<name>Shahar Biron</name>
29+
<email>shahar at falkordb.com</email>
30+
<organization>FalkorDB</organization>
31+
</developer>
32+
</developers>
33+
34+
<scm>
35+
<connection>scm:git:https://github.com/FalkorDB/spring-data-falkordb.git</connection>
36+
<developerConnection>scm:git:https://github.com/FalkorDB/spring-data-falkordb.git</developerConnection>
37+
<url>https://github.com/FalkorDB/spring-data-falkordb</url>
38+
</scm>
39+
40+
<properties>
41+
<java.version>17</java.version>
42+
<maven.compiler.source>17</maven.compiler.source>
43+
<maven.compiler.target>17</maven.compiler.target>
44+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
45+
46+
<spring-boot.version>3.4.0</spring-boot.version>
47+
<spring-data-falkordb.version>1.0.0-SNAPSHOT</spring-data-falkordb.version>
48+
</properties>
49+
50+
<dependencyManagement>
51+
<dependencies>
52+
<dependency>
53+
<groupId>org.springframework.boot</groupId>
54+
<artifactId>spring-boot-dependencies</artifactId>
55+
<version>${spring-boot.version}</version>
56+
<type>pom</type>
57+
<scope>import</scope>
58+
</dependency>
59+
</dependencies>
60+
</dependencyManagement>
61+
62+
<dependencies>
63+
<!-- Spring Boot Starter -->
64+
<dependency>
65+
<groupId>org.springframework.boot</groupId>
66+
<artifactId>spring-boot-starter</artifactId>
67+
</dependency>
68+
69+
<!-- Spring Data FalkorDB -->
70+
<dependency>
71+
<groupId>com.falkordb</groupId>
72+
<artifactId>spring-data-falkordb</artifactId>
73+
<version>${spring-data-falkordb.version}</version>
74+
</dependency>
75+
76+
<!-- Configuration Properties Processor -->
77+
<dependency>
78+
<groupId>org.springframework.boot</groupId>
79+
<artifactId>spring-boot-configuration-processor</artifactId>
80+
<optional>true</optional>
81+
</dependency>
82+
83+
<!-- Actuator (optional) -->
84+
<dependency>
85+
<groupId>org.springframework.boot</groupId>
86+
<artifactId>spring-boot-starter-actuator</artifactId>
87+
<optional>true</optional>
88+
</dependency>
89+
90+
<!-- Test Dependencies -->
91+
<dependency>
92+
<groupId>org.springframework.boot</groupId>
93+
<artifactId>spring-boot-starter-test</artifactId>
94+
<scope>test</scope>
95+
</dependency>
96+
</dependencies>
97+
98+
<build>
99+
<plugins>
100+
<plugin>
101+
<groupId>org.apache.maven.plugins</groupId>
102+
<artifactId>maven-compiler-plugin</artifactId>
103+
<version>3.13.0</version>
104+
<configuration>
105+
<source>${java.version}</source>
106+
<target>${java.version}</target>
107+
</configuration>
108+
</plugin>
109+
</plugins>
110+
</build>
111+
112+
<distributionManagement>
113+
<repository>
114+
<id>central</id>
115+
<url>https://central.sonatype.com/api/v1/publisher/upload/</url>
116+
</repository>
117+
<snapshotRepository>
118+
<id>central</id>
119+
<url>https://central.sonatype.com/repository/maven-snapshots/</url>
120+
</snapshotRepository>
121+
</distributionManagement>
122+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package org.springframework.boot.autoconfigure.data.falkordb;
2+
3+
import com.falkordb.Driver;
4+
import com.falkordb.GraphDatabase;
5+
import org.springframework.boot.autoconfigure.AutoConfiguration;
6+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
7+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
8+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.data.falkordb.core.DefaultFalkorDBClient;
11+
import org.springframework.data.falkordb.core.FalkorDBClient;
12+
import org.springframework.data.falkordb.core.FalkorDBTemplate;
13+
import org.springframework.data.falkordb.core.mapping.DefaultFalkorDBEntityConverter;
14+
import org.springframework.data.falkordb.core.mapping.FalkorDBMappingContext;
15+
import org.springframework.data.mapping.model.EntityInstantiators;
16+
17+
/**
18+
* Auto-configuration for Spring Data FalkorDB.
19+
*
20+
* @author Shahar Biron
21+
* @since 1.0
22+
*/
23+
@AutoConfiguration
24+
@ConditionalOnClass({Driver.class, FalkorDBTemplate.class})
25+
@EnableConfigurationProperties(FalkorDBProperties.class)
26+
public class FalkorDBAutoConfiguration {
27+
28+
/**
29+
* Creates a FalkorDB client bean.
30+
* @param properties the FalkorDB properties
31+
* @return configured FalkorDB client
32+
*/
33+
@Bean
34+
@ConditionalOnMissingBean
35+
public Driver falkorDBDriver(FalkorDBProperties properties) {
36+
String uri = properties.getUri();
37+
38+
// Parse URI and create FalkorDB connection
39+
// URI format: falkordb://host:port or redis://host:port
40+
String host = "localhost";
41+
int port = 6379;
42+
43+
if (uri != null && !uri.isEmpty()) {
44+
uri = uri.replace("falkordb://", "").replace("redis://", "");
45+
String[] parts = uri.split(":");
46+
if (parts.length > 0) {
47+
host = parts[0];
48+
}
49+
if (parts.length > 1) {
50+
try {
51+
port = Integer.parseInt(parts[1]);
52+
} catch (NumberFormatException e) {
53+
// Keep default port
54+
}
55+
}
56+
}
57+
58+
return GraphDatabase.driver(host, port);
59+
}
60+
61+
@Bean
62+
@ConditionalOnMissingBean
63+
public FalkorDBClient falkorDBClient(Driver driver, FalkorDBProperties properties) {
64+
String database = properties.getDatabase();
65+
66+
if (database == null || database.isEmpty()) {
67+
throw new IllegalStateException(
68+
"spring.data.falkordb.database must be configured");
69+
}
70+
71+
return new DefaultFalkorDBClient(driver, database);
72+
}
73+
74+
/**
75+
* Creates a FalkorDB mapping context bean.
76+
* @return FalkorDB mapping context
77+
*/
78+
@Bean
79+
@ConditionalOnMissingBean
80+
public FalkorDBMappingContext falkorDBMappingContext() {
81+
try {
82+
FalkorDBMappingContext context = new FalkorDBMappingContext();
83+
context.setInitialEntitySet(java.util.Collections.emptySet());
84+
context.initialize();
85+
return context;
86+
} catch (Exception e) {
87+
throw new IllegalStateException("Failed to initialize FalkorDBMappingContext", e);
88+
}
89+
}
90+
91+
/**
92+
* Creates a FalkorDB template bean.
93+
* @param client the FalkorDB client
94+
* @param mappingContext the mapping context
95+
* @return FalkorDB template
96+
*/
97+
@Bean
98+
@ConditionalOnMissingBean
99+
public FalkorDBTemplate falkorDBTemplate(FalkorDBClient client,
100+
FalkorDBMappingContext mappingContext) {
101+
DefaultFalkorDBEntityConverter converter = new DefaultFalkorDBEntityConverter(
102+
mappingContext, new EntityInstantiators());
103+
return new FalkorDBTemplate(client, converter);
104+
}
105+
}

0 commit comments

Comments
 (0)