Skip to content

Commit

Permalink
Make seed script pull from a data-export endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
nsilvestri committed Apr 12, 2024
1 parent 9207ec8 commit 2d2a1fb
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 456 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ Note: `DIRECT_URL` is only used for compatibility with pgBouncer in production,

The hosted version of this project uses Supabase's PostgreSQL service as a database host, but does not use any additional features of Supabase.

### Seed Data Source

The database seeding script used in development is set up to copy non-sensitive data on the current public website through the `/api/data-export` endpoint. Set the environment variable `SEED_DATA_SOURCE` to the URL of the website (as of writing, `https://algdb.vercel.app/api/data-export`) and the populated data will be copied to the local database.

### NextAuth

Set `NEXTAUTH_URL` to the canonical URL of the site. Use `http://localhost:3000` for development.
Expand Down
70 changes: 70 additions & 0 deletions app/api/data-export/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import prisma from "@/prisma/global-prisma-client";
import { Prisma } from "@prisma/client";
import { NextResponse } from "next/server";

export type DataExport = {
algorithms: Prisma.AlgorithmCreateManyInput[];
algorithmsForCase: Prisma.AlgorithmsForCaseCreateManyInput[];
cases: Prisma.CaseCreateManyInput[];
methods: Prisma.MethodCreateManyInput[];
puzzles: Prisma.PuzzleCreateManyInput[];
sets: {
id: string;
name: string;
slug: string;
puzzleId: string;
visualizationId: string;
cases: {
id: string;
name: string;
setup: string;
puzzleId: string;
}[];
methods: {
id: string;
name: string;
slug: string;
puzzleId: string;
visualizationId: string;
}[];
}[];
visualizations: Prisma.VisualizationCreateManyInput[];
};

export async function GET(request: Request) {
const algorithms = prisma.algorithm.findMany();
const algorithmsForCase = prisma.algorithmsForCase.findMany();
const cases = prisma.case.findMany();
const methods = prisma.method.findMany();
const puzzles = prisma.puzzle.findMany();
const sets = prisma.set.findMany({
include: {
cases: true,
methods: true,
},
});
const visualizations = prisma.visualization.findMany();

const data = await Promise.all([
algorithms,
algorithmsForCase,
cases,
methods,
puzzles,
sets,
visualizations,
]);

return NextResponse.json(
{
algorithms: data[0],
algorithmsForCase: data[1],
cases: data[2],
methods: data[3],
puzzles: data[4],
sets: data[5],
visualizations: data[6],
},
{ status: 200 }
);
}
2 changes: 2 additions & 0 deletions app/api/sets/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ const Set: z.ZodType<Prisma.SetCreateInput> = z.object({
z.object({
name: z.string().min(1).max(255),
setup: z.string().min(1).max(255),
puzzleId: z.string(),
puzzle: z.object({
connect: z.optional(
z.object({
Expand All @@ -101,6 +102,7 @@ const Set: z.ZodType<Prisma.SetCreateInput> = z.object({
.array(
z.object({
moves: z.string(),
algorithmId: z.string(),
})
)
.nonempty(),
Expand Down
41 changes: 0 additions & 41 deletions prisma/seed-data/algorithm.ts

This file was deleted.

115 changes: 0 additions & 115 deletions prisma/seed-data/algorithms-for-case.ts

This file was deleted.

66 changes: 0 additions & 66 deletions prisma/seed-data/case.ts

This file was deleted.

24 changes: 0 additions & 24 deletions prisma/seed-data/method.ts

This file was deleted.

43 changes: 0 additions & 43 deletions prisma/seed-data/puzzle.ts

This file was deleted.

Loading

0 comments on commit 2d2a1fb

Please sign in to comment.