new API getFieldRootsOfTangle

This commit is contained in:
Andre Staltz 2023-09-29 15:44:21 +03:00
parent cc7cf9858d
commit 8fe7b8717d
No known key found for this signature in database
GPG Key ID: 9EDE23EA7E8A4890
2 changed files with 27 additions and 0 deletions

View File

@ -333,6 +333,27 @@ function initRecord(peer, config) {
return fieldRoots.getAll(subdomain)
}
/**
* @param {string} tangleID
*/
function getFieldRootsOfTangle(tangleID) {
assertDBExists(peer)
// prettier-ignore
if (!accountID) throw new Error('Cannot getFieldRootsByTangle() before loading')
const rootMsg = peer.db.get(tangleID)
if (!rootMsg) throw new Error(`Cannot find tangle root "${tangleID}"`)
// prettier-ignore
if (!MsgV3.isMoot(rootMsg, accountID)) throw new Error(`"${tangleID}" is not a moot`)
const domain = rootMsg.metadata.domain
// prettier-ignore
if (!domain.startsWith(PREFIX)) throw new Error(`"${tangleID}" is not a record moot`)
const subdomain = toSubdomain(domain)
return getFieldRoots(subdomain)
}
/**
* @public
* @param {string} id
@ -416,6 +437,7 @@ function initRecord(peer, config) {
update,
read,
getFieldRoots,
getFieldRootsOfTangle,
squeeze,
_squeezePotential,

View File

@ -79,6 +79,8 @@ test('Record update() and get()', async (t) => {
{ age: ['9iTTqNabtnXmw4AiZxNMRq'], name: ['M2JhM7TE2KX5T5rfnxBh6M'] },
'fieldRoots'
)
})
test('Record squeeze', async (t) => {
@ -141,6 +143,9 @@ test('Record receives old branched update', async (t) => {
'fieldRoots'
)
const altFieldRoots7 = peer.record.getFieldRootsOfTangle(mootID)
assert.deepEqual(altFieldRoots7, fieldRoots7, 'getFieldRootsOfTangle')
assert.equal(peer.record._squeezePotential('profile'), 6, 'squeezePotential=6')
})