@coasys/hooks-helpers 0.13.0-test-3 → 0.13.0-test-8

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 CHANGED
@@ -24,5 +24,5 @@
24
24
  "@coasys/ad4m": "*"
25
25
  }
26
26
  },
27
- "version": "0.13.0-test-3"
27
+ "version": "0.13.0-test-8"
28
28
  }
package/src/index.ts CHANGED
@@ -1,3 +1,2 @@
1
1
  export * from './cache'
2
- export * from './getProfile'
3
- export * from './factory'
2
+ export * from './getProfile'
@@ -1,249 +0,0 @@
1
- import {
2
- PerspectiveProxy,
3
- Link,
4
- Subject,
5
- Literal,
6
- LinkQuery,
7
- getPropertiesMetadata,
8
- } from "@coasys/ad4m";
9
- import { setProperties } from "./model";
10
- import { v4 as uuidv4 } from "uuid";
11
-
12
- export const SELF = "ad4m://self";
13
-
14
- export type ModelProps = {
15
- perspective: PerspectiveProxy;
16
- source?: string;
17
- };
18
-
19
- export class SubjectRepository<SubjectClass extends { [x: string]: any }> {
20
- source = SELF;
21
- subject: SubjectClass | string;
22
- perspective: PerspectiveProxy;
23
- tempSubject: any | string;
24
-
25
- constructor(subject: { new (): SubjectClass } | string, props: ModelProps) {
26
- this.perspective = props.perspective;
27
- this.source = props.source || this.source;
28
- this.subject = typeof subject === "string" ? subject : new subject();
29
- this.tempSubject = subject;
30
- }
31
-
32
- get className(): string {
33
- return typeof this.subject === "string"
34
- ? this.subject
35
- : this.subject.className;
36
- }
37
-
38
- async ensureSubject() {
39
- if (typeof this.tempSubject === "string") return;
40
- await this.perspective.ensureSDNASubjectClass(this.tempSubject);
41
- }
42
-
43
- async create(
44
- data: SubjectClass,
45
- id?: string,
46
- source?: string
47
- ): Promise<SubjectClass> {
48
- await this.ensureSubject();
49
- const base = id || Literal.from(uuidv4()).toUrl();
50
-
51
- let newInstance = await this.perspective.createSubject(this.subject, base);
52
-
53
- if (!newInstance) {
54
- throw "Failed to create new instance of " + this.subject;
55
- }
56
-
57
- // Connect new instance to source
58
- await this.perspective.add(
59
- new Link({
60
- source: source || this.source,
61
- predicate: "ad4m://has_child",
62
- target: base,
63
- })
64
- );
65
-
66
- Object.keys(data).forEach((key) =>
67
- data[key] === undefined || data[key] === null ? delete data[key] : {}
68
- );
69
-
70
- setProperties(newInstance, data);
71
-
72
- // @ts-ignore
73
- return this.getSubjectData(newInstance);
74
- }
75
-
76
- async update(id: string, data: QueryPartialEntity<SubjectClass>) {
77
- const instance = await this.get(id);
78
-
79
- if (!instance) {
80
- throw "Failed to find instance of " + this.subject + " with id " + id;
81
- }
82
-
83
- Object.keys(data).forEach((key) =>
84
- data[key] === undefined ? delete data[key] : {}
85
- );
86
-
87
- // @ts-ignore
88
- setProperties(instance, data);
89
-
90
- return this.getSubjectData(instance);
91
- }
92
-
93
- async remove(id: string) {
94
- if (this.perspective) {
95
- const linksTo = await this.perspective.get(new LinkQuery({ target: id }));
96
- const linksFrom = await this.perspective.get(
97
- new LinkQuery({ source: id })
98
- );
99
- this.perspective.removeLinks([...linksFrom, ...linksTo]);
100
- }
101
- }
102
-
103
- async get(id: string): Promise<SubjectClass | null> {
104
- if (id) {
105
- await this.ensureSubject();
106
- const subjectProxy = await this.perspective.getSubjectProxy(
107
- id,
108
- this.subject
109
- );
110
-
111
- // @ts-ignore
112
- return subjectProxy || null;
113
- } else {
114
- const all = await this.getAll();
115
- return all[0] || null;
116
- }
117
- }
118
-
119
- async getData(id: string): Promise<SubjectClass | string | null> {
120
- const entry = await this.get(id);
121
- if (entry) {
122
- // @ts-ignore
123
- return await this.getSubjectData(entry);
124
- }
125
-
126
- return null;
127
- }
128
-
129
- private async getSubjectData(entry: any) {
130
- let links = await this.perspective.get(
131
- new LinkQuery({ source: entry.id })
132
- );
133
-
134
- let data: any = await this.perspective.getSubjectData(this.subject, entry.id)
135
-
136
- for (const key in data) {
137
- const propsMeta = getPropertiesMetadata(this.tempSubject);
138
- const transform = propsMeta[key]?.transform;
139
- if (transform) {
140
- data[key] = transform(data[key]);
141
- }
142
- }
143
-
144
- return {
145
- id: entry.id,
146
- timestamp: links[0].timestamp,
147
- author: links[0].author,
148
- ...data,
149
- }
150
- }
151
-
152
- async getAll(source?: string, query?: QueryOptions): Promise<SubjectClass[]> {
153
- await this.ensureSubject();
154
-
155
- const tempSource = source || this.source;
156
-
157
- let res = [];
158
-
159
- if (query) {
160
- try {
161
- const queryResponse = (
162
- await this.perspective.infer(
163
- `findall([Timestamp, Base], (subject_class("${this.className}", C), instance(C, Base), link("${tempSource}", Predicate, Base, Timestamp, Author)), AllData), length(AllData, DataLength), sort(AllData, SortedData).`
164
- )
165
- )[0];
166
-
167
- if (queryResponse.SortedData >= query.size) {
168
- const isOutofBound =
169
- query.size * query.page > queryResponse.DataLength;
170
-
171
- const newPageSize = isOutofBound
172
- ? queryResponse.DataLength - query.size * (query.page - 1)
173
- : query.size;
174
-
175
- 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).`;
176
- res = await this.perspective.infer(mainQuery);
177
-
178
- //@ts-ignore
179
- res = res[0].PageData.map((r) => ({
180
- Base: r[1],
181
- Timestamp: r[0],
182
- }));
183
- } else {
184
- res = await this.perspective.infer(
185
- `subject_class("${this.className}", C), instance(C, Base), triple("${tempSource}", Predicate, Base).`
186
- );
187
- }
188
- } catch (e) {
189
- console.log("Query failed", e);
190
- }
191
- } else {
192
- res = await this.perspective.infer(
193
- `subject_class("${this.className}", C), instance(C, Base), triple("${tempSource}", Predicate, Base).`
194
- );
195
- }
196
-
197
- const results =
198
- res &&
199
- res.filter(
200
- //@ts-ignore
201
- (obj, index, self) => index === self.findIndex((t) => t.Base === obj.Base)
202
- );
203
-
204
- if (!res) return [];
205
-
206
- const data = await Promise.all(
207
- results.map(async (result: any) => {
208
- let subject = new Subject(
209
- this.perspective!,
210
- //@ts-ignore
211
- result.Base,
212
- this.className
213
- );
214
-
215
- await subject.init();
216
-
217
- return subject;
218
- })
219
- );
220
-
221
- // @ts-ignore
222
- return data;
223
- }
224
-
225
- async getAllData(
226
- source?: string,
227
- query?: QueryOptions
228
- ): Promise<SubjectClass[]> {
229
- const subjects = await this.getAll(source, query);
230
-
231
- const entries = await Promise.all(
232
- subjects.map((e) => this.getSubjectData(e))
233
- );
234
-
235
- // @ts-ignore
236
- return entries;
237
- }
238
- }
239
-
240
- export type QueryPartialEntity<T> = {
241
- [P in keyof T]?: T[P] | (() => string);
242
- };
243
-
244
- export type QueryOptions = {
245
- page: number;
246
- size: number;
247
- infinite: boolean;
248
- uniqueKey: string;
249
- };
@@ -1 +0,0 @@
1
- export * from "./SubjectRepository";