Module Version Hex Docs License REUSE status Ask DeepWiki

Ash DataLayer for Neo4j, configurable using a simple DSL

Installation

mix igniter.install ash_neo4j

This automatically configures the formatter, adds Bolty connection config to config/runtime.exs, and wires Bolty into your supervision tree.

Manual

Add to deps in mix.exs:

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

Then follow the Bolty configuration steps below.

AI Coding Assistants

AshNeo4j ships usage rules for AI coding assistants. If your project uses usage_rules, add ash_neo4j to your :usage_rules config and run mix usage_rules.sync to merge the rules into your AGENTS.md (or CLAUDE.md).

Tutorial

To get started you need a running instance of Livebook

Run in Livebook

Usage

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

  use Ash.Resource,
    data_layer: AshNeo4j.DataLayer

Configuration

Each Ash.Resource allows configuration of its AshNeo4j.DataLayer. An example Comment resource is given below, it can belong to a Post resource. The neo4j configuration block below is actually unnecessary as written.

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

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

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

  attributes do
    uuid_primary_key :id
    attribute :title, :string, public?: true
    attribute :date_created, :date, source: :dateCreated
  end

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

Label

The DSL may be used to label the Ash Resource's underlying graph node. If omitted the Ash Resource's short module name will be used.

  neo4j do
    label :Comment
  end

Relate

The DSL may be used to specifically direct any relationship, in the form {relationship_name, edge_label, edge_direction, destination_label}. An entry can be provided for any relationship to override the default values created by AshNeo4j.

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

Default relate clauses are always :outgoing from the source resource, and the edgelabel is derived from the Ash relationship type. Relate clauses, whether specific or default must be unique {, edge_label, edge_direction, destination_label} for a given source_label to allow determination of the source relationship.

Guard

The DSL may be used to guard destroy actions, in the form {edge_label, edge_direction, destination_label}. By default incoming allow_nil? false belongs_to are guarded against deletion while relationships exist. Guards can be created independently of explicit relationships.

  neo4j do
    guard [{:WRITTEN_BY, :outgoing, :Post}]
  end

Guard is useful where the resource has no explicit relationships, but other resources expect the resource to exist while they are related. Guard can also be used where the underlying node has other edges which should prevent resource destruction.

Skip

The DSL may be used to skip storing attributes as node properties. This can be useful for 'transient' attributes, or attributes you want to default using the resource but not store explicitly.

  neo4j do
    skip [:other_id]
  end

Translate

Translation of resource attributes to/from Neo4j node properties is done without explicit Ash Neo4j DSL.

For convenience Ash Neo4j translates attributes with underscores to camelCase Neo4j properties. Neo4j uses the node property 'id' internally, so Ash Neo4j will translate the 'id' attribute using the camelCased short name of the type, e.g. an 'id' attribute of :uuid type is translated to the 'uuid' node property.

Ash Neo4j also supports the source field in Ash.Resource.Attribute DSL - if present this will be used for the node property.

Verifiers

The DSL is verified against misconfiguration and violation of accepted neo4j conventions providing compile time errors:

  • neo4j labels must be PascalCase
  • neo4j property names must be camelCase
  • edge label must be MACRO_CASE
  • edge direction must be in [:incoming, :outgoing]
  • relate: relationship_name must match the name of a relationship
  • relate: relationship enrichment not possible, edge_label, edge_direction and destination_label must be unique
  • attribute type requires unsupported term

Testing

AshNeo4j.Sandbox provides test isolation analogous to Ecto.Adapters.SQL.Sandbox. Each test that calls checkout/0 gets a dedicated Neo4j connection with an open transaction. All queries from that test run inside the transaction, which is rolled back automatically when the test process exits. Nothing is ever committed, so there is no data to clean up and tests can safely run in parallel.

Setup

Replace any Neo4jHelper.delete_all() or Neo4jHelper.delete_nodes/1 teardown with a sandbox checkout:

setup_all do
  AshNeo4j.BoltyHelper.start()
end

setup do
  AshNeo4j.Sandbox.checkout()
  on_exit(&AshNeo4j.Sandbox.rollback/0)
end

The on_exit call is optional — the transaction is rolled back automatically when the test process exits — but is recommended for clarity.

Parallel tests

Because each test's writes are confined to an uncommitted transaction, tests can run concurrently without interfering:

use ExUnit.Case, async: true

setup do
  AshNeo4j.Sandbox.checkout()
  on_exit(&AshNeo4j.Sandbox.rollback/0)
end

Installing Neo4j and Configuring Bolty

ash_neo4j uses neo4j which must be installed and running.

ash_neo4j uses bolty, a reluctant fork of boltx

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

