-
-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit of a utility to load a Structurizr workspace into a Ne…
…o4j database.
- Loading branch information
1 parent
8d57511
commit 2ed0cca
Showing
5 changed files
with
80 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# structurizr-neo4j | ||
|
||
[![Maven Central](https://img.shields.io/maven-central/v/com.structurizr/structurizr-neo4j.svg?label=Maven%20Central)](https://search.maven.org/artifact/com.structurizr/structurizr-neo4j) | ||
|
||
This library provides utilities to import a Structurizr workspace into Neo4j. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
dependencies { | ||
|
||
api project(':structurizr-core') | ||
implementation 'org.neo4j.driver:neo4j-java-driver:5.23.0' | ||
|
||
testImplementation project(':structurizr-client') | ||
testImplementation 'org.junit.jupiter:junit-jupiter:5.9.2' | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
structurizr-neo4j/src/main/java/com/structurizr/neo4j/SimpleLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.structurizr.neo4j; | ||
|
||
import com.structurizr.Workspace; | ||
import com.structurizr.model.Element; | ||
import com.structurizr.model.Relationship; | ||
import com.structurizr.util.StringUtils; | ||
import org.neo4j.driver.Driver; | ||
import org.neo4j.driver.SessionConfig; | ||
|
||
public class SimpleLoader { | ||
|
||
public void load(Workspace workspace, Driver driver, String database) { | ||
try (var session = driver.session(SessionConfig.builder().withDatabase(database).build())) { | ||
for (Element element : workspace.getModel().getElements()) { | ||
session.run(String.format( | ||
"CREATE ( :Element { id: '%s', name: \"%s\", type: \"%s\" })", | ||
element.getId(), element.getName(), element.getClass().getSimpleName().toLowerCase() | ||
)); | ||
} | ||
|
||
session.run("CREATE INDEX element_index FOR (n:Element) ON (n.id)"); | ||
|
||
for (Relationship relationship : workspace.getModel().getRelationships()) { | ||
session.run(String.format( | ||
""" | ||
MATCH ( from:Element { id: '%s' } ), ( to:Element { id: '%s' } ) | ||
CREATE (from)-[:HAS_RELATIONSHIP_WITH {role: '%s'}]->(to)""", | ||
relationship.getSource().getId(), | ||
relationship.getDestination().getId(), | ||
!StringUtils.isNullOrEmpty(relationship.getDescription()) ? relationship.getDescription() : "uses" | ||
)); | ||
} | ||
} | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
structurizr-neo4j/src/test/java/com/structurizr/Example.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.structurizr; | ||
|
||
import com.structurizr.neo4j.SimpleLoader; | ||
import com.structurizr.util.WorkspaceUtils; | ||
import org.neo4j.driver.AuthTokens; | ||
import org.neo4j.driver.Driver; | ||
import org.neo4j.driver.GraphDatabase; | ||
import org.neo4j.driver.Result; | ||
|
||
import java.io.File; | ||
|
||
public class Example { | ||
|
||
public static void main(String[] args) throws Exception { | ||
Workspace workspace = WorkspaceUtils.loadWorkspaceFromJson(new File("workspace.json")); | ||
|
||
try (Driver driver = GraphDatabase.driver("bolt://localhost:7687", AuthTokens.basic("neo4j", "password"))) { | ||
try (var session = driver.session()) { | ||
session.run("DROP DATABASE structurizr IF EXISTS"); | ||
Result result = session.run("CREATE DATABASE structurizr"); | ||
System.out.println(result.consume()); | ||
} | ||
|
||
new SimpleLoader().load(workspace, driver, "structurizr"); | ||
} | ||
} | ||
|
||
} |