Repository navigation
feat(examples): add OmniKernel document slice #124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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); | ||
| }); | ||
| }); |
| 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', | ||
| } |
| 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
|
||
|
|
||
| @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
|
||
|
|
||
| @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, | ||
| ) {} | ||
| 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
|
||||||||||
| if (!this.kind) { | |
| this.kind = OmniDocumentKind.Document; | |
| } | |
| this.kind = OmniDocumentKind.Document; |
| 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' }, | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| export enum OmniRecordStatus { | ||
| Draft = 'draft', | ||
| Active = 'active', | ||
| Archived = 'archived', | ||
| } |
There was a problem hiding this comment.
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.