EctoNeo4j v0.6.2 Ecto.Adapters.Neo4j View Source

Link to this section Summary

Functions

Called to autogenerate a value for id/embed_id/binary_id.

Execute given query in batch. There is two type of batches

Same as batch_query!/4 but raises in case of error.

Checks out a connection for the duration of the given function.

Deletes a single struct with the given filters.

Returns the dumpers for a given type.

Ensure all applications necessary to run the adapter are started.

Returns true if the given process is inside a transaction.

Initializes the adapter supervision tree by returning the children and adapter metadata.

Insert data into database and create relationship if necessary.

Same as insert/3 but raises in case of error.

Returns the loaders for a given type.

Commands invoked to prepare a query for all, update_all and delete_all.

Execute given query on the databsse. This will return a Bolt.Sips.Response.

Same as query/3 but raises in case of error.

Rolls back the current transaction.

Drops the storage given by options.

Creates the storage given by options.

Runs the given function inside a transaction.

Updates a changeset using its primary key.

Updates a single struct with the given filters.

Same as update3 but raises in case of error

Link to this section Functions

Link to this function

autogenerate(field_type) View Source

Called to autogenerate a value for id/embed_id/binary_id.

Returns the autogenerated value, or nil if it must be autogenerated inside the storage or raise if not supported.

Callback implementation for Ecto.Adapter.Schema.autogenerate/1.

Link to this function

batch_query(cql, params \\ %{}, batch_type \\ :basic, opts \\ []) View Source

Execute given query in batch. There is two type of batches:

  • :basic will use LIMIT to loop until every node is touched
  • :with_skip will use SKIP and LIMIT until every node is touched

In order to work, the query must contains:

  • for :basic, LIMIT {limit}
  • for :with_skip, SKIP {skip} LIMIT {limit}
  • in any case: RETURN COUNT(my_nodes) AS nb_touched_nodes with my_nodes being the nodes you're working on

The LIMIT is set by default to 10_000, but you can set your aown value via the option :chunk_size

Example

# :basic example
iex> cql = "
...> MATCH
...>     (n:Test)
...> WITH
...>   n AS n
...> LIMIT
...>   {limit}
...> DETACH DELETE n
...> RETURN
...>   COUNT(n) AS nb_touched_nodes
...> "
iex> Ecto.Adapters.Neo4j.batch_query(cql)
{:ok, []}
# :with_skip example
iex> cql = "
...> MATCH
...>     (n:Test)
...> WITH
...>   n AS n
...> ORDER BY
...>   n.nodeId
...> SKIP
...>   {skip}
...> LIMIT
...>   {limit}
...> SET
...>   n.value = {new_value}
...> RETURN
...>   COUNT(n) AS nb_touched_nodes
...> "
iex> Ecto.Adapters.Neo4j.batch_query(cql, %{new_value: 5}, :with_skip)
{:ok, []}
# :basic example with `chunk_size` option specifiedd
iex> cql = "
...> MATCH
...>     (n:Test)
...> WITH
...>   n AS n
...> LIMIT
...>   {limit}
...> DETACH DELETE n
...> RETURN
...>   COUNT(n) AS nb_touched_nodes
...> "
iex> Ecto.Adapters.Neo4j.batch_query(cql, %{}, :basic, chunk_size: 20_000)
{:ok, []}
Link to this function

batch_query!(cql, params \\ %{}, batch_type \\ :basic, opts \\ []) View Source

Same as batch_query!/4 but raises in case of error.

Link to this function

checkout(adapter_meta, opts, fun) View Source

Checks out a connection for the duration of the given function.

In case the adapter provides a pool, this guarantees all of the code inside the given fun runs against the same connection, which might improve performance by for instance allowing multiple related calls to the datastore to share cache information:

Repo.checkout(fn ->
  for _ <- 100 do
    Repo.insert!(%Post{})
  end
end)

If the adapter does not provide a pool, just calling the passed function and returning its result are enough.

If the adapter provides a pool, it is supposed to "check out" one of the pool connections for the duration of the function call. Which connection is checked out is not passed to the calling function, so it should be done using a stateful method like using the current process' dictionary, process tracking, or some kind of other lookup method. Make sure that this stored connection is then used in the other callbacks implementations, such as Ecto.Adapter.Queryable and Ecto.Adapter.Schema.

