In my node application, I am utilizing the typeorm library for entity mapping. My goal is to establish multiple type relations between entities. While following the documentation, I noticed that the entity properties are marked as public, allowing access from other entities. However, I have defined my entity properties as private. To circumvent this, I have implemented a getProperty
method within the entity to retrieve the property on the entity instance. But now, typeorm is throwing an error stating that
entity.getProperty is not a function
.
User Entity
@Entity()
class User {
private USER_ID_PREFIX: string = "uid-";
@PrimaryColumn()
private id: string;
@Column({nullable: false, length: 15})
private firstname: string;
@Column({nullable: false, length: 15})
private lastname: string;
@Column({nullable: false, unique: true, length: 25})
private email: string;
@Column({nullable: false})
private password: string;
@OneToMany(() => Role, (role) => role.getProperty(EntityProperty.USER))
private roles: Role[];
public constructor(...args: any[]) {
if (args.length === 0)
return;
if (args.length === 1) {
if (args[0] instanceof UserCreateRequestDto) {
let requestDto = args[0];
this.id = this.USER_ID_PREFIX + randomUUID();
this.firstname = requestDto.getProperty(DtoProperty.FIRSTNAME);
this.lastname = requestDto.getProperty(DtoProperty.LASTNAME);
this.email = requestDto.getProperty(DtoProperty.EMAIL);
this.password = requestDto.getProperty(DtoProperty.PASSWORD);
}
}
}
public getProperty(property: EntityProperty): any {
if (property === EntityProperty.ID)
return this.id;
if (property === EntityProperty.FIRSTNAME)
return this.firstname;
if (property === EntityProperty.LASTNAME)
return this.lastname;
if (property === EntityProperty.EMAIL)
return this.email;
if (property === EntityProperty.PASSWORD)
return this.password;
if (property === EntityProperty.ROLES)
return this.roles;
throw new Error(`No such entity property as ${property.toString()} exists in User`);
}
}
export default User;
Role Entity
@Entity()
class Role {
private ROLE_ID_PREFIX: string = "rid-";
@PrimaryColumn()
private id: string;
@Column({nullable: false})
private role: string;
@ManyToOne(() => User, (user) => user.getProperty(EntityProperty.ROLES))
private user: User;
public constructor(...args: any[]) {
if (args.length === 0)
return;
if (args.length === 1) {
if (args[0] instanceof RoleCreateRequestDto) {
let requestDto = args[0];
this.id = this.ROLE_ID_PREFIX + randomUUID();
this.role = requestDto.getProperty(DtoProperty.ROLE);
}
}
}
public getProperty(property: EntityProperty): any {
if (property === EntityProperty.ID)
return this.id;
if (property === EntityProperty.ROLE)
return this.role;;
if (property === EntityProperty.USER)
return this.user;
throw new Error(`No such entity property as ${property.toString()} exists in Role`);
}
}
export default Role;
Error stack
query: SELECT VERSION() AS `version`
Error initializing mysql datasource TypeError: user.getProperty is not a function
at RelationMetadata.givenInverseSidePropertyFactory (D:\Swivel_Projects\Ignite\Project 1\backend\ignite-auth-nodejs\src\entities\Role.ts:22:43)
at RelationMetadata.buildInverseSidePropertyPath (D:\Swivel_Projects\Ignite\Project 1\backend\ignite-auth-nodejs\node_modules\src\metadata\RelationMetadata.ts:628:29)
at D:\Swivel_Projects\Ignite\Project 1\backend\ignite-auth-nodejs\node_modules\src\metadata-builder\EntityMetadataBuilder.ts:1116:26
at Array.forEach (<anonymous>)
at EntityMetadataBuilder.computeInverseProperties (D:\Swivel_Projects\Ignite\Project 1\backend\ignite-auth-nodejs\node_modules\src\metadata-builder\EntityMetadataBuilder.ts:1096:34)
at D:\Swivel_Projects\Ignite\Project 1\backend\ignite-auth-nodejs\node_modules\src\metadata-builder\EntityMetadataBuilder.ts:158:18
at Array.forEach (<anonymous>)
at EntityMetadataBuilder.build (D:\Swivel_Projects\Ignite\Project 1\backend\ignite-auth-nodejs\node_modules\src\metadata-builder\EntityMetadataBuilder.ts:157:25)
at ConnectionMetadataBuilder.buildEntityMetadatas (D:\Swivel_Projects\Ignite\Project 1\backend\ignite-auth-nodejs\node_modules\src\connection\ConnectionMetadataBuilder.ts:106:11)
at processTicksAndRejections (node:internal/process/task_queues:96:5)