generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 57
/
records.js
103 lines (85 loc) · 3.33 KB
/
records.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { DwnConstant } from '@tbd54566975/dwn-sdk-js';
import { Interface } from './interface.js';
import { Record } from '../models/record.js';
import { dataToBytes } from '../../utils.js';
export class Records extends Interface {
constructor(dwn) {
super(dwn, 'Records');
}
get create() {
return this.write;
}
async createFrom(target, request) {
const { author: inheritedAuthor, target: _, ...inheritedProperties } = request.record.toJSON();
// If `data` is being updated then `dataCid` and `dataSize` must not be present.
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) {
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)) {
delete inheritedProperties.recordId;
}
return this.write(target, {
author: request?.author || inheritedAuthor,
data: request?.data,
message: {
...inheritedProperties,
...request.message,
},
});
}
async delete(target, request) {
const response = await this.send('Delete', target, request);
return response;
}
async read(target, request) {
const response = await this.send('Read', target, request);
let record;
if (response?.record) {
record = new Record(this.dwn, { ...response.record, target, author: request.author });
}
return { ...response, record };
}
async query(target, request) {
const response = await this.send('Query', target, request);
const entries = [];
response.entries.forEach(entry => {
entries.push(new Record(this.dwn, { ...entry, target, author: request.author }));
});
return { ...response, entries };
}
async write(target, request) {
let dataBytes, dataFormat;
if (request?.data) {
// If `data` is specified, convert string/object data to bytes before further processing.
({ dataBytes, dataFormat } = dataToBytes(request.data, request.message.dataFormat));
} else {
// If not, `dataFormat` must be specified in the request message.
dataFormat = request.message.dataFormat;
}
const response = await this.send('Write', target, {
...request,
data: dataBytes,
message: {
...request.message,
dataFormat,
},
});
let record;
if (response?.message) {
// Include data if `dataSize` is less than DWN 'max data size allowed to be encoded'.
const encodedData = (response.message?.descriptor?.dataSize <= DwnConstant.maxDataSizeAllowedToBeEncoded) ? dataBytes : null;
record = new Record(this.dwn, { ...response.message, encodedData, target, author: request.author });
}
return { ...response, record };
}
}