Skip to content

betrybe/neo4_ecto

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Neo4Ecto

Neo4Ecto is an Ecto adapter that sits on top of Bolt.Sips driver.

It allows you to deal with a Neo4j database through Ecto.

Check out the (documentation)

Installation

Add the lib to your mix.exs

def deps do
  [
    {:neo4_ecto, "~> 0.0.2"}
  ]
end

run: mix dep.get

setup your database config:

# config/dev.exs

config :my_app, ecto_repos: [MyApp.Repo]

config :my_app, MyApp.Repo,
  hostname: "localhost",
  basic_auth: [username: "neo4j", password: "neo4j"],
  pool_size: 5

# lib/my_app/repo.ex
defmodule MyApp.Repo do
  use Ecto.Repo, otp_app: :my_app, adapter: Ecto.Adapters.Neo4Ecto
end

Usage

It's currently available the following Ecto modules: [Schema, Changeset, Repo]

For example:

# lib/my_app/accounts/user.ex
defmodule Accounts.User do
  use Ecto.Schema
  import Ecto.Changeset

  schema "user" do
    field :name, :string
  end

  def changeset(user, attrs) do
    user
    |> cast(attrs, [:name])
  end
end

# lib/my_app/accounts.ex
defmodule Accounts do
  alias Accounts.User
  alias MyApp.Repo

  def create_user(attrs) do
    %User{}
    |> User.changeset(attrs)
    |> Repo.insert()
  end
end

# lib/my_app/app.ex
defmodule Example.App do
  alias Example.Repo

  def get_user_by_id do
    Repo.query("MATCH (u:User {id: $user_id}) RETURN u;", %{user_id: 1})
  end

  def get_user_followers! do
    Repo.query!("MATCH (u:User)-[:FOLLOWS]->(:User {id: $user_id}) RETURN u;", %{user_id: 1})
  end
end

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

Running containerized tests with Earthly

It is also possible to run tests under a containerized environment using earthly:

$ earthly -P +setup-code-check
$ earthly -P +test-neo4ecto

You can also use this to interactively debug any tests with diferent images of Neo4j and Elixir.

$ earthly -P -i --build-arg ELIXIR_BASE=1.12.0-rc.1-erlang-24.0-alpine-3.13.3 +setup-code-check
$ earthly -P -i --build-arg ELIXIR_BASE=1.8.2-erlang-20.3.8.26-alpine-3.11.6 --build-arg NEO4J=4.1 +test-neo4ecto

Copyright and License

The source code is under the Apache 2 License.

Copyright (c) 2021 Trybe

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.