mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
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:
parent
0cab0dd730
commit
99d8249e45
@ -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),
|
||||
)
|
||||
|
||||
|
||||
@ -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,
|
||||
)
|
||||
|
||||
@ -0,0 +1,3 @@
|
||||
export type CreateUserContract = {
|
||||
name: string
|
||||
}
|
||||
10
test/github-issues/10569/entity/user.ts
Normal file
10
test/github-issues/10569/entity/user.ts
Normal 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
|
||||
}
|
||||
42
test/github-issues/10569/ussue-10569.ts
Normal file
42
test/github-issues/10569/ussue-10569.ts
Normal 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
|
||||
}),
|
||||
))
|
||||
})
|
||||
Loading…
x
Reference in New Issue
Block a user