Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extract parentContextId from record when updating #494

Merged
merged 2 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/big-tomatoes-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@web5/api": patch
---

When updating a record, we must include the `parentContextId` in the create options
8 changes: 7 additions & 1 deletion packages/api/src/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,10 +572,16 @@ export class Record implements RecordModel {
* @beta
*/
async update({ dateModified, data, ...params }: RecordUpdateParams): Promise<DwnResponseStatus> {

// if there is a parentId, we remove it from the descriptor and set a parentContextId
const { parentId, ...descriptor } = this._descriptor;
const parentContextId = parentId ? this._contextId.split('/').slice(0, -1).join('/') : undefined;

// Begin assembling the update message.
let updateMessage: DwnMessageParams[DwnInterface.RecordsWrite] = {
...this._descriptor,
...descriptor,
...params,
parentContextId,
messageTimestamp : dateModified, // Map Record class `dateModified` property to DWN SDK `messageTimestamp`
recordId : this._recordId
};
Expand Down
54 changes: 54 additions & 0 deletions packages/api/tests/record.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2263,6 +2263,60 @@ describe('Record', () => {
expect(updatedData).to.equal('bye');
});

it('updates a record which has a parent reference', async () => {
// install a protocol
let { protocol, status: protocolStatus } = await dwnAlice.protocols.configure({
message: {
definition: emailProtocolDefinition,
}
});
expect(protocolStatus.code).to.equal(202);
expect(protocolStatus).to.exist;

// create a parent thread
const { status: threadStatus, record: threadRecord } = await dwnAlice.records.write({
data : 'Hello, world!',
message : {
protocol : protocol.definition.protocol,
schema : emailProtocolDefinition.types.thread.schema,
protocolPath : 'thread'
}
});

expect(threadStatus.code).to.equal(202);
expect(threadRecord).to.not.be.undefined;

// create an email with the thread as a parent
const { status: emailStatus, record: emailRecord } = await dwnAlice.records.write({
data : 'Hello, world!',
message : {
parentContextId : threadRecord.contextId,
protocol : emailProtocolDefinition.protocol,
protocolPath : 'thread/email',
schema : emailProtocolDefinition.types.email.schema
}
});
expect(emailStatus.code).to.equal(202);
expect(emailRecord).to.not.be.undefined;


// update email record
const updateResult = await emailRecord!.update({ data: 'updated email record' });
expect(updateResult.status.code).to.equal(202);

const readResult = await dwnAlice.records.read({
message: {
filter: {
recordId: emailRecord.id
}
}
});

expect(readResult.status.code).to.equal(200);
expect(readResult.record).to.not.be.undefined;
expect(await readResult.record.data.text()).to.equal('updated email record');
});

it('returns new dateModified after each update', async () => {
// Initial write of the record.
const { status, record } = await dwnAlice.records.write({
Expand Down
Loading