kreator

Types

The method of the query. This is what defines if the query is a select, insert, update, or delete.

pub opaque type Method

The plan of the query. Contains all the instructions needed to build a query string.

pub opaque type Plan

Functions

pub fn and_where(
  plan: Plan,
  fun: fn(List(Where)) -> List(Where),
) -> Plan

Used to add where clauses to the plan. Receives a function where the parameter is a new empty list of where clauses and it should return a list clauses. The difference from where and or_where is that this function will generate wrapped where clauses with AND.

pub fn delete(plan: Plan) -> Plan

Transforms the plan into a delete query. Will not fail if no where was set.

pub fn insert(plan: Plan, data: List(#(String, Value))) -> Plan

Transforms the plan into an insert query and sets the data to be inserted. It will not fail if the data is empty, tough the generated query will probably be invalid.

Example
import kreator.{table, insert, to_sqlite}

pub fn insert_users() {
  table("users")
  |> insert([("name", "Bruno")])
  |> to_sqlite()
  /// ==> "insert into `users` (`name`) values (?)"
}
pub fn not_returning(plan: Plan) -> Plan
pub fn or_where(
  plan: Plan,
  fun: fn(List(Where)) -> List(Where),
) -> Plan

Used to add where clauses to the plan. Receives a function where the parameter is a new empty list of where clauses and it should return a list clauses. The difference from where and and_where is that this function will generate wrapped where clauses with Or.

pub fn order_by(
  plan: Plan,
  column column: String,
  direction direction: Direction,
) -> Plan

Add an order by clause to the plan. Can be used multiple times to add multiple order by clauses.

pub fn returning(plan: Plan, returning: List(String)) -> Plan

Set the columns to be returned.

table("users") |> select(["name", "id"]) |> returning("id") |> to_sqlite() /// ==> "select `name`, `id` from `users` returning `id`"
pub fn select(plan: Plan, select: List(String)) -> Plan

Set the columns on a select statement. By default the value is ["*"].

table("users") |> select(["name", "id"]) |> to_sqlite() /// ==> "select `name`, `id` from `users`"
pub fn table(table: String) -> Plan

Start a new plan to build a query by setting the table. With just using this you can already create a simple select like this:

table("users") |> to_sqlite() /// ==> "select * from `users`"

If you want to know how to select specific columns you can jump to the select function.

pub fn to_postgres(plan: Plan) -> Query

Builds a query based on a Plan for the `Postgres`` dialect.

pub fn to_sqlite(plan: Plan) -> Query

Builds a query based on a Plan for the SQLite dialect.

pub fn update(plan: Plan, data: List(#(String, Value))) -> Plan

Transforms the plan into an unpdate query and sets the data to be updated. It will not fail if the data is empty, tough the generated query will probably be invalid. Also it will not fail if no where was set.

pub fn where(
  plan: Plan,
  fun: fn(List(Where)) -> List(Where),
) -> Plan

Used to add where clauses to the plan. Receives a function where the parameter is the current list of where clauses and it should return the new where clauses. OBS.: those clauses will be unwrapped.

Search Document