@coasys/ad4m 0.9.0-prerelease → 0.9.0-rc3
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/README.md +2 -2
- package/lib/index.cjs +228 -18
- package/lib/index.js +225 -15
- package/lib/src/perspectives/PerspectiveHandle.d.ts +6 -5
- package/lib/src/perspectives/PerspectiveHandle.js +6 -5
- package/lib/src/perspectives/PerspectiveProxy.d.ts +5 -0
- package/lib/src/perspectives/PerspectiveProxy.js +5 -0
- package/lib/src/subject/SDNADecorators.d.ts +152 -9
- package/lib/src/subject/SDNADecorators.js +115 -5
- package/lib/src/subject/Subject.d.ts +18 -0
- package/lib/src/subject/Subject.js +25 -1
- package/lib/src/subject/SubjectEntity.d.ts +71 -0
- package/lib/src/subject/SubjectEntity.js +73 -3
- package/package.json +4 -2
- package/src/perspectives/PerspectiveHandle.ts +6 -6
- package/src/perspectives/PerspectiveProxy.ts +5 -0
- package/src/subject/SDNADecorators.ts +196 -19
- package/src/subject/Subject.ts +26 -4
- package/src/subject/SubjectEntity.ts +80 -3
|
@@ -22,16 +22,42 @@ export function hasLink(predicate: string): string {
|
|
|
22
22
|
return `triple(this, "${predicate}", _)`
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
interface InstanceQueryParams {
|
|
26
|
-
|
|
27
|
-
|
|
25
|
+
export interface InstanceQueryParams {
|
|
26
|
+
/**
|
|
27
|
+
* An object representing the WHERE clause of the query.
|
|
28
|
+
*/
|
|
29
|
+
where?: object;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* A string representing the condition clause of the query.
|
|
33
|
+
*/
|
|
34
|
+
condition?: string;
|
|
28
35
|
}
|
|
29
36
|
|
|
30
|
-
|
|
37
|
+
/**
|
|
38
|
+
* Decorator for querying instances of a subject class.
|
|
39
|
+
*
|
|
40
|
+
* @category Decorators
|
|
41
|
+
*
|
|
42
|
+
* @description
|
|
43
|
+
* NOTE: Only works on methods that return a promise and will throw an error if not used on a method that returns a promise.
|
|
44
|
+
* This will allow you to query for instances of a subject class with custom clauses within the instance clauses.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* // Usage with where clause
|
|
48
|
+
* InstanceQuery({ where: { name: "John" }}) // this will return all the properties of the subject class with the name "John"
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* // Usage with condition clause
|
|
52
|
+
* InstanceQuery({ condition: "triple(Instance, 'age', Age), Age > 18" }) // this will return all the properties of the subject class with the age greater than 18
|
|
53
|
+
*
|
|
54
|
+
* @param {Object} [options] - Query options.
|
|
55
|
+
*/
|
|
56
|
+
export function InstanceQuery(options?: InstanceQueryParams) {
|
|
31
57
|
return function <T>(target: T, key: keyof T, descriptor: PropertyDescriptor) {
|
|
32
58
|
const originalMethod = descriptor.value;
|
|
33
59
|
if(typeof originalMethod !== "function") {
|
|
34
|
-
throw new Error("
|
|
60
|
+
throw new Error("InstanceQuery decorator can only be applied to methods");
|
|
35
61
|
}
|
|
36
62
|
|
|
37
63
|
descriptor.value = async function(perspective: PerspectiveProxy): Promise<T[]> {
|
|
@@ -70,18 +96,79 @@ export function instanceQuery(options?: InstanceQueryParams) {
|
|
|
70
96
|
}
|
|
71
97
|
|
|
72
98
|
|
|
73
|
-
interface PropertyOptions {
|
|
99
|
+
export interface PropertyOptions {
|
|
100
|
+
/**
|
|
101
|
+
* The predicate of the property. All properties must have this option.
|
|
102
|
+
*/
|
|
74
103
|
through?: string;
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* The initial value of the property. Required if the property is marked as required.
|
|
107
|
+
*/
|
|
108
|
+
initial?: string;
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Indicates whether the property is required. If true, an initial value must be provided.
|
|
112
|
+
*/
|
|
113
|
+
required?: boolean;
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Indicates whether the property is writable. If true, a setter will be available in the prolog engine.
|
|
117
|
+
*/
|
|
118
|
+
writable?: boolean;
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* The language used to store the property. Can be the default `Literal` Language or a custom language address.
|
|
122
|
+
*/
|
|
78
123
|
resolveLanguage?: string;
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Custom getter to get the value of the property in the prolog engine. If not provided, the default getter will be used.
|
|
127
|
+
*/
|
|
79
128
|
getter?: string;
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Custom setter to set the value of the property in the prolog engine. Only available if the property is writable.
|
|
132
|
+
*/
|
|
80
133
|
setter?: string;
|
|
81
|
-
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Indicates whether the property is stored locally in the perspective and not in the network. Useful for properties that are not meant to be shared with the network.
|
|
137
|
+
*/
|
|
138
|
+
local?: boolean;
|
|
82
139
|
}
|
|
83
|
-
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Decorator for defining properties of a subject class.
|
|
143
|
+
*
|
|
144
|
+
* @category Decorators
|
|
145
|
+
*
|
|
146
|
+
* @description
|
|
147
|
+
* This will allow you to define properties with different conditions and how they would be defined in proflog engine.
|
|
148
|
+
*
|
|
149
|
+
* - All properties must have a `through` option which is the predicate of the property.
|
|
150
|
+
* -e If the property is required, it must have an `initial` option which is the initial value of the property.
|
|
151
|
+
* - If the property is writable, it will have a setter in prolog engine. A custom setter can be defined with the `setter` option.
|
|
152
|
+
* - If resolveLanguage is defined, you can use the default `Literal` Language or use your custom language address that can be used to store the property.
|
|
153
|
+
* - If a custom getter is defined, it will be used to get the value of the property in prolog engine. If not, the default getter will be used.
|
|
154
|
+
* - If local is defined, the property will be stored locally in the perspective and not in the network. This is useful for properties that are not meant to be shared with the network
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* // Usage
|
|
158
|
+
* SubjectProperty({ through: "ad4m://name", initial: "John", required: true }) // this will define a property with the name "ad4m://name" and the initial value "John"
|
|
159
|
+
*
|
|
160
|
+
* @param {PropertyOptions} [opts] - Property options.
|
|
161
|
+
*/
|
|
162
|
+
export function SubjectProperty(opts: PropertyOptions) {
|
|
84
163
|
return function <T>(target: T, key: keyof T) {
|
|
164
|
+
if (opts.required && !opts.initial) {
|
|
165
|
+
throw new Error("SubjectProperty requires an 'initial' option if 'required' is true");
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (!opts.through && !opts.getter) {
|
|
169
|
+
throw new Error("SubjectProperty requires either 'through' or 'getter' option")
|
|
170
|
+
}
|
|
171
|
+
|
|
85
172
|
target["__properties"] = target["__properties"] || {};
|
|
86
173
|
target["__properties"][key] = target["__properties"][key] || {};
|
|
87
174
|
target["__properties"][key] = { ...target["__properties"][key], ...opts }
|
|
@@ -95,12 +182,51 @@ export function subjectProperty(opts: PropertyOptions) {
|
|
|
95
182
|
};
|
|
96
183
|
}
|
|
97
184
|
|
|
98
|
-
interface FlagOptions {
|
|
185
|
+
export interface FlagOptions {
|
|
186
|
+
/**
|
|
187
|
+
* The predicate of the property. All properties must have this option.
|
|
188
|
+
*/
|
|
99
189
|
through: string;
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* The value of the property.
|
|
193
|
+
*/
|
|
100
194
|
value: string;
|
|
101
195
|
}
|
|
102
|
-
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Decorator for defining flags of a subject class
|
|
199
|
+
*
|
|
200
|
+
* @category Decorators
|
|
201
|
+
*
|
|
202
|
+
* @description
|
|
203
|
+
* The idea behind flag decorator is to define a property that is required and has an initial value.
|
|
204
|
+
* This will allow you to define a strict instance query. This behaviour can also be achieved with the `SubjectProperty` decorator but the `SubjectFlag` decorator is a shorthand for that.
|
|
205
|
+
*
|
|
206
|
+
* NOTE: Use of Flag is discouraged and should be used only when necessary.
|
|
207
|
+
*
|
|
208
|
+
* - All properties must have a `through` & `initial` option which is the predicate of the property.
|
|
209
|
+
*
|
|
210
|
+
* @example
|
|
211
|
+
* // Usage
|
|
212
|
+
* SubjectFlag({ through: "ad4m://name", value: "John" }) // this will define a flag with the name "ad4m://name" and the initial value "John"
|
|
213
|
+
*
|
|
214
|
+
* @param {FlagOptions} [opts] Flag options.
|
|
215
|
+
*/
|
|
216
|
+
export function SubjectFlag(opts: FlagOptions) {
|
|
103
217
|
return function <T>(target: T, key: keyof T) {
|
|
218
|
+
if (!opts.through && !opts.value) {
|
|
219
|
+
throw new Error("SubjectFlag requires a 'through' and 'value' option")
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
if (!opts.through) {
|
|
223
|
+
throw new Error("SubjectFlag requires a 'through' option")
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if (!opts.value) {
|
|
227
|
+
throw new Error("SubjectFlag requires a 'value' option")
|
|
228
|
+
}
|
|
229
|
+
|
|
104
230
|
target["__properties"] = target["__properties"] || {};
|
|
105
231
|
target["__properties"][key] = target["__properties"][key] || {};
|
|
106
232
|
target["__properties"][key] = {
|
|
@@ -122,13 +248,45 @@ interface WhereOptions {
|
|
|
122
248
|
isInstance?: any
|
|
123
249
|
condition?: string
|
|
124
250
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
251
|
+
|
|
252
|
+
export interface CollectionOptions {
|
|
253
|
+
/**
|
|
254
|
+
* The predicate of the property. All properties must have this option.
|
|
255
|
+
*/
|
|
256
|
+
through: string;
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* An object representing the WHERE clause of the query.
|
|
260
|
+
*/
|
|
261
|
+
where?: WhereOptions;
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Indicates whether the property is stored locally in the perspective and not in the network. Useful for properties that are not meant to be shared with the network.
|
|
265
|
+
*/
|
|
266
|
+
local?: boolean;
|
|
129
267
|
}
|
|
130
268
|
|
|
131
|
-
|
|
269
|
+
/**
|
|
270
|
+
* Decorator for defining collections of a subject class.
|
|
271
|
+
*
|
|
272
|
+
* @category Decorators
|
|
273
|
+
*
|
|
274
|
+
* @description
|
|
275
|
+
* This will allow you to define collections with different conditions and how they would be defined in proflog engine.
|
|
276
|
+
*
|
|
277
|
+
* NOTE: The property needs to be an array for it to picked up during the initialization phase.
|
|
278
|
+
*
|
|
279
|
+
* - All collections must have a `through` option which is the predicate of the collection.
|
|
280
|
+
* - If the collection has a `where` option, it can be used to define a custom condition for the collection.
|
|
281
|
+
* - If the collection has a `local` option, the collection will be stored locally in the perspective and not in the network. This is useful for collections that are not meant to be shared with the network.
|
|
282
|
+
*
|
|
283
|
+
* @example
|
|
284
|
+
* // Usage
|
|
285
|
+
* SubjectCollection({ through: "ad4m://friends" }) // this will define a collection with the name "ad4m://friends"
|
|
286
|
+
*
|
|
287
|
+
* @param opts Collection options.
|
|
288
|
+
*/
|
|
289
|
+
export function SubjectCollection(opts: CollectionOptions) {
|
|
132
290
|
return function <T>(target: T, key: keyof T) {
|
|
133
291
|
target["__collections"] = target["__collections"] || {};
|
|
134
292
|
target["__collections"][key] = opts;
|
|
@@ -152,10 +310,29 @@ export function makeRandomPrologAtom(length: number): string {
|
|
|
152
310
|
return result;
|
|
153
311
|
}
|
|
154
312
|
|
|
155
|
-
interface SDNAClassOptions {
|
|
313
|
+
export interface SDNAClassOptions {
|
|
314
|
+
/**
|
|
315
|
+
* The name of the entity.
|
|
316
|
+
*/
|
|
156
317
|
name: string;
|
|
157
318
|
}
|
|
158
319
|
|
|
320
|
+
/**
|
|
321
|
+
* Decorator for defining an SDNA class.
|
|
322
|
+
*
|
|
323
|
+
* @category Decorators
|
|
324
|
+
*
|
|
325
|
+
* @description
|
|
326
|
+
* This will create a new SDNA class with the given name and add custom methods to generate the SDNA for the class, for this to work the class need to have the properties defined using the decorators like `SubjectProperty`.
|
|
327
|
+
*
|
|
328
|
+
* Note: This decorator is required for the class to be picked up during the initialization phase.
|
|
329
|
+
*
|
|
330
|
+
* @example
|
|
331
|
+
* // Usage
|
|
332
|
+
* SDNAClass({ name: "Person" }) // this will create a new SDNA class with the name "Person"
|
|
333
|
+
*
|
|
334
|
+
* @param opts SDNA class options.
|
|
335
|
+
*/
|
|
159
336
|
export function SDNAClass(opts: SDNAClassOptions) {
|
|
160
337
|
return function (target: any) {
|
|
161
338
|
target.prototype.className = opts.name;
|
package/src/subject/Subject.ts
CHANGED
|
@@ -1,31 +1,50 @@
|
|
|
1
1
|
import { PerspectiveProxy } from "../perspectives/PerspectiveProxy";
|
|
2
2
|
import { collectionSetterToName, collectionToAdderName, collectionToRemoverName, collectionToSetterName, propertyNameToSetterName } from "./util";
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Represents a subject in the perspective.
|
|
6
|
+
* A subject is an entity that has properties and collections.
|
|
7
|
+
*/
|
|
4
8
|
export class Subject {
|
|
5
9
|
#baseExpression: string;
|
|
6
10
|
#subjectClass: string;
|
|
7
11
|
#perspective: PerspectiveProxy
|
|
8
12
|
|
|
13
|
+
/**
|
|
14
|
+
* Constructs a new subject.
|
|
15
|
+
* @param perspective - The perspective that the subject belongs to.
|
|
16
|
+
* @param baseExpression - The base expression of the subject.
|
|
17
|
+
* @param subjectClass - The class of the subject.
|
|
18
|
+
*/
|
|
9
19
|
constructor(perspective: PerspectiveProxy, baseExpression: string, subjectClass: string) {
|
|
10
20
|
this.#baseExpression = baseExpression
|
|
11
21
|
this.#subjectClass = subjectClass
|
|
12
22
|
this.#perspective = perspective
|
|
13
23
|
}
|
|
14
24
|
|
|
25
|
+
/**
|
|
26
|
+
* Gets the base expression of the subject.
|
|
27
|
+
*/
|
|
15
28
|
get baseExpression() {
|
|
16
29
|
return this.#baseExpression
|
|
17
30
|
}
|
|
18
31
|
|
|
32
|
+
/**
|
|
33
|
+
* Initializes the subject by validating it and defining its properties and collections dynamically.
|
|
34
|
+
*
|
|
35
|
+
* NOTE: This method should be called before using the subject. All the properties and collections of the subject defined are not type-checked.
|
|
36
|
+
*/
|
|
19
37
|
async init() {
|
|
38
|
+
// Check if the subject is a valid instance of the subject class
|
|
20
39
|
let isInstance = await this.#perspective.isSubjectInstance(this.#baseExpression, this.#subjectClass)
|
|
21
40
|
if(!isInstance) {
|
|
22
41
|
throw `Not a valid subject instance of ${this.#subjectClass} for ${this.#baseExpression}`
|
|
23
42
|
}
|
|
24
43
|
|
|
44
|
+
// Define properties and collections dynamically
|
|
25
45
|
let results = await this.#perspective.infer(`subject_class("${this.#subjectClass}", C), property(C, Property)`)
|
|
26
46
|
let properties = results.map(result => result.Property)
|
|
27
47
|
|
|
28
|
-
|
|
29
48
|
for(let p of properties) {
|
|
30
49
|
const resolveExpressionURI = await this.#perspective.infer(`subject_class("${this.#subjectClass}", C), property_resolve(C, "${p}")`)
|
|
31
50
|
Object.defineProperty(this, p, {
|
|
@@ -61,10 +80,9 @@ export class Subject {
|
|
|
61
80
|
})
|
|
62
81
|
}
|
|
63
82
|
|
|
64
|
-
|
|
83
|
+
// Define setters
|
|
65
84
|
const setters = await this.#perspective.infer(`subject_class("${this.#subjectClass}", C), property_setter(C, Property, Setter)`)
|
|
66
|
-
|
|
67
|
-
//console.log("Subject setters: " + setters.map(setter => setter.Property))
|
|
85
|
+
|
|
68
86
|
for(let setter of (setters ? setters : [])) {
|
|
69
87
|
if(setter) {
|
|
70
88
|
const property = setter.Property
|
|
@@ -83,6 +101,7 @@ export class Subject {
|
|
|
83
101
|
}
|
|
84
102
|
}
|
|
85
103
|
|
|
104
|
+
// Define collections
|
|
86
105
|
let results2 = await this.#perspective.infer(`subject_class("${this.#subjectClass}", C), collection(C, Collection)`)
|
|
87
106
|
if(!results2) results2 = []
|
|
88
107
|
let collections = results2.map(result => result.Collection)
|
|
@@ -102,6 +121,7 @@ export class Subject {
|
|
|
102
121
|
})
|
|
103
122
|
}
|
|
104
123
|
|
|
124
|
+
// Define collection adders
|
|
105
125
|
let adders = await this.#perspective.infer(`subject_class("${this.#subjectClass}", C), collection_adder(C, Collection, Adder)`)
|
|
106
126
|
if(!adders) adders = []
|
|
107
127
|
|
|
@@ -119,6 +139,7 @@ export class Subject {
|
|
|
119
139
|
}
|
|
120
140
|
}
|
|
121
141
|
|
|
142
|
+
// Define collection removers
|
|
122
143
|
let removers = await this.#perspective.infer(`subject_class("${this.#subjectClass}", C), collection_remover(C, Collection, Remover)`)
|
|
123
144
|
if(!removers) removers = []
|
|
124
145
|
|
|
@@ -136,6 +157,7 @@ export class Subject {
|
|
|
136
157
|
}
|
|
137
158
|
}
|
|
138
159
|
|
|
160
|
+
// Define collection setters
|
|
139
161
|
let collectionSetters = await this.#perspective.infer(`subject_class("${this.#subjectClass}", C), collection_setter(C, Collection, Setter)`)
|
|
140
162
|
if(!collectionSetters) collectionSetters = []
|
|
141
163
|
|
|
@@ -9,6 +9,10 @@ export type QueryPartialEntity<T> = {
|
|
|
9
9
|
};
|
|
10
10
|
|
|
11
11
|
|
|
12
|
+
/**
|
|
13
|
+
* Class representing a subject entity.
|
|
14
|
+
* Can extend this class to create a new subject entity to add methods interact with SDNA and much better experience then using the bare bone methods.
|
|
15
|
+
*/
|
|
12
16
|
export class SubjectEntity {
|
|
13
17
|
#baseExpression: string;
|
|
14
18
|
#subjectClass: string;
|
|
@@ -17,12 +21,22 @@ export class SubjectEntity {
|
|
|
17
21
|
author: string;
|
|
18
22
|
timestamp: string;
|
|
19
23
|
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Constructs a new subject.
|
|
27
|
+
* @param perspective - The perspective that the subject belongs to.
|
|
28
|
+
* @param baseExpression - The base expression of the subject.
|
|
29
|
+
* @param soruce - The source of the subject, the expression this instance is linked too.
|
|
30
|
+
*/
|
|
20
31
|
constructor(perspective: PerspectiveProxy, baseExpression?: string, source?: string) {
|
|
21
32
|
this.#baseExpression = baseExpression ? baseExpression : Literal.from(makeRandomPrologAtom(24)).toUrl();
|
|
22
33
|
this.#perspective = perspective;
|
|
23
34
|
this.#source = source || "ad4m://self";
|
|
24
35
|
}
|
|
25
36
|
|
|
37
|
+
/**
|
|
38
|
+
* Gets the base expression of the subject.
|
|
39
|
+
*/
|
|
26
40
|
get baseExpression() {
|
|
27
41
|
return this.#baseExpression
|
|
28
42
|
}
|
|
@@ -155,6 +169,17 @@ export class SubjectEntity {
|
|
|
155
169
|
}
|
|
156
170
|
}
|
|
157
171
|
|
|
172
|
+
/**
|
|
173
|
+
* Save the subject entity.
|
|
174
|
+
* This method will create a new subject with the base expression and add a new link from the source to the base expression with the predicate "ad4m://has_child".
|
|
175
|
+
*
|
|
176
|
+
* If a property has an action, it will perform the action (Only for collections).
|
|
177
|
+
* If a property is an array and is not empty, it will set the collection.
|
|
178
|
+
* If a property is not undefined, not null, and not an empty string, it will set the property.
|
|
179
|
+
*
|
|
180
|
+
*
|
|
181
|
+
* @throws Will throw an error if the subject entity cannot be converted to a subject class, or if the subject cannot be created, or if the link cannot be added, or if the subject entity cannot be updated.
|
|
182
|
+
*/
|
|
158
183
|
async save() {
|
|
159
184
|
this.#subjectClass = await this.#perspective.stringOrTemplateObjectToSubjectClass(this)
|
|
160
185
|
|
|
@@ -171,13 +196,24 @@ export class SubjectEntity {
|
|
|
171
196
|
await this.update()
|
|
172
197
|
}
|
|
173
198
|
|
|
199
|
+
/**
|
|
200
|
+
* Update the subject entity.
|
|
201
|
+
*
|
|
202
|
+
* It will iterate over the properties of the subject entity.
|
|
203
|
+
*
|
|
204
|
+
* If a property has an action, it will perform the action (Only for collections).
|
|
205
|
+
* If a property is an array and is not empty, it will set the collection.
|
|
206
|
+
* If a property is not undefined, not null, and not an empty string, it will set the property.
|
|
207
|
+
*
|
|
208
|
+
* @throws Will throw an error if the subject entity cannot be converted to a subject class, or if a property cannot be set, or if a collection cannot be set, or if the data of the subject entity cannot be gotten.
|
|
209
|
+
*/
|
|
174
210
|
async update() {
|
|
175
211
|
this.#subjectClass = await this.#perspective.stringOrTemplateObjectToSubjectClass(this)
|
|
176
212
|
|
|
177
213
|
const entries = Object.entries(this);
|
|
178
214
|
|
|
179
215
|
for (const [key, value] of entries) {
|
|
180
|
-
if (value) {
|
|
216
|
+
if (value !== undefined && value !== null) {
|
|
181
217
|
if (value?.action) {
|
|
182
218
|
switch (value.action) {
|
|
183
219
|
case 'setter':
|
|
@@ -194,7 +230,7 @@ export class SubjectEntity {
|
|
|
194
230
|
}
|
|
195
231
|
} else if (Array.isArray(value) && value.length > 0) {
|
|
196
232
|
await this.setCollectionSetter(key, value)
|
|
197
|
-
} else {
|
|
233
|
+
} else if (value !== undefined && value !== null && value !== "") {
|
|
198
234
|
await this.setProperty(key, value);
|
|
199
235
|
}
|
|
200
236
|
}
|
|
@@ -203,17 +239,41 @@ export class SubjectEntity {
|
|
|
203
239
|
await this.getData();
|
|
204
240
|
}
|
|
205
241
|
|
|
242
|
+
/**
|
|
243
|
+
* Get the subject entity with all the properties & collection populated.
|
|
244
|
+
*
|
|
245
|
+
* @returns The subject entity.
|
|
246
|
+
*
|
|
247
|
+
* @throws Will throw an error if the subject entity cannot be converted to a subject class, or if the data of the subject entity cannot be gotten.
|
|
248
|
+
*/
|
|
206
249
|
async get() {
|
|
207
250
|
this.#subjectClass = await this.#perspective.stringOrTemplateObjectToSubjectClass(this)
|
|
208
251
|
|
|
209
252
|
return await this.getData()
|
|
210
253
|
}
|
|
211
254
|
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Delete the subject entity.
|
|
258
|
+
* This method will remove the subject from the perspective.
|
|
259
|
+
*
|
|
260
|
+
* @throws Will throw an error if the subject entity cannot be removed.
|
|
261
|
+
*/
|
|
212
262
|
async delete() {
|
|
213
263
|
await this.#perspective.removeSubject(this, this.#baseExpression);
|
|
214
264
|
}
|
|
215
265
|
|
|
216
|
-
|
|
266
|
+
/**
|
|
267
|
+
* Get all the subject entities of the subject class.
|
|
268
|
+
*
|
|
269
|
+
* NOTE: this is a static method and should be called on the class itself.
|
|
270
|
+
*
|
|
271
|
+
* @param perspective - The perspective that the subject belongs to.
|
|
272
|
+
*
|
|
273
|
+
* @returns The subject entities.
|
|
274
|
+
*
|
|
275
|
+
* @throws Will throw an error if the subject entity cannot be converted to a subject class, or if the subject proxies cannot be gotten.
|
|
276
|
+
*/
|
|
217
277
|
static async all(perspective: PerspectiveProxy) {
|
|
218
278
|
let subjectClass = await perspective.stringOrTemplateObjectToSubjectClass(this)
|
|
219
279
|
const proxies = await perspective.getAllSubjectProxies(subjectClass)
|
|
@@ -233,6 +293,18 @@ export class SubjectEntity {
|
|
|
233
293
|
return []
|
|
234
294
|
}
|
|
235
295
|
|
|
296
|
+
/**
|
|
297
|
+
* Query the subject entities of the subject class.
|
|
298
|
+
*
|
|
299
|
+
* NOTE: this is a static method and should be called on the class itself.
|
|
300
|
+
*
|
|
301
|
+
* @param perspective - The perspective that the subject belongs to.
|
|
302
|
+
* @param query - The query of the subject entities.
|
|
303
|
+
*
|
|
304
|
+
* @returns The subject entities.
|
|
305
|
+
*
|
|
306
|
+
* @throws Will throw an error if the subject entity cannot be converted to a subject class, or if the query cannot be inferred, or if the data of the subject entities cannot be gotten.
|
|
307
|
+
*/
|
|
236
308
|
static async query(perspective: PerspectiveProxy, query?: SubjectEntityQueryParam) {
|
|
237
309
|
const source = query?.source || "ad4m://self";
|
|
238
310
|
let subjectClass = await perspective.stringOrTemplateObjectToSubjectClass(this)
|
|
@@ -286,7 +358,12 @@ export type SubjectArray<T> = T[] | {
|
|
|
286
358
|
}
|
|
287
359
|
|
|
288
360
|
export type SubjectEntityQueryParam = {
|
|
361
|
+
// The source of the query.
|
|
289
362
|
source?: string;
|
|
363
|
+
|
|
364
|
+
// The size of the query.
|
|
290
365
|
size?: number;
|
|
366
|
+
|
|
367
|
+
// The page of the query.
|
|
291
368
|
page?: number;
|
|
292
369
|
}
|