View Source Glific.Repo (Glific v5.1.6)

A repository that maps to an underlying data store, controlled by the Postgres adapter.

We add a few functions to make our life easier with a few helper functions that ecto does not provide.

Link to this section Summary

Functions

Implement permission support via groups. This is the basic wrapper, it uses a context specific permission wrapper to add the actual clauses

Callback implementation for Ecto.Repo.all/2.

Callback implementation for Ecto.Repo.checked_out?/0.

Callback implementation for Ecto.Repo.checkout/2.

Callback implementation for Ecto.Repo.config/0.

We use this function also in most list_OBJECT api's, where we process the the filter. Centralizing this code at the top level, to make things cleaner

Callback implementation for Ecto.Repo.delete/2.

Callback implementation for Ecto.Repo.delete!/2.

In Join tables we rarely use the table id. We always know the object ids and hence more convenient to delete an entry via its object ids.

A convenience function for SQL-based repositories that forces all connections in the pool to disconnect within the given interval.

A convenience function for SQL-based repositories that executes an EXPLAIN statement or similar depending on the adapter to obtain statistics for the given query.

Glific version of get, which returns a tuple with an :ok | :error as the first element

Glific version of get_by, which returns a tuple with an :ok | :error as the first element

Add all the common filters here, rather than in each file

Callback implementation for Ecto.Repo.get/3.

Callback implementation for Ecto.Repo.get!/3.

Callback implementation for Ecto.Repo.get_dynamic_repo/0.

Callback implementation for Ecto.Repo.in_transaction?/0.

Callback implementation for Ecto.Repo.insert/2.

Callback implementation for Ecto.Repo.insert!/2.

Get map of field (typically label) to ids for easier lookup for various system objects - language, tag

Extracts the limit offset field, and adds to query

We use this function in most list_OBJECT api's, where we process the opts and the filter. Centralizing this code at the top level, to make things cleaner

This function builds the query, and is used in places where we want to layer permission on top of the query

Callback implementation for Ecto.Repo.load/2.

Callback implementation for Ecto.Repo.one/2.

Callback implementation for Ecto.Repo.one!/2.

