Skip to content

Implemented DE-695 #331

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,14 @@
*/

package com.arangodb.spring.demo;

import com.arangodb.spring.demo.runner.CrudRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class DemoApplication {
public static void main(final String... args) {
System.exit(SpringApplication.exit(
SpringApplication.run(CrudRunner.class, args)
SpringApplication.run(DemoApplication.class, args)
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public record Character(
@Id
String id,
String name,
String surname
String surname,
Long expiresAt
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,23 @@

package com.arangodb.spring.demo.runner;

import com.arangodb.model.TtlIndexOptions;
import com.arangodb.spring.demo.entity.Character;
import com.arangodb.spring.demo.repository.CharacterRepository;
import com.arangodb.springframework.core.ArangoOperations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Collections;
import java.util.Date;

import static org.assertj.core.api.Assertions.assertThat;

@ComponentScan("com.arangodb.spring.demo")
@Component
public class CrudRunner implements CommandLineRunner {

@Autowired
Expand All @@ -38,21 +45,41 @@ public class CrudRunner implements CommandLineRunner {
@Autowired
private CharacterRepository repository;

@Value("${arangodb.ddl.enabled:false}")
private boolean isDdlEnabled;

@Override
public void run(String... args) {
// first drop the database so that we can run this multiple times with the same dataset
operations.dropDatabase();
if (isDdlEnabled) {
operations.dropDatabase();
System.out.println("Database dropped for fresh DDL operations.");
createTTLIndex();

System.out.println("# CRUD operations");
System.out.println("Schema validation passed.");

// save a single entity in the database
// there is no need of creating the collection first. This happen automatically
Character nedStark = new Character(null, "Ned", "Stark");
Character saved = repository.save(nedStark);
System.out.println("Ned Stark saved in the database: " + saved);
// Set document expiration for 2 minutes from now
Long expiresAt = Instant.now().plus(2, ChronoUnit.MINUTES).getEpochSecond();
Character nedStark = new Character(null, "John", "Williams", expiresAt);
Character saved = repository.save(nedStark);

assertThat(saved.id()).isNotNull();
assertThat(saved.name()).isEqualTo(nedStark.name());
assertThat(saved.surname()).isEqualTo(nedStark.surname());
System.out.println("Saved character: " + saved);
System.out.println("Document expires at: " + new Date(saved.expiresAt() * 1000));
System.out.println("Current time: " + new Date());

assertThat(saved.id()).isNotNull();
assertThat(saved.name()).isEqualTo(nedStark.name());
assertThat(saved.surname()).isEqualTo(nedStark.surname());
} else {
System.out.println("DDL operations are disabled. Skipping schema creation.");
System.out.println(
"You can enable DDL operations by setting 'arangodb.ddl.enabled=true' in application.properties.");
}
}
private void createTTLIndex() {
System.out.println("Creating TTL index...");
operations.collection(Character.class).ensureTtlIndex(
Collections.singletonList("expiresAt"),
new TtlIndexOptions().expireAfter(0));
System.out.println(" TTL index created on 'expiresAt'.");
}
}
}
3 changes: 3 additions & 0 deletions tutorial/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# ArangoDB Configuration
arangodb.ddl.enabled=false