• Stars
    star
    30
  • Rank 812,771 (Top 17 %)
  • Language
    Crystal
  • License
    MIT License
  • Created over 6 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Pure-Crystal implementation of Neo4j's Bolt protocol

neo4j.cr

Neo4j connector for Crystal, supporting the Bolt protocol and routing queries to appropriate nodes in your Neo4j cluster.

Installation

Add this to your application's shard.yml:

dependencies:
  neo4j:
    github: jgaskins/neo4j.cr

Usage

First you need to set up a connection:

require "neo4j"

neo4j_uri = URI.parse("bolt://neo4j:password@localhost:7687")

# The `ssl` option defaults to `true` so you don't accidentally send the
# password to your production DB in cleartext.
driver = Neo4j.connect(neo4j_uri, ssl: false)

This will return a cluster driver or a direct driver depending on whether you provided a neo4j:// or bolt:// URI, respectively. neo4j:// can also be specified as bolt+routing://. Both drivers expose the same interface, but the cluster driver will route queries to a different server based on whether you specify that the query is a read or write query.

struct Person
  include Neo4j::Serializable::Node

  getter id: UUID
  getter name: String
  getter email: String
end

driver.session do |session|
  session.read_transaction do |read|
    query = <<-CYPHER
      MATCH (person:Person { name: $name })
      RETURN person
    CYPHER

    read.exec_cast(query, {Person}, name: "Jamie") do |(person)|
      pp person
    end
  end

  session.write_transaction do |write|
    write.execute <<-CYPHER, name: "Jamie"
      MATCH (person:Person { name: $name })
      SET person.login_count = person.login_count + 1
    CYPHER
  end
end

Neo4j::Result

  • type : (Neo4j::Success | Neo4j::Ignored)
    • If you get an Ignored result, it probably means an error occurred. Call connection#reset to get it back to working order.
    • If a query results in a Neo4j::Failure, an exception is raised rather than wrapping it in a Result.
  • data : Array(Array(Neo4j::Type))
    • This is the list of result values. For example, if you RETURN a, b, c from your query, then this will be an array of [a, b, c].

The Result object itself is an Enumerable. Calling Result#each will iterate over the data for you.

Neo4j::Node

These have a 1:1 mapping to nodes in your graph.

  • id : Int32: the node's internal id
    • WARNING: Do not store this id anywhere. These ids can be reused by the database. If you need an application-level unique id, store a UUID on the node. It is useful in querying nodes connected to this one when you already have it in memory, but not beyond that.
  • labels : Array(String): the labels stored on your node
  • properties : Hash(String, Neo4j::Type): the properties assigned to this node

Neo4j::Relationship

  • id: Int32: the relationship's internal id
  • type : String: the type of relationship
  • start : Int32: the internal id for the node on the starting end of this relationship
  • end : Int32: the internal id of the node this relationship points to
  • properties : Hash(String, Neo4j::Type): the properties assigned to this relationship

Neo4j::Value

Represents any data type that can be stored in a Neo4j database and communicated via the Bolt protocol. It's a shorthand for this union type:

Nil |
Bool |
String |
Int8 |
Int16 |
Int32 |
Int64 |
Float64 |
Time |
Neo4j::Point2D |
Neo4j::Point3D |
Neo4j::LatLng |
Array(Neo4j::Value) |
Hash(String, Neo4j::Value) |
Neo4j::Node |
Neo4j::Relationship |
Neo4j::UnboundRelationship |
Neo4j::Path

Mapping to Domain Objects

Similar to JSON.mapping in the Crystal standard library, you can map nodes and relationships to domain objects. For example:

require "uuid"

class User
  include Neo4j::Serializable::Node

  getter uuid : UUID
  getter email : String
  getter name : String
  getter registered_at : Time
end

class Product
  include Neo4j::Serializable::Node

  getter uuid : UUID
  getter name : String
  getter description : String
  getter price : Int32
  getter created_at : Time
end

class CartItem
  include Neo4j::Serializable::Relationship

  getter quantity : Int32
  getter price : Int32
end

Acknowledgements/Credits

The implementation of the wire protocol is heavily based on the MessagePack shard to understand how to serialize and deserialize a binary protocol in Crystal.

