mirror of
https://github.com/kmitresse/Compo-Service-Log-Project.git
synced 2026-05-14 01:21:48 +00:00
[Draft] Stockage des données sur MariaDB avec TypeORM
This commit is contained in:
@@ -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,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user