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) 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 * @public
* @param {string} id * @param {string} id
@ -416,6 +437,7 @@ function initRecord(peer, config) {
update, update,
read, read,
getFieldRoots, getFieldRoots,
getFieldRootsOfTangle,
squeeze, squeeze,
_squeezePotential, _squeezePotential,

View File

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