depo v1.4.1 Depo

Depo provides lightweight storage and querying capabilities in Elixir by providing a minimal and polished API that builds on the unique advantages of SQLite.

You can read about SQLite’s architecture to learn about the SQLite bytecode compiler and other modules within SQLite that you can utilize.

Depo provides create/1 to create a new Depo.DB object with which you can interact with the database.

Anywhere you give a command, you can give either a valid string of SQL or an atom registered to a cached statement. You can also include numbered variables like ?1 in your SQL statements and pass a list of values as the third parameter to write/3, read/3, and stream/3.

Usage Example

# Open a new in-memory database.
{:ok, db} = Depo.create(:memory)
Depo.write(db, "CREATE TABLE a (n, t)")
# Prepare, cache, and register named statements. 
Depo.teach(db,
  new_b: "INSERT INTO a VALUES(?1, 'b')",
  n2: "SELECT * FROM a WHERE n = 2",
)
# Group multiple statements in nestable transactions. 
Depo.transact(db, fn -> 
  # Write a named statement with bound variables.
  for i <- 1..4, do: Depo.write(db, :new_b, i)
end)
Depo.read(db, :n2) == [%{n: 2, t: "b"}]
Depo.close(db)

Summary

Functions

Safely close the database connection

Open a connection to a database and return a new Depo.DB object to manage the database connection

Synchronously read an SQL query from the database and return a list of the results

Synchronously query the database after binding the values to the variables in the statement

Asynchronously stream the results of an SQL query from the database to the given PID

Stream the results of a query to the PID after binding the given list of values to variables in the statement

Prepare, cache, and register named SQL statements for more efficient repeated use

Wrap any operations within the given closure in a nestable transaction. If any error occurs within, the transaction will be automatically rolled back

Asynchronously write SQL statements to the database

Write an SQL statement to the database after binding the values to the variables in the statement

Functions

close(db)

Safely close the database connection.

open(path)

Open a connection to a database and return a new Depo.DB object to manage the database connection.

There are a few ways you can open a database:

  • pass a path to open an existing on-disk database
  • pass create: path to create and open a database at the path
  • pass :memory to create a new in-memory database
read(db, query)

Synchronously read an SQL query from the database and return a list of the results.

read(db, query, values)

Synchronously query the database after binding the values to the variables in the statement.

stream(db, pid, query)

Asynchronously stream the results of an SQL query from the database to the given PID.

The given process will receive each result as a tuple {stream_id, value} where stream_id is the PID of the stream process that uniquely identifies the stream, and value is a single result map.

stream(db, pid, query, values)

Stream the results of a query to the PID after binding the given list of values to variables in the statement.

teach(db, stmts)

Prepare, cache, and register named SQL statements for more efficient repeated use.

statements should be a keyword list, where the keys are atoms and the values are SQL statements.

transact(db, code)

Wrap any operations within the given closure in a nestable transaction. If any error occurs within, the transaction will be automatically rolled back.

You can read about SQLite’s transactions in depth in its documentation.

write(db, cmd)

Asynchronously write SQL statements to the database.

write(db, cmd, values)

Write an SQL statement to the database after binding the values to the variables in the statement.