Skip to content

Commit

Permalink
chore: change seeding method
Browse files Browse the repository at this point in the history
  • Loading branch information
9helix committed Oct 25, 2024
1 parent f9138e7 commit 8eed8ce
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 159 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
"db:migrate": "drizzle-kit migrate",
"db:push": "drizzle-kit push",
"db:studio": "drizzle-kit studio",
"db:seed": "dotenv npx tsx ./src/server/db/seed.ts",
"db:seed:educations": "dotenv npx tsx ./src/server/db/seed/educations.seed.ts",
"db:seed:license": "dotenv npx tsx ./src/server/db/seed/license.seed.ts",
"db:seed:users": "dotenv npx tsx ./src/server/db/seed/user.seed.ts",
"db:seed:all": "dotenv npx tsx ./src/server/db/seed/index.ts",
"db:schema:generate": "node --experimental-specifier-resolution=node --loader ts-node/esm src/server/db/dbml-generator/dbml.ts && dbml-renderer -i schema.dbml -o erd.svg",
"dev": "next dev",
"lint": "next lint",
Expand Down
157 changes: 0 additions & 157 deletions src/server/db/seed.ts

This file was deleted.

17 changes: 17 additions & 0 deletions src/server/db/seed/education.seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface Education {
topics?: string;
type: EducationType;
}

const headerMapping: Record<string, keyof Education> = {
Title: "title",
Description: "description",
Expand All @@ -24,11 +25,13 @@ const headerMapping: Record<string, keyof Education> = {
"Trajanje obnove": "renewal_duration",
Teme: "topics",
};

const sheetNameMapping: Record<string, EducationType> = {
"za-volontere": EducationType.VOLUNTEERS,
"za-javnost": EducationType.PUBLIC,
"za-djelatnike": EducationType.EMPLOYEE,
};

const readExcelFile = (filePath: string): Education[] => {
const fileBuffer = readFileSync(filePath);
const workbook = XLSX.read(fileBuffer, { type: "buffer" });
Expand Down Expand Up @@ -63,9 +66,23 @@ const readExcelFile = (filePath: string): Education[] => {

return educations;
};

export const populateEducations = async () => {
const filePath = "scripts/educations_parser/edukacije.xlsx";
const _educations = readExcelFile(filePath);

return db.insert(educations).values(_educations).returning();
};

export const getEducations = async () => {
let _educations = await db.query.educations.findMany();
if (!_educations.length) {
_educations = await populateEducations();
}
};
getEducations()
.then((educations) => educations)
.catch((err) => {
console.log(err);
process.exit(1);
});
10 changes: 10 additions & 0 deletions src/server/db/seed/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { getEducations } from "./education.seed";
import { getLicenses } from "./license.seed";
import { getAdmins, getUsers } from "./user.seed";

void (async () => {
await getLicenses();
await getUsers();
await getAdmins();
await getEducations();
})();
15 changes: 15 additions & 0 deletions src/server/db/seed/license.seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,18 @@ export const populateLicenses = async () => {

return db.insert(licenses).values(_licenses).returning();
};

export const getLicenses = async () => {
let _licences = await db.query.licenses.findMany();
if (!_licences.length) {
_licences = await populateLicenses();
}
return _licences;
};

getLicenses()
.then((licenses) => licenses)
.catch((err) => {
console.log(err);
process.exit(1);
});
Loading

0 comments on commit 8eed8ce

Please sign in to comment.