Skip to content

Commit

Permalink
fix: use "_id" index hint for balance re-calc only of that ID was a r…
Browse files Browse the repository at this point in the history
…ecent one
  • Loading branch information
koresar committed May 15, 2024
1 parent eaa0f48 commit 29297bb
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Book.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,13 @@ export class Book<U extends ITransaction = ITransaction, J extends IJournal = IJ

const partialBalanceOptions = { ...options };
// If using a balance snapshot then make sure to use the appropriate (default "_id_") index for the additional balance calc.
if (parsedQuery._id) partialBalanceOptions.hint = { _id: 1 };
if (parsedQuery._id && balanceSnapshot) {
const lastTransactionDate = balanceSnapshot.transaction.getTimestamp();
if (lastTransactionDate.getTime() + this.expireBalanceSnapshotSec * 1000 > Date.now()) {
// last transaction for this balance was just recently, then let's use the "_id" index as it will likely be faster than any other.
partialBalanceOptions.hint = { _id: 1 };
}
}
const result = (await transactionModel.collection.aggregate([match, GROUP], partialBalanceOptions).toArray())[0];

let balance = 0;
Expand Down

0 comments on commit 29297bb

Please sign in to comment.