fix: type inferencing of EntityManager#create (#10569)

* fix: type inferencing of EntityManager#create

* test: type inferencing of EntityManager#create (#10569)
This commit is contained in:
Andrei Nikolaev 2024-01-26 09:17:01 +03:00 committed by GitHub
parent 0cab0dd730
commit 99d8249e45
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 63 additions and 8 deletions

View File

@ -257,34 +257,34 @@ export class EntityManager {
* Creates a new entity instance and copies all entity properties from this object into a new entity.
* Note that it copies only properties that present in entity schema.
*/
create<Entity>(
create<Entity, EntityLike extends DeepPartial<Entity>>(
entityClass: EntityTarget<Entity>,
plainObject?: DeepPartial<Entity>,
plainObject?: EntityLike,
): Entity
/**
* Creates a new entities and copies all entity properties from given objects into their new entities.
* Note that it copies only properties that present in entity schema.
*/
create<Entity>(
create<Entity, EntityLike extends DeepPartial<Entity>>(
entityClass: EntityTarget<Entity>,
plainObjects?: DeepPartial<Entity>[],
plainObjects?: EntityLike[],
): Entity[]
/**
* Creates a new entity instance or instances.
* Can copy properties from the given object into new entities.
*/
create<Entity>(
create<Entity, EntityLike extends DeepPartial<Entity>>(
entityClass: EntityTarget<Entity>,
plainObjectOrObjects?: DeepPartial<Entity> | DeepPartial<Entity>[],
plainObjectOrObjects?: EntityLike | EntityLike[],
): Entity | Entity[] {
const metadata = this.connection.getMetadata(entityClass)
if (!plainObjectOrObjects) return metadata.create(this.queryRunner)
if (Array.isArray(plainObjectOrObjects))
return (plainObjectOrObjects as DeepPartial<Entity>[]).map(
return (plainObjectOrObjects as EntityLike[]).map(
(plainEntityLike) => this.create(entityClass, plainEntityLike),
)

View File

@ -126,7 +126,7 @@ export class Repository<Entity extends ObjectLiteral> {
| DeepPartial<Entity>
| DeepPartial<Entity>[],
): Entity | Entity[] {
return this.manager.create<any>(
return this.manager.create(
this.metadata.target as any,
plainEntityLikeOrPlainEntityLikes as any,
)

View File

@ -0,0 +1,3 @@
export type CreateUserContract = {
name: string
}

View File

@ -0,0 +1,10 @@
import { Column, Entity, PrimaryGeneratedColumn } from "../../../../src"
@Entity({ name: "user" })
export class User {
@PrimaryGeneratedColumn("uuid")
id: string
@Column("varchar")
name: string
}

View File

@ -0,0 +1,42 @@
import "reflect-metadata"
import { expect } from "chai"
import { v4 } from "uuid"
import { DataSource } from "../../../src/data-source/DataSource"
import {
closeTestingConnections,
createTestingConnections,
reloadTestingDatabases,
} from "../../utils/test-utils"
import { CreateUserContract } from "./contract/create-user-contract"
import { User } from "./entity/user"
describe("github issues > #10569 Fix type inferencing of EntityManager#create", () => {
let dataSources: DataSource[]
before(
async () =>
(dataSources = await createTestingConnections({
entities: [__dirname + "/entity/*{.js,.ts}"],
schemaCreate: true,
dropSchema: true,
})),
)
beforeEach(() => reloadTestingDatabases(dataSources))
after(() => closeTestingConnections(dataSources))
it("should correctly inference entity type", () =>
Promise.all(
dataSources.map(async (dataSource) => {
const createUserContract: CreateUserContract = {
name: "John Doe",
}
const user = dataSource.manager.create(User, createUserContract)
user.id = v4()
expect(user.id).to.exist
}),
))
})