@coasys/ad4m 0.13.0-test-2 → 0.13.0-test-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 +1060 -147
- package/lib/index.js +1054 -147
- package/lib/src/index.d.ts +0 -1
- package/lib/src/index.js +0 -1
- package/lib/src/language/Language.d.ts +26 -214
- package/lib/src/language/Language.js +28 -2
- package/lib/src/model/Ad4mModel.d.ts +25 -1
- package/lib/src/model/Ad4mModel.js +139 -30
- package/lib/src/model/Ad4mModel.test.js +214 -32
- package/lib/src/model/ModelQueryBuilder.d.ts +19 -0
- package/lib/src/model/ModelQueryBuilder.js +62 -18
- package/lib/src/model/hydration-batching.test.d.ts +7 -0
- package/lib/src/model/hydration-batching.test.js +456 -0
- package/lib/src/model/hydration.d.ts +1 -0
- package/lib/src/model/hydration.js +279 -70
- package/lib/src/model/query-sparql.d.ts +78 -1
- package/lib/src/model/query-sparql.js +447 -16
- package/lib/src/model/query-sparql.test.d.ts +1 -0
- package/lib/src/model/query-sparql.test.js +495 -0
- package/lib/src/model/query-utils.js +4 -0
- package/lib/src/model/subscription-delta.test.d.ts +24 -0
- package/lib/src/model/subscription-delta.test.js +180 -0
- package/lib/src/model/subscription-pool.js +48 -2
- package/lib/src/model/types.d.ts +49 -1
- package/lib/src/perspectives/PerspectiveClient.d.ts +2 -0
- package/lib/src/perspectives/PerspectiveClient.js +37 -9
- package/lib/src/perspectives/PerspectiveClient.test.d.ts +1 -0
- package/lib/src/perspectives/PerspectiveClient.test.js +169 -0
- package/lib/src/perspectives/PerspectiveProxy.d.ts +3 -0
- package/lib/src/perspectives/PerspectiveProxy.js +15 -0
- package/package.json +1 -1
- package/src/Ad4mClient.test.ts +56 -1
- package/src/index.ts +0 -1
- package/src/language/Language.ts +54 -275
- package/src/model/Ad4mModel.test.ts +222 -35
- package/src/model/Ad4mModel.ts +144 -30
- package/src/model/ModelQueryBuilder.ts +66 -18
- package/src/model/hydration-batching.test.ts +563 -0
- package/src/model/hydration.ts +249 -47
- package/src/model/query-sparql.test.ts +568 -0
- package/src/model/query-sparql.ts +473 -16
- package/src/model/query-utils.ts +4 -0
- package/src/model/subscription-delta.test.ts +207 -0
- package/src/model/subscription-pool.ts +50 -2
- package/src/model/types.ts +50 -1
- package/src/perspectives/PerspectiveClient.test.ts +204 -0
- package/src/perspectives/PerspectiveClient.ts +36 -7
- package/src/perspectives/PerspectiveProxy.ts +18 -0
- package/lib/src/model/query-surreal.d.ts +0 -44
- package/lib/src/model/query-surreal.js +0 -249
- package/lib/src/model/surreal-utils.d.ts +0 -42
- package/lib/src/model/surreal-utils.js +0 -128
- package/src/language/LanguageContext.ts +0 -37
- package/tsconfig-browser.json +0 -82
|
@@ -11,7 +11,8 @@ import type {
|
|
|
11
11
|
Where, Order, IncludeMap, Query,
|
|
12
12
|
AllInstancesResult, ResultsWithTotalCount, PaginationResult,
|
|
13
13
|
} from "./types";
|
|
14
|
-
import { groupSPARQLResults } from "./query-sparql";
|
|
14
|
+
import { groupSPARQLResults, hasJsOnlyWhereFilters, parseSparqlCount } from "./query-sparql";
|
|
15
|
+
import { getRelationsMetadata } from "./decorators";
|
|
15
16
|
import { pooledSubscribe } from "./subscription-pool";
|
|
16
17
|
|
|
17
18
|
/** Query builder for Ad4mModel queries.
|
|
@@ -226,6 +227,29 @@ export class ModelQueryBuilder<T extends Ad4mModel> {
|
|
|
226
227
|
return this;
|
|
227
228
|
}
|
|
228
229
|
|
|
230
|
+
/**
|
|
231
|
+
* Opts in to evaluating SPARQL property getters during collection hydration.
|
|
232
|
+
*
|
|
233
|
+
* By default, collection queries skip getter evaluation for performance.
|
|
234
|
+
* Call `.deepQuery()` when you need getter-backed properties in the result set.
|
|
235
|
+
*
|
|
236
|
+
* @returns The query builder for chaining
|
|
237
|
+
*
|
|
238
|
+
* @example
|
|
239
|
+
* ```typescript
|
|
240
|
+
* const messages = await Message.query(perspective)
|
|
241
|
+
* .parent(channel)
|
|
242
|
+
* .deepQuery()
|
|
243
|
+
* .limit(30)
|
|
244
|
+
* .get();
|
|
245
|
+
* // messages[i].replyingTo is populated
|
|
246
|
+
* ```
|
|
247
|
+
*/
|
|
248
|
+
deepQuery(): ModelQueryBuilder<T> {
|
|
249
|
+
(this.queryParams as any).deepQuery = true;
|
|
250
|
+
return this;
|
|
251
|
+
}
|
|
252
|
+
|
|
229
253
|
/**
|
|
230
254
|
* Specifies which relations to eager-load (hydrate into model instances).
|
|
231
255
|
*
|
|
@@ -316,9 +340,9 @@ export class ModelQueryBuilder<T extends Ad4mModel> {
|
|
|
316
340
|
* Groups raw SPARQL results and hydrates them into model instances.
|
|
317
341
|
* Centralises the repeated groupSPARQLResults → instancesFromQueryResult pipeline.
|
|
318
342
|
*/
|
|
319
|
-
private async processSparqlResult(rawResult: any, queryOverride?: Query): Promise<{ results: T[], totalCount: number }> {
|
|
343
|
+
private async processSparqlResult(rawResult: any, queryOverride?: Query, computeTotalCount: boolean = false): Promise<{ results: T[], totalCount: number }> {
|
|
320
344
|
const grouped = groupSPARQLResults(Array.isArray(rawResult) ? rawResult : []);
|
|
321
|
-
return await this.ctor.instancesFromQueryResult(this.perspective, queryOverride || this.queryParams, grouped) as { results: T[], totalCount: number };
|
|
345
|
+
return await this.ctor.instancesFromQueryResult(this.perspective, queryOverride || this.queryParams, grouped, computeTotalCount) as { results: T[], totalCount: number };
|
|
322
346
|
}
|
|
323
347
|
|
|
324
348
|
/**
|
|
@@ -386,9 +410,11 @@ export class ModelQueryBuilder<T extends Ad4mModel> {
|
|
|
386
410
|
|
|
387
411
|
const buildFingerprint = (results: any[]) => {
|
|
388
412
|
if (results.length === 0) return '0:';
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
return
|
|
413
|
+
// Must include relation data (e.g. tags) — not just id/timestamp.
|
|
414
|
+
// Otherwise @HasMany changes on existing instances are suppressed as duplicates.
|
|
415
|
+
return JSON.stringify(results, (_, v) =>
|
|
416
|
+
typeof v === 'function' ? undefined : v
|
|
417
|
+
);
|
|
392
418
|
};
|
|
393
419
|
|
|
394
420
|
const hydrate = async (rawResult: any) => {
|
|
@@ -469,10 +495,19 @@ export class ModelQueryBuilder<T extends Ad4mModel> {
|
|
|
469
495
|
*/
|
|
470
496
|
async count(): Promise<number> {
|
|
471
497
|
if (this.engineFlag === 'sparql') {
|
|
472
|
-
|
|
473
|
-
const
|
|
474
|
-
const
|
|
475
|
-
|
|
498
|
+
// When query has JS-only where filters, fall back to full hydration
|
|
499
|
+
const metadata = this.ctor.getModelMetadata();
|
|
500
|
+
const allRelMeta = getRelationsMetadata(this.ctor as any);
|
|
501
|
+
if (hasJsOnlyWhereFilters(metadata, allRelMeta, this.queryParams.where)) {
|
|
502
|
+
const sparqlQuery = await this.ctor.queryToSPARQL(this.perspective, this.queryParams);
|
|
503
|
+
const rawResult = await this.perspective.querySparql(sparqlQuery);
|
|
504
|
+
const { totalCount } = await this.processSparqlResult(rawResult, undefined, true);
|
|
505
|
+
return totalCount;
|
|
506
|
+
}
|
|
507
|
+
// Use efficient COUNT query — no hydration
|
|
508
|
+
const countSparql = await this.ctor.countQueryToSPARQL(this.perspective, this.queryParams);
|
|
509
|
+
const countResult = await this.perspective.querySparql(countSparql);
|
|
510
|
+
return parseSparqlCount(countResult) ?? 0;
|
|
476
511
|
} else {
|
|
477
512
|
const query = await this.ctor.countQueryToProlog(this.perspective, this.queryParams, this.modelClassName);
|
|
478
513
|
const result = await this.perspective.infer(query);
|
|
@@ -534,12 +569,12 @@ export class ModelQueryBuilder<T extends Ad4mModel> {
|
|
|
534
569
|
this.currentSubscription = await this.perspective.subscribeQuery(sparqlQuery);
|
|
535
570
|
|
|
536
571
|
const processResults = async (result: any) => {
|
|
537
|
-
const { totalCount } = await this.processSparqlResult(result);
|
|
572
|
+
const { totalCount } = await this.processSparqlResult(result, undefined, true);
|
|
538
573
|
callback(totalCount);
|
|
539
574
|
};
|
|
540
575
|
|
|
541
576
|
this.currentSubscription.onResult(processResults);
|
|
542
|
-
const { totalCount } = await this.processSparqlResult(this.currentSubscription.result);
|
|
577
|
+
const { totalCount } = await this.processSparqlResult(this.currentSubscription.result, undefined, true);
|
|
543
578
|
// Initial count returned via Promise — callback for updates only
|
|
544
579
|
return totalCount;
|
|
545
580
|
} else {
|
|
@@ -568,12 +603,12 @@ export class ModelQueryBuilder<T extends Ad4mModel> {
|
|
|
568
603
|
this.currentSubscription = await this.perspective.subscribeQuery(sparqlQuery);
|
|
569
604
|
|
|
570
605
|
const processResults = async (result: any) => {
|
|
571
|
-
const { totalCount } = await this.processSparqlResult(result);
|
|
606
|
+
const { totalCount } = await this.processSparqlResult(result, undefined, true);
|
|
572
607
|
callback(totalCount);
|
|
573
608
|
};
|
|
574
609
|
|
|
575
610
|
this.currentSubscription.onResult(processResults);
|
|
576
|
-
const { totalCount } = await this.processSparqlResult(this.currentSubscription.result);
|
|
611
|
+
const { totalCount } = await this.processSparqlResult(this.currentSubscription.result, undefined, true);
|
|
577
612
|
// Initial count returned via Promise — callback for updates only
|
|
578
613
|
return totalCount;
|
|
579
614
|
}
|
|
@@ -598,7 +633,7 @@ export class ModelQueryBuilder<T extends Ad4mModel> {
|
|
|
598
633
|
if (this.engineFlag === 'sparql') {
|
|
599
634
|
const sparqlQuery = await this.ctor.queryToSPARQL(this.perspective, paginationQuery);
|
|
600
635
|
const result = await this.perspective.querySparql(sparqlQuery);
|
|
601
|
-
const { results, totalCount } = await this.processSparqlResult(result, paginationQuery) as ResultsWithTotalCount<T>;
|
|
636
|
+
const { results, totalCount } = await this.processSparqlResult(result, paginationQuery, true) as ResultsWithTotalCount<T>;
|
|
602
637
|
return { results, totalCount, pageSize, pageNumber };
|
|
603
638
|
} else {
|
|
604
639
|
const prologQuery = await this.ctor.queryToProlog(this.perspective, paginationQuery, this.modelClassName);
|
|
@@ -666,16 +701,29 @@ export class ModelQueryBuilder<T extends Ad4mModel> {
|
|
|
666
701
|
const paginationQuery = { ...(this.queryParams || {}), limit: pageSize, offset: pageSize * (pageNumber - 1), count: true };
|
|
667
702
|
|
|
668
703
|
if (this.engineFlag === 'sparql') {
|
|
669
|
-
|
|
704
|
+
// Subscribe to the FULL result set (no LIMIT/OFFSET) so the subscription
|
|
705
|
+
// detects changes anywhere in the dataset (e.g. new items beyond current page).
|
|
706
|
+
// Pagination is applied in JS when processing each update.
|
|
707
|
+
const subscriptionParams = { ...(this.queryParams || {}) };
|
|
708
|
+
delete subscriptionParams.limit;
|
|
709
|
+
delete subscriptionParams.offset;
|
|
710
|
+
const sparqlQuery = await this.ctor.queryToSPARQL(this.perspective, subscriptionParams);
|
|
670
711
|
this.currentSubscription = await this.perspective.subscribeQuery(sparqlQuery);
|
|
671
712
|
|
|
713
|
+
// Query for processSparqlResult has NO limit/offset — gets all results, totalCount via count:true
|
|
714
|
+
const fullQuery = { ...(this.queryParams || {}), count: true };
|
|
715
|
+
|
|
672
716
|
const processResults = async (result: any) => {
|
|
673
|
-
const { results, totalCount } = await this.processSparqlResult(result,
|
|
717
|
+
const { results: allResults, totalCount } = await this.processSparqlResult(result, fullQuery, true) as ResultsWithTotalCount<T>;
|
|
718
|
+
const start = pageSize * (pageNumber - 1);
|
|
719
|
+
const results = allResults.slice(start, start + pageSize);
|
|
674
720
|
callback({ results, totalCount, pageSize, pageNumber });
|
|
675
721
|
};
|
|
676
722
|
|
|
677
723
|
this.currentSubscription.onResult(processResults);
|
|
678
|
-
const { results, totalCount } = await this.processSparqlResult(this.currentSubscription.result,
|
|
724
|
+
const { results: allResults, totalCount } = await this.processSparqlResult(this.currentSubscription.result, fullQuery, true) as ResultsWithTotalCount<T>;
|
|
725
|
+
const start = pageSize * (pageNumber - 1);
|
|
726
|
+
const results = allResults.slice(start, start + pageSize);
|
|
679
727
|
const initialPage = { results, totalCount, pageSize, pageNumber };
|
|
680
728
|
// Initial page returned via Promise — callback for subsequent updates only
|
|
681
729
|
return initialPage;
|