Skip to content

Commit

Permalink
Collection of small enhancements
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Hinek <[email protected]>
  • Loading branch information
frankhinek committed Apr 21, 2023
1 parent 6a95eca commit 9870e2f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
2 changes: 1 addition & 1 deletion examples/test-dashboard/desktop-agent.html
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@
create_from_record_button.addEventListener('execute', async event => {
const { record: sourceRecord } = await web5.dwn.records.create(myDid, {
author: myDid,
data: 'Hello, world!',
data: 'Source record to create from',
message: {
published: true,
schema: 'foo/text',
Expand Down
25 changes: 13 additions & 12 deletions src/dwn/interfaces/records.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { DwnConstant } from '@tbd54566975/dwn-sdk-js';

import { Interface } from './interface.js';
import { Record } from '../models/record.js';
import { dataToBytes } from '../../utils.js';
import { dataToBytes, isEmptyObject } from '../../utils.js';

export class Records extends Interface {
constructor(dwn) {
Expand All @@ -14,31 +14,34 @@ export class Records extends Interface {
}

async createFrom(target, request) {
const { author: inheritedAuthor, target: _, ...inheritedProperties } = request.record.toJSON();
const { author: inheritedAuthor, ...inheritedProperties } = request.record.toJSON();

// Remove target from inherited properties since target is being explicitly defined in method parameters.
delete inheritedProperties.target;


// If `data` is being updated then `dataCid` and `dataSize` must not be present.
if (request?.data !== undefined) {
if (request.data !== undefined) {
delete inheritedProperties.dataCid;
delete inheritedProperties.dataSize;
}

// If `published` is set to false, ensure that `datePublished` is undefined. Otherwise, DWN SDK's schema validation
// will throw an error if `published` is false but `datePublished` is set.
if (request?.message?.published === false && inheritedProperties?.datePublished !== undefined) {
if (request.message?.published === false && inheritedProperties.datePublished !== undefined) {
delete inheritedProperties.datePublished;
delete inheritedProperties.published;
}

// If the request changes the `author` or message `descriptor` then the deterministic `recordId` will change.
// As a result, we will discard the `recordId` if either of these changes occur.
if ((request?.message && Object.keys(request.message).length > 0)
|| (request?.author && request.author !== inheritedAuthor)) {
if (!isEmptyObject(request.message) || (request.author && request.author !== inheritedAuthor)) {
delete inheritedProperties.recordId;
}

return this.write(target, {
author: request?.author || inheritedAuthor,
data: request?.data,
author: request.author || inheritedAuthor,
data: request.data,
message: {
...inheritedProperties,
...request.message,
Expand All @@ -64,11 +67,8 @@ export class Records extends Interface {

async query(target, request) {
const response = await this.send('Query', target, request);
const entries = response.entries.map(entry => new Record(this.dwn, { ...entry, target, author: request.author }));

const entries = [];
response.entries.forEach(entry => {
entries.push(new Record(this.dwn, { ...entry, target, author: request.author }));
});
return { ...response, entries };
}

Expand Down Expand Up @@ -98,6 +98,7 @@ export class Records extends Interface {

record = new Record(this.dwn, { ...response.message, encodedData, target, author: request.author });
}

return { ...response, record };
}
}
16 changes: 8 additions & 8 deletions src/dwn/models/record.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export class Record {
// RecordsWriteMessage properties.
const { author, contextId = undefined, descriptor, recordId = null, target } = options;
this.#contextId = contextId;
if (descriptor?.data) delete descriptor.data;
this.#descriptor = descriptor ?? { };
delete this.#descriptor.data;
this.#recordId = recordId;

// Store the target and author DIDs that were used to create the message to use for subsequent reads, etc.
Expand Down Expand Up @@ -143,7 +143,7 @@ export class Record {
// If `data` is being updated then `dataCid` and `dataSize` must be undefined and the `data` property is passed as
// a top-level property to `web5.dwn.records.write()`.
let data;
if (options?.data !== undefined) {
if (options.data !== undefined) {
delete updateMessage.dataCid;
delete updateMessage.dataSize;
data = options.data;
Expand All @@ -157,13 +157,13 @@ export class Record {
// If a new `dateModified` was not provided, remove it from the updateMessage to let the DWN SDK auto-fill.
// This is necessary because otherwise DWN SDK throws an Error 409 Conflict due to attempting to overwrite a record
// when the `dateModified` timestamps are identical.
if (options?.dateModified === undefined) {
if (options.dateModified === undefined) {
delete updateMessage.dateModified;
}

// If `published` is set to false, ensure that `datePublished` is undefined. Otherwise, DWN SDK's schema validation
// will throw an error if `published` is false but `datePublished` is set.
if (options?.published === false && updateMessage?.datePublished !== undefined) {
if (options.published === false && updateMessage.datePublished !== undefined) {
delete updateMessage.datePublished;
}

Expand Down Expand Up @@ -229,10 +229,10 @@ export class Record {
}

static #verifyPermittedMutation(propertiesToMutate, mutableDescriptorProperties) {
propertiesToMutate.forEach(propertyName => {
if (!mutableDescriptorProperties.includes(propertyName)) {
throw new Error(`${propertyName} is an immutable property. Its value cannot be changed.`);
for (const property of propertiesToMutate) {
if (!mutableDescriptorProperties.includes(property)) {
throw new Error(`${property} is an immutable property. Its value cannot be changed.`);
}
});
}
}
}

0 comments on commit 9870e2f

Please sign in to comment.