From 8fe7b8717d3054a563672c02a181335c451cc151 Mon Sep 17 00:00:00 2001 From: Andre Staltz Date: Fri, 29 Sep 2023 15:44:21 +0300 Subject: [PATCH] new API getFieldRootsOfTangle --- lib/index.js | 22 ++++++++++++++++++++++ test/index.test.js | 5 +++++ 2 files changed, 27 insertions(+) diff --git a/lib/index.js b/lib/index.js index ea318b7..a84a389 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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, diff --git a/test/index.test.js b/test/index.test.js index f640352..d81ae31 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -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') })