A function which handles the order clause for a data type that has a 'name/body/label' in its schema (which is true for a fair number of Glific's data types)

An empty function for objects that ignore the opts

A convenience function for SQL-based repositories that executes the given query.

A convenience function for SQL-based repositories that executes the given query.

A convenience function for SQL-based repositories that executes the given multi-result query.

A convenience function for SQL-based repositories that executes the given multi-result query.

Callback implementation for Ecto.Repo.reload/2.

Callback implementation for Ecto.Repo.rollback/1.

Can we skip checking permissions for this user. This eliminates a DB call in many a case

Callback implementation for Ecto.Repo.start_link/1.

Callback implementation for Ecto.Repo.stop/1.

Callback implementation for Ecto.Repo.stream/2.

A convenience function for SQL-based repositories that translates the given query to SQL.

Callback implementation for Ecto.Repo.update/2.

Callback implementation for Ecto.Repo.update!/2.

Link to this section Functions

Link to this function

add_permission(query, permission_fn, skip_permission \\ false)

View Source
@spec add_permission(
  Ecto.Query.t(),
  (Ecto.Query.t(), Glific.Users.User.t() -> Ecto.Query.t()),
  boolean()
) :: Ecto.Query.t()

Implement permission support via groups. This is the basic wrapper, it uses a context specific permission wrapper to add the actual clauses

Link to this function

aggregate(queryable, aggregate, opts \\ [])

View Source

Callback implementation for Ecto.Repo.aggregate/3.

Link to this function

aggregate(queryable, aggregate, field, opts)

View Source

Callback implementation for Ecto.Repo.aggregate/4.

Link to this function

all(queryable, opts \\ [])

View Source

Callback implementation for Ecto.Repo.all/2.

Callback implementation for Ecto.Repo.checked_out?/0.

Link to this function

checkout(fun, opts \\ [])

View Source

Callback implementation for Ecto.Repo.checkout/2.

Callback implementation for Ecto.Repo.config/0.

Link to this function

count_filter(args \\ %{}, object, filter_with_fn, repo_opts \\ [])

View Source
@spec count_filter(
  map(),
  atom(),
  (Ecto.Queryable.t(), %{optional(atom()) => any()} -> Ecto.Queryable.t()),
  Keyword.t()
) :: integer()

We use this function also in most list_OBJECT api's, where we process the the filter. Centralizing this code at the top level, to make things cleaner

Link to this function

delete(struct, opts \\ [])

View Source

Callback implementation for Ecto.Repo.delete/2.

Link to this function

delete!(struct, opts \\ [])

View Source

Callback implementation for Ecto.Repo.delete!/2.

Link to this function

delete_all(queryable, opts \\ [])

View Source

Callback implementation for Ecto.Repo.delete_all/2.

Link to this function

delete_relationships_by_ids(object, fields)

View Source
@spec delete_relationships_by_ids(
  atom(),
  {{atom(), integer()}, {atom(), [integer()]}}
) :: {integer(), nil | [term()]}

In Join tables we rarely use the table id. We always know the object ids and hence more convenient to delete an entry via its object ids.

Link to this function

disconnect_all(interval, opts \\ [])

View Source

A convenience function for SQL-based repositories that forces all connections in the pool to disconnect within the given interval.

See Ecto.Adapters.SQL.disconnect_all/3 for more information.

Link to this function

exists?(queryable, opts \\ [])

View Source

Callback implementation for Ecto.Repo.exists?/2.

Link to this function

explain(operation, queryable, opts \\ [])

View Source

A convenience function for SQL-based repositories that executes an EXPLAIN statement or similar depending on the adapter to obtain statistics for the given query.

See Ecto.Adapters.SQL.explain/4 for more information.

Link to this function

fetch(queryable, id, opts \\ [])

View Source
@spec fetch(Ecto.Queryable.t(), term(), Keyword.t()) ::
  {:ok, Ecto.Schema.t()} | {:error, [String.t()]}

Glific version of get, which returns a tuple with an :ok | :error as the first element

Link to this function

fetch_by(queryable, clauses, opts \\ [])

View Source
@spec fetch_by(Ecto.Queryable.t(), Keyword.t() | map(), Keyword.t()) ::
  {:ok, Ecto.Schema.t()} | {:error, [String.t()]}

Glific version of get_by, which returns a tuple with an :ok | :error as the first element

Link to this function

filter_with(query, filter)

View Source
@spec filter_with(Ecto.Queryable.t(), %{optional(atom()) => any()}) ::
  Ecto.Queryable.t()

Add all the common filters here, rather than in each file

Link to this function

get(queryable, id, opts \\ [])

View Source

Callback implementation for Ecto.Repo.get/3.

Link to this function

get!(queryable, id, opts \\ [])

View Source

Callback implementation for Ecto.Repo.get!/3.

Link to this function

get_by(queryable, clauses, opts \\ [])

View Source

Callback implementation for Ecto.Repo.get_by/3.

Link to this function

get_by!(queryable, clauses, opts \\ [])

View Source

Callback implementation for Ecto.Repo.get_by!/3.

Callback implementation for Ecto.Repo.get_dynamic_repo/0.

Callback implementation for Ecto.Repo.in_transaction?/0.

Link to this function

insert(struct, opts \\ [])

View Source

Callback implementation for Ecto.Repo.insert/2.

Link to this function

insert!(struct, opts \\ [])

View Source

Callback implementation for Ecto.Repo.insert!/2.

Link to this function

insert_all(schema_or_source, entries, opts \\ [])

View Source

Callback implementation for Ecto.Repo.insert_all/3.

Link to this function

insert_or_update(changeset, opts \\ [])

View Source

Callback implementation for Ecto.Repo.insert_or_update/2.

Link to this function

insert_or_update!(changeset, opts \\ [])

View Source

Callback implementation for Ecto.Repo.insert_or_update!/2.

Link to this function

label_id_map(queryable, values, organization_id, field \\ :label)

View Source
@spec label_id_map(Ecto.Queryable.t(), [String.t()], non_neg_integer(), atom()) :: %{
  required(String.t()) => integer()
}

Get map of field (typically label) to ids for easier lookup for various system objects - language, tag

Link to this function

limit_offset(query, opts)

View Source
@spec limit_offset(Ecto.Queryable.t(), map()) :: Ecto.Queryable.t()

Extracts the limit offset field, and adds to query

Link to this function

list_filter(args \\ %{}, object, opts_with_fn, filter_with_fn, repo_opts \\ [])

View Source
@spec list_filter(
  map(),
  atom(),
  (Ecto.Queryable.t(), %{optional(atom()) => any()} -> Ecto.Queryable.t()),
  (Ecto.Queryable.t(), %{optional(atom()) => any()} -> Ecto.Queryable.t()),
  Keyword.t()
) :: [any()]

We use this function in most list_OBJECT api's, where we process the opts and the filter. Centralizing this code at the top level, to make things cleaner

Link to this function

list_filter_query(args \\ %{}, object, opts_with_fn, filter_with_fn)

View Source
@spec list_filter_query(
  map(),
  atom(),
  (Ecto.Queryable.t(), %{optional(atom()) => any()} -> Ecto.Queryable.t())
  | nil,
  (Ecto.Queryable.t(), %{optional(atom()) => any()} -> Ecto.Queryable.t())
) :: Ecto.Queryable.t()

This function builds the query, and is used in places where we want to layer permission on top of the query

Link to this function

load(schema_or_types, data)

View Source

Callback implementation for Ecto.Repo.load/2.

Link to this function

one(queryable, opts \\ [])

View Source

Callback implementation for Ecto.Repo.one/2.

Link to this function

one!(queryable, opts \\ [])

View Source

Callback implementation for Ecto.Repo.one!/2.

Link to this function

opts_with_field(query, opts, field)

View Source
@spec opts_with_field(
  Ecto.Queryable.t(),
  map(),
  :name | :body | :label | :inserted_at | :id
) :: Ecto.Queryable.t()

A function which handles the order clause for a data type that has a 'name/body/label' in its schema (which is true for a fair number of Glific's data types)

Link to this function

opts_with_nil(query, opts)

View Source
@spec opts_with_nil(Ecto.Queryable.t(), any()) :: Ecto.Queryable.t()

An empty function for objects that ignore the opts

Link to this function

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

View Source

Callback implementation for Ecto.Repo.preload/3.

Link to this function

put_dynamic_repo(dynamic)

View Source

Callback implementation for Ecto.Repo.put_dynamic_repo/1.

Link to this function

query(sql, params \\ [], opts \\ [])

View Source

A convenience function for SQL-based repositories that executes the given query.

See Ecto.Adapters.SQL.query/4 for more information.

Link to this function

query!(sql, params \\ [], opts \\ [])

View Source

A convenience function for SQL-based repositories that executes the given query.

See Ecto.Adapters.SQL.query!/4 for more information.

Link to this function

query_many(sql, params \\ [], opts \\ [])

View Source

A convenience function for SQL-based repositories that executes the given multi-result query.

See Ecto.Adapters.SQL.query_many/4 for more information.

Link to this function

query_many!(sql, params \\ [], opts \\ [])

View Source

A convenience function for SQL-based repositories that executes the given multi-result query.

See Ecto.Adapters.SQL.query_many!/4 for more information.

Link to this function

reload(queryable, opts \\ [])

View Source

Callback implementation for Ecto.Repo.reload/2.

Link to this function

reload!(queryable, opts \\ [])

View Source

Callback implementation for Ecto.Repo.reload!/2.

@spec rollback(term()) :: no_return()

Callback implementation for Ecto.Repo.rollback/1.

Link to this function

skip_permission?(user \\ get_current_user())

View Source
@spec skip_permission?(Glific.Users.User.t() | nil) :: boolean()

Can we skip checking permissions for this user. This eliminates a DB call in many a case

Callback implementation for Ecto.Repo.start_link/1.

Callback implementation for Ecto.Repo.stop/1.

Link to this function

stream(queryable, opts \\ [])

View Source

Callback implementation for Ecto.Repo.stream/2.

Link to this function

to_sql(operation, queryable)

View Source

A convenience function for SQL-based repositories that translates the given query to SQL.

See Ecto.Adapters.SQL.to_sql/3 for more information.

Link to this function

transaction(fun_or_multi, opts \\ [])

View Source

Callback implementation for Ecto.Repo.transaction/2.

Link to this function

update(struct, opts \\ [])

View Source

Callback implementation for Ecto.Repo.update/2.

Link to this function

update!(struct, opts \\ [])

View Source

Callback implementation for Ecto.Repo.update!/2.

Link to this function

update_all(queryable, updates, opts \\ [])

View Source

Callback implementation for Ecto.Repo.update_all/3.