[Draft] Stockage des données sur MariaDB avec TypeORM

This commit is contained in:
Lucàs
2024-10-23 17:07:23 +02:00
parent c95c92e987
commit cde872ca55
14 changed files with 323 additions and 144 deletions
+34 -6
View File
@@ -1,17 +1,45 @@
import { Data } from "./";
import { Data, InvalidData } from "./";
import { Column, Entity, PrimaryColumn, PrimaryGeneratedColumn } from "typeorm";
type RawOpenFoodFactsData = {
code: string;
countries_en: string;
};
@Entity()
class OpenFoodFactsData implements Data {
input: string[] = [];
output: string[] = [];
@PrimaryGeneratedColumn({
type: "integer",
})
id?: number;
constructor({ code, countries_en }: RawOpenFoodFactsData) {
this.input = [code];
this.output = [countries_en];
@Column()
barcode_ean_13: string;
@Column()
country: string;
constructor(code: string, gs1_country: string) {
this.barcode_ean_13 = code;
this.country = gs1_country;
}
fromRaw({
code,
countries_en,
}: RawOpenFoodFactsData): OpenFoodFactsData {
if (!code || !countries_en || code.length !== 13) {
throw new InvalidData("Invalid data");
}
return new OpenFoodFactsData(code, countries_en);
}
asData(openData: OpenFoodFactsData): any {
return {
"Barcode (EAN 13)": openData.barcode_ean_13,
Country: openData.country,
};
}
}