@@ -8,7 +8,7 @@ import type { Web5PlatformAgent } from './types/agent.js';
8
8
import { TENANT_SEPARATOR } from './utils-internal.js' ;
9
9
import { getDataStoreTenant } from './utils-internal.js' ;
10
10
import { DwnInterface , DwnMessageParams } from './types/dwn.js' ;
11
- import { ProtocolDefinition } from '@tbd54566975/dwn-sdk-js' ;
11
+ import { ProtocolDefinition , RecordsReadReplyEntry } from '@tbd54566975/dwn-sdk-js' ;
12
12
13
13
export type DataStoreTenantParams = {
14
14
agent : Web5PlatformAgent ;
@@ -151,13 +151,15 @@ export class DwnDataStore<TStoreObject extends Record<string, any> = Jwk> implem
151
151
152
152
if ( updateExisting ) {
153
153
// 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 ) {
156
156
throw new Error ( `${ this . name } : Update failed due to missing entry for: ${ id } ` ) ;
157
157
}
158
158
159
159
// 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 ;
161
163
} else if ( preventDuplicates ) {
162
164
// Look up the DWN record ID of the object in the store with the given `id`.
163
165
const matchingRecordId = await this . lookupRecordId ( { id, tenantDid, agent } ) ;
@@ -175,7 +177,7 @@ export class DwnDataStore<TStoreObject extends Record<string, any> = Jwk> implem
175
177
author : tenantDid ,
176
178
target : tenantDid ,
177
179
messageType : DwnInterface . RecordsWrite ,
178
- messageParams : { ...this . _recordProperties } ,
180
+ messageParams : { ...this . _recordProperties , ... messageParams } ,
179
181
dataStream : new Blob ( [ dataBytes ] , { type : 'application/json' } )
180
182
} ) ;
181
183
@@ -307,6 +309,26 @@ export class DwnDataStore<TStoreObject extends Record<string, any> = Jwk> implem
307
309
308
310
return recordId ;
309
311
}
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
+ }
310
332
}
311
333
312
334
export class InMemoryDataStore < TStoreObject extends Record < string , any > = Jwk > implements AgentDataStore < TStoreObject > {
0 commit comments