Skip to content

Commit

Permalink
Example embedded object
Browse files Browse the repository at this point in the history
  • Loading branch information
doug-martin committed Feb 22, 2020
1 parent 9791c38 commit a2571dd
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class AddTodoItemChecklist1582352813008 implements MigrationInterface {
name = 'addTodoItemChecklist1582352813008';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "todo_item_entity" ADD "checklist" jsonb`, undefined);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "tag_entity" ADD "description" character varying`, undefined);
}
}
2 changes: 1 addition & 1 deletion examples/nest-graphql-typeorm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "APP_ROOT_PATH=. nest start --watch",
"start:prod": "node dist/main",
"migrate:up": "typeorm migration:run",
"migrate:up": "npm run build && typeorm migration:run",
"migrate:generate": "npm run build && typeorm migration:generate -d ./migrations ",
"seed": "ts-node ../../node_modules/typeorm-seeding/dist/cli.js --config ./ormconfig.json seed"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import { Field, InputType } from 'type-graphql';
import { IsString, Length } from 'class-validator';

@InputType('TodoItemChecklistInput')
export class TodoItemChecklistDTO {
@Field()
@IsString()
@Length(10)
name!: string;
}
@InputType('TodoItemInput')
export class TodoItemInputDTO {
@Field()
title!: string;

@Field()
completed!: boolean;

@Field(() => [TodoItemChecklistDTO], { nullable: true })
checklist?: TodoItemChecklistDTO[];
}
12 changes: 12 additions & 0 deletions examples/nest-graphql-typeorm/src/todo-item/dto/todo-item.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { FilterableField } from '@nestjs-query/query-graphql';
import { ObjectType, ID, GraphQLISODateTime, Field } from 'type-graphql';

@ObjectType('TodoItemChecklist')
export class TodoItemChecklistDTO {
@Field()
name!: string;

@Field({ defaultValue: false })
completed!: boolean;
}

@ObjectType('TodoItem')
export class TodoItemDTO {
@FilterableField(() => ID)
Expand All @@ -12,6 +21,9 @@ export class TodoItemDTO {
@FilterableField({ nullable: true })
description?: string;

@Field(() => [TodoItemChecklistDTO], { nullable: true })
checklist?: TodoItemChecklistDTO[];

@FilterableField()
completed!: boolean;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ import {
import { SubTaskEntity } from '../sub-task/sub-task.entity';
import { TagEntity } from '../tag/tag.entity';

class TodoItemChecklist {
name!: string;

completed!: boolean;
}

@Entity()
export class TodoItemEntity {
@PrimaryGeneratedColumn()
Expand All @@ -31,6 +37,9 @@ export class TodoItemEntity {
)
subTasks!: SubTaskEntity[];

@Column('jsonb', { nullable: true })
checklist?: TodoItemChecklist[];

@CreateDateColumn()
created!: Date;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ const guards = [AuthGuard];

@Resolver(() => TodoItemDTO)
export class TodoItemResolver extends CRUDResolver(TodoItemDTO, {
CreateDTOClass: TodoItemInputDTO,
UpdateDTOClass: TodoItemInputDTO,
create: { guards },
update: { guards },
create: { guards, CreateDTOClass: TodoItemInputDTO },
update: { guards, UpdateDTOClass: TodoItemInputDTO },
delete: { guards },
relations: {
many: {
Expand Down

0 comments on commit a2571dd

Please sign in to comment.