62 lines
1.7 KiB
TypeScript
62 lines
1.7 KiB
TypeScript
import { createId } from "@paralleldrive/cuid2";
|
|
import db from "..";
|
|
import { answers } from "../schema/answers";
|
|
import { options } from "../schema/options";
|
|
import { assessments } from "../schema/assessments";
|
|
|
|
type answers = {
|
|
id: string;
|
|
optionId: string;
|
|
assessmentId: string | null;
|
|
isFlagged: boolean;
|
|
filename: string;
|
|
validationInformation: string;
|
|
createdAt: Date;
|
|
updatedAt: Date;
|
|
};
|
|
|
|
const answersSeeder = async () => {
|
|
const optionsData = await db.select().from(options);
|
|
const assessmentData = await db.select().from(assessments);
|
|
|
|
const answersData: answers[] = [];
|
|
|
|
optionsData.forEach(option => {
|
|
// assessmentData.forEach(assessment => {
|
|
answersData.push({
|
|
id: createId(),
|
|
optionId: option.id,
|
|
assessmentId: null,
|
|
isFlagged: false,
|
|
filename: "answer.pdf",
|
|
validationInformation: "Sudah teruji dengan baik",
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
});
|
|
// });
|
|
});
|
|
|
|
console.log("Seeding answers...");
|
|
|
|
for (let submitAnswers of answersData) {
|
|
try {
|
|
const insertedAnswers = (
|
|
await db
|
|
.insert(answers)
|
|
.values(submitAnswers)
|
|
.onConflictDoNothing()
|
|
.returning()
|
|
)[0];
|
|
|
|
if (insertedAnswers) {
|
|
console.log(`Answers created`);
|
|
} else {
|
|
console.log(`Answers already exists`);
|
|
}
|
|
} catch (error) {
|
|
console.error(`Error inserting answers :`, error);
|
|
}
|
|
}
|
|
};
|
|
|
|
export default answersSeeder; |