Skip to content

Commit

Permalink
fix: type inferencing of EntityManager#create (#10569)
Browse files Browse the repository at this point in the history
* fix: type inferencing of EntityManager#create

* test: type inferencing of EntityManager#create (#10569)
  • Loading branch information
quaternion committed Jan 26, 2024
1 parent 0cab0dd commit 99d8249
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 8 deletions.
14 changes: 7 additions & 7 deletions src/entity-manager/EntityManager.ts
Expand Up @@ -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),
)

Expand Down
2 changes: 1 addition & 1 deletion src/repository/Repository.ts
Expand Up @@ -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,
)
Expand Down
3 changes: 3 additions & 0 deletions test/github-issues/10569/contract/create-user-contract.ts
@@ -0,0 +1,3 @@
export type CreateUserContract = {
name: string
}
10 changes: 10 additions & 0 deletions test/github-issues/10569/entity/user.ts
@@ -0,0 +1,10 @@
import { Column, Entity, PrimaryGeneratedColumn } from "../../../../src"

@Entity({ name: "user" })
export class User {
@PrimaryGeneratedColumn("uuid")
id: string

@Column("varchar")
name: string
}
42 changes: 42 additions & 0 deletions test/github-issues/10569/ussue-10569.ts
@@ -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
}),
))
})

0 comments on commit 99d8249

Please sign in to comment.