@coasys/ad4m 0.9.0-prerelease → 0.9.0-rc2
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
|
@@ -13,11 +13,30 @@ export function addLink(source, predicate, target) {
|
|
|
13
13
|
export function hasLink(predicate) {
|
|
14
14
|
return `triple(this, "${predicate}", _)`;
|
|
15
15
|
}
|
|
16
|
-
|
|
16
|
+
/**
|
|
17
|
+
* Decorator for querying instances of a subject class.
|
|
18
|
+
*
|
|
19
|
+
* @category Decorators
|
|
20
|
+
*
|
|
21
|
+
* @description
|
|
22
|
+
* NOTE: Only works on methods that return a promise and will throw an error if not used on a method that returns a promise.
|
|
23
|
+
* This will allow you to query for instances of a subject class with custom clauses within the instance clauses.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* // Usage with where clause
|
|
27
|
+
* InstanceQuery({ where: { name: "John" }}) // this will return all the properties of the subject class with the name "John"
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* // Usage with condition clause
|
|
31
|
+
* InstanceQuery({ condition: "triple(Instance, 'age', Age), Age > 18" }) // this will return all the properties of the subject class with the age greater than 18
|
|
32
|
+
*
|
|
33
|
+
* @param {Object} [options] - Query options.
|
|
34
|
+
*/
|
|
35
|
+
export function InstanceQuery(options) {
|
|
17
36
|
return function (target, key, descriptor) {
|
|
18
37
|
const originalMethod = descriptor.value;
|
|
19
38
|
if (typeof originalMethod !== "function") {
|
|
20
|
-
throw new Error("
|
|
39
|
+
throw new Error("InstanceQuery decorator can only be applied to methods");
|
|
21
40
|
}
|
|
22
41
|
descriptor.value = async function (perspective) {
|
|
23
42
|
let instances = [];
|
|
@@ -50,8 +69,35 @@ export function instanceQuery(options) {
|
|
|
50
69
|
};
|
|
51
70
|
};
|
|
52
71
|
}
|
|
53
|
-
|
|
72
|
+
/**
|
|
73
|
+
* Decorator for defining properties of a subject class.
|
|
74
|
+
*
|
|
75
|
+
* @category Decorators
|
|
76
|
+
*
|
|
77
|
+
* @description
|
|
78
|
+
* This will allow you to define properties with different conditions and how they would be defined in proflog engine.
|
|
79
|
+
*
|
|
80
|
+
* - All properties must have a `through` option which is the predicate of the property.
|
|
81
|
+
* -e If the property is required, it must have an `initial` option which is the initial value of the property.
|
|
82
|
+
* - If the property is writable, it will have a setter in prolog engine. A custom setter can be defined with the `setter` option.
|
|
83
|
+
* - 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.
|
|
84
|
+
* - 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.
|
|
85
|
+
* - 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
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* // Usage
|
|
89
|
+
* SubjectProperty({ through: "ad4m://name", initial: "John", required: true }) // this will define a property with the name "ad4m://name" and the initial value "John"
|
|
90
|
+
*
|
|
91
|
+
* @param {PropertyOptions} [opts] - Property options.
|
|
92
|
+
*/
|
|
93
|
+
export function SubjectProperty(opts) {
|
|
54
94
|
return function (target, key) {
|
|
95
|
+
if (opts.required && !opts.initial) {
|
|
96
|
+
throw new Error("SubjectProperty requires an 'initial' option if 'required' is true");
|
|
97
|
+
}
|
|
98
|
+
if (!opts.through && !opts.getter) {
|
|
99
|
+
throw new Error("SubjectProperty requires either 'through' or 'getter' option");
|
|
100
|
+
}
|
|
55
101
|
target["__properties"] = target["__properties"] || {};
|
|
56
102
|
target["__properties"][key] = target["__properties"][key] || {};
|
|
57
103
|
target["__properties"][key] = { ...target["__properties"][key], ...opts };
|
|
@@ -62,8 +108,36 @@ export function subjectProperty(opts) {
|
|
|
62
108
|
Object.defineProperty(target, key, { configurable: true, writable: true });
|
|
63
109
|
};
|
|
64
110
|
}
|
|
65
|
-
|
|
111
|
+
/**
|
|
112
|
+
* Decorator for defining flags of a subject class
|
|
113
|
+
*
|
|
114
|
+
* @category Decorators
|
|
115
|
+
*
|
|
116
|
+
* @description
|
|
117
|
+
* The idea behind flag decorator is to define a property that is required and has an initial value.
|
|
118
|
+
* 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.
|
|
119
|
+
*
|
|
120
|
+
* NOTE: Use of Flag is discouraged and should be used only when necessary.
|
|
121
|
+
*
|
|
122
|
+
* - All properties must have a `through` & `initial` option which is the predicate of the property.
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* // Usage
|
|
126
|
+
* SubjectFlag({ through: "ad4m://name", value: "John" }) // this will define a flag with the name "ad4m://name" and the initial value "John"
|
|
127
|
+
*
|
|
128
|
+
* @param {FlagOptions} [opts] Flag options.
|
|
129
|
+
*/
|
|
130
|
+
export function SubjectFlag(opts) {
|
|
66
131
|
return function (target, key) {
|
|
132
|
+
if (!opts.through && !opts.value) {
|
|
133
|
+
throw new Error("SubjectFlag requires a 'through' and 'value' option");
|
|
134
|
+
}
|
|
135
|
+
if (!opts.through) {
|
|
136
|
+
throw new Error("SubjectFlag requires a 'through' option");
|
|
137
|
+
}
|
|
138
|
+
if (!opts.value) {
|
|
139
|
+
throw new Error("SubjectFlag requires a 'value' option");
|
|
140
|
+
}
|
|
67
141
|
target["__properties"] = target["__properties"] || {};
|
|
68
142
|
target["__properties"][key] = target["__properties"][key] || {};
|
|
69
143
|
target["__properties"][key] = {
|
|
@@ -78,7 +152,27 @@ export function subjectFlag(opts) {
|
|
|
78
152
|
Object.defineProperty(target, key, { configurable: true, writable: true });
|
|
79
153
|
};
|
|
80
154
|
}
|
|
81
|
-
|
|
155
|
+
/**
|
|
156
|
+
* Decorator for defining collections of a subject class.
|
|
157
|
+
*
|
|
158
|
+
* @category Decorators
|
|
159
|
+
*
|
|
160
|
+
* @description
|
|
161
|
+
* This will allow you to define collections with different conditions and how they would be defined in proflog engine.
|
|
162
|
+
*
|
|
163
|
+
* NOTE: The property needs to be an array for it to picked up during the initialization phase.
|
|
164
|
+
*
|
|
165
|
+
* - All collections must have a `through` option which is the predicate of the collection.
|
|
166
|
+
* - If the collection has a `where` option, it can be used to define a custom condition for the collection.
|
|
167
|
+
* - 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.
|
|
168
|
+
*
|
|
169
|
+
* @example
|
|
170
|
+
* // Usage
|
|
171
|
+
* SubjectCollection({ through: "ad4m://friends" }) // this will define a collection with the name "ad4m://friends"
|
|
172
|
+
*
|
|
173
|
+
* @param opts Collection options.
|
|
174
|
+
*/
|
|
175
|
+
export function SubjectCollection(opts) {
|
|
82
176
|
return function (target, key) {
|
|
83
177
|
target["__collections"] = target["__collections"] || {};
|
|
84
178
|
target["__collections"][key] = opts;
|
|
@@ -98,6 +192,22 @@ export function makeRandomPrologAtom(length) {
|
|
|
98
192
|
}
|
|
99
193
|
return result;
|
|
100
194
|
}
|
|
195
|
+
/**
|
|
196
|
+
* Decorator for defining an SDNA class.
|
|
197
|
+
*
|
|
198
|
+
* @category Decorators
|
|
199
|
+
*
|
|
200
|
+
* @description
|
|
201
|
+
* 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`.
|
|
202
|
+
*
|
|
203
|
+
* Note: This decorator is required for the class to be picked up during the initialization phase.
|
|
204
|
+
*
|
|
205
|
+
* @example
|
|
206
|
+
* // Usage
|
|
207
|
+
* SDNAClass({ name: "Person" }) // this will create a new SDNA class with the name "Person"
|
|
208
|
+
*
|
|
209
|
+
* @param opts SDNA class options.
|
|
210
|
+
*/
|
|
101
211
|
export function SDNAClass(opts) {
|
|
102
212
|
return function (target) {
|
|
103
213
|
target.prototype.className = opts.name;
|
|
@@ -1,7 +1,25 @@
|
|
|
1
1
|
import { PerspectiveProxy } from "../perspectives/PerspectiveProxy";
|
|
2
|
+
/**
|
|
3
|
+
* Represents a subject in the perspective.
|
|
4
|
+
* A subject is an entity that has properties and collections.
|
|
5
|
+
*/
|
|
2
6
|
export declare class Subject {
|
|
3
7
|
#private;
|
|
8
|
+
/**
|
|
9
|
+
* Constructs a new subject.
|
|
10
|
+
* @param perspective - The perspective that the subject belongs to.
|
|
11
|
+
* @param baseExpression - The base expression of the subject.
|
|
12
|
+
* @param subjectClass - The class of the subject.
|
|
13
|
+
*/
|
|
4
14
|
constructor(perspective: PerspectiveProxy, baseExpression: string, subjectClass: string);
|
|
15
|
+
/**
|
|
16
|
+
* Gets the base expression of the subject.
|
|
17
|
+
*/
|
|
5
18
|
get baseExpression(): string;
|
|
19
|
+
/**
|
|
20
|
+
* Initializes the subject by validating it and defining its properties and collections dynamically.
|
|
21
|
+
*
|
|
22
|
+
* NOTE: This method should be called before using the subject. All the properties and collections of the subject defined are not type-checked.
|
|
23
|
+
*/
|
|
6
24
|
init(): Promise<void>;
|
|
7
25
|
}
|
|
@@ -11,7 +11,17 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
11
11
|
};
|
|
12
12
|
var _Subject_baseExpression, _Subject_subjectClass, _Subject_perspective;
|
|
13
13
|
import { collectionToAdderName, collectionToRemoverName, collectionToSetterName, propertyNameToSetterName } from "./util";
|
|
14
|
+
/**
|
|
15
|
+
* Represents a subject in the perspective.
|
|
16
|
+
* A subject is an entity that has properties and collections.
|
|
17
|
+
*/
|
|
14
18
|
export class Subject {
|
|
19
|
+
/**
|
|
20
|
+
* Constructs a new subject.
|
|
21
|
+
* @param perspective - The perspective that the subject belongs to.
|
|
22
|
+
* @param baseExpression - The base expression of the subject.
|
|
23
|
+
* @param subjectClass - The class of the subject.
|
|
24
|
+
*/
|
|
15
25
|
constructor(perspective, baseExpression, subjectClass) {
|
|
16
26
|
_Subject_baseExpression.set(this, void 0);
|
|
17
27
|
_Subject_subjectClass.set(this, void 0);
|
|
@@ -20,14 +30,24 @@ export class Subject {
|
|
|
20
30
|
__classPrivateFieldSet(this, _Subject_subjectClass, subjectClass, "f");
|
|
21
31
|
__classPrivateFieldSet(this, _Subject_perspective, perspective, "f");
|
|
22
32
|
}
|
|
33
|
+
/**
|
|
34
|
+
* Gets the base expression of the subject.
|
|
35
|
+
*/
|
|
23
36
|
get baseExpression() {
|
|
24
37
|
return __classPrivateFieldGet(this, _Subject_baseExpression, "f");
|
|
25
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* Initializes the subject by validating it and defining its properties and collections dynamically.
|
|
41
|
+
*
|
|
42
|
+
* NOTE: This method should be called before using the subject. All the properties and collections of the subject defined are not type-checked.
|
|
43
|
+
*/
|
|
26
44
|
async init() {
|
|
45
|
+
// Check if the subject is a valid instance of the subject class
|
|
27
46
|
let isInstance = await __classPrivateFieldGet(this, _Subject_perspective, "f").isSubjectInstance(__classPrivateFieldGet(this, _Subject_baseExpression, "f"), __classPrivateFieldGet(this, _Subject_subjectClass, "f"));
|
|
28
47
|
if (!isInstance) {
|
|
29
48
|
throw `Not a valid subject instance of ${__classPrivateFieldGet(this, _Subject_subjectClass, "f")} for ${__classPrivateFieldGet(this, _Subject_baseExpression, "f")}`;
|
|
30
49
|
}
|
|
50
|
+
// Define properties and collections dynamically
|
|
31
51
|
let results = await __classPrivateFieldGet(this, _Subject_perspective, "f").infer(`subject_class("${__classPrivateFieldGet(this, _Subject_subjectClass, "f")}", C), property(C, Property)`);
|
|
32
52
|
let properties = results.map(result => result.Property);
|
|
33
53
|
for (let p of properties) {
|
|
@@ -70,8 +90,8 @@ export class Subject {
|
|
|
70
90
|
}
|
|
71
91
|
});
|
|
72
92
|
}
|
|
93
|
+
// Define setters
|
|
73
94
|
const setters = await __classPrivateFieldGet(this, _Subject_perspective, "f").infer(`subject_class("${__classPrivateFieldGet(this, _Subject_subjectClass, "f")}", C), property_setter(C, Property, Setter)`);
|
|
74
|
-
//console.log("Subject setters: " + setters.map(setter => setter.Property))
|
|
75
95
|
for (let setter of (setters ? setters : [])) {
|
|
76
96
|
if (setter) {
|
|
77
97
|
const property = setter.Property;
|
|
@@ -89,6 +109,7 @@ export class Subject {
|
|
|
89
109
|
};
|
|
90
110
|
}
|
|
91
111
|
}
|
|
112
|
+
// Define collections
|
|
92
113
|
let results2 = await __classPrivateFieldGet(this, _Subject_perspective, "f").infer(`subject_class("${__classPrivateFieldGet(this, _Subject_subjectClass, "f")}", C), collection(C, Collection)`);
|
|
93
114
|
if (!results2)
|
|
94
115
|
results2 = [];
|
|
@@ -108,6 +129,7 @@ export class Subject {
|
|
|
108
129
|
}
|
|
109
130
|
});
|
|
110
131
|
}
|
|
132
|
+
// Define collection adders
|
|
111
133
|
let adders = await __classPrivateFieldGet(this, _Subject_perspective, "f").infer(`subject_class("${__classPrivateFieldGet(this, _Subject_subjectClass, "f")}", C), collection_adder(C, Collection, Adder)`);
|
|
112
134
|
if (!adders)
|
|
113
135
|
adders = [];
|
|
@@ -125,6 +147,7 @@ export class Subject {
|
|
|
125
147
|
};
|
|
126
148
|
}
|
|
127
149
|
}
|
|
150
|
+
// Define collection removers
|
|
128
151
|
let removers = await __classPrivateFieldGet(this, _Subject_perspective, "f").infer(`subject_class("${__classPrivateFieldGet(this, _Subject_subjectClass, "f")}", C), collection_remover(C, Collection, Remover)`);
|
|
129
152
|
if (!removers)
|
|
130
153
|
removers = [];
|
|
@@ -142,6 +165,7 @@ export class Subject {
|
|
|
142
165
|
};
|
|
143
166
|
}
|
|
144
167
|
}
|
|
168
|
+
// Define collection setters
|
|
145
169
|
let collectionSetters = await __classPrivateFieldGet(this, _Subject_perspective, "f").infer(`subject_class("${__classPrivateFieldGet(this, _Subject_subjectClass, "f")}", C), collection_setter(C, Collection, Setter)`);
|
|
146
170
|
if (!collectionSetters)
|
|
147
171
|
collectionSetters = [];
|
|
@@ -2,22 +2,93 @@ import { PerspectiveProxy } from "../perspectives/PerspectiveProxy";
|
|
|
2
2
|
export type QueryPartialEntity<T> = {
|
|
3
3
|
[P in keyof T]?: T[P] | (() => string);
|
|
4
4
|
};
|
|
5
|
+
/**
|
|
6
|
+
* Class representing a subject entity.
|
|
7
|
+
* 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.
|
|
8
|
+
*/
|
|
5
9
|
export declare class SubjectEntity {
|
|
6
10
|
#private;
|
|
7
11
|
author: string;
|
|
8
12
|
timestamp: string;
|
|
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 soruce - The source of the subject, the expression this instance is linked too.
|
|
18
|
+
*/
|
|
9
19
|
constructor(perspective: PerspectiveProxy, baseExpression?: string, source?: string);
|
|
20
|
+
/**
|
|
21
|
+
* Gets the base expression of the subject.
|
|
22
|
+
*/
|
|
10
23
|
get baseExpression(): string;
|
|
11
24
|
private getData;
|
|
12
25
|
private setProperty;
|
|
13
26
|
private setCollectionSetter;
|
|
14
27
|
private setCollectionAdder;
|
|
15
28
|
private setCollectionRemover;
|
|
29
|
+
/**
|
|
30
|
+
* Save the subject entity.
|
|
31
|
+
* 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".
|
|
32
|
+
*
|
|
33
|
+
* If a property has an action, it will perform the action (Only for collections).
|
|
34
|
+
* If a property is an array and is not empty, it will set the collection.
|
|
35
|
+
* If a property is not undefined, not null, and not an empty string, it will set the property.
|
|
36
|
+
*
|
|
37
|
+
*
|
|
38
|
+
* @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.
|
|
39
|
+
*/
|
|
16
40
|
save(): Promise<void>;
|
|
41
|
+
/**
|
|
42
|
+
* Update the subject entity.
|
|
43
|
+
*
|
|
44
|
+
* It will iterate over the properties of the subject entity.
|
|
45
|
+
*
|
|
46
|
+
* If a property has an action, it will perform the action (Only for collections).
|
|
47
|
+
* If a property is an array and is not empty, it will set the collection.
|
|
48
|
+
* If a property is not undefined, not null, and not an empty string, it will set the property.
|
|
49
|
+
*
|
|
50
|
+
* @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.
|
|
51
|
+
*/
|
|
17
52
|
update(): Promise<void>;
|
|
53
|
+
/**
|
|
54
|
+
* Get the subject entity with all the properties & collection populated.
|
|
55
|
+
*
|
|
56
|
+
* @returns The subject entity.
|
|
57
|
+
*
|
|
58
|
+
* @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.
|
|
59
|
+
*/
|
|
18
60
|
get(): Promise<this>;
|
|
61
|
+
/**
|
|
62
|
+
* Delete the subject entity.
|
|
63
|
+
* This method will remove the subject from the perspective.
|
|
64
|
+
*
|
|
65
|
+
* @throws Will throw an error if the subject entity cannot be removed.
|
|
66
|
+
*/
|
|
19
67
|
delete(): Promise<void>;
|
|
68
|
+
/**
|
|
69
|
+
* Get all the subject entities of the subject class.
|
|
70
|
+
*
|
|
71
|
+
* NOTE: this is a static method and should be called on the class itself.
|
|
72
|
+
*
|
|
73
|
+
* @param perspective - The perspective that the subject belongs to.
|
|
74
|
+
*
|
|
75
|
+
* @returns The subject entities.
|
|
76
|
+
*
|
|
77
|
+
* @throws Will throw an error if the subject entity cannot be converted to a subject class, or if the subject proxies cannot be gotten.
|
|
78
|
+
*/
|
|
20
79
|
static all(perspective: PerspectiveProxy): Promise<any[]>;
|
|
80
|
+
/**
|
|
81
|
+
* Query the subject entities of the subject class.
|
|
82
|
+
*
|
|
83
|
+
* NOTE: this is a static method and should be called on the class itself.
|
|
84
|
+
*
|
|
85
|
+
* @param perspective - The perspective that the subject belongs to.
|
|
86
|
+
* @param query - The query of the subject entities.
|
|
87
|
+
*
|
|
88
|
+
* @returns The subject entities.
|
|
89
|
+
*
|
|
90
|
+
* @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.
|
|
91
|
+
*/
|
|
21
92
|
static query(perspective: PerspectiveProxy, query?: SubjectEntityQueryParam): Promise<SubjectEntity[]>;
|
|
22
93
|
}
|
|
23
94
|
export type SubjectArray<T> = T[] | {
|
|
@@ -14,7 +14,17 @@ import { Literal } from "../Literal";
|
|
|
14
14
|
import { Link } from "../links/Links";
|
|
15
15
|
import { makeRandomPrologAtom } from "./SDNADecorators";
|
|
16
16
|
import { singularToPlural } from "./util";
|
|
17
|
+
/**
|
|
18
|
+
* Class representing a subject entity.
|
|
19
|
+
* 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.
|
|
20
|
+
*/
|
|
17
21
|
export class SubjectEntity {
|
|
22
|
+
/**
|
|
23
|
+
* Constructs a new subject.
|
|
24
|
+
* @param perspective - The perspective that the subject belongs to.
|
|
25
|
+
* @param baseExpression - The base expression of the subject.
|
|
26
|
+
* @param soruce - The source of the subject, the expression this instance is linked too.
|
|
27
|
+
*/
|
|
18
28
|
constructor(perspective, baseExpression, source) {
|
|
19
29
|
_SubjectEntity_baseExpression.set(this, void 0);
|
|
20
30
|
_SubjectEntity_subjectClass.set(this, void 0);
|
|
@@ -24,6 +34,9 @@ export class SubjectEntity {
|
|
|
24
34
|
__classPrivateFieldSet(this, _SubjectEntity_perspective, perspective, "f");
|
|
25
35
|
__classPrivateFieldSet(this, _SubjectEntity_source, source || "ad4m://self", "f");
|
|
26
36
|
}
|
|
37
|
+
/**
|
|
38
|
+
* Gets the base expression of the subject.
|
|
39
|
+
*/
|
|
27
40
|
get baseExpression() {
|
|
28
41
|
return __classPrivateFieldGet(this, _SubjectEntity_baseExpression, "f");
|
|
29
42
|
}
|
|
@@ -150,6 +163,17 @@ export class SubjectEntity {
|
|
|
150
163
|
}
|
|
151
164
|
}
|
|
152
165
|
}
|
|
166
|
+
/**
|
|
167
|
+
* Save the subject entity.
|
|
168
|
+
* 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".
|
|
169
|
+
*
|
|
170
|
+
* If a property has an action, it will perform the action (Only for collections).
|
|
171
|
+
* If a property is an array and is not empty, it will set the collection.
|
|
172
|
+
* If a property is not undefined, not null, and not an empty string, it will set the property.
|
|
173
|
+
*
|
|
174
|
+
*
|
|
175
|
+
* @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.
|
|
176
|
+
*/
|
|
153
177
|
async save() {
|
|
154
178
|
__classPrivateFieldSet(this, _SubjectEntity_subjectClass, await __classPrivateFieldGet(this, _SubjectEntity_perspective, "f").stringOrTemplateObjectToSubjectClass(this), "f");
|
|
155
179
|
await __classPrivateFieldGet(this, _SubjectEntity_perspective, "f").createSubject(this, __classPrivateFieldGet(this, _SubjectEntity_baseExpression, "f"));
|
|
@@ -160,11 +184,22 @@ export class SubjectEntity {
|
|
|
160
184
|
}));
|
|
161
185
|
await this.update();
|
|
162
186
|
}
|
|
187
|
+
/**
|
|
188
|
+
* Update the subject entity.
|
|
189
|
+
*
|
|
190
|
+
* It will iterate over the properties of the subject entity.
|
|
191
|
+
*
|
|
192
|
+
* If a property has an action, it will perform the action (Only for collections).
|
|
193
|
+
* If a property is an array and is not empty, it will set the collection.
|
|
194
|
+
* If a property is not undefined, not null, and not an empty string, it will set the property.
|
|
195
|
+
*
|
|
196
|
+
* @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.
|
|
197
|
+
*/
|
|
163
198
|
async update() {
|
|
164
199
|
__classPrivateFieldSet(this, _SubjectEntity_subjectClass, await __classPrivateFieldGet(this, _SubjectEntity_perspective, "f").stringOrTemplateObjectToSubjectClass(this), "f");
|
|
165
200
|
const entries = Object.entries(this);
|
|
166
201
|
for (const [key, value] of entries) {
|
|
167
|
-
if (value) {
|
|
202
|
+
if (value !== undefined && value !== null) {
|
|
168
203
|
if (value?.action) {
|
|
169
204
|
switch (value.action) {
|
|
170
205
|
case 'setter':
|
|
@@ -183,21 +218,44 @@ export class SubjectEntity {
|
|
|
183
218
|
else if (Array.isArray(value) && value.length > 0) {
|
|
184
219
|
await this.setCollectionSetter(key, value);
|
|
185
220
|
}
|
|
186
|
-
else {
|
|
221
|
+
else if (value !== undefined && value !== null && value !== "") {
|
|
187
222
|
await this.setProperty(key, value);
|
|
188
223
|
}
|
|
189
224
|
}
|
|
190
225
|
}
|
|
191
226
|
await this.getData();
|
|
192
227
|
}
|
|
228
|
+
/**
|
|
229
|
+
* Get the subject entity with all the properties & collection populated.
|
|
230
|
+
*
|
|
231
|
+
* @returns The subject entity.
|
|
232
|
+
*
|
|
233
|
+
* @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.
|
|
234
|
+
*/
|
|
193
235
|
async get() {
|
|
194
236
|
__classPrivateFieldSet(this, _SubjectEntity_subjectClass, await __classPrivateFieldGet(this, _SubjectEntity_perspective, "f").stringOrTemplateObjectToSubjectClass(this), "f");
|
|
195
237
|
return await this.getData();
|
|
196
238
|
}
|
|
239
|
+
/**
|
|
240
|
+
* Delete the subject entity.
|
|
241
|
+
* This method will remove the subject from the perspective.
|
|
242
|
+
*
|
|
243
|
+
* @throws Will throw an error if the subject entity cannot be removed.
|
|
244
|
+
*/
|
|
197
245
|
async delete() {
|
|
198
246
|
await __classPrivateFieldGet(this, _SubjectEntity_perspective, "f").removeSubject(this, __classPrivateFieldGet(this, _SubjectEntity_baseExpression, "f"));
|
|
199
247
|
}
|
|
200
|
-
|
|
248
|
+
/**
|
|
249
|
+
* Get all the subject entities of the subject class.
|
|
250
|
+
*
|
|
251
|
+
* NOTE: this is a static method and should be called on the class itself.
|
|
252
|
+
*
|
|
253
|
+
* @param perspective - The perspective that the subject belongs to.
|
|
254
|
+
*
|
|
255
|
+
* @returns The subject entities.
|
|
256
|
+
*
|
|
257
|
+
* @throws Will throw an error if the subject entity cannot be converted to a subject class, or if the subject proxies cannot be gotten.
|
|
258
|
+
*/
|
|
201
259
|
static async all(perspective) {
|
|
202
260
|
let subjectClass = await perspective.stringOrTemplateObjectToSubjectClass(this);
|
|
203
261
|
const proxies = await perspective.getAllSubjectProxies(subjectClass);
|
|
@@ -212,6 +270,18 @@ export class SubjectEntity {
|
|
|
212
270
|
}
|
|
213
271
|
return [];
|
|
214
272
|
}
|
|
273
|
+
/**
|
|
274
|
+
* Query the subject entities of the subject class.
|
|
275
|
+
*
|
|
276
|
+
* NOTE: this is a static method and should be called on the class itself.
|
|
277
|
+
*
|
|
278
|
+
* @param perspective - The perspective that the subject belongs to.
|
|
279
|
+
* @param query - The query of the subject entities.
|
|
280
|
+
*
|
|
281
|
+
* @returns The subject entities.
|
|
282
|
+
*
|
|
283
|
+
* @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.
|
|
284
|
+
*/
|
|
215
285
|
static async query(perspective, query) {
|
|
216
286
|
const source = query?.source || "ad4m://self";
|
|
217
287
|
let subjectClass = await perspective.stringOrTemplateObjectToSubjectClass(this);
|
package/package.json
CHANGED
|
@@ -55,10 +55,12 @@
|
|
|
55
55
|
"rollup": "^2.56.3",
|
|
56
56
|
"ts-jest": "^26.5.6",
|
|
57
57
|
"typescript": "^4.6.2",
|
|
58
|
-
"ws": "8.13.0"
|
|
58
|
+
"ws": "8.13.0",
|
|
59
|
+
"@types/node": "*",
|
|
60
|
+
"@types/yargs": "*"
|
|
59
61
|
},
|
|
60
62
|
"patchedDependencies": {},
|
|
61
|
-
"version": "0.9.0-
|
|
63
|
+
"version": "0.9.0-rc2",
|
|
62
64
|
"pnpm": {
|
|
63
65
|
"patchedDependencies": {
|
|
64
66
|
"graphql@15.7.2": "patches/graphql@15.7.2.patch"
|
|
@@ -2,13 +2,13 @@ import { Field, ObjectType } from "type-graphql";
|
|
|
2
2
|
import { NeighbourhoodExpression } from "../neighbourhood/Neighbourhood";
|
|
3
3
|
|
|
4
4
|
export enum PerspectiveState {
|
|
5
|
-
Private = "
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
Private = "PRIVATE",
|
|
6
|
+
NeighboudhoodCreationInitiated = "NEIGHBOURHOOD_CREATION_INITIATED",
|
|
7
|
+
NeighbourhoodJoinInitiated = "NEIGHBOURHOOD_JOIN_INITIATED",
|
|
8
|
+
LinkLanguageFailedToInstall = "LINK_LANGUAGE_FAILED_TO_INSTALL",
|
|
9
|
+
LinkLanguageInstalledButNotSynced = "LINK_LANGUAGE_INSTALLED_BUT_NOT_SYNCED",
|
|
10
|
+
Synced = "SYNCED",
|
|
10
11
|
}
|
|
11
|
-
|
|
12
12
|
// This type is used in the GraphQL interface to reference a mutable
|
|
13
13
|
// prespective that is implemented locally by the Ad4m runtime.
|
|
14
14
|
// The UUID is used in mutations to identify the perspective that gets mutated.
|
|
@@ -447,6 +447,11 @@ export class PerspectiveProxy {
|
|
|
447
447
|
return instances
|
|
448
448
|
}
|
|
449
449
|
|
|
450
|
+
/** Returns all subject proxies of the given subject class.
|
|
451
|
+
* @param subjectClass Either a string with the name of the subject class, or an object
|
|
452
|
+
* with the properties of the subject class. In the latter case, all subject classes
|
|
453
|
+
* that match the given properties will be used.
|
|
454
|
+
*/
|
|
450
455
|
async getAllSubjectProxies<T>(subjectClass: T): Promise<T[]> {
|
|
451
456
|
let classes = []
|
|
452
457
|
if(typeof subjectClass === "string") {
|