@nest-yalc-2/omnikernel-module 2.2.2 → 2.2.6

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.
Files changed (74) hide show
  1. package/package.json +17 -15
  2. package/src/__tests__/omni-collection.entity.spec.ts +49 -49
  3. package/src/__tests__/omni-collection.service.spec.ts +83 -83
  4. package/src/__tests__/omni-document.entity.spec.ts +55 -55
  5. package/src/__tests__/omni-document.service.spec.ts +83 -83
  6. package/src/__tests__/omni-extension-projection-constraints.spec.ts +518 -518
  7. package/src/__tests__/omni-extension-projection.registration.spec.ts +947 -946
  8. package/src/__tests__/omni-extension-projection.service.spec.ts +806 -806
  9. package/src/__tests__/omni-external-ref.entity.spec.ts +36 -36
  10. package/src/__tests__/omni-external-ref.service.spec.ts +252 -252
  11. package/src/__tests__/omni-named.entity.spec.ts +33 -33
  12. package/src/__tests__/omni-record.entity.spec.ts +32 -32
  13. package/src/__tests__/omni-record.service.spec.ts +107 -107
  14. package/src/__tests__/omni-relation-semantics.spec.ts +102 -102
  15. package/src/__tests__/omni-relation.entity.spec.ts +38 -38
  16. package/src/__tests__/omni-scope-substrate.spec.ts +81 -81
  17. package/src/__tests__/omni-scoped.backend.spec.ts +115 -115
  18. package/src/__tests__/omni-scoped.service.spec.ts +354 -354
  19. package/src/__tests__/omnikernel.diagnostics.spec.ts +94 -94
  20. package/src/__tests__/omnikernel.module.spec.ts +38 -40
  21. package/src/__tests__/omnikernel.persistence.spec.ts +154 -154
  22. package/src/__tests__/omnikernel.public-api.spec.ts +238 -238
  23. package/src/__tests__/omnikernel.query.service.spec.ts +155 -155
  24. package/src/base/omni-base.entity.ts +41 -41
  25. package/src/base/omni-external-ref.entity.ts +53 -53
  26. package/src/base/omni-named.entity.ts +16 -16
  27. package/src/base/omni-record.entity.ts +51 -51
  28. package/src/base/omni-relation.entity.ts +72 -72
  29. package/src/index.ts +49 -49
  30. package/src/omni-collection-kind.enum.ts +10 -10
  31. package/src/omni-collection.backend.ts +16 -16
  32. package/src/omni-collection.dto.ts +170 -170
  33. package/src/omni-collection.entity.ts +21 -21
  34. package/src/omni-collection.service.ts +75 -75
  35. package/src/omni-document-kind.enum.ts +12 -12
  36. package/src/omni-document.backend.ts +16 -16
  37. package/src/omni-document.dto.ts +198 -198
  38. package/src/omni-document.entity.ts +30 -30
  39. package/src/omni-document.service.ts +75 -75
  40. package/src/omni-dto.helpers.ts +21 -21
  41. package/src/omni-extension-projection.definition.ts +294 -294
  42. package/src/omni-extension-projection.resource.ts +274 -274
  43. package/src/omni-extension-projection.service.ts +452 -452
  44. package/src/omni-external-ref-binding.validator.ts +53 -53
  45. package/src/omni-external-ref-internal-type.enum.ts +11 -11
  46. package/src/omni-external-ref.backend.ts +25 -25
  47. package/src/omni-external-ref.dto.ts +128 -128
  48. package/src/omni-external-ref.service.ts +255 -255
  49. package/src/omni-migration.ts +365 -365
  50. package/src/omni-named.backend.ts +11 -11
  51. package/src/omni-named.dto.ts +86 -86
  52. package/src/omni-projection.catalog.ts +345 -345
  53. package/src/omni-projection.lifecycle.ts +53 -53
  54. package/src/omni-record-status.enum.ts +11 -11
  55. package/src/omni-record.backend.ts +19 -19
  56. package/src/omni-record.dto.ts +157 -157
  57. package/src/omni-record.service.ts +113 -113
  58. package/src/omni-relation-kind.contract.ts +49 -49
  59. package/src/omni-relation-kind.enum.ts +12 -12
  60. package/src/omni-relation-projection.definition.ts +166 -166
  61. package/src/omni-relation-projection.resource.ts +360 -360
  62. package/src/omni-relation-projection.service.ts +610 -610
  63. package/src/omni-relation-semantics.ts +52 -52
  64. package/src/omni-relation-status.enum.ts +11 -11
  65. package/src/omni-relation.backend.ts +21 -21
  66. package/src/omni-relation.dto.ts +142 -142
  67. package/src/omni-relation.service.ts +159 -159
  68. package/src/omni-scope.ts +161 -161
  69. package/src/omni-scoped.backend.ts +110 -110
  70. package/src/omni-scoped.repository.ts +34 -34
  71. package/src/omni-scoped.service.ts +338 -338
  72. package/src/omnikernel.diagnostics.ts +83 -83
  73. package/src/omnikernel.module.ts +97 -97
  74. package/src/omnikernel.query.service.ts +108 -108
