From 8ba742eb36586a21a918ed178208874a53ace3f9 Mon Sep 17 00:00:00 2001 From: Nelson Fleig Date: Sat, 2 Apr 2022 13:19:20 -0400 Subject: [PATCH] fix: Update DeepPartial for usage of generics with Repository class (#8817) * test: test usage of generics with Repository class * test: cleanup * fix: update DeepPartial to work with generic Repository * fix: restore package-lock.json from typeorm/master --- src/common/DeepPartial.ts | 20 ++++++++------- test/github-issues/8681/issue-8681.ts | 35 ++++++++++++++++++++++++--- 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/src/common/DeepPartial.ts b/src/common/DeepPartial.ts index a241f81bed..d2dad8584d 100644 --- a/src/common/DeepPartial.ts +++ b/src/common/DeepPartial.ts @@ -1,12 +1,14 @@ /** * Same as Partial but goes deeper and makes Partial all its properties and sub-properties. */ -export type DeepPartial = T extends Array - ? DeepPartial[] - : T extends Map - ? Map, DeepPartial> - : T extends Set - ? Set> - : T extends object - ? { [K in keyof T]?: DeepPartial } - : T +export type DeepPartial = + | T + | (T extends Array + ? DeepPartial[] + : T extends Map + ? Map, DeepPartial> + : T extends Set + ? Set> + : T extends object + ? { [K in keyof T]?: DeepPartial } + : T) diff --git a/test/github-issues/8681/issue-8681.ts b/test/github-issues/8681/issue-8681.ts index eafb91537d..1892880baa 100644 --- a/test/github-issues/8681/issue-8681.ts +++ b/test/github-issues/8681/issue-8681.ts @@ -1,13 +1,13 @@ +import { expect } from "chai" +import { DataSource, DeepPartial, Repository } from "../../../src" import "../../utils/test-setup" import { closeTestingConnections, createTestingConnections, reloadTestingDatabases, } from "../../utils/test-utils" -import { DataSource, DeepPartial } from "../../../src" -import { expect } from "chai" -import { Thing } from "./entity/thing.entity" import { Item } from "./entity/item.entity" +import { Thing } from "./entity/thing.entity" describe("github issues > #8681 DeepPartial simplification breaks the .create() and .save() method in certain cases.", () => { let connections: DataSource[] @@ -42,4 +42,33 @@ describe("github issues > #8681 DeepPartial simplification breaks the .create() return { thing, items } }), )) + it("should .save() and .create() complex deep partial entities using a generic repository", () => + Promise.all( + connections.map(async (connection) => { + class AbstractService { + private repository: Repository + constructor(target: any) { + this.repository = new Repository( + target, + connection.manager, + ) + } + create(data: DeepPartial): Promise { + const entity = this.repository.create(data) + return this.repository.save(entity) + } + } + + const thingService = new AbstractService(Thing) + + const myThing: DeepPartial = { id: 1 } + const thing = await thingService.create(myThing) + + const thingRepository = connection.getRepository(Thing) + const dbItems = await thingRepository.find() + expect(dbItems).to.have.length(1) + + return { thing } + }), + )) })