Hi,
I would model your data like this: Account <-- Transaction
Schema:
type Account {
account.Id
account.Balance
}
type Transaction {
transaction.Id
transaction.Amount
transaction.Account
}
transaction.Account: uid @reverse .
then I wouldn’t need to care about transaction upserts from the account perspective, as it would be solved through the revese edge.
Upserting Account just like in your post above.
Upserting Transaction with 2 query blocks, one for the Account of the transaction, the other one for the Transaction itself:
upsert {
query {
Account as var(func: eq(<account.Id>, "1"))
Transaction as var(func: eq(<transaction.Id>, "1"))
}
mutation {
set {
uid(Transaction) <transaction.Id> "1" .
uid(Transaction) <transaction.Amount> "75" .
uid(Transaction) <dgraph.type> "Transaction" .
uid(Transaction) <transaction.Account> uid(Account) .
# if an Account can not exist, when adding the transaction, add:
uid(Account) <account.Id> "1" .
uid(Account) <dgraph.type> "Account" .
}
}
}
So, answering questions:
- You don’t have to add data bi-directional in a graph. It will work correctly with @reverse directive. Just 2 query blocks for each transactions are ok, as shown above.
To query Account transactions:
query {
Account as var(func: eq(<account.Id>, "1")) {
account.Id
account.Balance
transactions: ~transaction.Account {
transaction.Id
transaction.Amount
}
}
- You can just process transactions row by row with this schema.
In summary, you will not need the “Transactions” column from the Account CSV at all.
Hope that helps!