Giter Site home page Giter Site logo

cypherex's Introduction

CypherEx

A proof of concept library inspired by Ecto that adds a object mapper to the Cypher query language used by graph databases such as Neo4J. While the feature set is extremely limited, it mainly attempts to add some kind of structure between nodes and their relationships which can get out of hand in complex graph schemas.

This is absolutely not for production usage in the slightest.

Installation

If available in Hex, the package can be installed by adding cypher_ex to your list of dependencies in mix.exs:

def deps do
  [
    {:cypher_ex, "~> 0.1.0"},
    // {:cypher_ex, git: "git://github.com/nathancyam/cypher_ex.ex"}
  ]
end

Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/cypher_ex.

Usage

At the heart of a graph database are nodes that typically represent entities which are related to each other via edges. A node is given its own module and uses the CypherEx.Schema macro.

defmodule CypherEx.Examples.Organisation do
  use CypherEx.Schema

  schema "Organisation", labels: ["Organisation"] do
    property(:id, :string)
    property(:email, :string)
    property(:type, :string)
    property(:visible, :boolean)
  end

  relations do
    outgoing(:governs, __MODULE__)
    incoming(:child_of, __MODULE__)
  end
end

defmodule CypherEx.Examples.Employee do
  use CypherEx.Schema

  schema "Employee", labels: ["Employee"] do
    property(:id, :string)
    property(:email, :string)
    property(:role, :string)
  end

  relations do
    outgoing(:works_at, CypherEx.Examples.Organisation)
  end
end

This declares the expected properties as well as relations between each node. With this constraint, we can use the CypherEx.Query functions to generate Cypher queries that ensure that these rules from the schema are valid.

defmodule CypherEx.Examples.SimpleQuery do
  import CypherEx.Query

  alias CypherEx.Examples.{Employee, Organisation}

  def test() do
    match(
      node(:org, Organisation, id: "Some ID")
      |> relation([:governs, :child_of], id: "dsf")
      |> node(:org_1, Organisation)
      |> relation([:works_at], id: "test")
      |> node(:worker, Employee)
    )
    |> where(worker.role == "Developer")
    |> return([:org_1, :worker])
  end
end

If the relationship between nodes is invalid, the query will fail at runtime and inform you that aspects of the query are incorrect. Piping this query into to_string() will return the Cypher string back in case inspection is required.

Todo

  • Add properties to relationships.
  • Add validations on relationships properties.
  • Add create function to generate nodes.
  • Add proper variable binding support.

cypherex's People

Contributors

nathancyam avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.