Arke.QueryManager (Arke v0.9.0-rc.0)

Copy Markdown View Source

Module to manage the CRUD operations to create the below elements and also manage the query to get the elements from db.

arke

operators

  • eq => equal => =
  • contains => contains a value (Case sensitive) => LIKE %word%
  • icontains => contains a value (Not case sensitive) => LIKE %word%
  • startswith => starts with the given value (Case sensitive) => LIKE %word
  • istartswith => starts with the given value (Not case sensitive) => LIKE %word
  • endswith => ends with the given value (Case sensitive) => LIKE word%
  • iendswith => ends with the given value (Not case sensitive) => LIKE word%
  • lte => less than or equal => <=
  • lt => less than => <
  • gt => greater than => >
  • gte => greater than or equal => >=
  • in => value is in a collection => IN

Summary

Functions

Return all the results from a query

Add an :and logic to a query

Return the count of the element returned from a query

Function to create an element

Function to delete a given unit

Filter of the query

Function to get all the element which match the given criteria

Function to get a single element identified by the opts. Use Arke.QueryManager.filter_by if more than one element is returned

Set the limit of the element to be returned from a query

Create a new topology query

Set the offset of the query

Return the first result of a query

Add an :or logic to a query

Define a criteria to order the element returned from a query

Return a string which represent the query itself

Create a new query

Return the query as a string

Runs fun inside a database transaction.

Function to update an element

Create query with specific options

Types

func_return()

@type func_return() :: {:ok, Arke.Core.Unit.t()} | Arke.Utils.ErrorGenerator.t()

operator()

@type operator() ::
  :eq
  | :contains
  | :icontains
  | :startswith
  | :istartswith
  | :endswith
  | :iendswith
  | :lte
  | :lt
  | :gt
  | :gte
  | :in
  | :isnull

Functions

all(query)

@spec all(query :: Arke.Core.Query.t()) :: [Arke.Core.Unit.t()] | []

Return all the results from a query

Parameter

and_(query, negate, filters)

@spec and_(query :: Arke.Core.Query.t(), negate :: boolean(), filters :: list()) ::
  Arke.Core.Query.t()

Add an :and logic to a query

Parameter

  • query => refer to query/1
  • negate => boolean => used to figure out whether the condition is to be denied
  • filters => refer to condition/3 | conditions/1

Example

iex> query = QueryManager.query(arke: nil, project: :arke_system)
...> query = QueryManager.and_(query, false, QueryManager.conditions(parameter__eq: "value"))

Return

%Arke.Core.Query{}

condition(parameter, operator, value, negate \\ false, path \\ [])

@spec condition(
  parameter :: Arke.Core.Arke.t() | atom(),
  negate :: boolean(),
  value :: String.t() | boolean() | nil,
  negate :: boolean(),
  path :: [Arke.Core.Arke.t()]
) :: Arke.Core.Query.BaseFilter.t()

Create a Arke.Core.Query.BaseFilter

Parameters

  • parameter => :atom | %Arke.Core.Arke{} => the parameter where to check the condition

  • operator => :atom => refer to operators
  • value => string | boolean | nil => the value the parameter and operator must check

  • negate => boolean => used to figure out whether the condition is to be denied

Example

iex> QueryManager.condition(:string, :eq, "test")

Return

%Arke.Core.Query.BaseFilter{}

conditions(opts \\ [])

@spec conditions(opts :: list()) :: [Arke.Core.Query.BaseFilter.t()]

Create a list of Arke.Core.Query.BaseFilter

Parameter

  • opts => %{map} || [keyword: value] || key1: value1, key2: value2 => the condtions used to create the BaseFilters

Example

iex>  QueryManager.conditions(parameter__eq: "test", string__contains: "t")

Return

[ %Arke.Core.Query.BaseFilter{}, ...]

count(query)

@spec count(query :: Arke.Core.Query.t()) :: integer()

Return the count of the element returned from a query

Parameter

create(project, arke, args)

@spec create(project :: atom(), arke :: Arke.Core.Arke.t(), args :: list()) ::
  func_return()

Function to create an element

Parameters

  • project => :atom => identify the Arke.Core.Project
  • arke => {arke_struct} => identify the struct of the element we want to create
  • args => [list] => list of key: value we want to assign to the {arke_struct} above