@@ -1,83 +1,83 @@
1
- import type { DataSource } from 'typeorm';
2
-
3
- export interface OmniKernelQueryPlanEvidence {
4
- dialect: 'sqlite' | 'postgres';
5
- recordGridPlan: readonly string[];
6
- relationSourcePlan: readonly string[];
7
- usesRecordGridIndex: boolean;
8
- usesRelationSourceIndex: boolean;
9
- }
10
-
11
- const recordGridIndex = 'omni_record_scope_kind_status_guid_idx';
12
- const relationSourceIndex =
13
- 'omni_relation_scope_source_kind_status_created_guid_idx';
14
-
15
- /**
16
- * Bounded operator evidence for the two high-fanout generated read shapes.
17
- * This is diagnostic-only: CRUD continues to use the normal TypeORM path.
18
- */
19
- export async function collectOmniKernelQueryPlanEvidence(
20
- dataSource: DataSource,
21
- sample: {
22
- scopeId: string;
23
- recordKind: string;
24
- recordStatus: string;
25
- sourceRecordId: string;
26
- relationKind: string;
27
- relationStatus: string;
28
- },
29
- ): Promise<OmniKernelQueryPlanEvidence> {
30
- const dialect = dataSource.options.type;
31
- if (dialect !== 'sqlite' && dialect !== 'postgres') {
32
- throw new TypeError(
33
- 'OmniKernel diagnostics support sqlite and postgres only.',
34
- );
35
- }
36
-
37
- const statements =
38
- dialect === 'sqlite'
39
- ? {
40
- record: `EXPLAIN QUERY PLAN SELECT "guid" FROM "omni-record" WHERE "scopeId" = ? AND "kind" = ? AND "status" = ? AND "deletedAt" IS NULL ORDER BY "guid"`,
41
- relation: `EXPLAIN QUERY PLAN SELECT "guid" FROM "omni-relation" WHERE "scopeId" = ? AND "sourceRecordId" = ? AND "kind" = ? AND "status" = ? ORDER BY "createdAt", "guid"`,
42
- }
43
- : {
44
- record: `EXPLAIN (FORMAT JSON) SELECT "guid" FROM "omni-record" WHERE "scopeId" = $1 AND "kind" = $2 AND "status" = $3 AND "deletedAt" IS NULL ORDER BY "guid"`,
45
- relation: `EXPLAIN (FORMAT JSON) SELECT "guid" FROM "omni-relation" WHERE "scopeId" = $1 AND "sourceRecordId" = $2 AND "kind" = $3 AND "status" = $4 ORDER BY "createdAt", "guid"`,
46
- };
47
-
48
- const recordResult = await dataSource.query(statements.record, [
49
- sample.scopeId,
50
- sample.recordKind,
51
- sample.recordStatus,
52
- ]);
53
- const relationResult = await dataSource.query(statements.relation, [
54
- sample.scopeId,
55
- sample.sourceRecordId,
56
- sample.relationKind,
57
- sample.relationStatus,
58
- ]);
59
- const recordGridPlan = flattenPlan(recordResult);
60
- const relationSourcePlan = flattenPlan(relationResult);
61
-
62
- return {
63
- dialect,
64
- recordGridPlan,
65
- relationSourcePlan,
66
- usesRecordGridIndex: containsIndex(recordGridPlan, recordGridIndex),
67
- usesRelationSourceIndex: containsIndex(
68
- relationSourcePlan,
69
- relationSourceIndex,
70
- ),
71
- };
72
- }
73
-
74
- function flattenPlan(result: unknown): string[] {
75
- return JSON.stringify(result)
76
- .replaceAll('\\n', ' ')
77
- .split(/(?<=\}),|\n/)
78
- .filter((line) => line.length > 0);
79
- }
80
-
81
- function containsIndex(plan: readonly string[], indexName: string): boolean {
82
- return plan.some((line) => line.includes(indexName));
83
- }
1
+ import type { DataSource } from 'typeorm';
2
+
3
+ export interface OmniKernelQueryPlanEvidence {
4
+ dialect: 'sqlite' | 'postgres';
5
+ recordGridPlan: readonly string[];
6
+ relationSourcePlan: readonly string[];
7
+ usesRecordGridIndex: boolean;
8
+ usesRelationSourceIndex: boolean;
9
+ }
10
+
11
+ const recordGridIndex = 'omni_record_scope_kind_status_guid_idx';
12
+ const relationSourceIndex =
13
+ 'omni_relation_scope_source_kind_status_created_guid_idx';
14
+
15
+ /**
16
+ * Bounded operator evidence for the two high-fanout generated read shapes.
17
+ * This is diagnostic-only: CRUD continues to use the normal TypeORM path.
18
+ */
19
+ export async function collectOmniKernelQueryPlanEvidence(
20
+ dataSource: DataSource,
21
+ sample: {
22
+ scopeId: string;
23
+ recordKind: string;
24
+ recordStatus: string;
25
+ sourceRecordId: string;
26
+ relationKind: string;
27
+ relationStatus: string;
28
+ },
29
+ ): Promise<OmniKernelQueryPlanEvidence> {
30
+ const dialect = dataSource.options.type;
31
+ if (dialect !== 'sqlite' && dialect !== 'postgres') {
32
+ throw new TypeError(
33
+ 'OmniKernel diagnostics support sqlite and postgres only.',
34
+ );
35
+ }
36
+
37
+ const statements =
38
+ dialect === 'sqlite'
39
+ ? {
40
+ record: `EXPLAIN QUERY PLAN SELECT "guid" FROM "omni-record" WHERE "scopeId" = ? AND "kind" = ? AND "status" = ? AND "deletedAt" IS NULL ORDER BY "guid"`,
41
+ relation: `EXPLAIN QUERY PLAN SELECT "guid" FROM "omni-relation" WHERE "scopeId" = ? AND "sourceRecordId" = ? AND "kind" = ? AND "status" = ? ORDER BY "createdAt", "guid"`,
42
+ }
43
+ : {
44
+ record: `EXPLAIN (FORMAT JSON) SELECT "guid" FROM "omni-record" WHERE "scopeId" = $1 AND "kind" = $2 AND "status" = $3 AND "deletedAt" IS NULL ORDER BY "guid"`,
45
+ relation: `EXPLAIN (FORMAT JSON) SELECT "guid" FROM "omni-relation" WHERE "scopeId" = $1 AND "sourceRecordId" = $2 AND "kind" = $3 AND "status" = $4 ORDER BY "createdAt", "guid"`,
46
+ };
47
+
48
+ const recordResult = await dataSource.query(statements.record, [
49
+ sample.scopeId,
50
+ sample.recordKind,
51
+ sample.recordStatus,
52
+ ]);
53
+ const relationResult = await dataSource.query(statements.relation, [
54
+ sample.scopeId,
55
+ sample.sourceRecordId,
56
+ sample.relationKind,
57
+ sample.relationStatus,
58
+ ]);
59
+ const recordGridPlan = flattenPlan(recordResult);
60
+ const relationSourcePlan = flattenPlan(relationResult);
61
+
62
+ return {
63
+ dialect,
64
+ recordGridPlan,
65
+ relationSourcePlan,
66
+ usesRecordGridIndex: containsIndex(recordGridPlan, recordGridIndex),
67
+ usesRelationSourceIndex: containsIndex(
68
+ relationSourcePlan,
69
+ relationSourceIndex,
70
+ ),
71
+ };
72
+ }
73
+
74
+ function flattenPlan(result: unknown): string[] {
75
+ return JSON.stringify(result)
76
+ .replaceAll('\\n', ' ')
77
+ .split(/(?<=\}),|\n/)
78
+ .filter((line) => line.length > 0);
79
+ }
80
+
81
+ function containsIndex(plan: readonly string[], indexName: string): boolean {
82
+ return plan.some((line) => line.includes(indexName));
83
+ }
@@ -1,97 +1,97 @@
1
- import { DynamicModule, Module } from '@nestjs/common';
2
- import { EventEmitter2 } from '@nestjs/event-emitter';
3
- import { TypeOrmModule } from '@nestjs/typeorm';
4
- import { OmniExternalRefEntity } from './base/omni-external-ref.entity.js';
5
- import { OmniNamedEntity } from './base/omni-named.entity.js';
6
- import { OmniRecordEntity } from './base/omni-record.entity.js';
7
- import { OmniRelationEntity } from './base/omni-relation.entity.js';
8
- import { OmniCollectionEntity } from './omni-collection.entity.js';
9
- import { omniCollectionBackendProvidersFactory } from './omni-collection.backend.js';
10
- import { OmniDocumentEntity } from './omni-document.entity.js';
11
- import { omniDocumentBackendProvidersFactory } from './omni-document.backend.js';
12
- import { omniExternalRefBackendProvidersFactory } from './omni-external-ref.backend.js';
13
- import { omniNamedBackendProvidersFactory } from './omni-named.backend.js';
14
- import { omniRecordBackendProvidersFactory } from './omni-record.backend.js';
15
- import { omniRelationBackendProvidersFactory } from './omni-relation.backend.js';
16
- import {
17
- OmniKernelQueryService,
18
- omniKernelQueryServiceProviderFactory,
19
- } from './omnikernel.query.service.js';
20
- import {
21
- OMNI_KERNEL_OPTIONS,
22
- OmniScopeContext,
23
- normalizeOmniKernelRegistrationOptions,
24
- type OmniKernelRegistrationOptions,
25
- } from './omni-scope.js';
26
-
27
- @Module({})
28
- export class OmniKernelModule {
29
- static register(
30
- registration: string | OmniKernelRegistrationOptions,
31
- ): DynamicModule {
32
- const options = normalizeOmniKernelRegistrationOptions(registration);
33
- const { dbConnection } = options;
34
- const omniNamedProviders =
35
- omniNamedBackendProvidersFactory(dbConnection).providers;
36
- const omniRecordProviders = omniRecordBackendProvidersFactory(
37
- dbConnection,
38
- options.reservedRecordKinds,
39
- ).providers;
40
- const omniRelationProviders =
41
- omniRelationBackendProvidersFactory(dbConnection).providers;
42
- const omniCollectionProviders =
43
- omniCollectionBackendProvidersFactory(dbConnection).providers;
44
- const omniDocumentProviders =
45
- omniDocumentBackendProvidersFactory(dbConnection).providers;
46
- const omniExternalRefProviders =
47
- omniExternalRefBackendProvidersFactory(dbConnection).providers;
48
- const omniKernelQueryServiceProvider =
49
- omniKernelQueryServiceProviderFactory(dbConnection);
50
- const eventEmitter = new EventEmitter2();
51
-
52
- return {
53
- module: OmniKernelModule,
54
- imports: [
55
- TypeOrmModule.forFeature(
56
- [
57
- OmniNamedEntity,
58
- OmniRecordEntity,
59
- OmniRelationEntity,
60
- OmniCollectionEntity,
61
- OmniDocumentEntity,
62
- OmniExternalRefEntity,
63
- ],
64
- dbConnection,
65
- ),
66
- ],
67
- providers: [
68
- { provide: OMNI_KERNEL_OPTIONS, useValue: options },
69
- OmniScopeContext,
70
- {
71
- provide: EventEmitter2,
72
- useValue: eventEmitter,
73
- },
74
- ...omniNamedProviders,
75
- ...omniRecordProviders,
76
- ...omniRelationProviders,
77
- ...omniCollectionProviders,
78
- ...omniDocumentProviders,
79
- ...omniExternalRefProviders,
80
- omniKernelQueryServiceProvider,
81
- ],
82
- exports: [
83
- OMNI_KERNEL_OPTIONS,
84
- OmniScopeContext,
85
- EventEmitter2,
86
- ...omniNamedProviders,
87
- ...omniRecordProviders,
88
- ...omniRelationProviders,
89
- ...omniCollectionProviders,
90
- ...omniDocumentProviders,
91
- ...omniExternalRefProviders,
92
- omniKernelQueryServiceProvider,
93
- OmniKernelQueryService,
94
- ],
95
- };
96
- }
97
- }
1
+ import { DynamicModule, Module } from '@nestjs/common';
2
+ import { EventEmitter2 } from '@nestjs/event-emitter';
3
+ import { TypeOrmModule } from '@nestjs/typeorm';
4
+ import { OmniExternalRefEntity } from './base/omni-external-ref.entity.js';
5
+ import { OmniNamedEntity } from './base/omni-named.entity.js';
6
+ import { OmniRecordEntity } from './base/omni-record.entity.js';
7
+ import { OmniRelationEntity } from './base/omni-relation.entity.js';
8
+ import { OmniCollectionEntity } from './omni-collection.entity.js';
9
+ import { omniCollectionBackendProvidersFactory } from './omni-collection.backend.js';
10
+ import { OmniDocumentEntity } from './omni-document.entity.js';
11
+ import { omniDocumentBackendProvidersFactory } from './omni-document.backend.js';
12
+ import { omniExternalRefBackendProvidersFactory } from './omni-external-ref.backend.js';
13
+ import { omniNamedBackendProvidersFactory } from './omni-named.backend.js';
14
+ import { omniRecordBackendProvidersFactory } from './omni-record.backend.js';
15
+ import { omniRelationBackendProvidersFactory } from './omni-relation.backend.js';
16
+ import {
17
+ OmniKernelQueryService,
18
+ omniKernelQueryServiceProviderFactory,
19
+ } from './omnikernel.query.service.js';
20
+ import {
21
+ OMNI_KERNEL_OPTIONS,
22
+ OmniScopeContext,
23
+ normalizeOmniKernelRegistrationOptions,
24
+ type OmniKernelRegistrationOptions,
25
+ } from './omni-scope.js';
26
+
27
+ @Module({})
28
+ export class OmniKernelModule {
29
+ static register(
30
+ registration: string | OmniKernelRegistrationOptions,
31
+ ): DynamicModule {
32
+ const options = normalizeOmniKernelRegistrationOptions(registration);
33
+ const { dbConnection } = options;
34
+ const omniNamedProviders =
35
+ omniNamedBackendProvidersFactory(dbConnection).providers;
36
+ const omniRecordProviders = omniRecordBackendProvidersFactory(
37
+ dbConnection,
38
+ options.reservedRecordKinds,
39
+ ).providers;
40
+ const omniRelationProviders =
41
+ omniRelationBackendProvidersFactory(dbConnection).providers;
42
+ const omniCollectionProviders =
43
+ omniCollectionBackendProvidersFactory(dbConnection).providers;
44
+ const omniDocumentProviders =
45
+ omniDocumentBackendProvidersFactory(dbConnection).providers;
46
+ const omniExternalRefProviders =
47
+ omniExternalRefBackendProvidersFactory(dbConnection).providers;
48
+ const omniKernelQueryServiceProvider =
49
+ omniKernelQueryServiceProviderFactory(dbConnection);
50
+ const eventEmitter = new EventEmitter2();
51
+
52
+ return {
53
+ module: OmniKernelModule,
54
+ imports: [
55
+ TypeOrmModule.forFeature(
56
+ [
57
+ OmniNamedEntity,
58
+ OmniRecordEntity,
59
+ OmniRelationEntity,
60
+ OmniCollectionEntity,
61
+ OmniDocumentEntity,
62
+ OmniExternalRefEntity,
63
+ ],
64
+ dbConnection,
65
+ ),
66
+ ],
67
+ providers: [
68
+ { provide: OMNI_KERNEL_OPTIONS, useValue: options },
69
+ OmniScopeContext,
70
+ {
71
+ provide: EventEmitter2,
72
+ useValue: eventEmitter,
73
+ },
74
+ ...omniNamedProviders,
75
+ ...omniRecordProviders,
76
+ ...omniRelationProviders,
77
+ ...omniCollectionProviders,
78
+ ...omniDocumentProviders,
79
+ ...omniExternalRefProviders,
80
+ omniKernelQueryServiceProvider,
81
+ ],
82
+ exports: [
83
+ OMNI_KERNEL_OPTIONS,
84
+ OmniScopeContext,
85
+ EventEmitter2,
86
+ ...omniNamedProviders,
87
+ ...omniRecordProviders,
88
+ ...omniRelationProviders,
89
+ ...omniCollectionProviders,
90
+ ...omniDocumentProviders,
91
+ ...omniExternalRefProviders,
92
+ omniKernelQueryServiceProvider,
93
+ OmniKernelQueryService,
94
+ ],
95
+ };
96
+ }
97
+ }
@@ -1,108 +1,108 @@
1
- import { FactoryProvider, Scope } from '@nestjs/common';
2
- import { getRepositoryToken } from '@nestjs/typeorm';
3
- import { Repository } from 'typeorm';
4
- import { OmniExternalRefEntity } from './base/omni-external-ref.entity.js';
5
- import { OmniRecordEntity } from './base/omni-record.entity.js';
6
- import { OmniRelationEntity } from './base/omni-relation.entity.js';
7
- import { OmniCollectionEntity } from './omni-collection.entity.js';
8
- import { OmniExternalRefInternalType } from './omni-external-ref-internal-type.enum.js';
9
- import { OmniRelationKind } from './omni-relation-kind.enum.js';
10
- import { OmniRelationStatus } from './omni-relation-status.enum.js';
11
- import { isOmniCollectionRecordKind } from './omni-relation-semantics.js';
12
- import { OmniScopeContext, type OmniScope } from './omni-scope.js';
13
-
14
- const defaultQueryScope: OmniScope = {
15
- scopeId: 'default',
16
- cacheKey: (key) => `default:${key}`,
17
- };
18
-
19
- export class OmniKernelQueryService {
20
- constructor(
21
- protected relationRepository: Repository<OmniRelationEntity>,
22
- protected externalRefRepository: Repository<OmniExternalRefEntity>,
23
- protected readonly scope: OmniScope = defaultQueryScope,
24
- ) {}
25
-
26
- async getCollectionMembers(
27
- collectionId: string,
28
- ): Promise<OmniRecordEntity[]> {
29
- const relations = await this.relationRepository.find({
30
- where: {
31
- scopeId: this.scope.scopeId,
32
- sourceRecordId: collectionId,
33
- kind: OmniRelationKind.Contains,
34
- status: OmniRelationStatus.Active,
35
- },
36
- relations: {
37
- targetRecord: true,
38
- },
39
- order: {
40
- createdAt: 'ASC',
41
- },
42
- });
43
-
44
- return relations.map((relation) => relation.targetRecord);
45
- }
46
-
47
- async getDocumentCollections(
48
- documentId: string,
49
- ): Promise<OmniCollectionEntity[]> {
50
- const relations = await this.relationRepository.find({
51
- where: {
52
- scopeId: this.scope.scopeId,
53
- targetRecordId: documentId,
54
- kind: OmniRelationKind.Contains,
55
- status: OmniRelationStatus.Active,
56
- },
57
- relations: {
58
- sourceRecord: true,
59
- },
60
- order: {
61
- createdAt: 'ASC',
62
- },
63
- });
64
-
65
- return relations
66
- .map((relation) => relation.sourceRecord)
67
- .filter(
68
- (record): record is OmniCollectionEntity =>
69
- !!record && isOmniCollectionRecordKind(record.kind),
70
- );
71
- }
72
-
73
- async getDocumentExternalRefs(documentId: string, provider?: string) {
74
- return this.externalRefRepository.find({
75
- where: {
76
- scopeId: this.scope.scopeId,
77
- internalType: OmniExternalRefInternalType.Document,
78
- internalId: documentId,
79
- ...(provider ? { provider } : {}),
80
- },
81
- order: {
82
- createdAt: 'ASC',
83
- },
84
- });
85
- }
86
- }
87
-
88
- export const omniKernelQueryServiceProviderFactory = (
89
- dbConnection: string,
90
- ): FactoryProvider<OmniKernelQueryService> => ({
91
- provide: OmniKernelQueryService,
92
- scope: Scope.REQUEST,
93
- useFactory: (
94
- relationRepository: Repository<OmniRelationEntity>,
95
- externalRefRepository: Repository<OmniExternalRefEntity>,
96
- scope: OmniScope,
97
- ) =>
98
- new OmniKernelQueryService(
99
- relationRepository,
100
- externalRefRepository,
101
- scope,
102
- ),
103
- inject: [
104
- getRepositoryToken(OmniRelationEntity, dbConnection),
105
- getRepositoryToken(OmniExternalRefEntity, dbConnection),
106
- OmniScopeContext,
107
- ],
108
- });
1
+ import { FactoryProvider, Scope } from '@nestjs/common';
2
+ import { getRepositoryToken } from '@nestjs/typeorm';
3
+ import { Repository } from 'typeorm';
4
+ import { OmniExternalRefEntity } from './base/omni-external-ref.entity.js';
5
+ import { OmniRecordEntity } from './base/omni-record.entity.js';
6
+ import { OmniRelationEntity } from './base/omni-relation.entity.js';
7
+ import { OmniCollectionEntity } from './omni-collection.entity.js';
8
+ import { OmniExternalRefInternalType } from './omni-external-ref-internal-type.enum.js';
9
+ import { OmniRelationKind } from './omni-relation-kind.enum.js';
10
+ import { OmniRelationStatus } from './omni-relation-status.enum.js';
11
+ import { isOmniCollectionRecordKind } from './omni-relation-semantics.js';
12
+ import { OmniScopeContext, type OmniScope } from './omni-scope.js';
13
+
14
+ const defaultQueryScope: OmniScope = {
15
+ scopeId: 'default',
16
+ cacheKey: (key) => `default:${key}`,
17
+ };
18
+
19
+ export class OmniKernelQueryService {
20
+ constructor(
21
+ protected relationRepository: Repository<OmniRelationEntity>,
22
+ protected externalRefRepository: Repository<OmniExternalRefEntity>,
23
+ protected readonly scope: OmniScope = defaultQueryScope,
24
+ ) {}
25
+
26
+ async getCollectionMembers(
27
+ collectionId: string,
28
+ ): Promise<OmniRecordEntity[]> {
29
+ const relations = await this.relationRepository.find({
30
+ where: {
31
+ scopeId: this.scope.scopeId,
32
+ sourceRecordId: collectionId,
33
+ kind: OmniRelationKind.Contains,
34
+ status: OmniRelationStatus.Active,
35
+ },
36
+ relations: {
37
+ targetRecord: true,
38
+ },
39
+ order: {
40
+ createdAt: 'ASC',
41
+ },
42
+ });
43
+
44
+ return relations.map((relation) => relation.targetRecord);
45
+ }
46
+
47
+ async getDocumentCollections(
48
+ documentId: string,
49
+ ): Promise<OmniCollectionEntity[]> {
50
+ const relations = await this.relationRepository.find({
51
+ where: {
52
+ scopeId: this.scope.scopeId,
53
+ targetRecordId: documentId,
54
+ kind: OmniRelationKind.Contains,
55
+ status: OmniRelationStatus.Active,
56
+ },
57
+ relations: {
58
+ sourceRecord: true,
59
+ },
60
+ order: {
61
+ createdAt: 'ASC',
62
+ },
63
+ });
64
+
65
+ return relations
66
+ .map((relation) => relation.sourceRecord)
67
+ .filter(
68
+ (record): record is OmniCollectionEntity =>
69
+ !!record && isOmniCollectionRecordKind(record.kind),
70
+ );
71
+ }
72
+
73
+ async getDocumentExternalRefs(documentId: string, provider?: string) {
74
+ return this.externalRefRepository.find({
75
+ where: {
76
+ scopeId: this.scope.scopeId,
77
+ internalType: OmniExternalRefInternalType.Document,
78
+ internalId: documentId,
79
+ ...(provider ? { provider } : {}),
80
+ },
81
+ order: {
82
+ createdAt: 'ASC',
83
+ },
84
+ });
85
+ }
86
+ }
87
+
88
+ export const omniKernelQueryServiceProviderFactory = (
89
+ dbConnection: string,
90
+ ): FactoryProvider<OmniKernelQueryService> => ({
91
+ provide: OmniKernelQueryService,
92
+ scope: Scope.REQUEST,
93
+ useFactory: (
94
+ relationRepository: Repository<OmniRelationEntity>,
95
+ externalRefRepository: Repository<OmniExternalRefEntity>,
96
+ scope: OmniScope,
97
+ ) =>
98
+ new OmniKernelQueryService(
99
+ relationRepository,
100
+ externalRefRepository,
101
+ scope,
102
+ ),
103
+ inject: [
104
+ getRepositoryToken(OmniRelationEntity, dbConnection),
105
+ getRepositoryToken(OmniExternalRefEntity, dbConnection),
106
+ OmniScopeContext,
107
+ ],
108
+ });