Callback implementation for Ecto.Adapter.checkout/3.

Link to this function

delete(adapter_meta, schema_meta, filters, options) View Source

Deletes a single struct with the given filters.

While filters can be any record column, it is expected that at least the primary key (or any other key that uniquely identifies an existing record) be given as a filter. Therefore, in case there is no record matching the given filters, {:error, :stale} is returned.

Callback implementation for Ecto.Adapter.Schema.delete/4.

Returns the dumpers for a given type.

It receives the primitive type and the Ecto type (which may be primitive as well). It returns a list of dumpers with the given type usually at the beginning.

This allows developers to properly translate values coming from the Ecto into adapter ones. For example, if the database does not support booleans but instead returns 0 and 1 for them, you could add:

def dumpers(:boolean, type), do: [type, &bool_encode/1]
def dumpers(_primitive, type), do: [type]

defp bool_encode(false), do: {:ok, 0}
defp bool_encode(true), do: {:ok, 1}

All adapters are required to implement a clause for :binary_id types, since they are adapter specific. If your adapter does not provide binary ids, you may simply use Ecto.UUID:

def dumpers(:binary_id, type), do: [type, Ecto.UUID]
def dumpers(_primitive, type), do: [type]

Callback implementation for Ecto.Adapter.dumpers/2.

Link to this function

ensure_all_started(config, type) View Source

Ensure all applications necessary to run the adapter are started.

Callback implementation for Ecto.Adapter.ensure_all_started/2.

Link to this function

execute(repo, query_meta, query_cache, sources, preprocess, opts \\ []) View Source

See Ecto.Adapters.Neo4j.Behaviour.Queryable.execute/6.

Link to this function

execute_ddl(repo, ddl, opts) View Source

See Ecto.Adapters.Neo4j.Storage.Migrator.execute_ddl/3.

Link to this function

in_transaction?(adapter_meta) View Source

Returns true if the given process is inside a transaction.

Callback implementation for Ecto.Adapter.Transaction.in_transaction?/1.

Initializes the adapter supervision tree by returning the children and adapter metadata.

Callback implementation for Ecto.Adapter.init/1.

Insert data into database and create relationship if necessary.

Link to this function

insert(adapter_meta, schema_meta, fields, on_conflict, returning, options) View Source

Inserts a single new struct in the data store.

Autogenerate

The primary key will be automatically included in returning if the field has type :id or :binary_id and no value was set by the developer or none was autogenerated by the adapter.

Callback implementation for Ecto.Adapter.Schema.insert/6.

Link to this function

insert!(repo, data, opts \\ []) View Source

Same as insert/3 but raises in case of error.

Link to this function

insert_all(adapter_meta, schema_meta, header, entries, on_conflict, returning, options) View Source

Inserts multiple entries into the data store.

In case an Ecto.Query given as any of the field values by the user, it will be sent to the adapter as a tuple with in the shape of {query, params}.

Callback implementation for Ecto.Adapter.Schema.insert_all/7.

Returns the loaders for a given type.

It receives the primitive type and the Ecto type (which may be primitive as well). It returns a list of loaders with the given type usually at the end.

This allows developers to properly translate values coming from the adapters into Ecto ones. For example, if the database does not support booleans but instead returns 0 and 1 for them, you could add:

def loaders(:boolean, type), do: [&bool_decode/1, type]
def loaders(_primitive, type), do: [type]

defp bool_decode(0), do: {:ok, false}
defp bool_decode(1), do: {:ok, true}

All adapters are required to implement a clause for :binary_id types, since they are adapter specific. If your adapter does not provide binary ids, you may simply use Ecto.UUID:

def loaders(:binary_id, type), do: [Ecto.UUID, type]
def loaders(_primitive, type), do: [type]

Callback implementation for Ecto.Adapter.loaders/2.

Link to this function

lock_for_migrations(repo, query, opts, fun) View Source

See Ecto.Adapters.Neo4j.Storage.Migrator.lock_for_migrations/4.

Link to this function

preload(struct_or_structs_or_nil, preloads, opts \\ []) View Source

See Ecto.Adapters.Neo4j.Behaviour.Repo.preload/3.

