Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions examples/omnikernel-module/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,18 @@ It follows the same `CrudGenDependencyFactory` pattern used by
`examples/task-system-module`, so each entity exposes the usual auto-wired
service, dataloader, and GraphQL resolver providers.

## PR 2 additions

- typed record and relation enums for status and relation semantics
- `OmniDocumentEntity` as the first concrete record family built on
`OmniRecordEntity`

Comment on lines +15 to +20

Copilot AI Apr 8, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Section title “PR 2 additions” is tied to a specific PR and will become stale once merged. Consider renaming this to something repo-agnostic (e.g. “Recent additions” / “Document slice additions”) or folding the bullets into the main module description.

Copilot uses AI. Check for mistakes.
## Entities

- `OmniNamedEntity`: basic named records with `externalId`, `title`, and `slug`.
- `OmniRecordEntity`: richer records with `kind`, `status`, and `payload`.
- `OmniDocumentEntity`: concrete document-like records with document subtype,
content, source URL, and publication timestamp.
- `OmniRelationEntity`: directional links between two Omni records.
- `OmniExternalRefEntity`: provider-specific mappings between internal records
and external systems.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { describe, expect, it } from '@jest/globals';
import { getMetadataArgsStorage } from 'typeorm';

const { OmniRecordEntity } = await import('../base/omni-record.entity.js');
const { OmniDocumentEntity } = await import('../omni-document.entity.js');
const { OmniDocumentKind } = await import('../omni-document-kind.enum.js');

describe('OmniDocumentEntity', () => {
it('extends OmniRecordEntity', () => {
expect(new OmniDocumentEntity()).toBeInstanceOf(OmniRecordEntity);
});

it('registers document-specific columns', () => {
const metadata = getMetadataArgsStorage();
const table = metadata.tables.find(
(item) => item.target === OmniDocumentEntity,
);
const entityColumns = metadata.columns
.filter((item) => item.target === OmniDocumentEntity)
.map((item) => item.propertyName);

expect(table?.name).toBe('omni-document');
expect(entityColumns).toEqual(
expect.arrayContaining([
'documentKind',
'content',
'contentMimeType',
'sourceUrl',
'publishedAt',
]),
);
});

it('defaults the base record kind to document when missing', () => {
const entity = new OmniDocumentEntity();

entity.ensureDocumentRecordKind();

expect(entity.kind).toBe(OmniDocumentKind.Document);
});

it('preserves an explicit base record kind when already set', () => {
const entity = new OmniDocumentEntity();
entity.kind = OmniDocumentKind.Document;

entity.ensureDocumentRecordKind();

expect(entity.kind).toBe(OmniDocumentKind.Document);
});
});
9 changes: 7 additions & 2 deletions examples/omnikernel-module/src/base/omni-record.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@ import { Column, Entity, OneToMany } from 'typeorm';
import type { Relation } from 'typeorm';
import { OmniNamedEntity } from './omni-named.entity.js';
import { OmniRelationEntity } from './omni-relation.entity.js';
import { OmniRecordStatus } from '../omni-record-status.enum.js';

