Skip to content

Commit

Permalink
fix issue whre dwn-store records were not actually being updated
Browse files Browse the repository at this point in the history
  • Loading branch information
LiranCohen committed Oct 22, 2024
1 parent 80c99ee commit 0f6c2e1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
36 changes: 31 additions & 5 deletions packages/agent/src/store-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { Web5PlatformAgent } from './types/agent.js';
import { TENANT_SEPARATOR } from './utils-internal.js';
import { getDataStoreTenant } from './utils-internal.js';
import { DwnInterface, DwnMessageParams } from './types/dwn.js';
import { ProtocolDefinition } from '@tbd54566975/dwn-sdk-js';
import { ProtocolDefinition, RecordsReadReplyEntry } from '@tbd54566975/dwn-sdk-js';

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

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

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

Expand Down Expand Up @@ -307,6 +309,30 @@ export class DwnDataStore<TStoreObject extends Record<string, any> = Jwk> implem

return recordId;
}

private async getExistingRecordEntry({ id, tenantDid, agent }: {
id: string;
tenantDid: string;
agent: Web5PlatformAgent;
}): Promise<RecordsReadReplyEntry | undefined> {
// Look up the DWN record ID of the object in the store with the given `id`.
const recordId = await this.lookupRecordId({ id, tenantDid, agent });
if (recordId) {
// Read the record from the store.
const { reply: readReply } = await agent.dwn.processRequest({
author : tenantDid,
target : tenantDid,
messageType : DwnInterface.RecordsRead,
messageParams : { filter: { recordId } }
});

if (readReply.status.code !== 200 || !readReply.entry) {
throw new Error(`${this.name}: Failed to read data from DWN for: ${recordId}`);
}

return readReply.entry;
}
}
}

export class InMemoryDataStore<TStoreObject extends Record<string, any> = Jwk> implements AgentDataStore<TStoreObject> {
Expand Down
8 changes: 8 additions & 0 deletions packages/agent/tests/store-data.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,7 @@ describe('AgentDataStore', () => {

describe('updateExisting', () => {
it('updates an existing record', async () => {

// Create and import a DID.
let bearerDid = await DidJwk.create();
const importedDid = await testHarness.agent.did.import({
Expand All @@ -723,6 +724,9 @@ describe('AgentDataStore', () => {
}
};

// get the length of the list before updating to confirm that no additional records are added
const listLength = (await testStore.list({ agent: testHarness.agent })).length;

// Update the DID in the store.
await testStore.set({
id : importedDid.uri,
Expand All @@ -736,6 +740,10 @@ describe('AgentDataStore', () => {
const storedDid = await testStore.get({ id: importedDid.uri, agent: testHarness.agent, tenant: testHarness.agent.agentDid.uri });
expect(storedDid!.uri).to.equal(updatedDid.uri);
expect(storedDid!.document).to.deep.equal(updatedDid.document);

// verify that no additional records were added
const updatedListLength = (await testStore.list({ agent: testHarness.agent })).length;
expect(updatedListLength).to.equal(listLength);
});

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

0 comments on commit 0f6c2e1

Please sign in to comment.