Moebius.Query

The main query interface for Moebius. Import this module into your code and query like a champ

Summary

Functions

The main starting point. Currently you specify a table here but, possibly, in the future you can override connection settings

Deletes a record based on your filter

Executes a pass-through query and returns a single result

A basic “WHERE” statement builder that builds an inclusive AND-based where statement

A basic “WHERE” statement builder that builds an IN statement

Executes a function with the given name, passed as an atom

A simple insert. Create your list of data and send it on in

Sets the limit of the return

Offsets the limit - so would produce SQL like “select * from users limit 10 offset 2;”

Executes a given pipeline and returns a list of mapped results

Executes a raw SQL query with paramters

Full text search using Postgres’ built in indexing

Builds the select statement based on what was piped together

Executes a given pipeline and returns a single result as a map

Sets the order by. Ascending using :asc is the default, you can send in :desc if you like

Executes the SQL in a given SQL file. Specify this by setting the scripts directive in the config. Pass the file name as an atom, without extension

A simple update based on the criteria you specify

Functions

db(table)

The main starting point. Currently you specify a table here but, possibly, in the future you can override connection settings

delete(cmd)

Deletes a record based on your filter.

Example:

db(:users)
  |> filter("id > $1", 1)
  |> delete
  |> execute
execute(cmd)

Executes a pass-through query and returns a single result

filter(cmd, criteria)

A basic “WHERE” statement builder that builds an inclusive AND-based where statement

Example:

{:ok, res} = db(:users)
    |> filter(name: "Mike")
    |> select
    |> run
filter(cmd, criteria, params)

A basic “WHERE” statement builder that builds an IN statement

Example:

{:ok, res} = db(:users)
    |> filter(:name, in: ["mark", "biff", "skip"])
    |> select
    |> run
function(cmd, function_name, params \\ [])

Executes a function with the given name, passed as an atom.

Example:

{:ok, res} = db(:users)
  |> function(:all_users, name: "steve")
  |> run
insert(cmd, criteria)

A simple insert. Create your list of data and send it on in.

Example:

{:ok, res} = db(:users)
    |> insert(email: "test@test.com", first: "Test", last: "User")
    |> execute
limit(cmd, bound)

Sets the limit of the return.

Example:

{:ok, res} = db(:users)
    |> limit(20)
    |> select
    |> run
offset(cmd, skip)

Offsets the limit - so would produce SQL like “select * from users limit 10 offset 2;”

Example:

{:ok, res} = db(:users)
    |> limit(20)
    |> offset(2)
    |> select
    |> run
run(sql)

Executes a given pipeline and returns a list of mapped results

run(sql, params)

Executes a raw SQL query with paramters

search(cmd, term, columns)

Full text search using Postgres’ built in indexing.

Example:

{:ok, res} = db(:users)
      |> search("Mike", [:first, :last, :email])
      |> run
select(cmd, cols \\ "*")

Builds the select statement based on what was piped together

Example:

{:ok, res} = db(:users)
    |> limit(20)
    |> offset(2)
    |> select
    |> run
single(cmd)

Executes a given pipeline and returns a single result as a map.

sort(cmd, cols, direction \\ :asc)

Sets the order by. Ascending using :asc is the default, you can send in :desc if you like.

Example:

{:ok, res} = db(:users)
    |> filter(id: 1, name: "Steve")
    |> sort(:name, :desc)
    |> select
    |> run
sql_file(file, params \\ [])

Executes the SQL in a given SQL file. Specify this by setting the scripts directive in the config. Pass the file name as an atom, without extension.

{:ok, res} = sql_file(:simple, 1)
  |> run
update(cmd, criteria)

A simple update based on the criteria you specify.

Example:

{:ok, res} = db(:users)
    |> filter(id: 1)
    |> update(email: "maggot@test.com")
    |> execute