View Source EctoSessions behaviour (Ecto Sessions v0.3.0)

This lib implements a set of methods to help you handle the storage and access to database-backend sessions with ecto.

In your application, use EctoSessions:

defmodule MyApp.Sessions do
  use EctoSessions,
    repo: MyApp.Repo,
    prefix: nil,
    table_name: "sessions",
    extra_fields: [field: [:user_id, :string]]
end

Parameters:

  • repo: your ecto repo module, required. Ex: MyApp.Repo.
  • prefix: ecto prefix, optional, default to nil.
  • table_name: The table name for these sessions. Create a module using EctoSessions for each table, ex: Sessions, ApiKeys, etc.
  • extra_fields: Extra, custom, high-level, fields (columns) for the session schema.

See EctoSessions.Migrations for instructions on how to migrate your database.

Link to this section Summary

Callbacks

Count the sessions matching the provided filters.

Creates a session. attrs is a map that contains EctoSessions.Session attributes, where the keys are atoms.

Same as create_session/1 but using Ecto.Repo.insert!.

Deletes expired sessions.

Deletes the session using Repo.delete.

Deletes the session using Repo.delete!.

Filters a session query.

Filters a session query by the given argument.

Retrieves a session from the database.

Retrieves a session from the database using Repo.one!

Retrieves a query to the sessions.

Retrieve sessions matching the provided filters.

Retrieve valid sessions matching the provided filters.

Given a session, ensure expires_at is updated according to the EctoSessions.Config.

Updates a session using Repo.update!.

Link to this section Callbacks

@callback count(Ecto.Changeset.t()) :: Ecto.Schema.t()

Count the sessions matching the provided filters.

@callback create_session(attrs :: map()) :: Ecto.Schema.t()

Creates a session. attrs is a map that contains EctoSessions.Session attributes, where the keys are atoms.

Uses Ecto.Repo.insert

examples

Examples

iex> create_session(%{user_id: "1234", data: %{device_name: "Sample Browser"}})
%Session{
  user_id: "1234",
  data: %{
    device_name: "Sample Browser",
    auth_token: "plaintext-auth-token"
    auth_token_digest: "hashed-token"
  }
}
Link to this callback

create_session!(filters, options)

View Source
@callback create_session!(filters :: map(), options :: list()) :: Ecto.Schema.t()

Same as create_session/1 but using Ecto.Repo.insert!.

@callback delete_expired(Ecto.Changeset.t()) :: Ecto.Schema.t()

Deletes expired sessions.

@callback delete_session(Ecto.Schema.t()) :: {atom(), any()}

Deletes the session using Repo.delete.

@callback delete_session!(Ecto.Schema.t()) :: any()

Deletes the session using Repo.delete!.

Link to this callback

filter_session_query(query, filters)

View Source
@callback filter_session_query(query :: Ecto.Queryable.t(), filters :: any()) ::
  Ecto.Queryable.t()

Filters a session query.

Link to this callback

filter_session_query_by(query, filters)

View Source
@callback filter_session_query_by(query :: any(), filters :: any()) :: Ecto.Queryable.t()

Filters a session query by the given argument.

Link to this callback

get_session(filters, options)

View Source
@callback get_session(filters :: any(), options :: list()) :: {atom(), Ecto.Schema.t()}

Retrieves a session from the database.

Link to this callback

get_session!(filters, options)

View Source
@callback get_session!(filters :: any(), options :: list()) :: Ecto.Schema.t()

Retrieves a session from the database using Repo.one!

Link to this callback

get_sessions_query(attrs)

View Source
@callback get_sessions_query(attrs :: any()) :: Ecto.Query.t()

Retrieves a query to the sessions.

Options:

  • delete_query: Boolean that indicates a delete query a should be returned. Instead of a select one (the default: false).
  • preload: Shorthand for preload query argument.
Link to this callback

list_sessions(filters, options)

View Source
@callback list_sessions(filters :: any(), options :: list()) :: [Ecto.Queryable.t()]

Retrieve sessions matching the provided filters.

Link to this callback

list_valid_sessions(filters, options)

View Source
@callback list_valid_sessions(filters :: any(), options :: list()) :: [Ecto.Queryable.t()]

Retrieve valid sessions matching the provided filters.

@callback refresh_session(Ecto.Schema.t()) :: Ecto.Schema.t()

Given a session, ensure expires_at is updated according to the EctoSessions.Config.

@callback update_session!(Ecto.Changeset.t()) :: Ecto.Schema.t()

Updates a session using Repo.update!.