ORM

is a database-agnostic ORM for Carp.

Installation

Load a backend from the package. The SQLite backend pulls in the ORM core and the sqlite3 package transitively:

(load "git@github.com:carpentry-org/orm@0.2.0" "backends/sqlite3.carp")

Usage

(load "git@github.com:carpentry-org/orm@0.2.0" "backends/sqlite3.carp")

(deftype Item [id Int text String done Bool])
(derive-model Item SQLiteBackend [id Int])

(defn main []
  (let-do [db (Result.unsafe-from-success (SQLite3.open "app.db"))]
    (ignore (Item.create-table &db))
    (ignore (Item.insert &db &(Item.init 0 @"buy milk" false)))
    (println* &(Item.find-all &db))
    (SQLite3.close db)))

derive-model reads the deftype's fields with members and emits CRUD functions in the type's module. The SQL dialect and row marshalling are delegated to a backend module, so the same model definition works against any database with a backend.

begin

macro

Macro

                        (begin backend db)
                    

begins a database transaction.

backend is the backend module and db is a reference to the database connection. Returns (Result () String).

(ORM.begin SQLiteBackend &db)

commit

macro

Macro

                        (commit backend db)
                    

commits the current transaction.

Returns (Result () String).

(ORM.commit SQLiteBackend &db)

rollback

macro

Macro

                        (rollback backend db)
                    

rolls back the current transaction.

Returns (Result () String).

(ORM.rollback SQLiteBackend &db)

with-transaction

macro

Macro

                        (with-transaction backend db body)
                    

runs body inside a transaction with auto-rollback.

body must evaluate to (Result a String). If it returns Success, the transaction is committed and the value is returned. If it returns Error, the transaction is rolled back and the error is propagated. If the commit itself fails, the transaction is also rolled back.

(ORM.with-transaction SQLiteBackend &db
  (do
    (ignore (Item.insert &db &item1))
    (Item.insert &db &item2)))