View Source ArangoXEcto.Migration (ArangoX Ecto v1.3.1)

Defines Ecto Migrations for ArangoDB

note

NOTE

ArangoXEcto dynamically creates collections for you by default. Depending on your project architecture you may decide to use static migrations instead in which case this module will be useful.

Migrations must use this module, otherwise migrations will not work. To do this, replace use Ecto.Migration with use ArangoXEcto.Migration.

Since ArangoDB is schemaless, no fields need to be provided, only the collection name. First create a collection struct using the collection/3 function. Then pass the collection struct to the create/1 function. To create indexes it is a similar process using the index/3 function.

Order matters!! Make sure you create collections before indexes and views, and analyzers before views if they are used. In general this is a good order to follow:

Analyzers > Collections > Indexes > Views

To drop the collection on a migration down, do the same as creation except use the drop/1 function instead of the create/1 function. Indexes are automatically removed when the collection is removed and cannot be deleted using the drop/1 function.

example

Example

defmodule MyProject.Repo.Migrations.CreateUsers do
  use ArangoXEcto.Migration

  def up do
    create(MyProject.Analyzers)

    create(collection(:users))

    create(index("users", [:email]))

    create(MyProject.UsersView)
  end

  def down do
    drop(collection(:users))
  end
end

Link to this section Summary

Link to this section Functions

Link to this function

collection(collection_name, type \\ :document, opts \\ [])

View Source

Creates a collection struct

Used to in functions that perform actions on the database.

Accepts a collection type parameter that can either be :document or :edge, otherwise it will raise an error. The default option is :document.

options

Options

Accepts an options parameter as the third argument. For available keys please refer to the ArangoDB API doc.

examples

Examples

iex> collection("users")
%ArangoXEcto.Migration.Collection{name: "users", type: 2)

iex> collection("users", :edge)
%ArangoXEcto.Migration.Collection{name: "users", type: 3)

iex> collection("users", :document, keyOptions: %{type: :uuid})
%ArangoXEcto.Migration.Collection{name: "users", type: 2, keyOptions: %{type: :uuid})
Link to this function

create(object_to_create, opts \\ [])

View Source
@spec create(
  ArangoXEcto.Migration.Collection.t()
  | ArangoXEcto.Migration.Index.t()
  | atom(),
  Keyword.t()
) :: :ok | {:error, binary()}

Creates an object

Will create the passed object, one of a collection, an index, analyzers or a view.

Since view schemas and analyzers are just module definitions we can use them directly here. Therefore the module is just passed directly.

There is the ability to pass additional options as the second argument

options

Options

  • :repo - The repo or connection to use for the migration create action
  • :prefix - The prefix to use for tenant creation

examples

Examples

Create a collection

iex> create(collection("users"))
:ok

Create an index

iex> create(index("users", [:email])
:ok

Create a view

iex> create(MyProject.UsersView)
:ok
Link to this function

drop(collection, conn \\ nil)

View Source

Deletes an object

Will delete an object passed, can only be a collection, indexes cannot be deleted here. This is because indexes have a randomly generated id and this needs to be known to delete the index, for now this is outside the scope of this project.

example

Example

iex> drop(collection("users"))
:ok
Link to this function

edge(edge_name, opts \\ [])

View Source

Creates an edge collection struct

Same as passing :edge as the second parameter to collection/3.

Link to this function

index(collection_name, fields, opts \\ [])

View Source

Creates an index struct

Default index type is a hash. To change this pass the :type option in options.

options

Options

Options only apply to the creation of indexes and has no effect when using the drop/1 function.

  • :type - The type of index to create
    • Accepts: :fulltext, :geo, :hash, :persistent, :skiplist or :ttl
  • :unique - If the index should be unique, defaults to false (hash, persistent & skiplist only)
  • :sparse - If index should be spares, defaults to false (hash, persistent & skiplist only)
  • :deduplication - If duplication of array values should be turned off, defaults to true (hash & skiplist only)
  • :minLength - Minimum character length of words to index (fulltext only)
  • :geoJson - If a geo-spatial index on a location is constructed and geoJson is true, then the order within the array is longitude followed by latitude (geo only)
  • :expireAfter - Time in seconds after a document's creation it should count as expired (ttl only)
  • :name - The name of the index (usefull for phoenix constraints)

examples

Examples

Create index on email field

iex> index("users", [:email])
%ArangoXEcto.Migration.Index{collection_name: "users", fields: [:email]}

Create dual index on email and ph_number fields

iex> index("users", [:email, :ph_number])
%ArangoXEcto.Migration.Index{collection_name: "users", fields: [:email, :ph_number]}

Create unique email index

iex> index("users", [:email], unique: true)
%ArangoXEcto.Migration.Index{collection_name: "users", fields: [:email], unique: true}