Refactor code structure for improved readability and maintainability
This commit is contained in:
@@ -4,6 +4,8 @@ import { sql, eq } from 'drizzle-orm';
|
|||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import { arc, character, devilFruit, characterScrapeValidation, type DevilFruitType } from '../src/lib/server/db/schema';
|
import { arc, character, devilFruit, characterScrapeValidation, type DevilFruitType } from '../src/lib/server/db/schema';
|
||||||
|
|
||||||
|
type Status = 'Alive' | 'Dead' | 'Unknown';
|
||||||
|
|
||||||
type ArcRecord = {
|
type ArcRecord = {
|
||||||
id: string;
|
id: string;
|
||||||
name: string;
|
name: string;
|
||||||
@@ -22,9 +24,11 @@ type DevilFruitRecord = {
|
|||||||
type CharacterRecord = {
|
type CharacterRecord = {
|
||||||
id: string;
|
id: string;
|
||||||
name: string;
|
name: string;
|
||||||
|
frName?: string | null;
|
||||||
gender?: string | null;
|
gender?: string | null;
|
||||||
age?: number | null;
|
age?: number | null;
|
||||||
affiliations?: string[] | string | null;
|
affiliations?: string[] | string | null;
|
||||||
|
frAffiliations?: string[] | string | null;
|
||||||
devilFruitId?: string | null;
|
devilFruitId?: string | null;
|
||||||
hakiObservation?: boolean;
|
hakiObservation?: boolean;
|
||||||
hakiArmament?: boolean;
|
hakiArmament?: boolean;
|
||||||
@@ -32,12 +36,15 @@ type CharacterRecord = {
|
|||||||
bounty?: number | null;
|
bounty?: number | null;
|
||||||
height?: number | null;
|
height?: number | null;
|
||||||
origin?: string | null;
|
origin?: string | null;
|
||||||
|
frOrigin?: string | null;
|
||||||
firstAppearance?: number;
|
firstAppearance?: number;
|
||||||
pictureUrl?: string | null;
|
pictureUrl?: string | null;
|
||||||
epithets?: string[] | string | null;
|
epithets?: string[] | string | null;
|
||||||
status?: string | null;
|
frEpithets?: string[] | string | null;
|
||||||
|
status?: Status | null;
|
||||||
arcId?: string | null;
|
arcId?: string | null;
|
||||||
url?: string | null;
|
url?: string | null;
|
||||||
|
frUrl?: string | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
const DATABASE_URL = process.env.DATABASE_URL || 'file:local.db';
|
const DATABASE_URL = process.env.DATABASE_URL || 'file:local.db';
|
||||||
@@ -86,7 +93,7 @@ function toJsonArray(value: string[] | string | null | undefined): string[] | nu
|
|||||||
|
|
||||||
function toDevilFruitType(value: DevilFruitType | string | null | undefined): DevilFruitType | null {
|
function toDevilFruitType(value: DevilFruitType | string | null | undefined): DevilFruitType | null {
|
||||||
if (!value) return null;
|
if (!value) return null;
|
||||||
if (value === 'Paramecia' || value === 'Zoan' || value === 'Logia' || value === 'Unknown') {
|
if (value === 'Paramecia' || value === 'Zoan' || value === 'Logia' || value === 'Smile' || value === 'Unknown') {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
return 'Unknown';
|
return 'Unknown';
|
||||||
@@ -115,59 +122,25 @@ function transformCharacterData(item: CharacterRecord) {
|
|||||||
gender: toNullable(item.gender),
|
gender: toNullable(item.gender),
|
||||||
age: toNullable(item.age),
|
age: toNullable(item.age),
|
||||||
affiliations: toJsonArray(item.affiliations),
|
affiliations: toJsonArray(item.affiliations),
|
||||||
|
frAffiliations: toJsonArray(item.frAffiliations),
|
||||||
devilFruitId: toNullable(item.devilFruitId),
|
devilFruitId: toNullable(item.devilFruitId),
|
||||||
hakiObservation: !!item.hakiObservation,
|
hakiObservation: !!item.hakiObservation,
|
||||||
hakiArmament: !!item.hakiArmament,
|
hakiArmament: !!item.hakiArmament,
|
||||||
hakiConqueror: !!item.hakiConqueror,
|
hakiConqueror: !!item.hakiConqueror,
|
||||||
bounty: item.bounty ?? 0,
|
bounty: item.bounty ?? 0,
|
||||||
height: toNumber(item.height as any),
|
height: toNumber(item.height as string | number | null),
|
||||||
origin: toNullable(item.origin),
|
origin: toNullable(item.origin),
|
||||||
|
frOrigin: toNullable(item.frOrigin),
|
||||||
firstAppearance: item.firstAppearance ?? 0,
|
firstAppearance: item.firstAppearance ?? 0,
|
||||||
pictureUrl: toNullable(item.pictureUrl),
|
pictureUrl: toNullable(item.pictureUrl),
|
||||||
epithets: toJsonArray(item.epithets),
|
epithets: toJsonArray(item.epithets),
|
||||||
|
frEpithets: toJsonArray(item.frEpithets),
|
||||||
status: toNullable(item.status),
|
status: toNullable(item.status),
|
||||||
arcId: toNullable(item.arcId),
|
arcId: toNullable(item.arcId),
|
||||||
url: toNullable(item.url)
|
url: toNullable(item.url)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function hasChanged(jsonData: any, dbData: any): boolean {
|
|
||||||
if (!dbData) return true;
|
|
||||||
|
|
||||||
// Print any differences for debugging
|
|
||||||
for (const key in jsonData) {
|
|
||||||
const jsonValue = jsonData[key];
|
|
||||||
const dbValue = dbData[key];
|
|
||||||
const jsonString = typeof jsonValue === 'object' ? JSON.stringify(jsonValue) : String(jsonValue);
|
|
||||||
const dbString = typeof dbValue === 'object' ? JSON.stringify(dbValue) : String(dbValue);
|
|
||||||
if (jsonString !== dbString) {
|
|
||||||
console.log(`\nField "${key}" changed for character ID ${jsonData.id}:`);
|
|
||||||
console.log(` JSON: ${jsonString}`);
|
|
||||||
console.log(` DB: ${dbString}`);
|
|
||||||
} }
|
|
||||||
|
|
||||||
// Compare each field
|
|
||||||
return (
|
|
||||||
jsonData.name != dbData.name ||
|
|
||||||
jsonData.gender != dbData.gender ||
|
|
||||||
jsonData.age != dbData.age ||
|
|
||||||
JSON.stringify(jsonData.affiliations) != JSON.stringify(dbData.affiliations) ||
|
|
||||||
jsonData.devilFruitId != dbData.devilFruitId ||
|
|
||||||
jsonData.hakiObservation != dbData.hakiObservation ||
|
|
||||||
jsonData.hakiArmament != dbData.hakiArmament ||
|
|
||||||
jsonData.hakiConqueror != dbData.hakiConqueror ||
|
|
||||||
jsonData.bounty != dbData.bounty ||
|
|
||||||
jsonData.height != dbData.height ||
|
|
||||||
jsonData.origin != dbData.origin ||
|
|
||||||
jsonData.firstAppearance != dbData.firstAppearance ||
|
|
||||||
jsonData.pictureUrl != dbData.pictureUrl ||
|
|
||||||
JSON.stringify(jsonData.epithets) != JSON.stringify(dbData.epithets) ||
|
|
||||||
jsonData.status != dbData.status ||
|
|
||||||
jsonData.arcId != dbData.arcId ||
|
|
||||||
jsonData.url != dbData.url
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function isCharacterTableEmpty(): Promise<boolean> {
|
async function isCharacterTableEmpty(): Promise<boolean> {
|
||||||
const result = await db.select({ count: sql<number>`COUNT(*)` }).from(character);
|
const result = await db.select({ count: sql<number>`COUNT(*)` }).from(character);
|
||||||
return result[0]?.count === 0;
|
return result[0]?.count === 0;
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user