Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions app/lib/db/seed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { drizzle } from "drizzle-orm/node-postgres";
import { Client } from "pg";
import { v4 as uuidv4 } from "uuid";
import type { Hai, SuhaiKind } from "../hai";
import { constructHai, haiToDBHai } from "../hai";
import { hai, haiyama } from "./schema";

function shuffleArray<T>(array: T[]): T[] {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
const temp = array[i];
if (temp !== undefined && array[j] !== undefined) {
array[i] = array[j];
array[j] = temp;
}
}
return array;
}

function createHaiyama(): Hai[] {
const kinds: SuhaiKind[] = ["manzu", "pinzu", "souzu"];
let sortedHaiyama: Hai[] = [];

for (const kind of kinds) {
for (let value = 1; value < 10; value++) {
sortedHaiyama = sortedHaiyama.concat(
new Array<Hai>(4).fill(constructHai(kind, value)),
);
}
}
return shuffleArray(sortedHaiyama);
}

async function main() {
const client = new Client({
connectionString: process.env.DATABASE_URL,
});
await client.connect();
const db = drizzle(client);

const newHaiyamaId = uuidv4();
const haiyamaData = createHaiyama();

await db.insert(haiyama).values({ id: newHaiyamaId });

const haiData = haiyamaData.map((h, i) => haiToDBHai(h, newHaiyamaId, i));

await db.insert(hai).values(haiData);

console.log("Seeding complete");
await client.end();
}

main().catch((err) => {
console.error(err);
process.exit(1);
});
9 changes: 6 additions & 3 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"build": "react-router build",
"cf-typegen": "wrangler types",
"deploy": "bun run build && wrangler deploy",
"db:seed": "bun run app/lib/db/seed.ts",
"dev": "react-router dev",
"postinstall": "npm run cf-typegen",
"preview": "bun run build && vite preview",
Expand Down Expand Up @@ -33,11 +34,13 @@
"@types/react": "^19.1.13",
"@types/react-dom": "^19.1.9",
"@types/redis": "^4.0.11",
"@types/uuid": "^11.0.0",
"daisyui": "^5.3.10",
"drizzle-kit": "^0.31.5",
"lefthook": "^2.0.2",
"tailwindcss": "^4.1.13",
"typescript": "^5.9.2",
"uuid": "^13.0.0",
"vite": "^7.1.7",
"vite-tsconfig-paths": "^5.1.4",
"wrangler": "^4.45.0"
Expand Down