@coasys/ad4m 0.13.0-postmessage-ws-proxy.0 → 0.13.0-postmessage-ws-proxy.3
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/lib/index.cjs +564 -461
- package/lib/index.js +561 -462
- package/lib/src/index.d.ts +1 -0
- package/lib/src/index.js +1 -0
- package/lib/src/model/Ad4mModel.d.ts +9 -17
- package/lib/src/model/Ad4mModel.js +34 -148
- package/lib/src/model/ModelQueryBuilder.d.ts +5 -10
- package/lib/src/model/ModelQueryBuilder.js +8 -14
- package/lib/src/model/shacl-gen.js +115 -46
- package/lib/src/model/types.d.ts +138 -3
- package/lib/src/perspectives/PerspectiveClient.d.ts +4 -4
- package/lib/src/perspectives/PerspectiveClient.js +5 -6
- package/lib/src/perspectives/PerspectiveProxy.d.ts +8 -7
- package/lib/src/perspectives/PerspectiveProxy.js +10 -9
- package/lib/src/perspectives/SparqlBindings.d.ts +47 -0
- package/lib/src/perspectives/SparqlBindings.js +65 -0
- package/lib/src/shacl/SHACLShape.d.ts +18 -0
- package/lib/src/shacl/SHACLShape.js +83 -0
- package/package.json +8 -11
- package/src/index.ts +1 -0
- package/src/model/Ad4mModel.test.ts +23 -64
- package/src/model/Ad4mModel.ts +52 -159
- package/src/model/ModelQueryBuilder.ts +17 -23
- package/src/model/relation-filtering.test.ts +46 -39
- package/src/model/shacl-gen.ts +122 -53
- package/src/model/shacl-roundtrip.test.ts +197 -0
- package/src/model/types-typed-query.test.ts +360 -0
- package/src/model/types.ts +194 -3
- package/src/perspectives/PerspectiveClient.ts +6 -8
- package/src/perspectives/PerspectiveProxy.ts +13 -13
- package/src/perspectives/SparqlBindings.ts +79 -0
- package/src/shacl/SHACLShape.test.ts +110 -0
- package/src/shacl/SHACLShape.ts +125 -0
package/lib/src/index.d.ts
CHANGED
|
@@ -16,6 +16,7 @@ export * from "./Literal";
|
|
|
16
16
|
export * from "./perspectives/Perspective";
|
|
17
17
|
export * from "./perspectives/PerspectiveHandle";
|
|
18
18
|
export * from "./perspectives/PerspectiveProxy";
|
|
19
|
+
export * from "./perspectives/SparqlBindings";
|
|
19
20
|
export * from "./perspectives/WakerSubscriptionManager";
|
|
20
21
|
export * from "./perspectives/PerspectiveDiff";
|
|
21
22
|
export * from "./perspectives/LinkQuery";
|
package/lib/src/index.js
CHANGED
|
@@ -16,6 +16,7 @@ export * from "./Literal";
|
|
|
16
16
|
export * from "./perspectives/Perspective";
|
|
17
17
|
export * from "./perspectives/PerspectiveHandle";
|
|
18
18
|
export * from "./perspectives/PerspectiveProxy";
|
|
19
|
+
export * from "./perspectives/SparqlBindings";
|
|
19
20
|
export * from "./perspectives/WakerSubscriptionManager";
|
|
20
21
|
export * from "./perspectives/PerspectiveDiff";
|
|
21
22
|
export * from "./perspectives/LinkQuery";
|
|
@@ -2,7 +2,7 @@ import { PerspectiveProxy } from "../perspectives/PerspectiveProxy";
|
|
|
2
2
|
import type { SHACLShape } from "../shacl/SHACLShape";
|
|
3
3
|
import type { JSONSchema, JSONSchemaToModelOptions } from "./json-schema";
|
|
4
4
|
import { ModelQueryBuilder } from "./ModelQueryBuilder";
|
|
5
|
-
import type { ParentScope, IncludeMap, Query, GetOptions, ResultsWithTotalCount, PaginationResult, ModelMetadata } from "./types";
|
|
5
|
+
import type { ParentScope, IncludeMap, Query, GetOptions, ResultsWithTotalCount, PaginationResult, ModelMetadata, TypedQuery, IncludeExtras, IncludeOf } from "./types";
|
|
6
6
|
/**
|
|
7
7
|
* Base class for defining data models in AD4M.
|
|
8
8
|
*
|
|
@@ -300,13 +300,13 @@ export declare class Ad4mModel {
|
|
|
300
300
|
static queryToSPARQL(perspective: PerspectiveProxy, query: Query): Promise<string>;
|
|
301
301
|
/**
|
|
302
302
|
* Build the JSON parameters needed for a model query/subscription endpoint.
|
|
303
|
-
* Returns the className
|
|
303
|
+
* Returns the className and queryJson that the Rust executor expects.
|
|
304
|
+
* Shape resolution happens server-side from the perspective's SHACL triples.
|
|
304
305
|
* @internal
|
|
305
306
|
*/
|
|
306
307
|
static prepareModelQueryParams(query?: Query, classNameOverride?: string | null): {
|
|
307
308
|
className: string;
|
|
308
309
|
queryJson: string;
|
|
309
|
-
shapeJson: string;
|
|
310
310
|
metadata: ModelMetadata;
|
|
311
311
|
};
|
|
312
312
|
/**
|
|
@@ -362,9 +362,7 @@ export declare class Ad4mModel {
|
|
|
362
362
|
* });
|
|
363
363
|
* ```
|
|
364
364
|
*/
|
|
365
|
-
static findAll<T extends Ad4mModel>(this: typeof Ad4mModel & (new (...args: any[]) => T), perspective: PerspectiveProxy, query?:
|
|
366
|
-
/** @deprecated Ignored — Prolog engine has been removed. */
|
|
367
|
-
_engine?: 'sparql' | 'prolog' | boolean): Promise<T[]>;
|
|
365
|
+
static findAll<T extends Ad4mModel, Q extends TypedQuery<T> = {}>(this: typeof Ad4mModel & (new (...args: any[]) => T), perspective: PerspectiveProxy, query?: Q): Promise<(T & IncludeExtras<T, IncludeOf<Q>>)[]>;
|
|
368
366
|
/**
|
|
369
367
|
* Finds the first instance matching the query, or `null` if none exists.
|
|
370
368
|
*
|
|
@@ -385,9 +383,7 @@ export declare class Ad4mModel {
|
|
|
385
383
|
* }
|
|
386
384
|
* ```
|
|
387
385
|
*/
|
|
388
|
-
static findOne<T extends Ad4mModel>(this: typeof Ad4mModel & (new (...args: any[]) => T), perspective: PerspectiveProxy, query?:
|
|
389
|
-
/** @deprecated Ignored — Prolog engine has been removed. */
|
|
390
|
-
_engine?: 'sparql' | 'prolog' | boolean): Promise<T | null>;
|
|
386
|
+
static findOne<T extends Ad4mModel, Q extends TypedQuery<T> = {}>(this: typeof Ad4mModel & (new (...args: any[]) => T), perspective: PerspectiveProxy, query?: Q): Promise<(T & IncludeExtras<T, IncludeOf<Q>>) | null>;
|
|
391
387
|
/**
|
|
392
388
|
* Gets all instances with count of total matches without offset & limit applied.
|
|
393
389
|
*
|
|
@@ -405,9 +401,7 @@ export declare class Ad4mModel {
|
|
|
405
401
|
* console.log(`Showing 10 of ${totalCount} dessert recipes`);
|
|
406
402
|
* ```
|
|
407
403
|
*/
|
|
408
|
-
static findAllAndCount<T extends Ad4mModel>(this: typeof Ad4mModel & (new (...args: any[]) => T), perspective: PerspectiveProxy, query?:
|
|
409
|
-
/** @deprecated Ignored — Prolog engine has been removed. */
|
|
410
|
-
_engine?: 'sparql' | 'prolog' | boolean): Promise<ResultsWithTotalCount<T>>;
|
|
404
|
+
static findAllAndCount<T extends Ad4mModel, Q extends TypedQuery<T> = {}>(this: typeof Ad4mModel & (new (...args: any[]) => T), perspective: PerspectiveProxy, query?: Q): Promise<ResultsWithTotalCount<T & IncludeExtras<T, IncludeOf<Q>>>>;
|
|
411
405
|
/**
|
|
412
406
|
* Helper function for pagination with explicit page size and number.
|
|
413
407
|
*
|
|
@@ -425,9 +419,7 @@ export declare class Ad4mModel {
|
|
|
425
419
|
* console.log(`Page ${page.pageNumber} of recipes, ${page.results.length} items`);
|
|
426
420
|
* ```
|
|
427
421
|
*/
|
|
428
|
-
static paginate<T extends Ad4mModel>(this: typeof Ad4mModel & (new (...args: any[]) => T), perspective: PerspectiveProxy, pageSize: number, pageNumber: number, query?:
|
|
429
|
-
/** @deprecated Ignored — Prolog engine has been removed. */
|
|
430
|
-
_engine?: 'sparql' | 'prolog' | boolean): Promise<PaginationResult<T>>;
|
|
422
|
+
static paginate<T extends Ad4mModel, Q extends TypedQuery<T> = {}>(this: typeof Ad4mModel & (new (...args: any[]) => T), perspective: PerspectiveProxy, pageSize: number, pageNumber: number, query?: Q): Promise<PaginationResult<T & IncludeExtras<T, IncludeOf<Q>>>>;
|
|
431
423
|
/**
|
|
432
424
|
* Generates a SPARQL COUNT query for the model.
|
|
433
425
|
*
|
|
@@ -452,7 +444,7 @@ export declare class Ad4mModel {
|
|
|
452
444
|
* });
|
|
453
445
|
* ```
|
|
454
446
|
*/
|
|
455
|
-
static count(perspective: PerspectiveProxy, query?:
|
|
447
|
+
static count<T extends Ad4mModel>(this: typeof Ad4mModel & (new (...args: any[]) => T), perspective: PerspectiveProxy, query?: TypedQuery<T>): Promise<number>;
|
|
456
448
|
private setProperty;
|
|
457
449
|
/** Resolve a relation argument to a plain string ID. Accepts either a raw
|
|
458
450
|
* string ID or an Ad4mModel instance (in which case `.id` is used). */
|
|
@@ -721,7 +713,7 @@ export declare class Ad4mModel {
|
|
|
721
713
|
* });
|
|
722
714
|
* ```
|
|
723
715
|
*/
|
|
724
|
-
static query<T extends Ad4mModel>(this: typeof Ad4mModel & (new (...args: any[]) => T), perspective: PerspectiveProxy, query?:
|
|
716
|
+
static query<T extends Ad4mModel>(this: typeof Ad4mModel & (new (...args: any[]) => T), perspective: PerspectiveProxy, query?: TypedQuery<T>): ModelQueryBuilder<T>;
|
|
725
717
|
/**
|
|
726
718
|
* Creates an Ad4mModel class from a JSON Schema definition.
|
|
727
719
|
*
|
|
@@ -2,7 +2,7 @@ import { Literal } from "../Literal";
|
|
|
2
2
|
import { Link } from "../links/Links";
|
|
3
3
|
import { LinkQuery } from "../perspectives/LinkQuery";
|
|
4
4
|
import { makeRandomId } from "./util";
|
|
5
|
-
import { getPropertiesMetadata, getRelationsMetadata,
|
|
5
|
+
import { getPropertiesMetadata, getRelationsMetadata, setPropertyRegistryEntry, setRelationRegistryEntry, Model } from "./decorators";
|
|
6
6
|
import { resolveParentPredicate } from "./query-common";
|
|
7
7
|
import { isArrayType, determinePredicate, determineNamespace, buildModelFromJSONSchema } from "./json-schema";
|
|
8
8
|
import { buildSPARQLQuery } from "./query-sparql";
|
|
@@ -53,46 +53,6 @@ function defaultFileDecode(resolved) {
|
|
|
53
53
|
// ---------------------------------------------------------------------------
|
|
54
54
|
// Helpers for Rust-side include resolution
|
|
55
55
|
// ---------------------------------------------------------------------------
|
|
56
|
-
/**
|
|
57
|
-
* Recursively enrich relation metadata in the shape with target class shapes
|
|
58
|
-
* so the Rust endpoint can resolve includes in-process (no extra GraphQL
|
|
59
|
-
* round-trips per relation).
|
|
60
|
-
*/
|
|
61
|
-
function enrichShapeForIncludes(metadata, include, allRelMeta) {
|
|
62
|
-
for (const [relName, includeVal] of Object.entries(include)) {
|
|
63
|
-
if (!includeVal)
|
|
64
|
-
continue;
|
|
65
|
-
const meta = allRelMeta[relName];
|
|
66
|
-
if (!meta?.target)
|
|
67
|
-
continue;
|
|
68
|
-
const TargetClass = meta.target();
|
|
69
|
-
// Deep-copy so we don't mutate the cached metadata
|
|
70
|
-
const targetMeta = JSON.parse(JSON.stringify(TargetClass.getModelMetadata()));
|
|
71
|
-
// Ensure the relation is in metadata.relations (fallback for edge cases)
|
|
72
|
-
if (!metadata.relations[relName]) {
|
|
73
|
-
metadata.relations[relName] = {
|
|
74
|
-
name: relName,
|
|
75
|
-
predicate: meta.predicate,
|
|
76
|
-
direction: meta.kind === 'belongsToMany' || meta.kind === 'belongsToOne'
|
|
77
|
-
? 'reverse'
|
|
78
|
-
: 'forward',
|
|
79
|
-
};
|
|
80
|
-
}
|
|
81
|
-
const rel = metadata.relations[relName];
|
|
82
|
-
rel.kind = meta.kind;
|
|
83
|
-
rel.maxCount = meta.maxCount;
|
|
84
|
-
rel.targetShape = targetMeta;
|
|
85
|
-
rel.targetClassName = targetMeta.className;
|
|
86
|
-
// Recurse for nested includes
|
|
87
|
-
const nested = typeof includeVal === 'object' && includeVal !== null
|
|
88
|
-
? includeVal.include
|
|
89
|
-
: undefined;
|
|
90
|
-
if (nested) {
|
|
91
|
-
const targetRelMeta = getRelationsMetadata(TargetClass);
|
|
92
|
-
enrichShapeForIncludes(targetMeta, nested, targetRelMeta);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
56
|
/**
|
|
97
57
|
* Construct a model class instance from a plain JSON object returned by the
|
|
98
58
|
* Rust endpoint. Recursively converts included relation values (which come
|
|
@@ -774,7 +734,8 @@ export class Ad4mModel {
|
|
|
774
734
|
}
|
|
775
735
|
/**
|
|
776
736
|
* Build the JSON parameters needed for a model query/subscription endpoint.
|
|
777
|
-
* Returns the className
|
|
737
|
+
* Returns the className and queryJson that the Rust executor expects.
|
|
738
|
+
* Shape resolution happens server-side from the perspective's SHACL triples.
|
|
778
739
|
* @internal
|
|
779
740
|
*/
|
|
780
741
|
static prepareModelQueryParams(query = {}, classNameOverride) {
|
|
@@ -814,16 +775,18 @@ export class Ad4mModel {
|
|
|
814
775
|
if (Object.keys(normalIncludes).length > 0)
|
|
815
776
|
queryInput.include = normalIncludes;
|
|
816
777
|
if (Object.keys(projections).length > 0) {
|
|
817
|
-
//
|
|
778
|
+
// Tag each projection with its target class name so the executor can
|
|
779
|
+
// resolve the target shape through its in-memory cache when applying
|
|
780
|
+
// projection where-clause filters.
|
|
818
781
|
const allRelMeta = getRelationsMetadata(this);
|
|
819
782
|
for (const [, proj] of Object.entries(projections)) {
|
|
820
783
|
const relMeta = allRelMeta[proj.from];
|
|
821
|
-
if (!proj.
|
|
784
|
+
if (!proj.targetClassName && relMeta?.target) {
|
|
822
785
|
try {
|
|
823
786
|
const TargetClass = relMeta.target();
|
|
824
787
|
const targetMeta = TargetClass.getModelMetadata?.();
|
|
825
|
-
if (targetMeta) {
|
|
826
|
-
proj.
|
|
788
|
+
if (targetMeta?.className) {
|
|
789
|
+
proj.targetClassName = targetMeta.className;
|
|
827
790
|
}
|
|
828
791
|
}
|
|
829
792
|
catch (e) {
|
|
@@ -846,64 +809,12 @@ export class Ad4mModel {
|
|
|
846
809
|
if (query.count !== undefined)
|
|
847
810
|
queryInput.count = query.count;
|
|
848
811
|
queryInput.deepQuery = query.deepQuery ?? true;
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
}
|
|
853
|
-
// Pre-compute conformance getters for Rust-side evaluation.
|
|
854
|
-
// Where clauses are NOT compiled into getter SPARQL because stored
|
|
855
|
-
// property values are signed expression envelopes (literal:json:...),
|
|
856
|
-
// not simple literal:string:X values. Instead, where clauses are
|
|
857
|
-
// attached as metadata for Rust-side post-evaluation filtering.
|
|
858
|
-
{
|
|
859
|
-
const allRelMeta = getRelationsMetadata(this);
|
|
860
|
-
for (const [relName, relMeta] of Object.entries(metadata.relations)) {
|
|
861
|
-
const rel = relMeta;
|
|
862
|
-
if (rel.getter || rel.direction === 'reverse' || rel.filter === false)
|
|
863
|
-
continue;
|
|
864
|
-
const meta = allRelMeta[relName];
|
|
865
|
-
if (!meta?.target)
|
|
866
|
-
continue;
|
|
867
|
-
try {
|
|
868
|
-
const TargetClass = meta.target();
|
|
869
|
-
const filter = buildConformanceFilter(meta.predicate, TargetClass);
|
|
870
|
-
if (filter) {
|
|
871
|
-
rel.getter = filter.getter;
|
|
872
|
-
}
|
|
873
|
-
// Attach where-clause metadata for Rust-side post-getter filtering
|
|
874
|
-
if (rel.where) {
|
|
875
|
-
try {
|
|
876
|
-
const targetMetadata = TargetClass.getModelMetadata?.() ?? null;
|
|
877
|
-
if (targetMetadata) {
|
|
878
|
-
const predicates = {};
|
|
879
|
-
for (const propName of Object.keys(rel.where)) {
|
|
880
|
-
if (['id', 'author', 'timestamp'].includes(propName))
|
|
881
|
-
continue;
|
|
882
|
-
const propMeta = targetMetadata.properties[propName];
|
|
883
|
-
if (propMeta?.predicate) {
|
|
884
|
-
predicates[propName] = propMeta.predicate;
|
|
885
|
-
}
|
|
886
|
-
}
|
|
887
|
-
if (Object.keys(predicates).length > 0) {
|
|
888
|
-
rel.whereFilter = rel.where;
|
|
889
|
-
rel.wherePredicates = predicates;
|
|
890
|
-
}
|
|
891
|
-
}
|
|
892
|
-
}
|
|
893
|
-
catch (e) {
|
|
894
|
-
console.debug(`prepareModelQueryParams: target metadata unavailable for relation '${relName}':`, e);
|
|
895
|
-
}
|
|
896
|
-
}
|
|
897
|
-
}
|
|
898
|
-
catch (e) {
|
|
899
|
-
console.debug(`prepareModelQueryParams: target class unavailable for relation '${relName}':`, e);
|
|
900
|
-
}
|
|
901
|
-
}
|
|
902
|
-
}
|
|
812
|
+
// Conformance getters, where filters, and target shapes for includes
|
|
813
|
+
// are all resolved by the executor from the perspective's SHACL triples
|
|
814
|
+
// — no client-side pre-computation is needed any more.
|
|
903
815
|
return {
|
|
904
816
|
className,
|
|
905
817
|
queryJson: JSON.stringify(queryInput),
|
|
906
|
-
shapeJson: JSON.stringify(metadata),
|
|
907
818
|
metadata,
|
|
908
819
|
};
|
|
909
820
|
}
|
|
@@ -980,10 +891,10 @@ export class Ad4mModel {
|
|
|
980
891
|
* @internal
|
|
981
892
|
*/
|
|
982
893
|
static async executeModelQuery(perspective, query = {}, classNameOverride) {
|
|
983
|
-
// Delegate
|
|
984
|
-
//
|
|
985
|
-
const { className, queryJson
|
|
986
|
-
const result = await perspective.modelQuery(className, queryJson
|
|
894
|
+
// Delegate query input building to the shared prepareModelQueryParams
|
|
895
|
+
// helper. The executor resolves the shape from SHACL server-side.
|
|
896
|
+
const { className, queryJson } = this.prepareModelQueryParams(query, classNameOverride);
|
|
897
|
+
const result = await perspective.modelQuery(className, queryJson);
|
|
987
898
|
// Convert JSON instances to model class instances, recursively constructing
|
|
988
899
|
// class instances for any included relations resolved by Rust.
|
|
989
900
|
const instances = result.instances.map((json) => {
|
|
@@ -1029,13 +940,12 @@ export class Ad4mModel {
|
|
|
1029
940
|
* });
|
|
1030
941
|
* ```
|
|
1031
942
|
*/
|
|
1032
|
-
static async findAll(perspective, query
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
if (query.properties && query.properties.length === 0) {
|
|
943
|
+
static async findAll(perspective, query) {
|
|
944
|
+
const q = (query ?? {});
|
|
945
|
+
if (q.properties && q.properties.length === 0) {
|
|
1036
946
|
throw new Error("properties[] must not be empty — omit the field to return all properties, or specify at least one field name");
|
|
1037
947
|
}
|
|
1038
|
-
const { results } = await this.executeModelQuery(perspective,
|
|
948
|
+
const { results } = await this.executeModelQuery(perspective, q);
|
|
1039
949
|
return results;
|
|
1040
950
|
}
|
|
1041
951
|
/**
|
|
@@ -1058,10 +968,8 @@ export class Ad4mModel {
|
|
|
1058
968
|
* }
|
|
1059
969
|
* ```
|
|
1060
970
|
*/
|
|
1061
|
-
static async findOne(perspective, query
|
|
1062
|
-
|
|
1063
|
-
_engine) {
|
|
1064
|
-
const limitedQuery = { ...query, limit: 1 };
|
|
971
|
+
static async findOne(perspective, query) {
|
|
972
|
+
const limitedQuery = { ...(query ?? {}), limit: 1 };
|
|
1065
973
|
const results = await this.findAll(perspective, limitedQuery);
|
|
1066
974
|
return results[0] ?? null;
|
|
1067
975
|
}
|
|
@@ -1082,10 +990,9 @@ export class Ad4mModel {
|
|
|
1082
990
|
* console.log(`Showing 10 of ${totalCount} dessert recipes`);
|
|
1083
991
|
* ```
|
|
1084
992
|
*/
|
|
1085
|
-
static async findAllAndCount(perspective, query
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
return await this.executeModelQuery(perspective, query);
|
|
993
|
+
static async findAllAndCount(perspective, query) {
|
|
994
|
+
const out = await this.executeModelQuery(perspective, (query ?? {}));
|
|
995
|
+
return out;
|
|
1089
996
|
}
|
|
1090
997
|
/**
|
|
1091
998
|
* Helper function for pagination with explicit page size and number.
|
|
@@ -1104,12 +1011,10 @@ export class Ad4mModel {
|
|
|
1104
1011
|
* console.log(`Page ${page.pageNumber} of recipes, ${page.results.length} items`);
|
|
1105
1012
|
* ```
|
|
1106
1013
|
*/
|
|
1107
|
-
static async paginate(perspective, pageSize, pageNumber, query
|
|
1108
|
-
|
|
1109
|
-
_engine) {
|
|
1110
|
-
const paginationQuery = { ...(query || {}), limit: pageSize, offset: pageSize * (pageNumber - 1), count: true };
|
|
1014
|
+
static async paginate(perspective, pageSize, pageNumber, query) {
|
|
1015
|
+
const paginationQuery = { ...(query ?? {}), limit: pageSize, offset: pageSize * (pageNumber - 1), count: true };
|
|
1111
1016
|
const { results, totalCount } = await this.executeModelQuery(perspective, paginationQuery);
|
|
1112
|
-
return { results, totalCount, pageSize, pageNumber };
|
|
1017
|
+
return { results: results, totalCount, pageSize, pageNumber };
|
|
1113
1018
|
}
|
|
1114
1019
|
/**
|
|
1115
1020
|
* Generates a SPARQL COUNT query for the model.
|
|
@@ -1136,8 +1041,8 @@ export class Ad4mModel {
|
|
|
1136
1041
|
* });
|
|
1137
1042
|
* ```
|
|
1138
1043
|
*/
|
|
1139
|
-
static async count(perspective, query
|
|
1140
|
-
const { totalCount } = await this.executeModelQuery(perspective, { ...query, limit: 0 });
|
|
1044
|
+
static async count(perspective, query) {
|
|
1045
|
+
const { totalCount } = await this.executeModelQuery(perspective, { ...(query ?? {}), limit: 0 });
|
|
1141
1046
|
return totalCount;
|
|
1142
1047
|
}
|
|
1143
1048
|
async setProperty(key, value, batchId) {
|
|
@@ -1448,29 +1353,10 @@ export class Ad4mModel {
|
|
|
1448
1353
|
if (instances.length === 0)
|
|
1449
1354
|
return;
|
|
1450
1355
|
const metadata = this.getModelMetadata();
|
|
1451
|
-
// Pre-compute conformance getters so the shape sent to Rust includes them
|
|
1452
|
-
const allRelMeta = getRelationsMetadata(this);
|
|
1453
|
-
for (const [relName, relMeta] of Object.entries(metadata.relations)) {
|
|
1454
|
-
const rel = relMeta;
|
|
1455
|
-
if (rel.getter || rel.direction === 'reverse' || rel.filter === false)
|
|
1456
|
-
continue;
|
|
1457
|
-
const meta = allRelMeta[relName];
|
|
1458
|
-
if (!meta?.target)
|
|
1459
|
-
continue;
|
|
1460
|
-
try {
|
|
1461
|
-
const TargetClass = meta.target();
|
|
1462
|
-
const filter = buildConformanceFilter(meta.predicate, TargetClass);
|
|
1463
|
-
if (filter)
|
|
1464
|
-
rel.getter = filter.getter;
|
|
1465
|
-
}
|
|
1466
|
-
catch (e) {
|
|
1467
|
-
console.debug(`evaluateGetters: target class unavailable for relation '${relName}':`, e);
|
|
1468
|
-
}
|
|
1469
|
-
}
|
|
1470
|
-
const shapeJson = JSON.stringify(metadata);
|
|
1471
1356
|
const instanceIds = instances.map(inst => inst.id || inst._baseExpression);
|
|
1472
|
-
//
|
|
1473
|
-
|
|
1357
|
+
// The executor reads getter SPARQL from the perspective's SHACL triples
|
|
1358
|
+
// (written by `addSdna`), so no shape JSON is shipped with the call.
|
|
1359
|
+
const result = await perspective.evaluateGetters(metadata.className, instanceIds, propertyNames);
|
|
1474
1360
|
// Determine which property names were evaluated for snapshot sync
|
|
1475
1361
|
const evaluatedPropertyNames = [];
|
|
1476
1362
|
for (const [propName, propMeta] of Object.entries(metadata.properties)) {
|
|
@@ -1834,7 +1720,7 @@ export class Ad4mModel {
|
|
|
1834
1720
|
* ```
|
|
1835
1721
|
*/
|
|
1836
1722
|
static query(perspective, query) {
|
|
1837
|
-
return new ModelQueryBuilder(perspective, this, query);
|
|
1723
|
+
return new ModelQueryBuilder(perspective, this, (query ?? {}));
|
|
1838
1724
|
}
|
|
1839
1725
|
/**
|
|
1840
1726
|
* Creates an Ad4mModel class from a JSON Schema definition.
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import type { Ad4mModel } from "./Ad4mModel";
|
|
8
8
|
import type { PerspectiveProxy } from "../perspectives/PerspectiveProxy";
|
|
9
|
-
import type {
|
|
9
|
+
import type { Query, PaginationResult, TypedWhere, TypedOrder, TypedIncludeMap, PropertyKeysOf } from "./types";
|
|
10
10
|
/** Query builder for Ad4mModel queries.
|
|
11
11
|
* Allows building queries with a fluent interface and either running them once
|
|
12
12
|
* or subscribing to updates.
|
|
@@ -63,7 +63,7 @@ export declare class ModelQueryBuilder<T extends Ad4mModel> {
|
|
|
63
63
|
* })
|
|
64
64
|
* ```
|
|
65
65
|
*/
|
|
66
|
-
where(conditions:
|
|
66
|
+
where(conditions: TypedWhere<T>): ModelQueryBuilder<T>;
|
|
67
67
|
/**
|
|
68
68
|
* Sets the order for the query results.
|
|
69
69
|
*
|
|
@@ -75,7 +75,7 @@ export declare class ModelQueryBuilder<T extends Ad4mModel> {
|
|
|
75
75
|
* .order({ createdAt: "DESC" })
|
|
76
76
|
* ```
|
|
77
77
|
*/
|
|
78
|
-
order(orderBy:
|
|
78
|
+
order(orderBy: TypedOrder<T>): ModelQueryBuilder<T>;
|
|
79
79
|
/**
|
|
80
80
|
* Sets the maximum number of results to return.
|
|
81
81
|
*
|
|
@@ -156,7 +156,7 @@ export declare class ModelQueryBuilder<T extends Ad4mModel> {
|
|
|
156
156
|
* .properties(["name", "description", "rating"])
|
|
157
157
|
* ```
|
|
158
158
|
*/
|
|
159
|
-
properties(properties: string[]): ModelQueryBuilder<T>;
|
|
159
|
+
properties(properties: PropertyKeysOf<T>[] | string[]): ModelQueryBuilder<T>;
|
|
160
160
|
/**
|
|
161
161
|
* Controls whether SPARQL property getters are evaluated during hydration.
|
|
162
162
|
*
|
|
@@ -202,13 +202,8 @@ export declare class ModelQueryBuilder<T extends Ad4mModel> {
|
|
|
202
202
|
* .run();
|
|
203
203
|
* ```
|
|
204
204
|
*/
|
|
205
|
-
include(map:
|
|
205
|
+
include(map: TypedIncludeMap<T>): ModelQueryBuilder<T>;
|
|
206
206
|
overrideModelClassName(className: string): ModelQueryBuilder<T>;
|
|
207
|
-
/**
|
|
208
|
-
* Sets the query engine to use.
|
|
209
|
-
* @deprecated Prolog engine has been removed. All queries use SPARQL/Rust.
|
|
210
|
-
*/
|
|
211
|
-
engine(_eng: 'sparql' | 'prolog'): ModelQueryBuilder<T>;
|
|
212
207
|
/**
|
|
213
208
|
* Executes the query once and returns the results.
|
|
214
209
|
*
|
|
@@ -256,13 +256,6 @@ export class ModelQueryBuilder {
|
|
|
256
256
|
this.modelClassName = className;
|
|
257
257
|
return this;
|
|
258
258
|
}
|
|
259
|
-
/**
|
|
260
|
-
* Sets the query engine to use.
|
|
261
|
-
* @deprecated Prolog engine has been removed. All queries use SPARQL/Rust.
|
|
262
|
-
*/
|
|
263
|
-
engine(_eng) {
|
|
264
|
-
return this;
|
|
265
|
-
}
|
|
266
259
|
/**
|
|
267
260
|
* Executes the query once and returns the results.
|
|
268
261
|
*
|
|
@@ -344,11 +337,12 @@ export class ModelQueryBuilder {
|
|
|
344
337
|
// Clean up any existing subscription
|
|
345
338
|
this.dispose();
|
|
346
339
|
const ctor = this.ctor;
|
|
347
|
-
// Build the model query params (className, queryJson
|
|
348
|
-
|
|
340
|
+
// Build the model query params (className, queryJson). The executor
|
|
341
|
+
// resolves the shape from the perspective's SHACL triples.
|
|
342
|
+
const { className, queryJson } = ctor.prepareModelQueryParams(this.queryParams, this.modelClassName);
|
|
349
343
|
// Register model subscription via Rust — this builds trigger SPARQL internally,
|
|
350
344
|
// registers the subscription, and runs the initial query in one call.
|
|
351
|
-
const { subscriptionId, result: initialModelResult } = await this.perspective.modelSubscribe(className, queryJson
|
|
345
|
+
const { subscriptionId, result: initialModelResult } = await this.perspective.modelSubscribe(className, queryJson);
|
|
352
346
|
// Convert JSON instances to model class instances
|
|
353
347
|
const parseResults = (raw) => {
|
|
354
348
|
return ctor.parseModelResult(this.perspective, raw, this.queryParams.include, this.queryParams.properties);
|
|
@@ -508,8 +502,8 @@ export class ModelQueryBuilder {
|
|
|
508
502
|
// Clean up any existing subscription
|
|
509
503
|
this.dispose();
|
|
510
504
|
const countParams = { ...this.queryParams, limit: 0 };
|
|
511
|
-
const { className, queryJson
|
|
512
|
-
const { subscriptionId, result: initialModelResult } = await this.perspective.modelSubscribe(className, queryJson
|
|
505
|
+
const { className, queryJson } = this.ctor.prepareModelQueryParams(countParams, this.modelClassName);
|
|
506
|
+
const { subscriptionId, result: initialModelResult } = await this.perspective.modelSubscribe(className, queryJson);
|
|
513
507
|
const parseCount = (raw) => {
|
|
514
508
|
const data = typeof raw === 'string' ? JSON.parse(raw) : raw;
|
|
515
509
|
return data.totalCount ?? 0;
|
|
@@ -644,8 +638,8 @@ export class ModelQueryBuilder {
|
|
|
644
638
|
const subscriptionParams = { ...(this.queryParams || {}) };
|
|
645
639
|
delete subscriptionParams.limit;
|
|
646
640
|
delete subscriptionParams.offset;
|
|
647
|
-
const { className, queryJson
|
|
648
|
-
const { subscriptionId } = await this.perspective.modelSubscribe(className, queryJson
|
|
641
|
+
const { className, queryJson } = ctor.prepareModelQueryParams(subscriptionParams, this.modelClassName);
|
|
642
|
+
const { subscriptionId } = await this.perspective.modelSubscribe(className, queryJson);
|
|
649
643
|
// Build the paginated query for Rust endpoint (count: true fetches both results and totalCount in one call)
|
|
650
644
|
const paginatedQuery = {
|
|
651
645
|
...(this.queryParams || {}),
|