I have the following;
@Entity()
export class User {
@PrimaryGeneratedColumn()
id!: number
@Column()
name: string
}
Let's say I add a new User with
{name: "Kahvi", gold: "200", exp: "500"}
How would I go about adding gold & exp to an inventory entity within User?
I would probably create a child entity for Inventory
with columns for type
and quantity
, and whatever other columns might make sense.
Example:
@Entity()
export class User {
@PrimaryGeneratedColumn()
id!: number;
@Column()
name: string;
@OneToMany(type => InventoryItem, item => item.user)
inventory: InventoryItem[];
}
@Entity()
export class InventoryItem {
@PrimaryGeneratedColumn()
id!: number;
@Column()
type: string;
@Column()
quantity: number;
@ManyToOne(type => User, user => user.inventory)
user: User;
}
Then you can add items to it that look more like this:
{
name: "Kahvi",
inventory: [
{
type: "gold",
quantity: 200
},
{
type: "exp",
quantity: 500
}
]
}