diff --git a/lib/index.js b/lib/index.js index 87eacf3..ea318b7 100644 --- a/lib/index.js +++ b/lib/index.js @@ -258,7 +258,7 @@ function initRecord(peer, config) { const mootID = MsgV3.getMootID(accountID, fromSubdomain(subdomain)) const tangle = peer.db.getTangle(mootID) const maxDepth = tangle.maxDepth - const fieldRoots = getFieldRoots(accountID, subdomain) + const fieldRoots = getFieldRoots(subdomain) let minDepth = Infinity for (const field in fieldRoots) { for (const msgID of fieldRoots[field]) { @@ -326,12 +326,10 @@ function initRecord(peer, config) { } /** - * @param {string} id * @param {string} subdomain */ - function getFieldRoots(id, subdomain) { - // prettier-ignore - if (id !== accountID) throw new Error(`Cannot getFieldRoots for another user's record. Given ID was "${id}"`) + function getFieldRoots(subdomain) { + if (!accountID) throw new Error('Cannot getFieldRoots() before loading') return fieldRoots.getAll(subdomain) } @@ -339,13 +337,17 @@ function initRecord(peer, config) { * @public * @param {string} id * @param {string} subdomain + * @returns {Record | null} */ - function get(id, subdomain) { + function read(id, subdomain) { assertDBExists(peer) const domain = fromSubdomain(subdomain) const mootID = MsgV3.getMootID(id, domain) const tangle = peer.db.getTangle(mootID) - if (!tangle || tangle.size === 0) return {} + if (!tangle || tangle.size === 0) { + if (id === accountID) return {} + else return null + } const msgIDs = tangle.topoSort() const record = /** @type {Record}*/ ({}) for (const msgID of msgIDs) { @@ -360,17 +362,18 @@ function initRecord(peer, config) { /** * @public - * @param {string} id * @param {string} subdomain * @param {Record} update * @param {CB} cb */ - function update(id, subdomain, update, cb) { - // prettier-ignore - if (id !== accountID) return cb(new Error(`Cannot update another user's record. Given ID was "${id}"`)) + function update(subdomain, update, cb) { + if (!accountID) return cb(new Error('Cannot update before loading')) loaded(() => { - const record = get(id, subdomain) + if (!accountID) return cb(new Error('Expected account to be loaded')) + const record = read(accountID, subdomain) + // prettier-ignore + if (!record) return cb(new Error(`Cannot update non-existent record "${subdomain}`)) let hasChanges = false for (const [field, value] of Object.entries(update)) { @@ -385,18 +388,19 @@ function initRecord(peer, config) { } /** - * @param {string} id * @param {string} subdomain * @param {CB} cb */ - function squeeze(id, subdomain, cb) { - // prettier-ignore - if (id !== accountID) return cb(new Error(`Cannot squeeze another user's record. Given ID was "${id}"`)) + function squeeze(subdomain, cb) { + if (!accountID) return cb(new Error('Cannot squeeze before loading')) const potential = _squeezePotential(subdomain) if (potential < 1) return cb(null, false) loaded(() => { - const record = get(id, subdomain) + if (!accountID) return cb(new Error('Expected account to be loaded')) + const record = read(accountID, subdomain) + // prettier-ignore + if (!record) return cb(new Error(`Cannot squeeze non-existent record "${subdomain}`)) forceUpdate(subdomain, record, (err, _forceUpdated) => { // prettier-ignore if (err) return cb(new Error('Failed to force update when squeezing Record', { cause: err })) @@ -410,7 +414,7 @@ function initRecord(peer, config) { return { load, update, - get, + read, getFieldRoots, squeeze, diff --git a/test/index.test.js b/test/index.test.js index 9d7cd26..f640352 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -29,22 +29,22 @@ test('setup', async (t) => { test('Record update() and get()', async (t) => { assert( - await p(peer.record.update)(aliceID, 'profile', { name: 'alice' }), + await p(peer.record.update)('profile', { name: 'alice' }), 'update .name' ) - assert.deepEqual(peer.record.get(aliceID, 'profile'), { name: 'alice' }, 'get') + assert.deepEqual(peer.record.read(aliceID, 'profile'), { name: 'alice' }, 'get') - const fieldRoots1 = peer.record.getFieldRoots(aliceID, 'profile') + const fieldRoots1 = peer.record.getFieldRoots('profile') assert.deepEqual(fieldRoots1, { name: ['PbwnLbJS4oninQ1RPCdgRn'] }, 'fieldRoots') - assert(await p(peer.record.update)(aliceID, 'profile', { age: 20 }), 'update .age') + assert(await p(peer.record.update)('profile', { age: 20 }), 'update .age') assert.deepEqual( - peer.record.get(aliceID, 'profile'), + peer.record.read(aliceID, 'profile'), { name: 'alice', age: 20 }, 'get' ) - const fieldRoots2 = peer.record.getFieldRoots(aliceID, 'profile') + const fieldRoots2 = peer.record.getFieldRoots('profile') assert.deepEqual( fieldRoots2, { name: ['PbwnLbJS4oninQ1RPCdgRn'], age: ['9iTTqNabtnXmw4AiZxNMRq'] }, @@ -52,28 +52,28 @@ test('Record update() and get()', async (t) => { ) assert.equal( - await p(peer.record.update)(aliceID, 'profile', { name: 'alice' }), + await p(peer.record.update)('profile', { name: 'alice' }), false, 'redundant update .name' ) assert.deepEqual( - peer.record.get(aliceID, 'profile'), + peer.record.read(aliceID, 'profile'), { name: 'alice', age: 20 }, 'get' ) assert.equal( - await p(peer.record.update)(aliceID, 'profile', { name: 'Alice' }), + await p(peer.record.update)('profile', { name: 'Alice' }), true, 'update .name' ) assert.deepEqual( - peer.record.get(aliceID, 'profile'), + peer.record.read(aliceID, 'profile'), { name: 'Alice', age: 20 }, 'get' ) - const fieldRoots3 = peer.record.getFieldRoots(aliceID, 'profile') + const fieldRoots3 = peer.record.getFieldRoots('profile') assert.deepEqual( fieldRoots3, { age: ['9iTTqNabtnXmw4AiZxNMRq'], name: ['M2JhM7TE2KX5T5rfnxBh6M'] }, @@ -82,11 +82,11 @@ test('Record update() and get()', async (t) => { }) test('Record squeeze', async (t) => { - assert(await p(peer.record.update)(aliceID, 'profile', { age: 21 }), 'update .age') - assert(await p(peer.record.update)(aliceID, 'profile', { age: 22 }), 'update .age') - assert(await p(peer.record.update)(aliceID, 'profile', { age: 23 }), 'update .age') + assert(await p(peer.record.update)('profile', { age: 21 }), 'update .age') + assert(await p(peer.record.update)('profile', { age: 22 }), 'update .age') + assert(await p(peer.record.update)('profile', { age: 23 }), 'update .age') - const fieldRoots4 = peer.record.getFieldRoots(aliceID, 'profile') + const fieldRoots4 = peer.record.getFieldRoots('profile') assert.deepEqual( fieldRoots4, { name: ['M2JhM7TE2KX5T5rfnxBh6M'], age: ['S3xiydrT6Y34Bp1vg6wN7P'] }, @@ -94,9 +94,9 @@ test('Record squeeze', async (t) => { ) assert.equal(peer.record._squeezePotential('profile'), 3, 'squeezePotential=3') - assert.equal(await p(peer.record.squeeze)(aliceID, 'profile'), true, 'squeezed') + assert.equal(await p(peer.record.squeeze)('profile'), true, 'squeezed') - const fieldRoots5 = peer.record.getFieldRoots(aliceID, 'profile') + const fieldRoots5 = peer.record.getFieldRoots('profile') assert.deepEqual( fieldRoots5, { name: ['Y4JkpPCHN8Avtz4VALaAmK'], age: ['Y4JkpPCHN8Avtz4VALaAmK'] }, @@ -104,9 +104,9 @@ test('Record squeeze', async (t) => { ) assert.equal(peer.record._squeezePotential('profile'), 0, 'squeezePotential=0') - assert.equal(await p(peer.record.squeeze)(aliceID, 'profile'), false,'squeeze idempotent') + assert.equal(await p(peer.record.squeeze)('profile'), false,'squeeze idempotent') - const fieldRoots6 = peer.record.getFieldRoots(aliceID, 'profile') + const fieldRoots6 = peer.record.getFieldRoots('profile') assert.deepEqual(fieldRoots6, fieldRoots5, 'fieldRoots') }) @@ -131,7 +131,7 @@ test('Record receives old branched update', async (t) => { const rec = await p(peer.db.add)(msg, mootID) assert.equal(rec.id, 'XZWr3DZFG253awsWXgSkS2', 'msg ID') - const fieldRoots7 = peer.record.getFieldRoots(aliceID, 'profile') + const fieldRoots7 = peer.record.getFieldRoots('profile') assert.deepEqual( fieldRoots7, {