Neo4j Graph Database client for Koltin + Gradle project.
Layered architecture exposing multiple levels of API.
Recommended top-level DSL API enables you to perform DB operations in declarative type-safe semantics specific to your domain.
Based on the verbs used in your Entity-Relation definitions (see Define Entities and Relations),
custom DSL is auto-generated with semantic verbs as method names.
In this example, we have …
-
Two entities -
(User)and(Role) -
Directed relation from User to Role -
(User)-[HAS_ROLE]→(Role).
First you have to Define Entities and Relations.
Based on those definitions, custom DSL methods will be auto-generated for you.
Then you can do operations on Entities such as …
// create
create { User(id = "foo", name = "Jane Doe") }
// read
val user = get( User withId "foo" )
// update
update { User(id = "foo", name = "John Doe") }
// delete
delete { User withId "foo" }And operations on Entity Relations such as
// create
link { (User withId "foo") hasRole (Role withId "admin") }
// read
val user = get( User withRole (Role withId "admin"))
val role = get( Role ofUser (User withId "foo"))
// delete
unlink { (User withId "foo") hasRole (Role withId "admin") }|
ℹ️
|
|
For e.g., to define…
-
entities -
(User)and(Role) -
relation
(User)-[HAS_ROLE]→(Role).
package dev.vihang.neo4jstore.examples.dslclient
import dev.vihang.neo4jstore.schema.model.HasId
import dev.vihang.neo4jstore.dsl.model.annotation.Entity
import dev.vihang.neo4jstore.dsl.model.annotation.Relation
@Entity // (4)
@Relation( // (5)
name = "HAS_ROLE", // (6)
to = "dev.vihang.neo4jstore.examples.dslclient.Role", // (7)
forwardRelation = "hasRole", // [User] hasRole [Role] // (8)
reverseRelation = "ofUser", // [Role] ofUser [User] // (8)
forwardQuery = "withRole", // get(User withRole [Role]) // (9)
reverseQuery = "ofUser") // get(Role ofUser [User]) // (9)
data class User( // (1)
override val id: String, // (2)
val name: String
) : HasId { // (2)
// Needed for DSL
companion object // (3)
}
@Entity // (4)
data class Role( // (1)
override val id: String, // (2)
val description: String
) : HasId { // (2)
// Needed for DSL
companion object // (3)
}To define an Entity (steps 1 to 4) and Relation (steps 5 onwards):
-
Create Entity class as simple Koltin
data class. Name of the class will be used as label in Neo4j nodes. -
Make entity classes implement
interface HasIdand hence add a fieldoverride val id: String. -
Add
companion objectfor entity classes. It is used to add extension functions such aswithIdfor the DSL such asget( User withId "foo" ). -
Add
@Entityannotation to the entity class. -
Only on from entity of a directed relation, add
@Relationannotation.
In this example, the relation is fromUserentity. -
nameof the relation has to be capital snake case (i.e., separated by underscore). It will be used as label in Neo4j relations. -
toof the relation is the to entity class’s qualified name, i.e., including package name. -
forwardRelationandreverseRelationare relation verbs in camel case to define (create/link or delete/unlink) relation.
E.g.User hasRole RoleandRole ofUser User.
Actual DSL code will be:-
link { (User withId "foo") hasRole (Role withId "admin") } -
link { (Role withId "admin") ofUser (User withId "foo")}
-
-
forwardQueryandreverseQueryare query verbs, which are used in the query based on relations.
E.g.get( User withRole Role)andget( Role ofUser User )
Actual DSL code will be:-
get( User withRole (Role withId "admin")) -
get( Role ofUser (User withId "foo")).
-
|
ℹ️
|
|
|
💡
|
|
|
ℹ️
|
This quick start guide is only for layer 3 API. |
This project was a module written by me named neo4j-store in a larger Open-Source mono-repo project - ostelco-core with Apache License 2.0. The module has been ported out to be project of its own so that it can be used independently. The new project is made generic and does not take any code with business logic from its origin project.