I am writing a program which scrapes a web API and gathers a list of cryptocurrency symbols, and their daily price history.
I am running into an issue where, due to the construction of my database tables, if I encounter the same symbol more than once, the program adds a new row to the database which duplicates fully the data from the first time around.
Below are the two tables, as Typeorm entities:
First, the symbol:
@Entity()
export class CryptoSymbol {
@PrimaryColumn({ type: "int", unique: true })
id!: number;
@Column("varchar")
currency_code!: string;
@Column("varchar")
currency_name!: string;
@Column({ type: "boolean", nullable: true })
active!: boolean;
@OneToMany(
(type) => CryptoDailyData,
(cryptoDailyData) => cryptoDailyData.cryptoSymbol
)
cryptoDailyData!: CryptoDailyData[];
}
Then the DailyData:
@Entity()
export class CryptoDailyData {
@PrimaryGeneratedColumn()
id!: number;
@Column({ type: "datetime" })
date!: Date;
@Column({ type: "varchar" })
foreign_currency_type!: string;
@Column("float")
open_USD!: number;
@Column("float")
open_foreign_currency!: number;
@Column("float")
high_USD!: number;
@Column("float")
high_foreign_currency!: number;
@Column("float")
low_USD!: number;
@Column("float")
low_foreign_currency!: number;
@Column("float")
close_USD!: number;
@Column("float")
close_foreign_currency!: number;
@Column("float")
volume!: number;
@Column("float")
market_cap!: number;
@ManyToOne(
(type) => CryptoSymbol,
(cryptoSymbol) => cryptoSymbol.cryptoDailyData
)
cryptoSymbol!: CryptoSymbol;
}
For example: if I scrape "BTC" for BitCoin, and I store all of its daily history in the DailyData table, if I run the program again and encounter "BTC" again, it will reproduce the entire history of BTC for a second time.
What I think I'm running into is the fact that the "real" primary key of the DailyData table should be a combination of "Date" and "cryptoSymbolID," since dates can only repeat for different symbols. But I'm not sure you can define that kind of columnal relationship in Typeorm?