Lantern.Source (Lantern v0.6.0)

Copy Markdown View Source

A normalized description of how to reach a Postgres database.

Lantern is connection-agnostic: a host application hands it a source and Lantern opens one-shot connections from it. A source can be built from:

  • a postgres:// / postgresql:// URL string,
  • a keyword list / map of connection options, or
  • any struct/map exposing host/hostname, port, username, password, and database (e.g. an Ecto-backed connection record), via from/1.

The struct mirrors the subset of Postgrex.start_link/1 options Lantern needs. to_postgrex_opts/1 produces the final option list.

Summary

Functions

Builds a Source from a URL string, keyword list, map, or existing struct.

Parses a postgres://user:pass@host:port/database?sslmode=... URL.

Converts a Source into the keyword list passed to Postgrex.start_link/1.

Types

t()

@type t() :: %Lantern.Source{
  database: String.t(),
  hostname: String.t(),
  password: String.t() | nil,
  port: pos_integer(),
  ssl: boolean(),
  username: String.t()
}

Functions

from(source)

@spec from(t() | String.t() | keyword() | map()) :: {:ok, t()} | {:error, String.t()}

Builds a Source from a URL string, keyword list, map, or existing struct.

Returns {:ok, source} or {:error, reason}.

parse_url(url)

@spec parse_url(String.t()) :: {:ok, t()} | {:error, String.t()}

Parses a postgres://user:pass@host:port/database?sslmode=... URL.

to_postgrex_opts(source)

@spec to_postgrex_opts(t()) :: keyword()

Converts a Source into the keyword list passed to Postgrex.start_link/1.

pool_size is fixed at 1 — Lantern opens single, short-lived connections.