From 9870e2fc5d088c1ad216eeecefd0d81b23d0e8f8 Mon Sep 17 00:00:00 2001 From: Frank Hinek Date: Fri, 21 Apr 2023 10:11:20 -0400 Subject: [PATCH] Collection of small enhancements Signed-off-by: Frank Hinek --- examples/test-dashboard/desktop-agent.html | 2 +- src/dwn/interfaces/records.js | 25 +++++++++++----------- src/dwn/models/record.js | 16 +++++++------- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/examples/test-dashboard/desktop-agent.html b/examples/test-dashboard/desktop-agent.html index f09f5ab5b..ce4aed003 100644 --- a/examples/test-dashboard/desktop-agent.html +++ b/examples/test-dashboard/desktop-agent.html @@ -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', diff --git a/src/dwn/interfaces/records.js b/src/dwn/interfaces/records.js index d9ef8e3e2..95c70ea2f 100644 --- a/src/dwn/interfaces/records.js +++ b/src/dwn/interfaces/records.js @@ -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) { @@ -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, @@ -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 }; } @@ -98,6 +98,7 @@ export class Records extends Interface { record = new Record(this.dwn, { ...response.message, encodedData, target, author: request.author }); } + return { ...response, record }; } } diff --git a/src/dwn/models/record.js b/src/dwn/models/record.js index 6ff2471b6..b75ebb4c1 100644 --- a/src/dwn/models/record.js +++ b/src/dwn/models/record.js @@ -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. @@ -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; @@ -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; } @@ -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.`); } - }); + } } }