Contributing

  1. Fork it ( https://github.com/jgaskins/neo4j.cr/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

  • jgaskins Jamie Gaskins - creator, maintainer

More Repositories

1

perpetuity

Persistence gem for Ruby objects using the Data Mapper pattern
Ruby
251
star
2

live_view

Server-rendering for client-side interactions
Crystal
72
star
3

grpc

Pure-Crystal implementation of gRPC
Crystal
69
star
4

redis

Pure-Crystal Redis client, supporting clustering, RedisJSON, RediSearch, RedisGraph, and RedisTimeSeries
Crystal
49
star
5

primalize

Convert objects into primitives for serialization
Ruby
42
star
6

crystal_live_view_example

Proof of concept for a Crystal version of Phoenix Live View
Crystal
23
star
7

armature

Roda-inspired HTTP framework for Crystal, providing routing, sessions, forms, etc
Crystal
20
star
8

method_pattern

Pattern matching for Ruby methods
Ruby
20
star
9

nats

NATS client in pure Crystal with JetStream support
Crystal
20
star
10

interro

Framework for query objects and model objects
Crystal
19
star
11

turbo

Crystal
18
star
12

moku

ActivityPub server
Crystal
15
star
13

hot_topic

A fake HTTP client for making requests to your HTTP::Handler classes
Crystal
13
star
14

opal-slim

Sprockets integration to compile Slim templates for Opal apps
Ruby
13
star
15

datadog

Datadog client for APM tracing and metrics in Crystal
Crystal
12
star
16

opentelemetry

OpenTelemetry SDK and exporters for the Crystal language
Crystal
12
star
17

kubernetes

Kubernetes API client in Crystal, providing a framework for writing controllers/operators
Crystal
11
star
18

aws

AWS Client for the Crystal programming language
Crystal
11
star
19

mpsc

Multi-Producer/Single-Consumer channels in Crystal
Crystal
10
star
20

perpetuity-postgres

Postgres adapter for Perpetuity
Ruby
10
star
21

primalize-jsonapi

Ruby
8
star
22

rails_app_operator

Kubernetes Rails app operator, allowing simple day-1 Rails deployment to a Kubernetes cluster
Crystal
8
star
23

grpc_example

Example of using the GRPC Crystal shard in an application
Crystal
8
star
24

slow_ride

Ruby
5
star
25

elasticsearch

Elasticsearch client for Crystal
Crystal
4
star
26

opal_blog

An example of using the Clearwater framework with Opal/Rails
Ruby
4
star
27

pennant

Feature flags in Crystal applications with pluggable backends (in-memory and Redis currently supported)
Crystal
3
star
28

perpetuity-mongodb

Ruby
3
star
29

degradable

Automate degradation of a service or feature when a failure threshold has passed. Coordinates multiple instances via Redis.
Crystal
2
star
30

crystal-docker-example

Crystal
2
star
31

postgis

PostGIS extensions for the Crystal Postgres client
Crystal
2
star
32

clearwater

Front-end Ruby framework using Opal
2
star
33

clearwater-roda-example

An example app comparing Clearwater and React in a Roda app
Ruby
1
star
34

redis-cluster-operator

Crystal
1
star
35

turbolinks-vs-clearwater

Performance comparison of Turbolinks and a virtual DOM
Ruby
1
star
36

sidekiq_lucky_example

Crystal
1
star
37

mastodon_intake_debugging

Crystal
1
star
38

advent_of_code-2018

My solutions for Advent of Code 2018
Crystal
1
star
39

pg-age

Crystal
1
star
40

cloud_events

Implementation of CloudEvents in Crystal
Crystal
1
star
41

fauxrem-redis

Redis/DEV Hackathon 2022 entry
Crystal
1
star
42

bugsnag

Crystal
1
star
43

k8s_rails_example

Example Dockerized Rails app for deployment on Kubernetes
Ruby
1
star
44

redis-docs

Documentation for the jgaskins/redis Crystal shard
HTML
1
star
45

opal-google_maps

Gem for using Google Maps in a Ruby front-end app
Ruby
1
star
46

opal-pusher

Opal bindings for the Pusher JS API
Ruby
1
star
47

nats-streaming

Crystal
1
star
48

example_crystal_app

Crystal
1
star
49

jgaskins.github.io

HTML
1
star
50

server_rendered_clearwater_example

Server-rendered Clearwater example
Ruby
1
star
51

clearwater_todomvc

TodoMVC on Clearwater
Ruby
1
star
52

mprop-crystal

Crystal port of Mitchell Henke's Phoenix.LiveView Milwaukee Property Search app
CSS
1
star
53

github

GitHub API client
Crystal
1
star