AshNeo4j

View Source

Module Version Hex Docs License

Ash DataLayer for Neo4j, configurable using a simple DSL

Installation

Add to the deps:

def deps do
  [
    {:ash_neo4j, "~> 0.1.3"},
  ]
end

Usage

Configure AshNeo4j.DataLayer as data_layer: within use Ash.Resource options:

  use Ash.Resource,
    data_layer: AshNeo4j.DataLayer

Configuration

Each Ash.Resource requires configuration of its AshNeo4j.DataLayer. An example Comment resource is given below, it can belong to a Post resource.

defmodule Comment.Resource do
  use Ash.Resource,
    data_layer: AshNeo4j.DataLayer

  neo4j do
    label :Comment
    store [:id, :title]
    translate id: :uuid
    relate [{:post, :BELONGS_TO, :outgoing}]
  end

  actions do
    default_accept(:*)
    defaults([:create, :read, :update, :destroy])
  end

  attributes do
    uuid_primary_key(:id)
    attribute(:title, :string, public?: true)
  end

  relationships do
    belongs_to(:post, Post, public?: true)
  end
end

Label

The DSL is used to label the Ash Resource's underlying graph node.

  neo4j do
    label :Comment
  end

Store

The DSL is used to store the Ash Resource's attributes as node properties, without translation.

  neo4j do
    store [:id, :title, :score, :public, :unique]
  end

Translate

The DSL may be used to translate the Ash Resource's attributes to node properties.

  neo4j do
    translate id: :uuid
  end

Relate

The DSL is used to direct any node relationships.

  neo4j do
    relate [{:post, :BELONGS_TO, :outgoing}]
  end

Installing Neo4j and Configuring Boltx

ash_neo4j uses neo4j which must be installed and running.

Your Ash application needs to configure, start and supervise boltx, see boltx documentation. Make sure to configure any required authorisation.

I've used Neo4j community edition 4.4 (bolt 4.4) and to connect using boltx I needed to also set the environment variable BOLT_VERSIONS=4.4 to steer [bolt protocol handshake] (https://neo4j.com/docs/bolt/current/bolt/handshake).

Elixir, Ash and Neo4j Types

We've made some decisions around how Ash/Elixir types are used to persist attributes as Neo4j properties. Where possible we've used 'native' Neo4j types, where this is not possible we've simply quoted to strings. Ash Array support is limited by Neo4j to lists of simple types which must be homogenous.

Ash Type shortnameAsh Type ModuleElixir Type ModuleAttribute Value ExampleNeo4j Node Property Value Cypher ExampleCypher Type
:atomAsh.Type.AtomAtom:a":a"STRING
:binaryAsh.Type.BinaryBitString<<1, 2, 3>>"\u0001\u0002\u0003"STRING
:booleanAsh.Type.BooleanBooleantruetrueBOOLEAN
:integerAsh.Type.IntegerInteger11INTEGER
:floatAsh.Type.FloatFloat1.234567891.23456789FLOAT
:stringAsh.Type.StringBitString"hello""hello"STRING
:tupleAsh.Type.TupleTuple{:a, 1, false}"{:a, 1, false}"STRING
:keywordAsh.Type.KeywordKeyword[{:a, :atom}, {:s, "string"}]["{:a, :atom}\","{:s, string}"]LIST
:mapAsh.Type.MapMap%{c: false, a: "a", b: 1, n: nil}"%{c: false, a: "a", b: 1, n: nil}"STRING
:mapsetAsh.Type.MapSetMapSetMapSet.new([1, false, :two])"MapSet.new([1, false, :two])"STRING
:structAsh.Type.StructStruct%MyApp.Struct{a: :a, s: "Hello"}"%MyApp.Struct{a: :a, s: \"Hello\"}"STRING
:uuidAsh.Type.UUIDBitString"0274972c-161c-4dc9-882f-6851704c2af9""0274972c-161c-4dc9-882f-6851704c2af9STRING
:url_encoded_binaryAsh.Type.UrlEncodedBinaryBitString"aHR0cHM6Ly93d3cuZGlmZm8uZGV2Lw""aHR0cHM6Ly93d3cuZGlmZm8uZGV2LwSTRING
:decimalAsh.Type.DecimalDecimalDecimal.new("4.2")"Decimal.new(\"4.2\")"STRING
:ci_stringAsh.Type.CiStringBitString"HELLO""HELLO"STRING
:functionAsh.Type.FunctionFunction&AshNeo4j.Neo4jHelper.create_node/2"&AshNeo4j.Neo4jHelper.create_node/2"STRING
:moduleAsh.Type.ModuleModuleAshNeo4j.DataLayer":Elixir.AshNeo4j.DataLayer"STRING
:regexAsh.Type.RegexRegex~r/foo/iu"~r/foo/iu"STRING
{:array, :atom}-List[:a,:b,:c][":a",":b",":c"]LIST
{:array, :boolean}-List[true,true,false][true,true,false]LIST
{:array, :integer}-List[1,2,3][1,2,3]LIST
{:array, :map}-List[%MyApp.Struct{a: :a, s: "Hello"}]["%MyApp.Struct{a: :a, s: \"Hello\"}"]LIST
{:array, :term}-List[%MyApp.Struct{a: :a, s: "Hello"}]["%MyApp.Struct{a: :a, s: \"Hello\"}"]LIST
:dateAsh.Type.DateDate~D[2025-02-25]2025-05-11DATE
:datetimeAsh.Type.DateTimeDateTime~U[2025-02-25 11:59:00Z]2025-05-11T07:45:41ZZONED_DATETIME
:utc_datetime_usecAsh.Type.UtcDateTimeUsecDateTime~U[2025-02-25 11:59:00.123456Z]2025-05-11T07:45:41.429903ZZONED_DATETIME
:naive_datetimeAsh.Type.NaiveDateTimeNaiveDateTime~N[2025-05-11 07:45:41]2025-05-11T07:45:41LOCAL_DATETIME
:timeAsh.Type.TimeTime~T[07:45:41Z]07:45:41TIME
:time_usecAsh.Type.TimeUsecTime~T[07:45:41.429903Z]07:45:41.429903TIME
:durationAsh.Type.DurationDuration%Duration{month: 2}PT2HDURATION

Ash :date, :datetime, :time and :naive_datetime are second precision, whereas :utc_datetime_usec and :time_usec are microsecond precision. Note that :time_usec requires ash ~> 3.5.10 (to be released).

Elixir nil and Neo4j Null

Generally attributes with nil value are not persisted, rather than created with Null value. However values of nil within string quoted 'Elixir' types (keyword, tuple, map and struct) are persisted.

Limitations and Future Work

Ash Neo4j is early stage, it is likely that the dsl will evolve and this may break back compatibility. Store is likely to be removed in favour of introspecting resource attributes. The dsl lacks validation and doesn't yet make appropriate use of transform.

Currently ash_neo4j has limited support for Ash create, update, read, destroy actions, and is in 'build' phase.

Collaboration on ash_neo4j welcome via github, please use discussions and/or issues as appropriate.

Acknowledgements

Thanks to the Ash Core for ash 🚀, including ash_csv which was an exemplar.

Thanks to Sagastume for boltx which AFAIK was based on bolt_sips by Florin Patrascu.

Thanks to the Neo4j Core for neo4j and pioneering work on graph databases.

Diffo.dev Neo4j Deployment Centre.