@coasys/hooks-helpers 0.9.0 → 0.9.1-prerelease.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +22 -22
- package/src/cache.ts +70 -70
- package/src/factory/SubjectRepository.ts +253 -257
- package/src/factory/index.ts +1 -1
- package/src/factory/model.ts +57 -57
- package/src/getProfile.ts +26 -26
- package/src/index.ts +2 -2
package/package.json
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@coasys/hooks-helpers",
|
|
3
|
-
"version": "0.9.
|
|
4
|
-
"description": "",
|
|
5
|
-
"main": "./src/index.ts",
|
|
6
|
-
"module": "./src/index.ts",
|
|
7
|
-
"private": false,
|
|
8
|
-
"type": "module",
|
|
9
|
-
"files": [
|
|
10
|
-
"dist",
|
|
11
|
-
"src"
|
|
12
|
-
],
|
|
13
|
-
"scripts": {},
|
|
14
|
-
"keywords": [],
|
|
15
|
-
"author": "",
|
|
16
|
-
"license": "ISC",
|
|
17
|
-
"dependencies": {
|
|
18
|
-
"@coasys/ad4m": "
|
|
19
|
-
"@coasys/ad4m-connect": "
|
|
20
|
-
"uuid": "*"
|
|
21
|
-
}
|
|
22
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@coasys/hooks-helpers",
|
|
3
|
+
"version": "0.9.1-prerelease.1",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "./src/index.ts",
|
|
6
|
+
"module": "./src/index.ts",
|
|
7
|
+
"private": false,
|
|
8
|
+
"type": "module",
|
|
9
|
+
"files": [
|
|
10
|
+
"dist",
|
|
11
|
+
"src"
|
|
12
|
+
],
|
|
13
|
+
"scripts": {},
|
|
14
|
+
"keywords": [],
|
|
15
|
+
"author": "",
|
|
16
|
+
"license": "ISC",
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@coasys/ad4m": "0.9.1-prerelease.1",
|
|
19
|
+
"@coasys/ad4m-connect": "0.9.1-prerelease.1",
|
|
20
|
+
"uuid": "*"
|
|
21
|
+
}
|
|
22
|
+
}
|
package/src/cache.ts
CHANGED
|
@@ -1,70 +1,70 @@
|
|
|
1
|
-
import { PerspectiveProxy } from "@coasys/ad4m";
|
|
2
|
-
|
|
3
|
-
const cache: Map<string, any> = new Map();
|
|
4
|
-
const subscribers: Map<string, Function[]> = new Map();
|
|
5
|
-
|
|
6
|
-
export function getCache<T>(key: string) {
|
|
7
|
-
const match: T | undefined = cache.get(key);
|
|
8
|
-
return match;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export function setCache<T>(key: string, value: T) {
|
|
12
|
-
cache.set(key, value);
|
|
13
|
-
getSubscribers(key).forEach((cb) => cb());
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export function subscribe(key: string, callback: Function) {
|
|
17
|
-
getSubscribers(key).push(callback);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export function unsubscribe(key: string, callback: Function) {
|
|
21
|
-
const subs = getSubscribers(key);
|
|
22
|
-
const index = subs.indexOf(callback);
|
|
23
|
-
if (index >= 0) {
|
|
24
|
-
subs.splice(index, 1);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export function getSubscribers(key: string) {
|
|
29
|
-
if (!subscribers.has(key)) subscribers.set(key, []);
|
|
30
|
-
return subscribers.get(key)!;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export function subscribeToPerspective(
|
|
34
|
-
perspective: PerspectiveProxy,
|
|
35
|
-
added: Function,
|
|
36
|
-
removed: Function
|
|
37
|
-
) {
|
|
38
|
-
const addedKey = `perspective-${perspective.uuid}-added`;
|
|
39
|
-
const removedKey = `perspective-${perspective.uuid}-removed`;
|
|
40
|
-
|
|
41
|
-
if (!subscribers.has(addedKey)) {
|
|
42
|
-
console.log("subscribing!");
|
|
43
|
-
perspective.addListener("link-added", (link) => {
|
|
44
|
-
subscribers.get(addedKey).forEach((cb) => cb(link));
|
|
45
|
-
return null;
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
if (!subscribers.has(removedKey)) {
|
|
50
|
-
perspective.addListener("link-removed", (link) => {
|
|
51
|
-
subscribers.get(removedKey).forEach((cb) => cb(link));
|
|
52
|
-
return null;
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
subscribe(addedKey, added);
|
|
57
|
-
subscribe(removedKey, removed);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export function unsubscribeFromPerspective(
|
|
61
|
-
perspective: PerspectiveProxy,
|
|
62
|
-
added: Function,
|
|
63
|
-
removed: Function
|
|
64
|
-
) {
|
|
65
|
-
const addedKey = `perspective-${perspective.uuid}-added`;
|
|
66
|
-
const removedKey = `perspective-${perspective.uuid}-removed`;
|
|
67
|
-
|
|
68
|
-
unsubscribe(addedKey, added);
|
|
69
|
-
unsubscribe(removedKey, removed);
|
|
70
|
-
}
|
|
1
|
+
import { PerspectiveProxy } from "@coasys/ad4m";
|
|
2
|
+
|
|
3
|
+
const cache: Map<string, any> = new Map();
|
|
4
|
+
const subscribers: Map<string, Function[]> = new Map();
|
|
5
|
+
|
|
6
|
+
export function getCache<T>(key: string) {
|
|
7
|
+
const match: T | undefined = cache.get(key);
|
|
8
|
+
return match;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function setCache<T>(key: string, value: T) {
|
|
12
|
+
cache.set(key, value);
|
|
13
|
+
getSubscribers(key).forEach((cb) => cb());
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function subscribe(key: string, callback: Function) {
|
|
17
|
+
getSubscribers(key).push(callback);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function unsubscribe(key: string, callback: Function) {
|
|
21
|
+
const subs = getSubscribers(key);
|
|
22
|
+
const index = subs.indexOf(callback);
|
|
23
|
+
if (index >= 0) {
|
|
24
|
+
subs.splice(index, 1);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function getSubscribers(key: string) {
|
|
29
|
+
if (!subscribers.has(key)) subscribers.set(key, []);
|
|
30
|
+
return subscribers.get(key)!;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function subscribeToPerspective(
|
|
34
|
+
perspective: PerspectiveProxy,
|
|
35
|
+
added: Function,
|
|
36
|
+
removed: Function
|
|
37
|
+
) {
|
|
38
|
+
const addedKey = `perspective-${perspective.uuid}-added`;
|
|
39
|
+
const removedKey = `perspective-${perspective.uuid}-removed`;
|
|
40
|
+
|
|
41
|
+
if (!subscribers.has(addedKey)) {
|
|
42
|
+
console.log("subscribing!");
|
|
43
|
+
perspective.addListener("link-added", (link) => {
|
|
44
|
+
subscribers.get(addedKey).forEach((cb) => cb(link));
|
|
45
|
+
return null;
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (!subscribers.has(removedKey)) {
|
|
50
|
+
perspective.addListener("link-removed", (link) => {
|
|
51
|
+
subscribers.get(removedKey).forEach((cb) => cb(link));
|
|
52
|
+
return null;
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
subscribe(addedKey, added);
|
|
57
|
+
subscribe(removedKey, removed);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function unsubscribeFromPerspective(
|
|
61
|
+
perspective: PerspectiveProxy,
|
|
62
|
+
added: Function,
|
|
63
|
+
removed: Function
|
|
64
|
+
) {
|
|
65
|
+
const addedKey = `perspective-${perspective.uuid}-added`;
|
|
66
|
+
const removedKey = `perspective-${perspective.uuid}-removed`;
|
|
67
|
+
|
|
68
|
+
unsubscribe(addedKey, added);
|
|
69
|
+
unsubscribe(removedKey, removed);
|
|
70
|
+
}
|
|
@@ -1,257 +1,253 @@
|
|
|
1
|
-
import {
|
|
2
|
-
PerspectiveProxy,
|
|
3
|
-
Link,
|
|
4
|
-
Subject,
|
|
5
|
-
Literal,
|
|
6
|
-
LinkQuery,
|
|
7
|
-
} from "@coasys/ad4m";
|
|
8
|
-
import { setProperties } from "./model";
|
|
9
|
-
import { v4 as uuidv4 } from "uuid";
|
|
10
|
-
|
|
11
|
-
export const SELF = "ad4m://self";
|
|
12
|
-
|
|
13
|
-
export type ModelProps = {
|
|
14
|
-
perspective: PerspectiveProxy;
|
|
15
|
-
source?: string;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
export class SubjectRepository<SubjectClass extends { [x: string]: any }> {
|
|
19
|
-
source = SELF;
|
|
20
|
-
subject: SubjectClass | string;
|
|
21
|
-
perspective: PerspectiveProxy;
|
|
22
|
-
tempSubject: any | string;
|
|
23
|
-
|
|
24
|
-
constructor(subject: { new (): SubjectClass } | string, props: ModelProps) {
|
|
25
|
-
this.perspective = props.perspective;
|
|
26
|
-
this.source = props.source || this.source;
|
|
27
|
-
this.subject = typeof subject === "string" ? subject : new subject();
|
|
28
|
-
this.tempSubject = subject;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
get className(): string {
|
|
32
|
-
return typeof this.subject === "string"
|
|
33
|
-
? this.subject
|
|
34
|
-
: this.subject.className;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
async ensureSubject() {
|
|
38
|
-
if (typeof this.tempSubject === "string") return;
|
|
39
|
-
await this.perspective.ensureSDNASubjectClass(this.tempSubject);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
async create(
|
|
43
|
-
data: SubjectClass,
|
|
44
|
-
id?: string,
|
|
45
|
-
source?: string
|
|
46
|
-
): Promise<SubjectClass> {
|
|
47
|
-
await this.ensureSubject();
|
|
48
|
-
const base = id || Literal.from(uuidv4()).toUrl();
|
|
49
|
-
|
|
50
|
-
let newInstance = await this.perspective.createSubject(this.subject, base);
|
|
51
|
-
|
|
52
|
-
if (!newInstance) {
|
|
53
|
-
throw "Failed to create new instance of " + this.subject;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
// Connect new instance to source
|
|
57
|
-
await this.perspective.add(
|
|
58
|
-
new Link({
|
|
59
|
-
source: source || this.source,
|
|
60
|
-
predicate: "ad4m://has_child",
|
|
61
|
-
target: base,
|
|
62
|
-
})
|
|
63
|
-
);
|
|
64
|
-
|
|
65
|
-
Object.keys(data).forEach((key) =>
|
|
66
|
-
data[key] === undefined || data[key] === null ? delete data[key] : {}
|
|
67
|
-
);
|
|
68
|
-
|
|
69
|
-
setProperties(newInstance, data);
|
|
70
|
-
|
|
71
|
-
// @ts-ignore
|
|
72
|
-
return this.getSubjectData(newInstance);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
async update(id: string, data: QueryPartialEntity<SubjectClass>) {
|
|
76
|
-
await this.
|
|
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
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
)
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
res =
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
this.
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
);
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
query
|
|
234
|
-
|
|
235
|
-
await
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
export type
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
size: number;
|
|
255
|
-
infinite: boolean;
|
|
256
|
-
uniqueKey: string;
|
|
257
|
-
};
|
|
1
|
+
import {
|
|
2
|
+
PerspectiveProxy,
|
|
3
|
+
Link,
|
|
4
|
+
Subject,
|
|
5
|
+
Literal,
|
|
6
|
+
LinkQuery,
|
|
7
|
+
} from "@coasys/ad4m";
|
|
8
|
+
import { setProperties } from "./model";
|
|
9
|
+
import { v4 as uuidv4 } from "uuid";
|
|
10
|
+
|
|
11
|
+
export const SELF = "ad4m://self";
|
|
12
|
+
|
|
13
|
+
export type ModelProps = {
|
|
14
|
+
perspective: PerspectiveProxy;
|
|
15
|
+
source?: string;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export class SubjectRepository<SubjectClass extends { [x: string]: any }> {
|
|
19
|
+
source = SELF;
|
|
20
|
+
subject: SubjectClass | string;
|
|
21
|
+
perspective: PerspectiveProxy;
|
|
22
|
+
tempSubject: any | string;
|
|
23
|
+
|
|
24
|
+
constructor(subject: { new (): SubjectClass } | string, props: ModelProps) {
|
|
25
|
+
this.perspective = props.perspective;
|
|
26
|
+
this.source = props.source || this.source;
|
|
27
|
+
this.subject = typeof subject === "string" ? subject : new subject();
|
|
28
|
+
this.tempSubject = subject;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
get className(): string {
|
|
32
|
+
return typeof this.subject === "string"
|
|
33
|
+
? this.subject
|
|
34
|
+
: this.subject.className;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async ensureSubject() {
|
|
38
|
+
if (typeof this.tempSubject === "string") return;
|
|
39
|
+
await this.perspective.ensureSDNASubjectClass(this.tempSubject);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async create(
|
|
43
|
+
data: SubjectClass,
|
|
44
|
+
id?: string,
|
|
45
|
+
source?: string
|
|
46
|
+
): Promise<SubjectClass> {
|
|
47
|
+
await this.ensureSubject();
|
|
48
|
+
const base = id || Literal.from(uuidv4()).toUrl();
|
|
49
|
+
|
|
50
|
+
let newInstance = await this.perspective.createSubject(this.subject, base);
|
|
51
|
+
|
|
52
|
+
if (!newInstance) {
|
|
53
|
+
throw "Failed to create new instance of " + this.subject;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Connect new instance to source
|
|
57
|
+
await this.perspective.add(
|
|
58
|
+
new Link({
|
|
59
|
+
source: source || this.source,
|
|
60
|
+
predicate: "ad4m://has_child",
|
|
61
|
+
target: base,
|
|
62
|
+
})
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
Object.keys(data).forEach((key) =>
|
|
66
|
+
data[key] === undefined || data[key] === null ? delete data[key] : {}
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
setProperties(newInstance, data);
|
|
70
|
+
|
|
71
|
+
// @ts-ignore
|
|
72
|
+
return this.getSubjectData(newInstance);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async update(id: string, data: QueryPartialEntity<SubjectClass>) {
|
|
76
|
+
const instance = await this.get(id);
|
|
77
|
+
|
|
78
|
+
if (!instance) {
|
|
79
|
+
throw "Failed to find instance of " + this.subject + " with id " + id;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
Object.keys(data).forEach((key) =>
|
|
83
|
+
data[key] === undefined ? delete data[key] : {}
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
// @ts-ignore
|
|
87
|
+
setProperties(instance, data);
|
|
88
|
+
|
|
89
|
+
return this.getSubjectData(instance);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async remove(id: string) {
|
|
93
|
+
if (this.perspective) {
|
|
94
|
+
const linksTo = await this.perspective.get(new LinkQuery({ target: id }));
|
|
95
|
+
const linksFrom = await this.perspective.get(
|
|
96
|
+
new LinkQuery({ source: id })
|
|
97
|
+
);
|
|
98
|
+
this.perspective.removeLinks([...linksFrom, ...linksTo]);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async get(id: string): Promise<SubjectClass | null> {
|
|
103
|
+
if (id) {
|
|
104
|
+
await this.ensureSubject();
|
|
105
|
+
const subjectProxy = await this.perspective.getSubjectProxy(
|
|
106
|
+
id,
|
|
107
|
+
this.subject
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
// @ts-ignore
|
|
111
|
+
return subjectProxy || null;
|
|
112
|
+
} else {
|
|
113
|
+
const all = await this.getAll();
|
|
114
|
+
return all[0] || null;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
async getData(id: string): Promise<SubjectClass | string | null> {
|
|
119
|
+
const entry = await this.get(id);
|
|
120
|
+
if (entry) {
|
|
121
|
+
// @ts-ignore
|
|
122
|
+
return await this.getSubjectData(entry);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
private async getSubjectData(entry: any) {
|
|
129
|
+
let links = await this.perspective.get(
|
|
130
|
+
new LinkQuery({ source: entry.baseExpression })
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
console.log("kkkkk 1", links);
|
|
134
|
+
|
|
135
|
+
let data: any = await this.perspective.getSubjectData(this.subject, entry.baseExpression)
|
|
136
|
+
|
|
137
|
+
console.log("kkkkk 2", data);
|
|
138
|
+
|
|
139
|
+
for (const key in data) {
|
|
140
|
+
if (this.tempSubject.prototype?.__properties[key]?.transform) {
|
|
141
|
+
console.log("kkkkk 3", this.tempSubject.prototype?.__properties[key]);
|
|
142
|
+
|
|
143
|
+
data[key] =
|
|
144
|
+
this.tempSubject.prototype.__properties[key].transform(data[key]);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
console.log("kkkkk 4", data);
|
|
149
|
+
|
|
150
|
+
return {
|
|
151
|
+
id: entry.baseExpression,
|
|
152
|
+
timestamp: links[0].timestamp,
|
|
153
|
+
author: links[0].author,
|
|
154
|
+
...data,
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
async getAll(source?: string, query?: QueryOptions): Promise<SubjectClass[]> {
|
|
159
|
+
await this.ensureSubject();
|
|
160
|
+
|
|
161
|
+
const tempSource = source || this.source;
|
|
162
|
+
|
|
163
|
+
let res = [];
|
|
164
|
+
|
|
165
|
+
if (query) {
|
|
166
|
+
try {
|
|
167
|
+
const queryResponse = (
|
|
168
|
+
await this.perspective.infer(
|
|
169
|
+
`findall([Timestamp, Base], (subject_class("${this.className}", C), instance(C, Base), link("${tempSource}", Predicate, Base, Timestamp, Author)), AllData), length(AllData, DataLength), sort(AllData, SortedData).`
|
|
170
|
+
)
|
|
171
|
+
)[0];
|
|
172
|
+
|
|
173
|
+
if (queryResponse.SortedData >= query.size) {
|
|
174
|
+
const isOutofBound =
|
|
175
|
+
query.size * query.page > queryResponse.DataLength;
|
|
176
|
+
|
|
177
|
+
const newPageSize = isOutofBound
|
|
178
|
+
? queryResponse.DataLength - query.size * (query.page - 1)
|
|
179
|
+
: query.size;
|
|
180
|
+
|
|
181
|
+
const mainQuery = `findall([Timestamp, Base], (subject_class("${this.className}", C), instance(C, Base), link("${tempSource}", Predicate, Base, Timestamp, Author)), AllData), sort(AllData, SortedData), reverse(SortedData, ReverseSortedData), paginate(ReverseSortedData, ${query.page}, ${newPageSize}, PageData).`;
|
|
182
|
+
res = await this.perspective.infer(mainQuery);
|
|
183
|
+
|
|
184
|
+
res = res[0].PageData.map((r) => ({
|
|
185
|
+
Base: r[1],
|
|
186
|
+
Timestamp: r[0],
|
|
187
|
+
}));
|
|
188
|
+
} else {
|
|
189
|
+
res = await this.perspective.infer(
|
|
190
|
+
`subject_class("${this.className}", C), instance(C, Base), triple("${tempSource}", Predicate, Base).`
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
} catch (e) {
|
|
194
|
+
console.log("Query failed", e);
|
|
195
|
+
}
|
|
196
|
+
} else {
|
|
197
|
+
res = await this.perspective.infer(
|
|
198
|
+
`subject_class("${this.className}", C), instance(C, Base), triple("${tempSource}", Predicate, Base).`
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const results =
|
|
203
|
+
res &&
|
|
204
|
+
res.filter(
|
|
205
|
+
(obj, index, self) =>
|
|
206
|
+
index === self.findIndex((t) => t.Base === obj.Base)
|
|
207
|
+
);
|
|
208
|
+
|
|
209
|
+
if (!res) return [];
|
|
210
|
+
|
|
211
|
+
const data = await Promise.all(
|
|
212
|
+
results.map(async (result) => {
|
|
213
|
+
let subject = new Subject(
|
|
214
|
+
this.perspective!,
|
|
215
|
+
result.Base,
|
|
216
|
+
this.className
|
|
217
|
+
);
|
|
218
|
+
|
|
219
|
+
await subject.init();
|
|
220
|
+
|
|
221
|
+
return subject;
|
|
222
|
+
})
|
|
223
|
+
);
|
|
224
|
+
|
|
225
|
+
// @ts-ignore
|
|
226
|
+
return data;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
async getAllData(
|
|
230
|
+
source?: string,
|
|
231
|
+
query?: QueryOptions
|
|
232
|
+
): Promise<SubjectClass[]> {
|
|
233
|
+
const subjects = await this.getAll(source, query);
|
|
234
|
+
|
|
235
|
+
const entries = await Promise.all(
|
|
236
|
+
subjects.map((e) => this.getSubjectData(e))
|
|
237
|
+
);
|
|
238
|
+
|
|
239
|
+
// @ts-ignore
|
|
240
|
+
return entries;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export type QueryPartialEntity<T> = {
|
|
245
|
+
[P in keyof T]?: T[P] | (() => string);
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
export type QueryOptions = {
|
|
249
|
+
page: number;
|
|
250
|
+
size: number;
|
|
251
|
+
infinite: boolean;
|
|
252
|
+
uniqueKey: string;
|
|
253
|
+
};
|
package/src/factory/index.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from "./SubjectRepository";
|
|
1
|
+
export * from "./SubjectRepository";
|
package/src/factory/model.ts
CHANGED
|
@@ -1,57 +1,57 @@
|
|
|
1
|
-
type Target = String;
|
|
2
|
-
|
|
3
|
-
export type PropertyValueMap = {
|
|
4
|
-
[property: string]: Target | Target[];
|
|
5
|
-
};
|
|
6
|
-
|
|
7
|
-
export function capitalize(str: string) {
|
|
8
|
-
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
// e.g. "name" -> "setName"
|
|
12
|
-
export function propertyNameToSetterName(property: string): string {
|
|
13
|
-
return `set${capitalize(property)}`;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export function pluralToSingular(plural: string): string {
|
|
17
|
-
if (plural.endsWith("ies")) {
|
|
18
|
-
return plural.slice(0, -3) + "y";
|
|
19
|
-
} else if (plural.endsWith("s")) {
|
|
20
|
-
return plural.slice(0, -1);
|
|
21
|
-
} else {
|
|
22
|
-
return plural;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
// e.g. "comments" -> "addComment"
|
|
27
|
-
export function collectionToAdderName(collection: string): string {
|
|
28
|
-
return `add${capitalize(collection)}`;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export function collectionToSetterName(collection: string): string {
|
|
32
|
-
return `setCollection${capitalize(collection)}`;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export function setProperties(subject: any, properties: PropertyValueMap) {
|
|
36
|
-
Object.keys(properties).forEach((key) => {
|
|
37
|
-
if (Array.isArray(properties[key])) {
|
|
38
|
-
// it's a collection
|
|
39
|
-
const adderName = collectionToAdderName(key);
|
|
40
|
-
const adderFunction = subject[adderName];
|
|
41
|
-
if (adderFunction) {
|
|
42
|
-
adderFunction(properties[key]);
|
|
43
|
-
} else {
|
|
44
|
-
throw "No adder function found for collection: " + key;
|
|
45
|
-
}
|
|
46
|
-
} else {
|
|
47
|
-
// it's a property
|
|
48
|
-
const setterName = propertyNameToSetterName(key);
|
|
49
|
-
const setterFunction = subject[setterName];
|
|
50
|
-
if (setterFunction) {
|
|
51
|
-
setterFunction(properties[key]);
|
|
52
|
-
} else {
|
|
53
|
-
throw "No setter function found for property: " + key;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
});
|
|
57
|
-
}
|
|
1
|
+
type Target = String;
|
|
2
|
+
|
|
3
|
+
export type PropertyValueMap = {
|
|
4
|
+
[property: string]: Target | Target[];
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
export function capitalize(str: string) {
|
|
8
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// e.g. "name" -> "setName"
|
|
12
|
+
export function propertyNameToSetterName(property: string): string {
|
|
13
|
+
return `set${capitalize(property)}`;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function pluralToSingular(plural: string): string {
|
|
17
|
+
if (plural.endsWith("ies")) {
|
|
18
|
+
return plural.slice(0, -3) + "y";
|
|
19
|
+
} else if (plural.endsWith("s")) {
|
|
20
|
+
return plural.slice(0, -1);
|
|
21
|
+
} else {
|
|
22
|
+
return plural;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// e.g. "comments" -> "addComment"
|
|
27
|
+
export function collectionToAdderName(collection: string): string {
|
|
28
|
+
return `add${capitalize(collection)}`;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function collectionToSetterName(collection: string): string {
|
|
32
|
+
return `setCollection${capitalize(collection)}`;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function setProperties(subject: any, properties: PropertyValueMap) {
|
|
36
|
+
Object.keys(properties).forEach((key) => {
|
|
37
|
+
if (Array.isArray(properties[key])) {
|
|
38
|
+
// it's a collection
|
|
39
|
+
const adderName = collectionToAdderName(key);
|
|
40
|
+
const adderFunction = subject[adderName];
|
|
41
|
+
if (adderFunction) {
|
|
42
|
+
adderFunction(properties[key]);
|
|
43
|
+
} else {
|
|
44
|
+
throw "No adder function found for collection: " + key;
|
|
45
|
+
}
|
|
46
|
+
} else {
|
|
47
|
+
// it's a property
|
|
48
|
+
const setterName = propertyNameToSetterName(key);
|
|
49
|
+
const setterFunction = subject[setterName];
|
|
50
|
+
if (setterFunction) {
|
|
51
|
+
setterFunction(properties[key]);
|
|
52
|
+
} else {
|
|
53
|
+
throw "No setter function found for property: " + key;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
}
|
package/src/getProfile.ts
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
import { Ad4mClient } from "@coasys/ad4m";
|
|
2
|
-
// @ts-ignore
|
|
3
|
-
import { getAd4mClient } from "@coasys/ad4m-connect/utils";
|
|
4
|
-
import { LinkExpression } from "@coasys/ad4m";
|
|
5
|
-
|
|
6
|
-
export interface Payload {
|
|
7
|
-
url: string;
|
|
8
|
-
perspectiveUuid: string;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export async function getProfile<T>(did: string, formatter?: (links: LinkExpression[]) => T): Promise<T | any> {
|
|
12
|
-
const cleanedDid = did.replace("did://", "");
|
|
13
|
-
const client: Ad4mClient = await getAd4mClient();
|
|
14
|
-
|
|
15
|
-
const agentPerspective = await client.agent.byDID(cleanedDid);
|
|
16
|
-
|
|
17
|
-
if (agentPerspective) {
|
|
18
|
-
const links = agentPerspective!.perspective!.links;
|
|
19
|
-
|
|
20
|
-
if (formatter) {
|
|
21
|
-
return formatter(links);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
return agentPerspective
|
|
25
|
-
}
|
|
26
|
-
}
|
|
1
|
+
import { Ad4mClient } from "@coasys/ad4m";
|
|
2
|
+
// @ts-ignore
|
|
3
|
+
import { getAd4mClient } from "@coasys/ad4m-connect/utils";
|
|
4
|
+
import { LinkExpression } from "@coasys/ad4m";
|
|
5
|
+
|
|
6
|
+
export interface Payload {
|
|
7
|
+
url: string;
|
|
8
|
+
perspectiveUuid: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export async function getProfile<T>(did: string, formatter?: (links: LinkExpression[]) => T): Promise<T | any> {
|
|
12
|
+
const cleanedDid = did.replace("did://", "");
|
|
13
|
+
const client: Ad4mClient = await getAd4mClient();
|
|
14
|
+
|
|
15
|
+
const agentPerspective = await client.agent.byDID(cleanedDid);
|
|
16
|
+
|
|
17
|
+
if (agentPerspective) {
|
|
18
|
+
const links = agentPerspective!.perspective!.links;
|
|
19
|
+
|
|
20
|
+
if (formatter) {
|
|
21
|
+
return formatter(links);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return agentPerspective
|
|
25
|
+
}
|
|
26
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export * from './cache'
|
|
2
|
-
export * from './getProfile'
|
|
1
|
+
export * from './cache'
|
|
2
|
+
export * from './getProfile'
|
|
3
3
|
export * from './factory'
|