Example

iex> string = ArkeManager.get(:string, :default)
...> Arke.QueryManager.create(:default, string, [id: "name", label: "Nome"])

Returns

{:ok, %Arke.Core.Unit{}}

delete(project, unit)

@spec delete(project :: atom(), Arke.Core.Unit.t()) :: {:ok, any()}

Function to delete a given unit

Parameters

  • project => :atom => identify the Arke.Core.Project
  • unit => %{arke_struct} => the unit to delete

Example

iex> element = Arke.QueryManager.get_by(id: "name")
...> Arke.QueryManager.delete(element)

Returns

{:ok, _}

filter(query, filter)

@spec filter(query :: Arke.Core.Query.t(), filter :: Arke.Core.Query.Filter.t()) ::
  Arke.Core.Query.t()

Filter of the query

Parameters

Example

iex> query = Arke.QueryManager.Query.t
...> parameter = Arke.Boundary.ParameterManager.get(:id,:arke_system)
...> Arke.Core.Query.new_filter(parameter,:equal,"name",false)
...> Arke.Core.Query.filter(query, filter

filter(query, parameter, operator, value, negate \\ false)

@spec filter(
  query :: Arke.Core.Query.t(),
  parameter :: Arke.Core.Arke.t() | String.t() | atom(),
  operator :: operator(),
  value :: String.t() | boolean() | number(),
  negate :: boolean()
) :: Arke.Core.Query.t()

Filter of the query

Parameters

  • query => refer to query/1
  • parameter => %{arke_struct} => arke struct of the parameter
  • operator => :atom => refer to operators
  • value => string | boolean | nil => the value the parameter and operator must check

  • negate => boolean => used to figure out whether the condition is to be denied

Example

iex> query = Arke.QueryManager.query()
...> QueryManager.filter(query, Arke.Core.Query.new_filter(Arke.Boundary.ParameterManager.get(:id,:default),:equal,"name",false))

Return

%Arke.Core.Query{...}

filter_by(opts \\ [])

@spec filter_by(opts :: list()) :: [Arke.Core.Unit.t()] | []

Function to get all the element which match the given criteria

Parameters

  • opts => %{map} || [keyword: value] || key1: value1, key2: value2 => identify the element to get
  • operator => :atom => refer to operators

Example

iex> Arke.QueryManager.filter_by(id: "name")

Return

[ Arke.Core.Unit{}, ...]

get_by(opts \\ [])

@spec get_by(opts :: list()) :: Arke.Core.Unit.t() | nil

Function to get a single element identified by the opts. Use Arke.QueryManager.filter_by if more than one element is returned

Parameters

  • opts => %{map} || [keyword: value] || key1: value1, key2: value2 => identify the element to get

Example

iex> Arke.QueryManager.get_by(id: "name")

limit(query, limit)

@spec limit(query :: Arke.Core.Query.t(), limit :: integer()) :: Arke.Core.Query.t()

Set the limit of the element to be returned from a query

Parameter

  • query => refer to query/1
  • limit => int => number of element to return

Example

iex> query = QueryManager..query()
...> QueryManager.where(query, id: "name") |> QueryManager.limit(1)

link(query, unit, opts \\ [])

@spec link(Arke.Core.Query.t(), unit :: Arke.Core.Unit.t(), opts :: list()) ::
  Arke.Core.Query.t()

Create a new topology query

Parameter

  • query => refer to query/1
  • unit => %{arke_struct} => struct of the unit used as reference for the query
  • opts => [keyword: value] => KeywordList containing the link conditions
  • depth => int => max depth of the topoplogy
  • direction => :atom => :child/:parent => the direction of the link. From parent to child or viceversa
  • connection_type => string => name of the connection where to search

Example

iex> Arke.QueryManager.query(project: :public)
...> unit = QueryManager.get_by([project: :arke_system, id: "test"])
...> QueryManager.link(query, unit)

Return

%Arke.Core.Query{}

offset(query, offset)

@spec offset(query :: Arke.Core.Query.t(), offset :: integer()) :: Arke.Core.Query.t()

Set the offset of the query

Parameter

  • query => refer to query/1
  • offset => int => offset of the query

Example

iex> query = QueryManager.query()
...> QueryManager.where(query, id: "name") |> QueryManager.offset(5)

one(query)

@spec one(query :: Arke.Core.Query.t()) :: Arke.Core.Unit.t() | nil

Return the first result of a query

Parameter

or_(query, negate, filters)

@spec or_(query :: Arke.Core.Query.t(), negate :: boolean(), filters :: list()) ::
  Arke.Core.Query.t()

Add an :or logic to a query

Parameter

  • query => refer to query/1
  • negate => boolean => used to figure out whether the condition is to be denied
  • filters => refer to condition/3 | conditions/1

Example

iex> query = QueryManager.query(arke: nil, project: :arke_system)
...> query = QueryManager.or_(query, false, QueryManager.conditions(parameter__eq: "value"))

Return

%Arke.Core.Query{}

order(query, parameter, direction)

@spec order(
  query :: Arke.Core.Query.t(),
  parameter ::
    Arke.Core.Arke.t()
    | String.t()
    | atom()
    | [Arke.Core.Arke.t()]
    | [String.t()]
    | [atom()],
  direction :: atom()
) :: Arke.Core.Query.t()

Define a criteria to order the element returned from a query

Parameter

  • query => refer to query/1
  • order => int => number of element to return

Example

iex> query = QueryManager.query()
...> parameter = Arke.Boundary.ParameterManager.get(:id,:default)
...> QueryManager.order(query, parameter, :asc)

pagination(query, offset, limit)

pseudo_query(query)

@spec pseudo_query(query :: Arke.Core.Query.t()) :: Ecto.Query.t()

Return a string which represent the query itself

Parameter

Example

iex> query = QueryManager.query()
...> QueryManager.where(query, id: "name") |> QueryManager.pseudo_query

Return

#Ecto.Query<>

query(opts)

@spec query(list()) :: Arke.Core.Query.t()

Create a new query

Parameter

  • opts => %{map} || [keyword: value] || key1: value1, key2: value2 => map containing the project and the arke where to apply the query

Example

iex> Arke.QueryManager.query(project: :public)

raw(query)

@spec raw(query :: Arke.Core.Query.t()) :: String.t()

Return the query as a string

Parameter

transaction(project, fun, opts \\ [])

@spec transaction(project :: atom(), fun :: (-> any()), opts :: keyword()) ::
  {:ok, any()} | {:error, any()}

Runs fun inside a database transaction.

Every Arke.QueryManager write performed inside fun joins the transaction; their after_commit hooks defer to the outermost commit. Returning {:error, reason} from fun rolls everything back and returns {:error, reason}; {:ok, value} commits and returns {:ok, value}.

Options

  • timeout: => transaction timeout in ms, passed to the adapter

Example

iex> Arke.QueryManager.transaction(:my_project, fn ->
...>   with {:ok, a} <- QueryManager.create(...),
...>        {:ok, b} <- QueryManager.update(...),
...>        do: {:ok, {a, b}}
...> end, timeout: 600_000)

update(current_unit, args)

@spec update(Arke.Core.Unit.t(), args :: list()) :: func_return()

Function to update an element

Parameters

  • project => :atom => identify the Arke.Core.Project
  • unit => %{arke_struct} => unit to update
  • args => [list] => list of key: value to update

Example

iex> name = QueryManager.get_by(id: "name")
...> QueryManager.update(:default, name, [max_length: 20])

Returns

{:ok,  %Arke.Core.Unit{} }
{:error, [msg]}

update_key(current_unit, args)

where(query, opts \\ [])

@spec where(query :: Arke.Core.Query.t(), opts :: list()) :: Arke.Core.Query.t()

Create query with specific options

Parameters

  • query => refer to query/1
  • opts => %{map} || [keyword: value] || key1: value1, key2: value2 => keyword list containing the filter to apply

Example

iex> query = Arke.QueryManager.query()
...> QueryManager.where(query, [id__contains: "me", id__contains: "n"])

Return

%Arke.Core.Query{ %Arke.Core.Query.Filter{ ... base_filters: %Arke.Core.Query.BaseFilter{ ... }}}