Skip to content

Commit

Permalink
chore: db seed
Browse files Browse the repository at this point in the history
  • Loading branch information
krystxf committed Oct 20, 2024
1 parent cf82348 commit 46c0cfe
Show file tree
Hide file tree
Showing 7 changed files with 250,959 additions and 2 deletions.
6 changes: 5 additions & 1 deletion apps/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"prisma:generate": "prisma generate",
"prisma:migrate:create": "dotenv -e .env.local -- prisma migrate dev",
"prisma:migrate:deploy": "dotenv -e .env.local -- prisma migrate deploy",
"prisma:push": "dotenv -e .env.local -- prisma db push"
"prisma:push": "dotenv -e .env.local -- prisma db push",
"prisma:seed": "prisma db seed"
},
"dependencies": {
"@apollo/server": "^4.11.0",
Expand Down Expand Up @@ -91,5 +92,8 @@
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
},
"prisma": {
"seed": "dotenv -e .env.local -- ts-node prisma/seed.ts"
}
}
100 changes: 100 additions & 0 deletions apps/backend/prisma/seed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import * as fs from "fs";

import { PrismaClient } from "@prisma/client";

type Stop = {
id: string;
name: string;
avgLatitude: number;
avgLongitude: number;
};

type Route = {
id: string;
name: string;
isNight: boolean | null;
vehicleType: null;
};

type Platform = {
id: string;
name: string;
isMetro: boolean;
latitude: number;
longitude: number;
stopId: string;
};

type PlatformOnRoute = {
routeId: string;
platformId: string;
};

const parseSeedFile = <T>(path: string): T => {
const raw = fs.readFileSync(path).toString();

return JSON.parse(raw);
};

const prisma = new PrismaClient();
async function main() {
const stops = parseSeedFile<Stop[]>("./prisma/seeds/stops.json");
const routes = parseSeedFile<Route[]>("./prisma/seeds/routes.json");
const platforms = parseSeedFile<Platform[]>(
"./prisma/seeds/platforms.json",
);
const platformsOnRoutes = parseSeedFile<PlatformOnRoute[]>(
"./prisma/seeds/platforms-on-routes.json",
);

await prisma.$transaction(async (transaction) => {
await transaction.platformsOnRoutes.deleteMany();
await transaction.route.deleteMany();
await transaction.platform.deleteMany();
await transaction.stop.deleteMany();

await transaction.stop.createMany({
data: stops.map((stop) => ({
id: stop.id,
name: stop.name,
avgLongitude: stop.avgLongitude,
avgLatitude: stop.avgLatitude,
})),
});

await transaction.platform.createMany({
data: platforms.map((platform) => ({
id: platform.id,
name: platform.name,
isMetro: platform.isMetro,
latitude: platform.latitude,
longitude: platform.longitude,
stopId: platform.stopId ?? null,
})),
});

await transaction.route.createMany({
data: routes.map((platform) => ({
id: platform.id,
name: platform.name,
})),
});

await transaction.platformsOnRoutes.createMany({
data: platformsOnRoutes.map((platformOnRoute) => ({
platformId: platformOnRoute.platformId,
routeId: platformOnRoute.routeId,
})),
skipDuplicates: true,
});
});
}
main()
.then(async () => {
await prisma.$disconnect();
})
.catch(async (e) => {
console.error(e);
await prisma.$disconnect();
process.exit(1);
});
Loading

0 comments on commit 46c0cfe

Please sign in to comment.