Skip to content
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

fix: Update DeepPartial for usage of generics with Repository class #8817

Merged
merged 4 commits into from
Apr 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 11 additions & 9 deletions src/common/DeepPartial.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
/**
* Same as Partial<T> but goes deeper and makes Partial<T> all its properties and sub-properties.
*/
export type DeepPartial<T> = T extends Array<infer U>
? DeepPartial<U>[]
: T extends Map<infer K, infer V>
? Map<DeepPartial<K>, DeepPartial<V>>
: T extends Set<infer M>
? Set<DeepPartial<M>>
: T extends object
? { [K in keyof T]?: DeepPartial<T[K]> }
: T
export type DeepPartial<T> =
| T
| (T extends Array<infer U>
? DeepPartial<U>[]
: T extends Map<infer K, infer V>
? Map<DeepPartial<K>, DeepPartial<V>>
: T extends Set<infer M>
? Set<DeepPartial<M>>
: T extends object
? { [K in keyof T]?: DeepPartial<T[K]> }
: T)
35 changes: 32 additions & 3 deletions test/github-issues/8681/issue-8681.ts
Original file line number Diff line number Diff line change
@@ -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[]
Expand Down Expand Up @@ -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<T> {
private repository: Repository<T>
constructor(target: any) {
this.repository = new Repository(
target,
connection.manager,
)
}
create(data: DeepPartial<T>): Promise<T> {
const entity = this.repository.create(data)
return this.repository.save(entity)
}
}

const thingService = new AbstractService<Thing>(Thing)

const myThing: DeepPartial<Thing> = { 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 }
}),
))
})