1

I am new to watermelon DB and was stuck on one use case. What if I have a two records

Transaction: id, account_id, amount, time Account: id, title, description

I need to fetch all the transactions along with the accounts which are linked to them, not just the relation. How can it be achieved. Your suggestion would be helpful for me Thank you

Code for Transactions

accounts: {type: 'belongs_to', key: 'id'},
transaction_categories: {type: 'belongs_to', key: 'id'},
};

@text('title') title;
@text('description') description;
@text('transaction_type') transactionType;
@text('transaction_category_id') transactionCategoryID;
@text('account_id') accountID;
@Date('input_time') inputTime;
@field('amount') amount;

@readonly @Date('created_at') createdAt;
@readonly @Date('updated_at') updatedAt;

@relation('accounts', 'account_id') account;
@relation('transaction_categories', 'transaction_category_id') transactionCategory;

Code for Accounts

static associations = {
transactions: {type: 'has_many', foreignKey: 'account_id'},
};

@text('title') title;
@text('description') description;
@text('currency') currency;

@readonly @Date('created_at') createdAt;
@readonly @Date('updated_at') updatedAt;

@children('transactions') transactions;

1 Answers1

0

you can map all the transactions and then in each element access the relationship with your account.

const test =async () => {
  // Get all transactions
  const transactions = await database.get("transactionTable").query().fetch();
  // Map to access the account of each transaction
  transactions.map(async(transaction) => {
    const accountRelated = await transaction.accountTable;
    console.log("Account:", accountRelated?._raw)
  })
}
test()