Link to this function

prepare(operation, query) View Source

Commands invoked to prepare a query for all, update_all and delete_all.

The returned result is given to execute/6.

Callback implementation for Ecto.Adapter.Queryable.prepare/2.

Link to this function

query(cql, params \\ %{}, opts \\ []) View Source

Execute given query on the databsse. This will return a Bolt.Sips.Response.

Example

iex> cql = "RETURN {num} AS n"
iex> params = %{num: 5}
...> {:ok,
...>   %Bolt.Sips.Response{
...>     bookmark: _,
...>     fields: ["n"],
...>     notifications: [],
...>     plan: nil,
...>     profile: nil,
...>     records: [[5]],
...>     results: [%{"n" => 5}],
...>     stats: [],
...>     type: "r"
...>   }} = Ecto.Adapters.Neo4j.query(cql, params)
iex> :ok
:ok
Link to this function

query!(cql, params \\ %{}, opts \\ []) View Source

Same as query/3 but raises in case of error.

Link to this function

rollback(adapter_meta, opts) View Source

Rolls back the current transaction.

The transaction will return the value given as {:error, value}.

See Ecto.Repo.rollback/1.

Callback implementation for Ecto.Adapter.Transaction.rollback/2.

Drops the storage given by options.

Returns :ok if it was dropped successfully.

Returns {:error, :already_down} if the storage has already been dropped or {:error, term} in case anything else goes wrong.

Examples

storage_down(username: postgres,
             database: 'ecto_test',
             hostname: 'localhost')

Callback implementation for Ecto.Adapter.Storage.storage_down/1.

Creates the storage given by options.

Returns :ok if it was created successfully.

Returns {:error, :already_up} if the storage has already been created or {:error, term} in case anything else goes wrong.

Examples

storage_up(username: postgres,
           database: 'ecto_test',
           hostname: 'localhost')

Callback implementation for Ecto.Adapter.Storage.storage_up/1.

Link to this function

stream(adapter_meta, query_meta, query_cache, params, opts \\ []) View Source

Streams a previously prepared query.

It returns a stream of values.

The adapter_meta field is a map containing some of the fields found in the Ecto.Query struct.

Callback implementation for Ecto.Adapter.Queryable.stream/5.

Link to this function

supports_ddl_transaction?() View Source

Link to this function

transaction(adapter_meta, opts, fun_or_multi) View Source

Runs the given function inside a transaction.

Returns {:ok, value} if the transaction was successful where value is the value return by the function or {:error, value} if the transaction was rolled back where value is the value given to rollback/1.

Callback implementation for Ecto.Adapter.Transaction.transaction/3.

Link to this function

update(changeset, repo, opts \\ []) View Source
update(Ecto.Changeset.t(), module(), Keyword.t()) ::
  {:ok, Ecto.Schema.t()} | {:error, any()}

Updates a changeset using its primary key.

This alternative to Ecto.Repo.update takes care of relationships, therefore they can be added / deleted.

Use put_assoc to update relationships.

Example

# Remove all relationship of a kind MyRepo.get!(User, "ec1741ba-28f2-47fc-8a96-a3c5e24c42da") |> Ecto.Adapters.Neo4j.preload(:wrote_post) |> Ecto.Changeset.change() |> put_assoc(:wrote_post, []) |> Ecto.Adapters.Neo4j.update()

# Add a new relationship new_post = MyRepo.insert(%Post{title: "New post"})

user = MyRepo.get!(User, "ec1741ba-28f2-47fc-8a96-a3c5e24c42da") |> Ecto.Adapters.Neo4j.preload(:wrote_post)

user |> Ecto.Changeset.change() |> put_assoc(:wrote_post, user.wrote_post ++ [new_post]) |> Ecto.Adapters.Neo4j.update()

Link to this function

update(adapter_meta, schema_meta, fields, filters, returning, options) View Source

Updates a single struct with the given filters.

While filters can be any record column, it is expected that at least the primary key (or any other key that uniquely identifies an existing record) be given as a filter. Therefore, in case there is no record matching the given filters, {:error, :stale} is returned.

Callback implementation for Ecto.Adapter.Schema.update/6.

Link to this function

update!(changeset, repo, opts) View Source

Same as update3 but raises in case of error