For one of my classes, I need to create a minimal database scenario. The task involves setting up a portfolio and storing the amount of coins within it.
So far, I've created tables for coins and portfolios. Each portfolio can contain multiple coins, and coins can belong to multiple portfolios, establishing a many-to-many relationship.
Now, the challenge is determining how to store the quantity of coins within each portfolio, considering that it varies for each one.
Do I need to create a third table with another many-to-many relationship? If so, what would this third table look like? Currently, our framework, mikro-orm, automatically generates a table called 'portfolio_coins' for the m:n relationship.
Here is an excerpt of my code:
Portfolio.ts
@Entity()
export class Portfolio extends BaseEntity {
@Property()
name: string;
@ManyToOne(() => User, { nullable: true, cascade: [] })
owner?: User;
@ManyToMany(() => Coin)
coins = new Collection<Coin>(this);
constructor({ name, owner }: CreatePortfolioDTO) {
super();
this.name = name;
this.owner = owner;
}
Coin.ts
@Entity()
export class Coin {
@PrimaryKey()
id: string;
@Property()
name: string;
@Property()
symbol: string;
@Property()
rank: number;
@Property()
marketCapUsd: number;
@Property()
priceUsd: number;
@ManyToMany(() => Portfolio, (portfolio) => portfolio.coins)
portfolios = new Collection<Portfolio>(this);
constructor({ id, name, symbol, rank, marketCapUsd, priceUsd}: CreateCoinDTO) {
this.id = id;
this.name = name;
this.symbol = symbol;
this.rank = rank;
this.marketCapUsd = marketCapUsd;
this.priceUsd = priceUsd;
}
Although we are using typescript, I believe the implementation will not be affected once I have a clear plan. I hope my question is clear. Thank you in advance!