@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
package/lib/index.js
CHANGED
|
@@ -4415,7 +4415,17 @@ var __classPrivateFieldGet$a = (undefined && undefined.__classPrivateFieldGet) |
|
|
|
4415
4415
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
4416
4416
|
};
|
|
4417
4417
|
var _Subject_baseExpression, _Subject_subjectClass, _Subject_perspective;
|
|
4418
|
+
/**
|
|
4419
|
+
* Represents a subject in the perspective.
|
|
4420
|
+
* A subject is an entity that has properties and collections.
|
|
4421
|
+
*/
|
|
4418
4422
|
class Subject {
|
|
4423
|
+
/**
|
|
4424
|
+
* Constructs a new subject.
|
|
4425
|
+
* @param perspective - The perspective that the subject belongs to.
|
|
4426
|
+
* @param baseExpression - The base expression of the subject.
|
|
4427
|
+
* @param subjectClass - The class of the subject.
|
|
4428
|
+
*/
|
|
4419
4429
|
constructor(perspective, baseExpression, subjectClass) {
|
|
4420
4430
|
_Subject_baseExpression.set(this, void 0);
|
|
4421
4431
|
_Subject_subjectClass.set(this, void 0);
|
|
@@ -4424,14 +4434,24 @@ class Subject {
|
|
|
4424
4434
|
__classPrivateFieldSet$a(this, _Subject_subjectClass, subjectClass, "f");
|
|
4425
4435
|
__classPrivateFieldSet$a(this, _Subject_perspective, perspective, "f");
|
|
4426
4436
|
}
|
|
4437
|
+
/**
|
|
4438
|
+
* Gets the base expression of the subject.
|
|
4439
|
+
*/
|
|
4427
4440
|
get baseExpression() {
|
|
4428
4441
|
return __classPrivateFieldGet$a(this, _Subject_baseExpression, "f");
|
|
4429
4442
|
}
|
|
4443
|
+
/**
|
|
4444
|
+
* Initializes the subject by validating it and defining its properties and collections dynamically.
|
|
4445
|
+
*
|
|
4446
|
+
* NOTE: This method should be called before using the subject. All the properties and collections of the subject defined are not type-checked.
|
|
4447
|
+
*/
|
|
4430
4448
|
async init() {
|
|
4449
|
+
// Check if the subject is a valid instance of the subject class
|
|
4431
4450
|
let isInstance = await __classPrivateFieldGet$a(this, _Subject_perspective, "f").isSubjectInstance(__classPrivateFieldGet$a(this, _Subject_baseExpression, "f"), __classPrivateFieldGet$a(this, _Subject_subjectClass, "f"));
|
|
4432
4451
|
if (!isInstance) {
|
|
4433
4452
|
throw `Not a valid subject instance of ${__classPrivateFieldGet$a(this, _Subject_subjectClass, "f")} for ${__classPrivateFieldGet$a(this, _Subject_baseExpression, "f")}`;
|
|
4434
4453
|
}
|
|
4454
|
+
// Define properties and collections dynamically
|
|
4435
4455
|
let results = await __classPrivateFieldGet$a(this, _Subject_perspective, "f").infer(`subject_class("${__classPrivateFieldGet$a(this, _Subject_subjectClass, "f")}", C), property(C, Property)`);
|
|
4436
4456
|
let properties = results.map(result => result.Property);
|
|
4437
4457
|
for (let p of properties) {
|
|
@@ -4474,8 +4494,8 @@ class Subject {
|
|
|
4474
4494
|
}
|
|
4475
4495
|
});
|
|
4476
4496
|
}
|
|
4497
|
+
// Define setters
|
|
4477
4498
|
const setters = await __classPrivateFieldGet$a(this, _Subject_perspective, "f").infer(`subject_class("${__classPrivateFieldGet$a(this, _Subject_subjectClass, "f")}", C), property_setter(C, Property, Setter)`);
|
|
4478
|
-
//console.log("Subject setters: " + setters.map(setter => setter.Property))
|
|
4479
4499
|
for (let setter of (setters ? setters : [])) {
|
|
4480
4500
|
if (setter) {
|
|
4481
4501
|
const property = setter.Property;
|
|
@@ -4493,6 +4513,7 @@ class Subject {
|
|
|
4493
4513
|
};
|
|
4494
4514
|
}
|
|
4495
4515
|
}
|
|
4516
|
+
// Define collections
|
|
4496
4517
|
let results2 = await __classPrivateFieldGet$a(this, _Subject_perspective, "f").infer(`subject_class("${__classPrivateFieldGet$a(this, _Subject_subjectClass, "f")}", C), collection(C, Collection)`);
|
|
4497
4518
|
if (!results2)
|
|
4498
4519
|
results2 = [];
|
|
@@ -4512,6 +4533,7 @@ class Subject {
|
|
|
4512
4533
|
}
|
|
4513
4534
|
});
|
|
4514
4535
|
}
|
|
4536
|
+
// Define collection adders
|
|
4515
4537
|
let adders = await __classPrivateFieldGet$a(this, _Subject_perspective, "f").infer(`subject_class("${__classPrivateFieldGet$a(this, _Subject_subjectClass, "f")}", C), collection_adder(C, Collection, Adder)`);
|
|
4516
4538
|
if (!adders)
|
|
4517
4539
|
adders = [];
|
|
@@ -4529,6 +4551,7 @@ class Subject {
|
|
|
4529
4551
|
};
|
|
4530
4552
|
}
|
|
4531
4553
|
}
|
|
4554
|
+
// Define collection removers
|
|
4532
4555
|
let removers = await __classPrivateFieldGet$a(this, _Subject_perspective, "f").infer(`subject_class("${__classPrivateFieldGet$a(this, _Subject_subjectClass, "f")}", C), collection_remover(C, Collection, Remover)`);
|
|
4533
4556
|
if (!removers)
|
|
4534
4557
|
removers = [];
|
|
@@ -4546,6 +4569,7 @@ class Subject {
|
|
|
4546
4569
|
};
|
|
4547
4570
|
}
|
|
4548
4571
|
}
|
|
4572
|
+
// Define collection setters
|
|
4549
4573
|
let collectionSetters = await __classPrivateFieldGet$a(this, _Subject_perspective, "f").infer(`subject_class("${__classPrivateFieldGet$a(this, _Subject_subjectClass, "f")}", C), collection_setter(C, Collection, Setter)`);
|
|
4550
4574
|
if (!collectionSetters)
|
|
4551
4575
|
collectionSetters = [];
|
|
@@ -4952,6 +4976,11 @@ class PerspectiveProxy {
|
|
|
4952
4976
|
}
|
|
4953
4977
|
return instances;
|
|
4954
4978
|
}
|
|
4979
|
+
/** Returns all subject proxies of the given subject class.
|
|
4980
|
+
* @param subjectClass Either a string with the name of the subject class, or an object
|
|
4981
|
+
* with the properties of the subject class. In the latter case, all subject classes
|
|
4982
|
+
* that match the given properties will be used.
|
|
4983
|
+
*/
|
|
4955
4984
|
async getAllSubjectProxies(subjectClass) {
|
|
4956
4985
|
let classes = [];
|
|
4957
4986
|
if (typeof subjectClass === "string") {
|
|
@@ -7039,11 +7068,12 @@ var __metadata$1 = (undefined && undefined.__metadata) || function (k, v) {
|
|
|
7039
7068
|
};
|
|
7040
7069
|
var PerspectiveState;
|
|
7041
7070
|
(function (PerspectiveState) {
|
|
7042
|
-
PerspectiveState["Private"] = "
|
|
7043
|
-
PerspectiveState["
|
|
7044
|
-
PerspectiveState["
|
|
7045
|
-
PerspectiveState["
|
|
7046
|
-
PerspectiveState["
|
|
7071
|
+
PerspectiveState["Private"] = "PRIVATE";
|
|
7072
|
+
PerspectiveState["NeighboudhoodCreationInitiated"] = "NEIGHBOURHOOD_CREATION_INITIATED";
|
|
7073
|
+
PerspectiveState["NeighbourhoodJoinInitiated"] = "NEIGHBOURHOOD_JOIN_INITIATED";
|
|
7074
|
+
PerspectiveState["LinkLanguageFailedToInstall"] = "LINK_LANGUAGE_FAILED_TO_INSTALL";
|
|
7075
|
+
PerspectiveState["LinkLanguageInstalledButNotSynced"] = "LINK_LANGUAGE_INSTALLED_BUT_NOT_SYNCED";
|
|
7076
|
+
PerspectiveState["Synced"] = "SYNCED";
|
|
7047
7077
|
})(PerspectiveState || (PerspectiveState = {}));
|
|
7048
7078
|
// This type is used in the GraphQL interface to reference a mutable
|
|
7049
7079
|
// prespective that is implemented locally by the Ad4m runtime.
|
|
@@ -7198,11 +7228,30 @@ function addLink(source, predicate, target) {
|
|
|
7198
7228
|
function hasLink(predicate) {
|
|
7199
7229
|
return `triple(this, "${predicate}", _)`;
|
|
7200
7230
|
}
|
|
7201
|
-
|
|
7231
|
+
/**
|
|
7232
|
+
* Decorator for querying instances of a subject class.
|
|
7233
|
+
*
|
|
7234
|
+
* @category Decorators
|
|
7235
|
+
*
|
|
7236
|
+
* @description
|
|
7237
|
+
* NOTE: Only works on methods that return a promise and will throw an error if not used on a method that returns a promise.
|
|
7238
|
+
* This will allow you to query for instances of a subject class with custom clauses within the instance clauses.
|
|
7239
|
+
*
|
|
7240
|
+
* @example
|
|
7241
|
+
* // Usage with where clause
|
|
7242
|
+
* InstanceQuery({ where: { name: "John" }}) // this will return all the properties of the subject class with the name "John"
|
|
7243
|
+
*
|
|
7244
|
+
* @example
|
|
7245
|
+
* // Usage with condition clause
|
|
7246
|
+
* InstanceQuery({ condition: "triple(Instance, 'age', Age), Age > 18" }) // this will return all the properties of the subject class with the age greater than 18
|
|
7247
|
+
*
|
|
7248
|
+
* @param {Object} [options] - Query options.
|
|
7249
|
+
*/
|
|
7250
|
+
function InstanceQuery(options) {
|
|
7202
7251
|
return function (target, key, descriptor) {
|
|
7203
7252
|
const originalMethod = descriptor.value;
|
|
7204
7253
|
if (typeof originalMethod !== "function") {
|
|
7205
|
-
throw new Error("
|
|
7254
|
+
throw new Error("InstanceQuery decorator can only be applied to methods");
|
|
7206
7255
|
}
|
|
7207
7256
|
descriptor.value = async function (perspective) {
|
|
7208
7257
|
let instances = [];
|
|
@@ -7235,8 +7284,35 @@ function instanceQuery(options) {
|
|
|
7235
7284
|
};
|
|
7236
7285
|
};
|
|
7237
7286
|
}
|
|
7238
|
-
|
|
7287
|
+
/**
|
|
7288
|
+
* Decorator for defining properties of a subject class.
|
|
7289
|
+
*
|
|
7290
|
+
* @category Decorators
|
|
7291
|
+
*
|
|
7292
|
+
* @description
|
|
7293
|
+
* This will allow you to define properties with different conditions and how they would be defined in proflog engine.
|
|
7294
|
+
*
|
|
7295
|
+
* - All properties must have a `through` option which is the predicate of the property.
|
|
7296
|
+
* -e If the property is required, it must have an `initial` option which is the initial value of the property.
|
|
7297
|
+
* - If the property is writable, it will have a setter in prolog engine. A custom setter can be defined with the `setter` option.
|
|
7298
|
+
* - 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.
|
|
7299
|
+
* - 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.
|
|
7300
|
+
* - 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
|
|
7301
|
+
*
|
|
7302
|
+
* @example
|
|
7303
|
+
* // Usage
|
|
7304
|
+
* SubjectProperty({ through: "ad4m://name", initial: "John", required: true }) // this will define a property with the name "ad4m://name" and the initial value "John"
|
|
7305
|
+
*
|
|
7306
|
+
* @param {PropertyOptions} [opts] - Property options.
|
|
7307
|
+
*/
|
|
7308
|
+
function SubjectProperty(opts) {
|
|
7239
7309
|
return function (target, key) {
|
|
7310
|
+
if (opts.required && !opts.initial) {
|
|
7311
|
+
throw new Error("SubjectProperty requires an 'initial' option if 'required' is true");
|
|
7312
|
+
}
|
|
7313
|
+
if (!opts.through && !opts.getter) {
|
|
7314
|
+
throw new Error("SubjectProperty requires either 'through' or 'getter' option");
|
|
7315
|
+
}
|
|
7240
7316
|
target["__properties"] = target["__properties"] || {};
|
|
7241
7317
|
target["__properties"][key] = target["__properties"][key] || {};
|
|
7242
7318
|
target["__properties"][key] = { ...target["__properties"][key], ...opts };
|
|
@@ -7247,8 +7323,36 @@ function subjectProperty(opts) {
|
|
|
7247
7323
|
Object.defineProperty(target, key, { configurable: true, writable: true });
|
|
7248
7324
|
};
|
|
7249
7325
|
}
|
|
7250
|
-
|
|
7326
|
+
/**
|
|
7327
|
+
* Decorator for defining flags of a subject class
|
|
7328
|
+
*
|
|
7329
|
+
* @category Decorators
|
|
7330
|
+
*
|
|
7331
|
+
* @description
|
|
7332
|
+
* The idea behind flag decorator is to define a property that is required and has an initial value.
|
|
7333
|
+
* 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.
|
|
7334
|
+
*
|
|
7335
|
+
* NOTE: Use of Flag is discouraged and should be used only when necessary.
|
|
7336
|
+
*
|
|
7337
|
+
* - All properties must have a `through` & `initial` option which is the predicate of the property.
|
|
7338
|
+
*
|
|
7339
|
+
* @example
|
|
7340
|
+
* // Usage
|
|
7341
|
+
* SubjectFlag({ through: "ad4m://name", value: "John" }) // this will define a flag with the name "ad4m://name" and the initial value "John"
|
|
7342
|
+
*
|
|
7343
|
+
* @param {FlagOptions} [opts] Flag options.
|
|
7344
|
+
*/
|
|
7345
|
+
function SubjectFlag(opts) {
|
|
7251
7346
|
return function (target, key) {
|
|
7347
|
+
if (!opts.through && !opts.value) {
|
|
7348
|
+
throw new Error("SubjectFlag requires a 'through' and 'value' option");
|
|
7349
|
+
}
|
|
7350
|
+
if (!opts.through) {
|
|
7351
|
+
throw new Error("SubjectFlag requires a 'through' option");
|
|
7352
|
+
}
|
|
7353
|
+
if (!opts.value) {
|
|
7354
|
+
throw new Error("SubjectFlag requires a 'value' option");
|
|
7355
|
+
}
|
|
7252
7356
|
target["__properties"] = target["__properties"] || {};
|
|
7253
7357
|
target["__properties"][key] = target["__properties"][key] || {};
|
|
7254
7358
|
target["__properties"][key] = {
|
|
@@ -7263,7 +7367,27 @@ function subjectFlag(opts) {
|
|
|
7263
7367
|
Object.defineProperty(target, key, { configurable: true, writable: true });
|
|
7264
7368
|
};
|
|
7265
7369
|
}
|
|
7266
|
-
|
|
7370
|
+
/**
|
|
7371
|
+
* Decorator for defining collections of a subject class.
|
|
7372
|
+
*
|
|
7373
|
+
* @category Decorators
|
|
7374
|
+
*
|
|
7375
|
+
* @description
|
|
7376
|
+
* This will allow you to define collections with different conditions and how they would be defined in proflog engine.
|
|
7377
|
+
*
|
|
7378
|
+
* NOTE: The property needs to be an array for it to picked up during the initialization phase.
|
|
7379
|
+
*
|
|
7380
|
+
* - All collections must have a `through` option which is the predicate of the collection.
|
|
7381
|
+
* - If the collection has a `where` option, it can be used to define a custom condition for the collection.
|
|
7382
|
+
* - 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.
|
|
7383
|
+
*
|
|
7384
|
+
* @example
|
|
7385
|
+
* // Usage
|
|
7386
|
+
* SubjectCollection({ through: "ad4m://friends" }) // this will define a collection with the name "ad4m://friends"
|
|
7387
|
+
*
|
|
7388
|
+
* @param opts Collection options.
|
|
7389
|
+
*/
|
|
7390
|
+
function SubjectCollection(opts) {
|
|
7267
7391
|
return function (target, key) {
|
|
7268
7392
|
target["__collections"] = target["__collections"] || {};
|
|
7269
7393
|
target["__collections"][key] = opts;
|
|
@@ -7283,6 +7407,22 @@ function makeRandomPrologAtom(length) {
|
|
|
7283
7407
|
}
|
|
7284
7408
|
return result;
|
|
7285
7409
|
}
|
|
7410
|
+
/**
|
|
7411
|
+
* Decorator for defining an SDNA class.
|
|
7412
|
+
*
|
|
7413
|
+
* @category Decorators
|
|
7414
|
+
*
|
|
7415
|
+
* @description
|
|
7416
|
+
* 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`.
|
|
7417
|
+
*
|
|
7418
|
+
* Note: This decorator is required for the class to be picked up during the initialization phase.
|
|
7419
|
+
*
|
|
7420
|
+
* @example
|
|
7421
|
+
* // Usage
|
|
7422
|
+
* SDNAClass({ name: "Person" }) // this will create a new SDNA class with the name "Person"
|
|
7423
|
+
*
|
|
7424
|
+
* @param opts SDNA class options.
|
|
7425
|
+
*/
|
|
7286
7426
|
function SDNAClass(opts) {
|
|
7287
7427
|
return function (target) {
|
|
7288
7428
|
target.prototype.className = opts.name;
|
|
@@ -7847,7 +7987,17 @@ var __classPrivateFieldGet = (undefined && undefined.__classPrivateFieldGet) ||
|
|
|
7847
7987
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
7848
7988
|
};
|
|
7849
7989
|
var _SubjectEntity_baseExpression, _SubjectEntity_subjectClass, _SubjectEntity_source, _SubjectEntity_perspective;
|
|
7990
|
+
/**
|
|
7991
|
+
* Class representing a subject entity.
|
|
7992
|
+
* 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.
|
|
7993
|
+
*/
|
|
7850
7994
|
class SubjectEntity {
|
|
7995
|
+
/**
|
|
7996
|
+
* Constructs a new subject.
|
|
7997
|
+
* @param perspective - The perspective that the subject belongs to.
|
|
7998
|
+
* @param baseExpression - The base expression of the subject.
|
|
7999
|
+
* @param soruce - The source of the subject, the expression this instance is linked too.
|
|
8000
|
+
*/
|
|
7851
8001
|
constructor(perspective, baseExpression, source) {
|
|
7852
8002
|
_SubjectEntity_baseExpression.set(this, void 0);
|
|
7853
8003
|
_SubjectEntity_subjectClass.set(this, void 0);
|
|
@@ -7857,6 +8007,9 @@ class SubjectEntity {
|
|
|
7857
8007
|
__classPrivateFieldSet(this, _SubjectEntity_perspective, perspective, "f");
|
|
7858
8008
|
__classPrivateFieldSet(this, _SubjectEntity_source, source || "ad4m://self", "f");
|
|
7859
8009
|
}
|
|
8010
|
+
/**
|
|
8011
|
+
* Gets the base expression of the subject.
|
|
8012
|
+
*/
|
|
7860
8013
|
get baseExpression() {
|
|
7861
8014
|
return __classPrivateFieldGet(this, _SubjectEntity_baseExpression, "f");
|
|
7862
8015
|
}
|
|
@@ -7983,6 +8136,17 @@ class SubjectEntity {
|
|
|
7983
8136
|
}
|
|
7984
8137
|
}
|
|
7985
8138
|
}
|
|
8139
|
+
/**
|
|
8140
|
+
* Save the subject entity.
|
|
8141
|
+
* 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".
|
|
8142
|
+
*
|
|
8143
|
+
* If a property has an action, it will perform the action (Only for collections).
|
|
8144
|
+
* If a property is an array and is not empty, it will set the collection.
|
|
8145
|
+
* If a property is not undefined, not null, and not an empty string, it will set the property.
|
|
8146
|
+
*
|
|
8147
|
+
*
|
|
8148
|
+
* @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.
|
|
8149
|
+
*/
|
|
7986
8150
|
async save() {
|
|
7987
8151
|
__classPrivateFieldSet(this, _SubjectEntity_subjectClass, await __classPrivateFieldGet(this, _SubjectEntity_perspective, "f").stringOrTemplateObjectToSubjectClass(this), "f");
|
|
7988
8152
|
await __classPrivateFieldGet(this, _SubjectEntity_perspective, "f").createSubject(this, __classPrivateFieldGet(this, _SubjectEntity_baseExpression, "f"));
|
|
@@ -7993,11 +8157,22 @@ class SubjectEntity {
|
|
|
7993
8157
|
}));
|
|
7994
8158
|
await this.update();
|
|
7995
8159
|
}
|
|
8160
|
+
/**
|
|
8161
|
+
* Update the subject entity.
|
|
8162
|
+
*
|
|
8163
|
+
* It will iterate over the properties of the subject entity.
|
|
8164
|
+
*
|
|
8165
|
+
* If a property has an action, it will perform the action (Only for collections).
|
|
8166
|
+
* If a property is an array and is not empty, it will set the collection.
|
|
8167
|
+
* If a property is not undefined, not null, and not an empty string, it will set the property.
|
|
8168
|
+
*
|
|
8169
|
+
* @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.
|
|
8170
|
+
*/
|
|
7996
8171
|
async update() {
|
|
7997
8172
|
__classPrivateFieldSet(this, _SubjectEntity_subjectClass, await __classPrivateFieldGet(this, _SubjectEntity_perspective, "f").stringOrTemplateObjectToSubjectClass(this), "f");
|
|
7998
8173
|
const entries = Object.entries(this);
|
|
7999
8174
|
for (const [key, value] of entries) {
|
|
8000
|
-
if (value) {
|
|
8175
|
+
if (value !== undefined && value !== null) {
|
|
8001
8176
|
if (value?.action) {
|
|
8002
8177
|
switch (value.action) {
|
|
8003
8178
|
case 'setter':
|
|
@@ -8016,21 +8191,44 @@ class SubjectEntity {
|
|
|
8016
8191
|
else if (Array.isArray(value) && value.length > 0) {
|
|
8017
8192
|
await this.setCollectionSetter(key, value);
|
|
8018
8193
|
}
|
|
8019
|
-
else {
|
|
8194
|
+
else if (value !== undefined && value !== null && value !== "") {
|
|
8020
8195
|
await this.setProperty(key, value);
|
|
8021
8196
|
}
|
|
8022
8197
|
}
|
|
8023
8198
|
}
|
|
8024
8199
|
await this.getData();
|
|
8025
8200
|
}
|
|
8201
|
+
/**
|
|
8202
|
+
* Get the subject entity with all the properties & collection populated.
|
|
8203
|
+
*
|
|
8204
|
+
* @returns The subject entity.
|
|
8205
|
+
*
|
|
8206
|
+
* @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.
|
|
8207
|
+
*/
|
|
8026
8208
|
async get() {
|
|
8027
8209
|
__classPrivateFieldSet(this, _SubjectEntity_subjectClass, await __classPrivateFieldGet(this, _SubjectEntity_perspective, "f").stringOrTemplateObjectToSubjectClass(this), "f");
|
|
8028
8210
|
return await this.getData();
|
|
8029
8211
|
}
|
|
8212
|
+
/**
|
|
8213
|
+
* Delete the subject entity.
|
|
8214
|
+
* This method will remove the subject from the perspective.
|
|
8215
|
+
*
|
|
8216
|
+
* @throws Will throw an error if the subject entity cannot be removed.
|
|
8217
|
+
*/
|
|
8030
8218
|
async delete() {
|
|
8031
8219
|
await __classPrivateFieldGet(this, _SubjectEntity_perspective, "f").removeSubject(this, __classPrivateFieldGet(this, _SubjectEntity_baseExpression, "f"));
|
|
8032
8220
|
}
|
|
8033
|
-
|
|
8221
|
+
/**
|
|
8222
|
+
* Get all the subject entities of the subject class.
|
|
8223
|
+
*
|
|
8224
|
+
* NOTE: this is a static method and should be called on the class itself.
|
|
8225
|
+
*
|
|
8226
|
+
* @param perspective - The perspective that the subject belongs to.
|
|
8227
|
+
*
|
|
8228
|
+
* @returns The subject entities.
|
|
8229
|
+
*
|
|
8230
|
+
* @throws Will throw an error if the subject entity cannot be converted to a subject class, or if the subject proxies cannot be gotten.
|
|
8231
|
+
*/
|
|
8034
8232
|
static async all(perspective) {
|
|
8035
8233
|
let subjectClass = await perspective.stringOrTemplateObjectToSubjectClass(this);
|
|
8036
8234
|
const proxies = await perspective.getAllSubjectProxies(subjectClass);
|
|
@@ -8045,6 +8243,18 @@ class SubjectEntity {
|
|
|
8045
8243
|
}
|
|
8046
8244
|
return [];
|
|
8047
8245
|
}
|
|
8246
|
+
/**
|
|
8247
|
+
* Query the subject entities of the subject class.
|
|
8248
|
+
*
|
|
8249
|
+
* NOTE: this is a static method and should be called on the class itself.
|
|
8250
|
+
*
|
|
8251
|
+
* @param perspective - The perspective that the subject belongs to.
|
|
8252
|
+
* @param query - The query of the subject entities.
|
|
8253
|
+
*
|
|
8254
|
+
* @returns The subject entities.
|
|
8255
|
+
*
|
|
8256
|
+
* @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.
|
|
8257
|
+
*/
|
|
8048
8258
|
static async query(perspective, query) {
|
|
8049
8259
|
const source = query?.source || "ad4m://self";
|
|
8050
8260
|
let subjectClass = await perspective.stringOrTemplateObjectToSubjectClass(this);
|
|
@@ -8082,4 +8292,4 @@ class SubjectEntity {
|
|
|
8082
8292
|
}
|
|
8083
8293
|
_SubjectEntity_baseExpression = new WeakMap(), _SubjectEntity_subjectClass = new WeakMap(), _SubjectEntity_source = new WeakMap(), _SubjectEntity_perspective = new WeakMap();
|
|
8084
8294
|
|
|
8085
|
-
export { Ad4mClient, Agent, AgentClient, AgentExpression, AgentSignature, AgentStatus, Apps, AuthInfo, AuthInfoInput, Capability, CapabilityInput, Dna, EntanglementProof, EntanglementProofInput, ExceptionType, Expression, ExpressionGeneric, ExpressionGenericInput, ExpressionProof, ExpressionProofInput, ExpressionRef, ExpressionRendered, Icon, InteractionCall, InteractionMeta, InteractionParameter, LanguageExpression, LanguageHandle, LanguageLanguageInput, LanguageMeta, LanguageMetaInput, LanguageMetaInternal, LanguageRef, Link, LinkExpression, LinkExpressionInput, LinkExpressionMutations, LinkExpressionUpdated, LinkInput, LinkMutations, LinkQuery, Literal, Neighbourhood, NeighbourhoodExpression, NeighbourhoodProxy, OnlineAgent, Perspective, PerspectiveAction, PerspectiveDiff, PerspectiveDiffExpression, PerspectiveExpression, PerspectiveHandle, PerspectiveInput, PerspectiveProxy, PerspectiveState, PerspectiveUnsignedInput, Resource, ResourceInput, SDNAClass, SMART_LITERAL_CONTENT_PREDICATE, SmartLiteral, Subject, SubjectEntity, addLink, capSentence, exprRef2String, formatList, hasLink,
|
|
8295
|
+
export { Ad4mClient, Agent, AgentClient, AgentExpression, AgentSignature, AgentStatus, Apps, AuthInfo, AuthInfoInput, Capability, CapabilityInput, Dna, EntanglementProof, EntanglementProofInput, ExceptionType, Expression, ExpressionGeneric, ExpressionGenericInput, ExpressionProof, ExpressionProofInput, ExpressionRef, ExpressionRendered, Icon, InstanceQuery, InteractionCall, InteractionMeta, InteractionParameter, LanguageExpression, LanguageHandle, LanguageLanguageInput, LanguageMeta, LanguageMetaInput, LanguageMetaInternal, LanguageRef, Link, LinkExpression, LinkExpressionInput, LinkExpressionMutations, LinkExpressionUpdated, LinkInput, LinkMutations, LinkQuery, Literal, Neighbourhood, NeighbourhoodExpression, NeighbourhoodProxy, OnlineAgent, Perspective, PerspectiveAction, PerspectiveDiff, PerspectiveDiffExpression, PerspectiveExpression, PerspectiveHandle, PerspectiveInput, PerspectiveProxy, PerspectiveState, PerspectiveUnsignedInput, Resource, ResourceInput, SDNAClass, SMART_LITERAL_CONTENT_PREDICATE, SmartLiteral, Subject, SubjectCollection, SubjectEntity, SubjectFlag, SubjectProperty, addLink, capSentence, exprRef2String, formatList, hasLink, isExpression, isLink, linkEqual, makeRandomPrologAtom, parseExprUrl, typeDefsString };
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { NeighbourhoodExpression } from "../neighbourhood/Neighbourhood";
|
|
2
2
|
export declare enum PerspectiveState {
|
|
3
|
-
Private = "
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
Private = "PRIVATE",
|
|
4
|
+
NeighboudhoodCreationInitiated = "NEIGHBOURHOOD_CREATION_INITIATED",
|
|
5
|
+
NeighbourhoodJoinInitiated = "NEIGHBOURHOOD_JOIN_INITIATED",
|
|
6
|
+
LinkLanguageFailedToInstall = "LINK_LANGUAGE_FAILED_TO_INSTALL",
|
|
7
|
+
LinkLanguageInstalledButNotSynced = "LINK_LANGUAGE_INSTALLED_BUT_NOT_SYNCED",
|
|
8
|
+
Synced = "SYNCED"
|
|
8
9
|
}
|
|
9
10
|
export declare class PerspectiveHandle {
|
|
10
11
|
uuid: string;
|
|
@@ -11,11 +11,12 @@ import { Field, ObjectType } from "type-graphql";
|
|
|
11
11
|
import { NeighbourhoodExpression } from "../neighbourhood/Neighbourhood";
|
|
12
12
|
export var PerspectiveState;
|
|
13
13
|
(function (PerspectiveState) {
|
|
14
|
-
PerspectiveState["Private"] = "
|
|
15
|
-
PerspectiveState["
|
|
16
|
-
PerspectiveState["
|
|
17
|
-
PerspectiveState["
|
|
18
|
-
PerspectiveState["
|
|
14
|
+
PerspectiveState["Private"] = "PRIVATE";
|
|
15
|
+
PerspectiveState["NeighboudhoodCreationInitiated"] = "NEIGHBOURHOOD_CREATION_INITIATED";
|
|
16
|
+
PerspectiveState["NeighbourhoodJoinInitiated"] = "NEIGHBOURHOOD_JOIN_INITIATED";
|
|
17
|
+
PerspectiveState["LinkLanguageFailedToInstall"] = "LINK_LANGUAGE_FAILED_TO_INSTALL";
|
|
18
|
+
PerspectiveState["LinkLanguageInstalledButNotSynced"] = "LINK_LANGUAGE_INSTALLED_BUT_NOT_SYNCED";
|
|
19
|
+
PerspectiveState["Synced"] = "SYNCED";
|
|
19
20
|
})(PerspectiveState || (PerspectiveState = {}));
|
|
20
21
|
// This type is used in the GraphQL interface to reference a mutable
|
|
21
22
|
// prespective that is implemented locally by the Ad4m runtime.
|
|
@@ -153,6 +153,11 @@ export declare class PerspectiveProxy {
|
|
|
153
153
|
* that match the given properties will be used.
|
|
154
154
|
*/
|
|
155
155
|
getAllSubjectInstances<T>(subjectClass: T): Promise<T[]>;
|
|
156
|
+
/** Returns all subject proxies of the given subject class.
|
|
157
|
+
* @param subjectClass Either a string with the name of the subject class, or an object
|
|
158
|
+
* with the properties of the subject class. In the latter case, all subject classes
|
|
159
|
+
* that match the given properties will be used.
|
|
160
|
+
*/
|
|
156
161
|
getAllSubjectProxies<T>(subjectClass: T): Promise<T[]>;
|
|
157
162
|
/** Returns all subject classes that match the given template object.
|
|
158
163
|
* This function looks at the properties of the template object and
|
|
@@ -388,6 +388,11 @@ export class PerspectiveProxy {
|
|
|
388
388
|
}
|
|
389
389
|
return instances;
|
|
390
390
|
}
|
|
391
|
+
/** Returns all subject proxies of the given subject class.
|
|
392
|
+
* @param subjectClass Either a string with the name of the subject class, or an object
|
|
393
|
+
* with the properties of the subject class. In the latter case, all subject classes
|
|
394
|
+
* that match the given properties will be used.
|
|
395
|
+
*/
|
|
391
396
|
async getAllSubjectProxies(subjectClass) {
|
|
392
397
|
let classes = [];
|
|
393
398
|
if (typeof subjectClass === "string") {
|
|
@@ -6,40 +6,183 @@ export declare class PerspectiveAction {
|
|
|
6
6
|
}
|
|
7
7
|
export declare function addLink(source: string, predicate: string, target: string): PerspectiveAction;
|
|
8
8
|
export declare function hasLink(predicate: string): string;
|
|
9
|
-
interface InstanceQueryParams {
|
|
9
|
+
export interface InstanceQueryParams {
|
|
10
|
+
/**
|
|
11
|
+
* An object representing the WHERE clause of the query.
|
|
12
|
+
*/
|
|
10
13
|
where?: object;
|
|
14
|
+
/**
|
|
15
|
+
* A string representing the condition clause of the query.
|
|
16
|
+
*/
|
|
11
17
|
condition?: string;
|
|
12
18
|
}
|
|
13
|
-
|
|
14
|
-
|
|
19
|
+
/**
|
|
20
|
+
* Decorator for querying instances of a subject class.
|
|
21
|
+
*
|
|
22
|
+
* @category Decorators
|
|
23
|
+
*
|
|
24
|
+
* @description
|
|
25
|
+
* NOTE: Only works on methods that return a promise and will throw an error if not used on a method that returns a promise.
|
|
26
|
+
* This will allow you to query for instances of a subject class with custom clauses within the instance clauses.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* // Usage with where clause
|
|
30
|
+
* InstanceQuery({ where: { name: "John" }}) // this will return all the properties of the subject class with the name "John"
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* // Usage with condition clause
|
|
34
|
+
* InstanceQuery({ condition: "triple(Instance, 'age', Age), Age > 18" }) // this will return all the properties of the subject class with the age greater than 18
|
|
35
|
+
*
|
|
36
|
+
* @param {Object} [options] - Query options.
|
|
37
|
+
*/
|
|
38
|
+
export declare function InstanceQuery(options?: InstanceQueryParams): <T>(target: T, key: keyof T, descriptor: PropertyDescriptor) => void;
|
|
39
|
+
export interface PropertyOptions {
|
|
40
|
+
/**
|
|
41
|
+
* The predicate of the property. All properties must have this option.
|
|
42
|
+
*/
|
|
15
43
|
through?: string;
|
|
44
|
+
/**
|
|
45
|
+
* The initial value of the property. Required if the property is marked as required.
|
|
46
|
+
*/
|
|
16
47
|
initial?: string;
|
|
48
|
+
/**
|
|
49
|
+
* Indicates whether the property is required. If true, an initial value must be provided.
|
|
50
|
+
*/
|
|
17
51
|
required?: boolean;
|
|
52
|
+
/**
|
|
53
|
+
* Indicates whether the property is writable. If true, a setter will be available in the prolog engine.
|
|
54
|
+
*/
|
|
18
55
|
writable?: boolean;
|
|
56
|
+
/**
|
|
57
|
+
* The language used to store the property. Can be the default `Literal` Language or a custom language address.
|
|
58
|
+
*/
|
|
19
59
|
resolveLanguage?: string;
|
|
60
|
+
/**
|
|
61
|
+
* Custom getter to get the value of the property in the prolog engine. If not provided, the default getter will be used.
|
|
62
|
+
*/
|
|
20
63
|
getter?: string;
|
|
64
|
+
/**
|
|
65
|
+
* Custom setter to set the value of the property in the prolog engine. Only available if the property is writable.
|
|
66
|
+
*/
|
|
21
67
|
setter?: string;
|
|
68
|
+
/**
|
|
69
|
+
* 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.
|
|
70
|
+
*/
|
|
22
71
|
local?: boolean;
|
|
23
72
|
}
|
|
24
|
-
|
|
25
|
-
|
|
73
|
+
/**
|
|
74
|
+
* Decorator for defining properties of a subject class.
|
|
75
|
+
*
|
|
76
|
+
* @category Decorators
|
|
77
|
+
*
|
|
78
|
+
* @description
|
|
79
|
+
* This will allow you to define properties with different conditions and how they would be defined in proflog engine.
|
|
80
|
+
*
|
|
81
|
+
* - All properties must have a `through` option which is the predicate of the property.
|
|
82
|
+
* -e If the property is required, it must have an `initial` option which is the initial value of the property.
|
|
83
|
+
* - If the property is writable, it will have a setter in prolog engine. A custom setter can be defined with the `setter` option.
|
|
84
|
+
* - 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.
|
|
85
|
+
* - 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.
|
|
86
|
+
* - 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
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* // Usage
|
|
90
|
+
* SubjectProperty({ through: "ad4m://name", initial: "John", required: true }) // this will define a property with the name "ad4m://name" and the initial value "John"
|
|
91
|
+
*
|
|
92
|
+
* @param {PropertyOptions} [opts] - Property options.
|
|
93
|
+
*/
|
|
94
|
+
export declare function SubjectProperty(opts: PropertyOptions): <T>(target: T, key: keyof T) => void;
|
|
95
|
+
export interface FlagOptions {
|
|
96
|
+
/**
|
|
97
|
+
* The predicate of the property. All properties must have this option.
|
|
98
|
+
*/
|
|
26
99
|
through: string;
|
|
100
|
+
/**
|
|
101
|
+
* The value of the property.
|
|
102
|
+
*/
|
|
27
103
|
value: string;
|
|
28
104
|
}
|
|
29
|
-
|
|
105
|
+
/**
|
|
106
|
+
* Decorator for defining flags of a subject class
|
|
107
|
+
*
|
|
108
|
+
* @category Decorators
|
|
109
|
+
*
|
|
110
|
+
* @description
|
|
111
|
+
* The idea behind flag decorator is to define a property that is required and has an initial value.
|
|
112
|
+
* 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.
|
|
113
|
+
*
|
|
114
|
+
* NOTE: Use of Flag is discouraged and should be used only when necessary.
|
|
115
|
+
*
|
|
116
|
+
* - All properties must have a `through` & `initial` option which is the predicate of the property.
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* // Usage
|
|
120
|
+
* SubjectFlag({ through: "ad4m://name", value: "John" }) // this will define a flag with the name "ad4m://name" and the initial value "John"
|
|
121
|
+
*
|
|
122
|
+
* @param {FlagOptions} [opts] Flag options.
|
|
123
|
+
*/
|
|
124
|
+
export declare function SubjectFlag(opts: FlagOptions): <T>(target: T, key: keyof T) => void;
|
|
30
125
|
interface WhereOptions {
|
|
31
126
|
isInstance?: any;
|
|
32
127
|
condition?: string;
|
|
33
128
|
}
|
|
34
|
-
interface CollectionOptions {
|
|
129
|
+
export interface CollectionOptions {
|
|
130
|
+
/**
|
|
131
|
+
* The predicate of the property. All properties must have this option.
|
|
132
|
+
*/
|
|
35
133
|
through: string;
|
|
134
|
+
/**
|
|
135
|
+
* An object representing the WHERE clause of the query.
|
|
136
|
+
*/
|
|
36
137
|
where?: WhereOptions;
|
|
138
|
+
/**
|
|
139
|
+
* 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.
|
|
140
|
+
*/
|
|
37
141
|
local?: boolean;
|
|
38
142
|
}
|
|
39
|
-
|
|
143
|
+
/**
|
|
144
|
+
* Decorator for defining collections of a subject class.
|
|
145
|
+
*
|
|
146
|
+
* @category Decorators
|
|
147
|
+
*
|
|
148
|
+
* @description
|
|
149
|
+
* This will allow you to define collections with different conditions and how they would be defined in proflog engine.
|
|
150
|
+
*
|
|
151
|
+
* NOTE: The property needs to be an array for it to picked up during the initialization phase.
|
|
152
|
+
*
|
|
153
|
+
* - All collections must have a `through` option which is the predicate of the collection.
|
|
154
|
+
* - If the collection has a `where` option, it can be used to define a custom condition for the collection.
|
|
155
|
+
* - 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.
|
|
156
|
+
*
|
|
157
|
+
* @example
|
|
158
|
+
* // Usage
|
|
159
|
+
* SubjectCollection({ through: "ad4m://friends" }) // this will define a collection with the name "ad4m://friends"
|
|
160
|
+
*
|
|
161
|
+
* @param opts Collection options.
|
|
162
|
+
*/
|
|
163
|
+
export declare function SubjectCollection(opts: CollectionOptions): <T>(target: T, key: keyof T) => void;
|
|
40
164
|
export declare function makeRandomPrologAtom(length: number): string;
|
|
41
|
-
interface SDNAClassOptions {
|
|
165
|
+
export interface SDNAClassOptions {
|
|
166
|
+
/**
|
|
167
|
+
* The name of the entity.
|
|
168
|
+
*/
|
|
42
169
|
name: string;
|
|
43
170
|
}
|
|
171
|
+
/**
|
|
172
|
+
* Decorator for defining an SDNA class.
|
|
173
|
+
*
|
|
174
|
+
* @category Decorators
|
|
175
|
+
*
|
|
176
|
+
* @description
|
|
177
|
+
* 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`.
|
|
178
|
+
*
|
|
179
|
+
* Note: This decorator is required for the class to be picked up during the initialization phase.
|
|
180
|
+
*
|
|
181
|
+
* @example
|
|
182
|
+
* // Usage
|
|
183
|
+
* SDNAClass({ name: "Person" }) // this will create a new SDNA class with the name "Person"
|
|
184
|
+
*
|
|
185
|
+
* @param opts SDNA class options.
|
|
186
|
+
*/
|
|
44
187
|
export declare function SDNAClass(opts: SDNAClassOptions): (target: any) => void;
|
|
45
188
|
export {};
|