#accounting #sqlx #ledger #journal #template #built #input


An embeddable double sided accounting ledger built on PG/SQLx

42 releases

0.11.3 Mar 6, 2024
0.11.2 Nov 10, 2023
0.11.1 Oct 18, 2023
0.9.0 Jul 19, 2023
0.5.8 Mar 31, 2023

#783 in Database interfaces

Download history 20/week @ 2023-12-24 130/week @ 2023-12-31 42/week @ 2024-01-07 25/week @ 2024-01-14 13/week @ 2024-01-21 32/week @ 2024-01-28 16/week @ 2024-02-04 59/week @ 2024-02-11 73/week @ 2024-02-18 45/week @ 2024-02-25 262/week @ 2024-03-03 82/week @ 2024-03-10 57/week @ 2024-03-17 423/week @ 2024-03-24 108/week @ 2024-03-31 15/week @ 2024-04-07

606 downloads per month

MIT license



This crate provides primitives for double sided accounting built on top of the sqlx Postgres integration.

It features:

  • Accounts can have balances on multiple journals
  • Multi currency / multi layer support
  • Template based transaction inputs for consistency
  • CEL based template interpolation
  • Event streaming to be notified of changes

The CEL interpreter is not complete but provides enough to support basic use cases. More will be added as the need arises.

To use it copy the migrations into your project and add the crate via cargo add sqlx-ledger.

Check out the docs for an example of how to use it



This crate builds on the sqlx crate to provide a set of primitives for implementing an SQL-compatible double-entry accounting system. This system is engineered specifically for dealing with money and building financial products.

Quick Start

Add and execute the migrations from the migrations directory before usage.

cp ./migrations/* <path/to/your/projects/migrations>
# in your project
cargo sqlx migrate

Here is how to initialize a ledger create a primitive template and post a transaction. This is a toy example that brings all pieces together end-to-end. Not recommended for real use.

use uuid::uuid;
use rust_decimal::Decimal;
use sqlx_ledger::{*, journal::*, account::*, tx_template::*};

async fn init_ledger(journal_id: JournalId) -> SqlxLedger {
    let pg_con =
    let pool = sqlx::PgPool::connect(&pg_con).await.unwrap();
    let ledger = SqlxLedger::new(&pool);

    // Initialize the journal - all entities are constructed via builders
    let new_journal = NewJournal::builder()
        .description("General ledger".to_string())
        .expect("Couldn't build NewJournal");

    let _ = ledger.journals().create(new_journal).await;

    // Initialize an income omnibus account
    let main_account_id = uuid!("00000000-0000-0000-0000-000000000001");
    let new_account = NewAccount::builder()

    let _ = ledger.accounts().create(new_account).await;

    // Create the trivial 'income' template
    // Here are the 'parameters' that the template will require as inputs.
    let params = vec![

    // The templates for the Entries that will be created as part of the transaction.
    let entries = vec![
            // Reference the input parameters via CEL syntax
    let tx_code = "GENERAL_INCOME";
    let new_template = NewTxTemplate::builder()
            // Template for the Transaction metadata.

    let _ = ledger.tx_templates().create(new_template).await;


tokio_test::block_on(async {
    let journal_id = JournalId::from(uuid!("00000000-0000-0000-0000-000000000001"));
    let ledger = init_ledger(journal_id).await;

    // The account that is sending to the general income account
    let sender_account_id = AccountId::new();
    let sender_account = NewAccount::builder()

    // Prepare the input parameters that the template requires
    let mut params = TxParams::new();
    params.insert("sender_account_id", sender_account_id);
    params.insert("units", Decimal::ONE);

    // Create the transaction via the template
        .post_transaction(TransactionId::new(), "GENERAL_INCOME", Some(params))

    // Check the resulting balance
    let account_balance = ledger
        .find(journal_id, sender_account_id, "BTC".parse().unwrap())

    assert_eq!(account_balance.unwrap().settled(), Decimal::NEGATIVE_ONE);


~1M SLoC