Skip to content

Commit ca89770

Browse files
authored
Fix issue where dwn-store records are not actually being updated (#961)
Fix issue where dwn-store records are not actually being updated
1 parent 80c99ee commit ca89770

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

.changeset/curvy-fireants-dress.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@web5/agent": patch
3+
"@web5/identity-agent": patch
4+
"@web5/proxy-agent": patch
5+
"@web5/user-agent": patch
6+
---
7+
8+
Fix error where `dwn-store` records were not being updated when marked as such.

packages/agent/src/store-data.ts

+27-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type { Web5PlatformAgent } from './types/agent.js';
88
import { TENANT_SEPARATOR } from './utils-internal.js';
99
import { getDataStoreTenant } from './utils-internal.js';
1010
import { DwnInterface, DwnMessageParams } from './types/dwn.js';
11-
import { ProtocolDefinition } from '@tbd54566975/dwn-sdk-js';
11+
import { ProtocolDefinition, RecordsReadReplyEntry } from '@tbd54566975/dwn-sdk-js';
1212

1313
export type DataStoreTenantParams = {
1414
agent: Web5PlatformAgent;
@@ -151,13 +151,15 @@ export class DwnDataStore<TStoreObject extends Record<string, any> = Jwk> implem
151151

152152
if (updateExisting) {
153153
// Look up the DWN record ID of the object in the store with the given `id`.
154-
const matchingRecordId = await this.lookupRecordId({ id, tenantDid, agent });
155-
if (!matchingRecordId) {
154+
const matchingRecordEntry = await this.getExistingRecordEntry({ id, tenantDid, agent });
155+
if (!matchingRecordEntry) {
156156
throw new Error(`${this.name}: Update failed due to missing entry for: ${id}`);
157157
}
158158

159159
// set the recordId in the messageParams to update the existing record
160-
messageParams.recordId = matchingRecordId;
160+
// set the dateCreated to the existing dateCreated as this is an immutable property
161+
messageParams.recordId = matchingRecordEntry.recordsWrite!.recordId;
162+
messageParams.dateCreated = matchingRecordEntry.recordsWrite!.descriptor.dateCreated;
161163
} else if (preventDuplicates) {
162164
// Look up the DWN record ID of the object in the store with the given `id`.
163165
const matchingRecordId = await this.lookupRecordId({ id, tenantDid, agent });
@@ -175,7 +177,7 @@ export class DwnDataStore<TStoreObject extends Record<string, any> = Jwk> implem
175177
author : tenantDid,
176178
target : tenantDid,
177179
messageType : DwnInterface.RecordsWrite,
178-
messageParams : { ...this._recordProperties },
180+
messageParams : { ...this._recordProperties, ...messageParams },
179181
dataStream : new Blob([dataBytes], { type: 'application/json' })
180182
});
181183

@@ -307,6 +309,26 @@ export class DwnDataStore<TStoreObject extends Record<string, any> = Jwk> implem
307309

308310
return recordId;
309311
}
312+
313+
private async getExistingRecordEntry({ id, tenantDid, agent }: {
314+
id: string;
315+
tenantDid: string;
316+
agent: Web5PlatformAgent;
317+
}): Promise<RecordsReadReplyEntry | undefined> {
318+
// Look up the DWN record ID of the object in the store with the given `id`.
319+
const recordId = await this.lookupRecordId({ id, tenantDid, agent });
320+
if (recordId) {
321+
// Read the record from the store.
322+
const { reply: readReply } = await agent.dwn.processRequest({
323+
author : tenantDid,
324+
target : tenantDid,
325+
messageType : DwnInterface.RecordsRead,
326+
messageParams : { filter: { recordId } }
327+
});
328+
329+
return readReply.entry;
330+
}
331+
}
310332
}
311333

312334
export class InMemoryDataStore<TStoreObject extends Record<string, any> = Jwk> implements AgentDataStore<TStoreObject> {

packages/agent/tests/store-data.spec.ts

+8
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,7 @@ describe('AgentDataStore', () => {
705705

706706
describe('updateExisting', () => {
707707
it('updates an existing record', async () => {
708+
708709
// Create and import a DID.
709710
let bearerDid = await DidJwk.create();
710711
const importedDid = await testHarness.agent.did.import({
@@ -723,6 +724,9 @@ describe('AgentDataStore', () => {
723724
}
724725
};
725726

727+
// get the length of the list before updating to confirm that no additional records are added
728+
const listLength = (await testStore.list({ agent: testHarness.agent })).length;
729+
726730
// Update the DID in the store.
727731
await testStore.set({
728732
id : importedDid.uri,
@@ -736,6 +740,10 @@ describe('AgentDataStore', () => {
736740
const storedDid = await testStore.get({ id: importedDid.uri, agent: testHarness.agent, tenant: testHarness.agent.agentDid.uri });
737741
expect(storedDid!.uri).to.equal(updatedDid.uri);
738742
expect(storedDid!.document).to.deep.equal(updatedDid.document);
743+
744+
// verify that no additional records were added
745+
const updatedListLength = (await testStore.list({ agent: testHarness.agent })).length;
746+
expect(updatedListLength).to.equal(listLength);
739747
});
740748

741749
it('throws an error if the record does not exist', async () => {

0 commit comments

Comments
 (0)