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: improve DeepPartial recursion #8732

Merged
merged 6 commits into from
Mar 26, 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
12 changes: 8 additions & 4 deletions src/common/DeepPartial.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
/**
* Same as Partial<T> but goes deeper and makes Partial<T> all its properties and sub-properties.
*/
export type DeepPartial<T> = T extends object
? {
[P in keyof T]?: DeepPartial<T[P]>
}
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
11 changes: 11 additions & 0 deletions test/github-issues/8681/entity/item.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Entity, ManyToOne, PrimaryGeneratedColumn } from "../../../../src"
import { Thing } from "./thing.entity"

@Entity()
export class Item {
@PrimaryGeneratedColumn()
id!: number

@ManyToOne(() => Thing, (thing) => thing.items)
thing!: Thing
}
11 changes: 11 additions & 0 deletions test/github-issues/8681/entity/thing.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Entity, OneToMany, PrimaryGeneratedColumn } from "../../../../src"
import { Item } from "./item.entity"

@Entity()
export class Thing {
@PrimaryGeneratedColumn()
id!: number

@OneToMany(() => Item, (item) => item.thing)
items!: Item[]
}
45 changes: 45 additions & 0 deletions test/github-issues/8681/issue-8681.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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"

describe("github issues > #8681 DeepPartial simplification breaks the .create() and .save() method in certain cases.", () => {
let connections: DataSource[]
before(
async () =>
(connections = await createTestingConnections({
entities: [__dirname + "/entity/*{.js,.ts}"],
schemaCreate: true,
dropSchema: true,
})),
)
beforeEach(() => reloadTestingDatabases(connections))
after(() => closeTestingConnections(connections))

it("should .save() and .create() complex deep partial entities", () =>
Promise.all(
connections.map(async (connection) => {
const myThing: DeepPartial<Thing> = {
items: [{ id: 1 }, { id: 2 }],
}

const thing = connection.manager.create(Thing, myThing)
await connection.getRepository(Thing).save(myThing)

const items = connection.manager.create(Item, myThing.items)
if (myThing.items)
await connection.getRepository(Item).save(myThing.items)

const dbItems = await connection.manager.find(Item)
expect(dbItems).to.have.length(2)

return { thing, items }
}),
))
})