I've used a few Neo4j 5.x community edition's up to 5.6.22 (bolty limits to bolt 5.4). I've also used DozerDB 5.26.3 with multi-database. I don't recommend either Neo4j 4.x or Neo4 5.x with BOLT BOLT 4.x, while it should work I haven't regressioned tested these.

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 Ash.Type.dump_to_native/cast_stored and 'native' Neo4j types, in many cases encoding to ISO8601, JSON or Base64 strings.

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>>"AQID"STRING
:booleanAsh.Type.BooleanBooleantruetrueBOOLEAN
:ci_stringAsh.Type.CiStringAsh.CiStringAsh.CiString.new("Hello")"Hello"STRING
:dateAsh.Type.DateDate~D[2025-05-11]2025-05-11DATE
:datetimeAsh.Type.DateTimeDateTime~U[2025-05-11 07:45:41Z]2025-05-11T07:45:41ZDATETIME
:decimalAsh.Type.DecimalDecimalDecimal.new("4.2")"\"4.2\""STRING
:durationAsh.Type.DurationDuration%Duration{month: 2}PT2HDURATION
:duration_nameAsh.Type.DurationNameAtom:day"day"STRING
:integerAsh.Type.IntegerInteger11INTEGER
:floatAsh.Type.FloatFloat1.234567891.23456789FLOAT
:functionAsh.Type.FunctionFunction&AshNeo4j.Neo4jHelper.create_node/2"&AshNeo4j.Neo4jHelper.create_node/2"STRING
subtype_of: :keywordDogKeyword using Ash.Type.NewTypeDogKeyword[name: "Henry", age: 8, breed: :groodle]"{\"age\":8,\"breed\":\"groodle\",\"name\":\"Henry\"}"STRING
:mapAsh.Type.MapMap%{name: "Henry", age: 8, breed: :groodle}"{\"age\":8,\"breed\":\"groodle\",\"name\":\"Henry\"}"STRING
:moduleAsh.Type.ModuleModuleAshNeo4j.DataLayer"Elixir.AshNeo4j.DataLayer"STRING
:naive_datetimeAsh.Type.NaiveDateTimeNaiveDateTime~N[2025-05-11 07:45:41]2025-05-11T07:45:41LOCAL_DATETIME
:stringAsh.Type.StringBitString"hello""hello"STRING
subtype_of: :structDogStruct using Ash.Type.NewTypeDogStruct%DogStruct{name: "Henry", age: 8, breed: :groodle}"{\"age\":8,\"breed\":\"groodle\",\"name\":\"Henry\"}"STRING
:timeAsh.Type.TimeTime~T[07:45:41Z]07:45:41ZTIME
:time_usecAsh.Type.TimeUsecTime~T[07:45:41.429903Z]07:45:41.429903000ZTIME
subtype_of: :tupleDogTuple using Ash.Type.NewTypeTuple{"Henry", 8, :groodle}"{\"age\":8,\"breed\":\"groodle\",\"name\":\"Henry\"}"STRING
:subtype_of :structDogTypedStruct using Ash.TypedStructDogTypedStruct%DogTypedStruct{name: "Henry", age: 8, breed: :groodle}"{\"age\":8,\"breed\":\"groodle\",\"name\":\"Henry\"}"STRING
:unionAsh.Type.UnionAsh.Union%Ash.Union{type: :typed_struct, value: %Dog{age: 8}}"{\"type\":\"typed_struct\",\"value\":{\"age\":8}}"STRING
:url_encoded_binaryAsh.Type.UrlEncodedBinaryBitString<<1, 2, 3>>"AQID"STRING
:utc_datetimeAsh.Type.UtcDatetimeDateTime~U[2025-05-11 07:45:41Z]2025-05-11T07:45:41ZDATETIME
:utc_datetime_usecAsh.Type.UtcDatetimeUsecDateTime~U[2025-05-11 07:45:41.429903Z]2025-05-11T07:45:41.429903000Z.DATETIME
:uuidAsh.Type.UUIDBitString"0274972c-161c-4dc9-882f-6851704c2af9""0274972c-161c-4dc9-882f-6851704c2af9"STRING
:uuid7Ash.Type.UUIDv7BitString"019d85f7-8450-7695-9426-4ede74026140""019d85f7-8450-7695-9426-4ede74026140"STRING

Ash :date, :datetime, :time and :naive_datetime are second precision, whereas :utc_datetime_usec and :time_usec are microsecond precision. Neo4j is capable of nanoseconds however Ash/Elixir is not.

Struct is supported, however must implement Ash.Type. Ash arrays are supported as arrays in neo4j.

Ash.Type.NewType including Ash.TypedStruct are supported, as are embedded resources.

Ash.Type.File, Ash.Type.Term and Ash.Type.Vector are not supported.

Storage Types

Generally AshNeo4j uses Ash.Type.dump_to_native and Ash.Type.cast_stored. Post/prior to this we may encode/decode either as JSON or Base64.

Ash.Type.Keyword, Ash.Type.Map, Ash.Type.Struct, Ash.Type.Tuple and Ash.Type.Union are stored as JSON. Ash.Type that have storage type map and aren't built in are also stored as JSON. This covers TypedStruct, embedded resources and Ash.Type.NewType you create subtype_of keyword, map, struct, tuple or union.

