From d28313ced15cfcc2940e3b83baca1f0ef1823fd3 Mon Sep 17 00:00:00 2001 From: tknkaa Date: Wed, 5 Nov 2025 12:26:27 +0800 Subject: [PATCH] seed --- app/lib/db/seed.ts | 57 ++++++++++++++++++++++++++++++++++++++++++++++ bun.lock | 9 +++++--- package.json | 3 +++ 3 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 app/lib/db/seed.ts diff --git a/app/lib/db/seed.ts b/app/lib/db/seed.ts new file mode 100644 index 0000000..866bec4 --- /dev/null +++ b/app/lib/db/seed.ts @@ -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(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(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); +}); diff --git a/bun.lock b/bun.lock index 8be8a8a..5ef96a0 100644 --- a/bun.lock +++ b/bun.lock @@ -14,7 +14,6 @@ "react-dom": "^19.1.1", "react-router": "^7.9.2", "redis": "^5.9.0", - "zustand": "^5.0.8", }, "devDependencies": { "@cloudflare/vite-plugin": "^1.13.5", @@ -25,11 +24,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", @@ -391,6 +392,8 @@ "@types/redis": ["@types/redis@4.0.11", "", { "dependencies": { "redis": "*" } }, "sha512-bI+gth8La8Wg/QCR1+V1fhrL9+LZUSWfcqpOj2Kc80ZQ4ffbdL173vQd5wovmoV9i071FU9oP2g6etLuEwb6Rg=="], + "@types/uuid": ["@types/uuid@11.0.0", "", { "dependencies": { "uuid": "*" } }, "sha512-HVyk8nj2m+jcFRNazzqyVKiZezyhDKrGUA3jlEcg/nZ6Ms+qHwocba1Y/AaVaznJTAM9xpdFSh+ptbNrhOGvZA=="], + "acorn": ["acorn@8.14.0", "", { "bin": { "acorn": "bin/acorn" } }, "sha512-cl669nCJTZBsL97OF4kUQm5g5hC2uihk0NxY3WENAC0TYdILVkAyHymAntgxGkl7K+t0cXIrH5siy5S4XkFycA=="], "acorn-walk": ["acorn-walk@8.3.2", "", {}, "sha512-cjkyv4OtNCIeqhHrfS81QWXoCBPExR/J62oyEqepVw8WaQeSqpW2uhuLPh1m9eWhDuOo/jUXVTlifvesOWp/4A=="], @@ -761,6 +764,8 @@ "update-browserslist-db": ["update-browserslist-db@1.1.4", "", { "dependencies": { "escalade": "^3.2.0", "picocolors": "^1.1.1" }, "peerDependencies": { "browserslist": ">= 4.21.0" }, "bin": { "update-browserslist-db": "cli.js" } }, "sha512-q0SPT4xyU84saUX+tomz1WLkxUbuaJnR1xWt17M7fJtEJigJeWUNGUqrauFXsHnqev9y9JTRGwk13tFBuKby4A=="], + "uuid": ["uuid@13.0.0", "", { "bin": { "uuid": "dist-node/bin/uuid" } }, "sha512-XQegIaBTVUjSHliKqcnFqYypAd4S+WCYt5NIeRs6w/UAry7z8Y9j5ZwRRL4kzq9U3sD6v+85er9FvkEaBpji2w=="], + "valibot": ["valibot@1.1.0", "", { "peerDependencies": { "typescript": ">=5" }, "optionalPeers": ["typescript"] }, "sha512-Nk8lX30Qhu+9txPYTwM0cFlWLdPFsFr6LblzqIySfbZph9+BFsAHsNvHOymEviUepeIW6KFHzpX8TKhbptBXXw=="], "validate-npm-package-license": ["validate-npm-package-license@3.0.4", "", { "dependencies": { "spdx-correct": "^3.0.0", "spdx-expression-parse": "^3.0.0" } }, "sha512-DpKm2Ui/xN7/HQKCtpZxoRWBhZ9Z0kqtygG8XCgNQ8ZlDnxuQmWhj566j8fN4Cu3/JmbhsDo7fcAJq4s9h27Ew=="], @@ -795,8 +800,6 @@ "zod": ["zod@4.1.12", "", {}, "sha512-JInaHOamG8pt5+Ey8kGmdcAcg3OL9reK8ltczgHTAwNhMys/6ThXHityHxVV2p3fkw/c+MAvBHFVYHFZDmjMCQ=="], - "zustand": ["zustand@5.0.8", "", { "peerDependencies": { "@types/react": ">=18.0.0", "immer": ">=9.0.6", "react": ">=18.0.0", "use-sync-external-store": ">=1.2.0" }, "optionalPeers": ["@types/react", "immer", "react", "use-sync-external-store"] }, "sha512-gyPKpIaxY9XcO2vSMrLbiER7QMAMGOQZVRdJ6Zi782jkbzZygq5GI9nG8g+sMgitRtndwaBSl7uiqC49o1SSiw=="], - "@babel/core/semver": ["semver@6.3.1", "", { "bin": { "semver": "bin/semver.js" } }, "sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA=="], "@babel/helper-compilation-targets/semver": ["semver@6.3.1", "", { "bin": { "semver": "bin/semver.js" } }, "sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA=="], diff --git a/package.json b/package.json index c951092..3956f76 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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"