{"id":32167613,"url":"https://github.com/coinjar/ex_double_entry","last_synced_at":"2025-10-21T15:33:56.057Z","repository":{"id":43682208,"uuid":"405954616","full_name":"coinjar/ex_double_entry","owner":"coinjar","description":"An Elixir double-entry library inspired by Ruby's DoubleEntry. Brought to you by CoinJar.","archived":false,"fork":false,"pushed_at":"2022-03-05T02:59:50.000Z","size":90,"stargazers_count":23,"open_issues_count":2,"forks_count":7,"subscribers_count":9,"default_branch":"main","last_synced_at":"2025-09-27T01:34:52.863Z","etag":null,"topics":["double-entry","double-entry-accounting","double-entry-bookkeeping"],"latest_commit_sha":null,"homepage":"","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/coinjar.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-09-13T11:59:50.000Z","updated_at":"2025-09-17T05:23:56.000Z","dependencies_parsed_at":"2022-08-23T03:11:17.618Z","dependency_job_id":null,"html_url":"https://github.com/coinjar/ex_double_entry","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/coinjar/ex_double_entry","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coinjar%2Fex_double_entry","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coinjar%2Fex_double_entry/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coinjar%2Fex_double_entry/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coinjar%2Fex_double_entry/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/coinjar","download_url":"https://codeload.github.com/coinjar/ex_double_entry/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coinjar%2Fex_double_entry/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":280287207,"owners_count":26304891,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-21T02:00:06.614Z","response_time":58,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["double-entry","double-entry-accounting","double-entry-bookkeeping"],"created_at":"2025-10-21T15:33:51.998Z","updated_at":"2025-10-21T15:33:56.052Z","avatar_url":"https://github.com/coinjar.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ExDoubleEntry\n\n[![Build Status](https://github.com/coinjar/ex_double_entry/actions/workflows/ci.yml/badge.svg)](https://github.com/coinjar/ex_double_entry/actions)\n\nAn Elixir double-entry library inspired by Ruby's [DoubleEntry](https://github.com/envato/double_entry). Brought to you by [CoinJar](https://coinjar.com).\n\n![](https://i.imgur.com/QqrlYZ9.png)\n\n## Supported Databases\n\n- Postgres 9.4+ (for `JSONB` support)\n- MySQL 8.0+ (for row locking support)\n\n## Installation\n\n```elixir\ndef deps do\n  [\n    {:ex_double_entry, github: \"coinjar/ex_double_entry\"},\n    # pick one DB package\n    {:postgrex, \"\u003e= 0.0.0\"},\n    {:myxql, \"\u003e= 0.0.0\"},\n  ]\nend\n```\n\n### DB Migration\n\nYou will need to copy and run the [migration file](priv/repo/migrations/001_ex_double_entry_tables.exs) to create the DB tables.\n\n## Configuration\n\n```elixir\nconfig :ex_double_entry,\n  db: :postgres,\n  db_table_prefix: \"ex_double_entry_\",\n  repo: YourProject.Repo,\n  default_currency: :USD,\n  # all accounts need to be defined here\n  accounts: %{\n    # account identifier: account options\n    #\n    # valid options are:\n    #   \"positive_only\": whether the account can go into negative balance\n    bank: [],\n    savings: [positive_only: true],\n    checking: [],\n  },\n  # all transfers need to be defined here\n  transfers: %{\n    # transfer code: transfer pairs\n    #\n    # for each transfer pair:\n    #   - the first element is the source account\n    #   - the second element is the destination account\n    deposit: [\n      {:bank, :savings},\n      {:bank, :checking},\n      {:checking, :savings},\n    ],\n    withdraw: [\n      {:savings, :checking},\n    ],\n  }\n```\n\n## Usage\n\n### Accounts \u0026 Balances\n\n```elixir\n# creates a new account with 0 balance\nExDoubleEntry.make_account!(\n  # identifier of the account, in atom\n  :savings,\n  # currency can be any arbitrary atom\n  currency: :USD,\n  # optional, scope can be any arbitrary string\n  #\n  # due to DB index on `NULL` values, scope value can only be `nil` (stored as\n  # an empty string in the DB) or non-empty strings\n  scope: \"user/1\"\n)\n\n# looks up an account with its balance\nExDoubleEntry.lookup_account!(\n  :savings,\n  currency: :USD,\n  scope: \"user/1\"\n)\n```\n\nBoth functions return an `ExDoubleEntry.Account` struct that looks like this:\n\n```elixir\n%ExDoubleEntry.Account{\n  id: 1,\n  identifier: :savings,\n  currency: :USD,\n  scope: \"user/1\",\n  positive_only?: true,\n  balance: Money.new(0, :USD),\n}\n```\n\n### Transfers\n\nThere are two transfer modes, `transfer` and `transfer!`.\n\nNote: ExDoubleEntry relies on the [money](https://github.com/elixirmoney/money)\nlibrary for balances and amounts.\n\n```elixir\n# accounts need to exist in the DB otherwise\n# `ExDoubleEntry.Account.NotFoundError` is raised\nExDoubleEntry.transfer(\n  money: Money.new(100_00, :USD),\n  # accounts need to be defined in the config\n  from: account_a,\n  to: account_b,\n  # transfer code is required, and must be defined in the config\n  code: :deposit,\n  # optional, metadata can be any arbitrary map, it gets stored in the DB\n  # as either a JSON string (MySQL) or a JSONB object (Postgres)\n  metadata: %{diamond: \"hands\"}\n)\n\n# accounts will be created in the DB if they don't exist\n# once accounts are created they will be locked during the transfer\nExDoubleEntry.transfer!(\n  money: Money.new(100_00, :USD),\n  from: account_a,\n  to: account_b,\n  code: :deposit\n)\n```\n\n### Locking\n\nTransfer itself will already lock the accounts involved. However, if there are\nother tasks that need to be performed atomically with the transfer, you can\nperform them using `lock_accounts`.\n\nTransactions can be nested arbitrarily, since in Ecto, transactions are\nflattened and are committed or rolled back based on the outer most transaction.\n\nRead more on Ecto's transaction handling [here](https://hexdocs.pm/ecto/Ecto.Repo.html#c:transaction/2).\n\n```elixir\nExDoubleEntry.lock_accounts([account_a, account_b], fn -\u003e\n  ExDoubleEntry.transfer!(\n    money: Money.new(100, :USD),\n    from: account_a,\n    to: account_b,\n    code: :deposit\n  )\n\n  # perform other tasks that should be committed atomically with the transfer\nend)\n```\n\n## License\n\nLicensed under [MIT](LICENSE.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoinjar%2Fex_double_entry","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcoinjar%2Fex_double_entry","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoinjar%2Fex_double_entry/lists"}