JSON types are stored as maps. We encode with AshNeo4j.Util.json_encode, which erases Struct's and orders keys. It deliberately avoids using Jason.Encoder on structs other than those it has converted to Jason.OrderedObject. This means you are free to use Jason.Encoder (possibly via ash_jason) for other concerns such as presentation or communications.

Interestingly many Ash.Types have identical JSON representations (e.g. Map, Struct, Tuple, Keyword). Neo4j lists are used for arrays since JSON and Base64 are strings.

A few things to note:

  • Ash.Type.UUID, Ash.Type.UUIDv7 - we persist in the 'cast_input' format rather than as compacted binary for readability, so we don't use Ash.Type.dump_to_native and Ash.Type.cast_stored at all. However foreign keys aren't persisted using properties, we of course use relationships.
  • Ash.Type.Function - we persist external functions as a string MFA, rather than binary, so we don't use Ash.Type.dump_to_native and Ash.Type.cast_stored at all. Persisting local functions is not supported.

Keys

We've generally used :uuid_primary_key, which Ash creates. While it may be possible to use other types for primary keys, we haven't done so yet.

Elixir nil and Neo4j Null

Generally attributes with nil value are not persisted, rather they are simply not created or removed on update to nil.

Other Notable

Transactions are supported.

Aggregates

AshNeo4j supports Ash aggregates. Declare them in the standard Ash aggregates block:

aggregates do
  count :comment_count, :comments
  exists :has_comments, :comments
  sum :total_score, :comments, field: :score
  avg :avg_score, :comments, field: :score
  min :min_score, :comments, field: :score
  max :max_score, :comments, field: :score
  first :first_comment_title, :comments, field: :title
  list :comment_titles, :comments, field: :title
end

Supported kinds: :count, :exists, :sum, :avg, :min, :max, :first, :list. The :custom kind is not supported.

Aggregates are computed in Cypher via OPTIONAL MATCH traversal. Single-hop and multi-hop relationship paths are both supported.

Embedded struct and JSON-type fields are supported. When field: refers to an attribute stored as JSON — Ash.TypedStruct, Ash.Type.NewType with map storage, embedded resources, Ash.Type.Map, Ash.Type.Union, etc. — AshNeo4j collects the raw JSON strings from Neo4j and deserializes them in Elixir using Ash.Type.cast_stored/3. :list and :first aggregates return fully deserialized struct values. :sum, :avg, :min, :max work when the deserialized values are directly comparable/numeric. To aggregate a sub-field within a struct, use an expr: aggregate.

aggregates do
  list :all_metadata, :related_things, field: :metadata   # returns [%MetadataStruct{}, ...]
  first :first_metadata, :related_things, field: :metadata # returns %MetadataStruct{}
end

# No elevation needed — navigate into the struct with an expression aggregate:
Ash.aggregate(MyResource, {:total_bandwidth, :sum, [
  path: [:characteristics],
  expr: Ash.Expr.expr(get_path(value, [:bandwidth])),
  expr_type: :integer
]})

For expr: aggregates, AshNeo4j fetches full destination records, evaluates the Ash expression on each via Ash.Expr.eval_hydrated/2, and aggregates in Elixir. Any valid Ash expression works — get_path for nested struct navigation, arithmetic, etc. Note: expr: is a programmatic API and is not available in the resource-level aggregates do DSL block.

Calculations

AshNeo4j supports expression calculations — calculations declared with expr(...) in the calculations block. They are evaluated in Elixir after records are loaded from Neo4j.

calculations do
  calculate :score_doubled, :integer, expr(score * 2)
  calculate :full_name, :string, expr(first_name <> " " <> last_name)
  calculate :dog_age, :integer, expr(get_path(dog, [:age]))
end

Calculations can be:

  • LoadedAsh.load!(records, [:score_doubled])
  • Filtered onAsh.Query.filter(score_doubled > 10) — AshNeo4j loads all matching nodes then evaluates the filter in Elixir
  • Sorted onAsh.Query.sort(score_doubled: :asc) — applied in Elixir after records are loaded via Ash.Actions.Sort.runtime_sort/3

Embedded struct fields work without elevation. get_path(dog, [:age]) navigates into a DogTypedStruct directly — records arrive with embedded types fully deserialized, so any Ash expression that works in-memory works in a calculation.

Only expr(...) calculations are currently supported. Custom :calculate callback modules are not.

Limitations and Future Work

Ash Neo4j has support for Ash create, update, read, destroy actions, aggregates, and expression calculations. The cypher is now parameterised but is by no means optimised. The DSL is likely to evolve further and this may break back compatibility. Storage formats are subject to infrequent change so upgrade may require data migration (not included).

Future work may include: cached calculations and aggregates, vectors/semantic search, geospatial support.

Collaboration on ash_neo4j welcome via github, please use discussions and/or raise issues as you encounter them. If going straight for a PR, please include explanation and test cases.

Acknowledgements

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

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

Thanks to the Neo4j Core for neo4j and pioneering work on graph databases. Thanks to DozerDB for enterprise features on community neo4j.

Diffo.dev Neo4j Deployment Centre.