mirror of
https://github.com/ash-project/ash_double_entry.git
synced 2024-09-19 21:13:23 +12:00
docs: update getting started guide
fix: don't require pagination
This commit is contained in:
parent
7a64c8f41c
commit
69b61103ae
2 changed files with 30 additions and 18 deletions
|
@ -15,7 +15,7 @@ Ash Double Entry is implemented as a set of Ash resource extensions. You build t
|
|||
#### Example
|
||||
|
||||
```elixir
|
||||
defmodule YourApp.Account do
|
||||
defmodule YourApp.Leger.Account do
|
||||
use Ash.Resource,
|
||||
data_layer: AshPostgres.DataLayer,
|
||||
extensions: [AshDoubleEntry.Account]
|
||||
|
@ -27,8 +27,8 @@ defmodule YourApp.Account do
|
|||
|
||||
account do
|
||||
# configure the other resources it will interact with
|
||||
transfer_resource YourApp.Transfer
|
||||
balance_resource YourApp.Balance
|
||||
transfer_resource YourApp.Ledger.Transfer
|
||||
balance_resource YourApp.Ledger.Balance
|
||||
# accept custom attributes in the autogenerated `open` create action
|
||||
open_action_accept [:account_number]
|
||||
end
|
||||
|
@ -78,8 +78,8 @@ defmodule YourApp.Transfer do
|
|||
|
||||
transfer do
|
||||
# configure the other resources it will interact with
|
||||
account_resource YourApp.Account
|
||||
balance_resource YourApp.Balance
|
||||
account_resource YourApp.Ledger.Account
|
||||
balance_resource YourApp.Ledger.Balance
|
||||
end
|
||||
end
|
||||
```
|
||||
|
@ -102,7 +102,7 @@ end
|
|||
#### Example
|
||||
|
||||
```elixir
|
||||
defmodule YourApp.Balance do
|
||||
defmodule YourApp.Ledger.Balance do
|
||||
use Ash.Resource,
|
||||
data_layer: AshPostgres.DataLayer,
|
||||
extensions: [AshDoubleEntry.Balance]
|
||||
|
@ -114,8 +114,8 @@ defmodule YourApp.Balance do
|
|||
|
||||
balance do
|
||||
# configure the other resources it will interact with
|
||||
transfer_resource Transfer
|
||||
account_resource Account
|
||||
transfer_resource YourApp.Ledger.Transfer
|
||||
account_resource YourApp.Ledger.Account
|
||||
end
|
||||
|
||||
changes do
|
||||
|
@ -162,40 +162,52 @@ defmodule YourApp.Ledger do
|
|||
use Ash.Api
|
||||
|
||||
resources do
|
||||
resource YourApp.Account
|
||||
resource YourApp.Balance
|
||||
resource YourApp.Transfer
|
||||
resource YourApp.Ledger.Account
|
||||
resource YourApp.Ledger.Balance
|
||||
resource YourApp.Ledger.Transfer
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
And add the API to your config
|
||||
|
||||
`config :your_app, ash_apis: [..., YourApp.Ledger]
|
||||
|
||||
### Generate Migrations
|
||||
|
||||
`mix ash_postgres.generate_migrations --name add_double_entry_ledger`
|
||||
|
||||
### Run them
|
||||
|
||||
`mix ash_postgres.migrate`
|
||||
|
||||
### Use them
|
||||
|
||||
#### Create an account
|
||||
|
||||
```elixir
|
||||
Account
|
||||
YourApp.Ledger.Account
|
||||
|> Ash.Changeset.for_create(:open, %{identifier: "account_one", currency: "USD"})
|
||||
|> Api.create!()
|
||||
|> YourApp.Ledger.create!()
|
||||
```
|
||||
|
||||
#### Create transfers between accounts
|
||||
|
||||
```elixir
|
||||
Transfer
|
||||
YourApp.Ledger.Transfer
|
||||
|> Ash.Changeset.for_create(:transfer, %{
|
||||
amount: Decimal.new(20),
|
||||
from_account_id: account_one.id,
|
||||
to_account_id: account_two.id
|
||||
})
|
||||
|> Api.create!()
|
||||
|> YourApp.LedgerApi.create!()
|
||||
```
|
||||
|
||||
#### Check an account's balance
|
||||
|
||||
```elixir
|
||||
Account
|
||||
|> Api.get!(account_id, load: :balance)
|
||||
YourApp.Leger.Account
|
||||
|> YourApp.Ledger.get!(account_id, load: :balance)
|
||||
|> Map.get(:balance)
|
||||
# => Decimal.new("20")
|
||||
```
|
||||
|
|
|
@ -101,7 +101,7 @@ defmodule AshDoubleEntry.Account.Transformers.AddStructure do
|
|||
else
|
||||
Ash.Resource.Builder.add_action(dsl, :read, :_autogenerated_primary_read,
|
||||
primary?: true,
|
||||
pagination: Ash.Resource.Builder.build_pagination(keyset?: true)
|
||||
pagination: Ash.Resource.Builder.build_pagination(keyset?: true, required?: false)
|
||||
)
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue