This repository has been archived by the owner on Mar 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sdk): fix for incorrectly indexing notes for linked accounts
- Loading branch information
1 parent
fc56d87
commit 57d38cf
Showing
2 changed files
with
46 additions
and
7 deletions.
There are no files selected for viewing
39 changes: 37 additions & 2 deletions
39
packages/extension/src/background/database/helpers/Model/latest.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,43 @@ | ||
import { | ||
errorLog, | ||
} from '~/utils/log'; | ||
import { | ||
getDB, | ||
} from '../..'; | ||
|
||
export default async function latest(modelName, { networkId }, { orderBy }) { | ||
export default async function latest( | ||
modelName, | ||
{ networkId }, | ||
{ | ||
orderBy, | ||
filterOptions, | ||
}, | ||
) { | ||
if (!orderBy) { | ||
errorLog(`Field 'orderBy' is required to get the latest ${modelName}.`); | ||
return null; | ||
} | ||
|
||
let matchingIds; | ||
if (filterOptions) { | ||
const query = getDB(networkId)[modelName].where(filterOptions); | ||
matchingIds = new Set(await query.primaryKeys()); | ||
if (!matchingIds.size) { | ||
return null; | ||
} | ||
} | ||
|
||
const table = getDB(networkId)[modelName]; | ||
return table.orderBy(orderBy).last(); | ||
let targetId; | ||
await table | ||
.orderBy(orderBy) | ||
.reverse() | ||
.until(() => !!targetId) | ||
.eachPrimaryKey(async (id) => { | ||
if (!matchingIds || matchingIds.has(id)) { | ||
targetId = id; | ||
} | ||
}); | ||
|
||
return targetId ? table.get(targetId) : null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters