depo v1.1.0 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.

Usage Example

# Open a new in-memory database.
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 SQL queries from the database

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. You can pass an atom registered to a cached statement or a string of valid SQL

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, sql)

Synchronously read SQL queries from the database.

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, sql)

Asynchronously write SQL statements to the database. You can pass an atom registered to a cached statement or a string of valid SQL.

write(db, sql, values)

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