-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: convert the join table ID to the referenceColumn ID type (#9887)
* fix: Convert the join table ID to the referenceColumn ID type * test: add auto-increment-id-as-string test * style: format auto-increment-id-as-string test
- Loading branch information
Showing
4 changed files
with
113 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
test/other-issues/auto-increment-id-as-string/auto-increment-id-as-string.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import "reflect-metadata" | ||
import { | ||
closeTestingConnections, | ||
createTestingConnections, | ||
reloadTestingDatabases, | ||
} from "../../utils/test-utils" | ||
import { DataSource } from "../../../src/data-source/DataSource" | ||
import { User } from "./entity/User" | ||
import { Role } from "./entity/Role" | ||
|
||
describe("other issues > auto-increment id as string", () => { | ||
let connections: DataSource[] | ||
before( | ||
async () => | ||
(connections = await createTestingConnections({ | ||
entities: [__dirname + "/entity/*{.js,.ts}"], | ||
})), | ||
) | ||
beforeEach(() => reloadTestingDatabases(connections)) | ||
after(() => closeTestingConnections(connections)) | ||
|
||
it("should relationIds exist", () => | ||
Promise.all( | ||
connections.map(async function (connection) { | ||
const role1 = new Role() | ||
role1.roleName = "#role 1" | ||
const role2 = new Role() | ||
role2.roleName = "#role 2" | ||
|
||
const user = new User() | ||
user.userName = "#user 1" | ||
user.roles = [ | ||
await connection.manager.save(role1), | ||
await connection.manager.save(role2), | ||
] | ||
|
||
const user2 = await connection.manager.save(user) | ||
|
||
const user3 = await connection.manager.findOne(User, { | ||
where: { | ||
userId: user2.userId, | ||
}, | ||
loadRelationIds: true, | ||
}) | ||
user3!.roles.length.should.be.equal(2) | ||
}), | ||
)) | ||
}) |
24 changes: 24 additions & 0 deletions
24
test/other-issues/auto-increment-id-as-string/entity/Role.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Column } from "../../../../src/decorator/columns/Column" | ||
import { Entity } from "../../../../src/decorator/entity/Entity" | ||
|
||
@Entity() | ||
export class Role { | ||
@Column({ | ||
name: "role_id", | ||
primary: true, | ||
type: "int", | ||
generated: "increment", | ||
transformer: { | ||
to(value: object) { | ||
return value?.toString() | ||
}, | ||
from(value: object) { | ||
return value?.toString() | ||
}, | ||
}, | ||
}) | ||
roleId: string | ||
|
||
@Column({ name: "role_name" }) | ||
roleName: string | ||
} |
40 changes: 40 additions & 0 deletions
40
test/other-issues/auto-increment-id-as-string/entity/User.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { JoinTable, ManyToMany } from "../../../../src" | ||
import { Column } from "../../../../src/decorator/columns/Column" | ||
import { Entity } from "../../../../src/decorator/entity/Entity" | ||
import { Role } from "./Role" | ||
|
||
@Entity() | ||
export class User { | ||
@Column({ | ||
name: "user_id", | ||
primary: true, | ||
type: "int", | ||
generated: "increment", | ||
transformer: { | ||
to(value: object) { | ||
return value?.toString() | ||
}, | ||
from(value: object) { | ||
return value?.toString() | ||
}, | ||
}, | ||
}) | ||
userId: string | ||
|
||
@Column({ name: "user_name" }) | ||
userName: string | ||
|
||
@ManyToMany((type) => Role) | ||
@JoinTable({ | ||
name: "user_role", | ||
joinColumn: { | ||
name: "user_id", | ||
referencedColumnName: "userId", | ||
}, | ||
inverseJoinColumn: { | ||
name: "role_id", | ||
referencedColumnName: "roleId", | ||
}, | ||
}) | ||
roles: Role[] | ||
} |