@Entity('omni-record')
@ObjectType({ isAbstract: true })
export class OmniRecordEntity extends OmniNamedEntity {
@Column('varchar', { length: 64 })
kind!: string;

@Column('varchar', { default: 'draft', length: 32 })
status!: string;
@Column('varchar', {
default: OmniRecordStatus.Draft,
enum: Object.values(OmniRecordStatus),
length: 32,
})
status!: OmniRecordStatus;

@Column('simple-json', { nullable: true })
payload?: Record<string, unknown> | null;
Expand Down
17 changes: 13 additions & 4 deletions examples/omnikernel-module/src/base/omni-relation.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { Column, Entity, JoinColumn, ManyToOne } from 'typeorm';
import type { Relation } from 'typeorm';
import { OmniBaseEntity } from './omni-base.entity.js';
import { OmniRecordEntity } from './omni-record.entity.js';
import { OmniRelationKind } from '../omni-relation-kind.enum.js';
import { OmniRelationStatus } from '../omni-relation-status.enum.js';

@Entity('omni-relation')
@ObjectType({ isAbstract: true })
Expand All @@ -21,11 +23,18 @@ export class OmniRelationEntity extends OmniBaseEntity {
@JoinColumn({ name: 'targetRecordId', referencedColumnName: 'guid' })
targetRecord!: Relation<OmniRecordEntity>;

@Column('varchar', { length: 64 })
kind!: string;
@Column('varchar', {
enum: Object.values(OmniRelationKind),
length: 64,
})
kind!: OmniRelationKind;

@Column('varchar', { default: 'active', length: 32 })
status!: string;
@Column('varchar', {
default: OmniRelationStatus.Active,
enum: Object.values(OmniRelationStatus),
length: 32,
})
status!: OmniRelationStatus;

@Column('simple-json', { nullable: true })
payload?: Record<string, unknown> | null;
Expand Down
7 changes: 7 additions & 0 deletions examples/omnikernel-module/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@ export * from './base/omni-relation.entity.js';
export * from './base/omni-external-ref.entity.js';
export * from './omni-named.dto.js';
export * from './omni-named.resolver.js';
export * from './omni-record-status.enum.js';
export * from './omni-relation-status.enum.js';
export * from './omni-relation-kind.enum.js';
export * from './omni-record.dto.js';
export * from './omni-record.resolver.js';
export * from './omni-document-kind.enum.js';
export * from './omni-document.entity.js';
export * from './omni-document.dto.js';
export * from './omni-document.resolver.js';
export * from './omni-relation.dto.js';
export * from './omni-relation.resolver.js';
export * from './omni-external-ref.dto.js';
Expand Down
6 changes: 6 additions & 0 deletions examples/omnikernel-module/src/omni-document-kind.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export enum OmniDocumentKind {
Document = 'document',
Note = 'note',
Article = 'article',
Page = 'page',
}
172 changes: 172 additions & 0 deletions examples/omnikernel-module/src/omni-document.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
import {
Field,
InputType,
ObjectType,
OmitType,
PartialType,
} from '@nestjs/graphql';
import {
ModelField,
ModelObject,
} from '@nestjs-yalc/crud-gen/object.decorator.js';
import { UUIDScalar } from '@nestjs-yalc/graphql/scalars/uuid.scalar.js';
import returnValue from '@nestjs-yalc/utils/returnValue.js';
import {
IsDate,
IsEnum,
IsIn,
IsObject,
IsOptional,
IsString,
IsUrl,
IsUUID,
MaxLength,
} from 'class-validator';
import { GraphQLJSONObject } from 'graphql-type-json';
import type { Relation } from 'typeorm';
import { OmniDocumentEntity } from './omni-document.entity.js';
import { OmniDocumentKind } from './omni-document-kind.enum.js';
import { OmniRecordStatus } from './omni-record-status.enum.js';
import { OmniRelationEntity } from './base/omni-relation.entity.js';
import { OmniRelationType } from './omni-relation.dto.js';

@ObjectType()
@ModelObject()
export class OmniDocumentType extends OmniDocumentEntity {
constructor(data?: Partial<OmniDocumentType>) {
super();
if (data) {
Object.assign(this, data);
}
}

@ModelField({ gqlType: returnValue(UUIDScalar), isRequired: true })
@Field(() => UUIDScalar)
@IsUUID()
guid!: string;

@ModelField({ gqlOptions: { nullable: true } })
@Field({ nullable: true })
@IsOptional()
@IsString()
@MaxLength(128)
externalId?: string | null;

@ModelField({})
@Field()
@IsString()
@MaxLength(255)
title!: string;

@ModelField({ gqlOptions: { nullable: true } })
@Field({ nullable: true })
@IsOptional()
@IsString()
@MaxLength(255)
slug?: string | null;

@ModelField({})
@Field()
@IsString()
@IsIn([OmniDocumentKind.Document])
kind!: string;

@ModelField({})
@Field()
@IsEnum(OmniRecordStatus)
status!: OmniRecordStatus;
Comment on lines +74 to +77

Copilot AI Apr 8, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

status is typed as OmniRecordStatus but the GraphQL decorator is @Field() without an explicit type. If enum reflection resolves to Object, schema generation can fail. Consider registering OmniRecordStatus as a GraphQL enum and using @Field(() => OmniRecordStatus) (or switch the property back to string if you want a string field).

Copilot uses AI. Check for mistakes.

@ModelField({
gqlType: returnValue(GraphQLJSONObject),
gqlOptions: { nullable: true },
})
@Field(() => GraphQLJSONObject, { nullable: true })
@IsOptional()
@IsObject()
payload?: Record<string, unknown> | null;

@ModelField({
gqlType: returnValue([OmniRelationType]),
gqlOptions: { nullable: true },
relation: {
relationType: 'one-to-many',
sourceKey: { dst: 'guid', alias: 'guid' },
targetKey: { dst: 'sourceRecordId', alias: 'sourceRecordId' },
type: () => OmniRelationEntity,
},
})
@Field(() => [OmniRelationType], { nullable: true })
outgoingRelations?: Relation<OmniRelationType[]>;

@ModelField({
gqlType: returnValue([OmniRelationType]),
gqlOptions: { nullable: true },
relation: {
relationType: 'one-to-many',
sourceKey: { dst: 'guid', alias: 'guid' },
targetKey: { dst: 'targetRecordId', alias: 'targetRecordId' },
type: () => OmniRelationEntity,
},
})
@Field(() => [OmniRelationType], { nullable: true })
incomingRelations?: Relation<OmniRelationType[]>;

@ModelField({})
@Field()
@IsEnum(OmniDocumentKind)
documentKind!: OmniDocumentKind;
Comment on lines +114 to +117

Copilot AI Apr 8, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

documentKind is a TypeScript enum but uses @Field() without an explicit type. This can break Nest GraphQL type inference when emitDecoratorMetadata is on. Recommend registerEnumType(OmniDocumentKind, ...) and @Field(() => OmniDocumentKind) (or expose it as string explicitly).

Copilot uses AI. Check for mistakes.

@ModelField({ gqlOptions: { nullable: true } })
@Field({ nullable: true })
@IsOptional()
@IsString()
content?: string | null;

@ModelField({ gqlOptions: { nullable: true } })
@Field({ nullable: true })
@IsOptional()
@IsString()
@MaxLength(128)
contentMimeType?: string | null;

@ModelField({ gqlOptions: { nullable: true } })
@Field({ nullable: true })
@IsOptional()
@IsUrl()
@MaxLength(2048)
sourceUrl?: string | null;

@ModelField({ gqlOptions: { nullable: true } })
@Field({ nullable: true })
@IsOptional()
@IsDate()
publishedAt?: Date | null;
}

@InputType()
@ModelObject()
export class OmniDocumentCreateInput extends OmitType(
OmniDocumentType,
[
'createdAt',
'updatedAt',
'kind',
'outgoingRelations',
'incomingRelations',
] as const,
InputType,
) {}

@InputType()
@ModelObject({ copyFrom: OmniDocumentType })
export class OmniDocumentCondition extends PartialType(
OmniDocumentCreateInput,
InputType,
) {}

@InputType()
@ModelObject({ copyFrom: OmniDocumentType })
export class OmniDocumentUpdateInput extends PartialType(
OmniDocumentCreateInput,
InputType,
) {}
35 changes: 35 additions & 0 deletions examples/omnikernel-module/src/omni-document.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { ObjectType } from '@nestjs/graphql';
import { BeforeInsert, BeforeUpdate, Column, Entity } from 'typeorm';
import { OmniRecordEntity } from './base/omni-record.entity.js';
import { OmniDocumentKind } from './omni-document-kind.enum.js';

@Entity('omni-document')
@ObjectType({ isAbstract: true })
export class OmniDocumentEntity extends OmniRecordEntity {
@Column('varchar', {
default: OmniDocumentKind.Document,
enum: Object.values(OmniDocumentKind),
length: 32,
})
documentKind!: OmniDocumentKind;

@Column('text', { nullable: true })
content?: string | null;

@Column('varchar', { nullable: true, length: 128 })
contentMimeType?: string | null;

@Column('varchar', { nullable: true, length: 2048 })
sourceUrl?: string | null;

@Column('datetime', { nullable: true })
publishedAt?: Date | null;

@BeforeInsert()
@BeforeUpdate()
ensureDocumentRecordKind() {
if (!this.kind) {
this.kind = OmniDocumentKind.Document;
}
Comment on lines +31 to +33

Copilot AI Apr 8, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ensureDocumentRecordKind() only sets kind when it’s falsy, so it won’t prevent persisting an OmniDocumentEntity with a non-document kind (which would also contradict the DTO validation that only allows kind === 'document'). Consider enforcing the invariant (always set kind to OmniDocumentKind.Document, or throw if a different value is provided).

Suggested change
if (!this.kind) {
this.kind = OmniDocumentKind.Document;
}
this.kind = OmniDocumentKind.Document;

Copilot uses AI. Check for mistakes.
}
}
27 changes: 27 additions & 0 deletions examples/omnikernel-module/src/omni-document.resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { CrudGenDependencyFactory } from '@nestjs-yalc/crud-gen/crud-gen.helpers.js';
import {
OmniDocumentCondition,
OmniDocumentCreateInput,
OmniDocumentType,
OmniDocumentUpdateInput,
} from './omni-document.dto.js';
import { OmniDocumentEntity } from './omni-document.entity.js';

export const omniDocumentProvidersFactory = (dbConnection: string) =>
CrudGenDependencyFactory<OmniDocumentEntity>({
entityModel: OmniDocumentEntity,
resolver: {
dto: OmniDocumentType,
input: {
create: OmniDocumentCreateInput,
update: OmniDocumentUpdateInput,
conditions: OmniDocumentCondition,
},
prefix: 'OmniKernel_',
},
service: {
dbConnection,
entityModel: OmniDocumentEntity,
},
dataloader: { databaseKey: 'guid' },
});
5 changes: 5 additions & 0 deletions examples/omnikernel-module/src/omni-record-status.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export enum OmniRecordStatus {
Draft = 'draft',
Active = 'active',
Archived = 'archived',
}
Loading
Loading