Skip to content

Commit

Permalink
Handle duplicate records
Browse files Browse the repository at this point in the history
  • Loading branch information
rocktimsaikia committed Dec 25, 2024
1 parent b675fd3 commit d1193cb
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions server/scripts/populate-altname.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { PrismaClientKnownRequestError } from "@prisma/client/runtime/library";
import type { Options } from "csv-parse";
import { parse } from "csv-parse";
import fs from "node:fs";
Expand All @@ -18,19 +19,27 @@ fs.createReadStream(filePath)
.on("data", async (row) => {
if (!row.malId || row.malId === "") return;

const updatedRecord = await prisma.anime.update({
where: {
id: Number.parseInt(row.id),
},
data: {
name: row.name,
altName: row.altName,
malId: Number.parseInt(row.malId),
episodeCount: Number.parseInt(row.episodeCount),
},
});
if (updatedRecord) {
console.log(`Updated ${updatedRecord.id}`);
try {
const updatedRecord = await prisma.anime.update({
where: {
id: Number.parseInt(row.id),
},
data: {
name: row.name,
altName: row.altName,
malId: Number.parseInt(row.malId),
episodeCount: Number.parseInt(row.episodeCount),
},
});
if (updatedRecord) {
console.log(`Updated ${updatedRecord.id}`);
}
} catch (error) {
if (error instanceof PrismaClientKnownRequestError) {
if (error.code === "P2002") {
console.log("Duplicate Record: ", row.id);
}
}
}
})
.on("error", (err) => {
Expand Down

0 comments on commit d1193cb

Please sign in to comment.