Skip to content

Commit 986cb11

Browse files
authored
Merge pull request #18 from ut-code/Database
バックエンドとデータベースの導入
2 parents 3465f71 + 01eafae commit 986cb11

12 files changed

Lines changed: 257 additions & 3 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@ Thumbs.db
2121
# Vite
2222
vite.config.js.timestamp-*
2323
vite.config.ts.timestamp-*
24+
25+
# prisma
26+
/generated/

.prettierignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,8 @@ yarn.lock
55
bun.lock
66
bun.lockb
77

8+
# generated files
9+
/generated
10+
811
# Miscellaneous
912
/static/

bun.lock

Lines changed: 91 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
"version": "0.0.1",
55
"type": "module",
66
"scripts": {
7-
"dev": "vite dev",
7+
"dev": "bun --env-file=.env vite dev",
88
"build": "vite build",
99
"preview": "vite preview",
10-
"prepare": "svelte-kit sync; lefthook install; true",
10+
"prepare": "svelte-kit sync; prisma generate; lefthook install; true",
1111
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
1212
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
1313
"format": "prettier --write .",
@@ -29,11 +29,16 @@
2929
"lefthook": "^1.13.6",
3030
"prettier": "^3.6.2",
3131
"prettier-plugin-svelte": "^3.4.0",
32+
"prisma": "^6.19.0",
3233
"svelte": "^5.39.5",
3334
"svelte-check": "^4.3.2",
3435
"tailwindcss": "^4.1.14",
3536
"typescript": "^5.9.2",
3637
"typescript-eslint": "^8.44.1",
3738
"vite": "^7.1.7"
39+
},
40+
"dependencies": {
41+
"@prisma/adapter-pg": "^6.19.0",
42+
"@prisma/client": "^6.19.0"
3843
}
3944
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Please do not edit this file manually
2+
# It should be added in your version-control system (e.g., Git)
3+
provider = "postgresql"

prisma/schema.prisma

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// This is your Prisma schema file,
2+
// learn more about it in the docs: https://pris.ly/d/prisma-schema
3+
4+
// Looking for ways to speed up your queries, or scale easily with your serverless or edge functions?
5+
// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init
6+
7+
generator client {
8+
provider = "prisma-client-js"
9+
output = "../node_modules/@prisma/generated"
10+
engineType = "client" // no Rust engine
11+
}
12+
13+
datasource db {
14+
provider = "postgresql"
15+
url = env("DATABASE_URL")
16+
}
17+
18+
model BoardState {
19+
id Int @id @default(autoincrement())
20+
createdAt DateTime @default(now())
21+
boardData Json
22+
}

src/iframe/life-game.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ <h1>
1212
<button id="sizeChangeButton">サイズ変更</button>
1313

1414
<table id="game-board" style="border-collapse: collapse"></table>
15-
<button id="randombutton">RANDOM</button>      
15+
<button id="randombutton">RANDOM</button>
1616
<button id="resetbutton">RESET</button>
17+
<button id="saveButton">SAVE</button>
18+
<button id="loadButton">LOAD</button>
1719
<div id="pattern-button-container"></div>
1820
<script src="./life-game.js"></script>
1921
</body>

src/iframe/life-game.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ const generation = document.getElementById("generation"); //世代を表す文
3131
const randomButton = document.getElementById("randombutton");
3232
const resetButton = document.getElementById("resetbutton");
3333
const sizeChangeButton = document.getElementById("sizeChangeButton");
34+
const saveButton = document.getElementById("saveButton");
35+
const loadButton = document.getElementById("loadButton");
3436
//サイズ入力欄
3537
const sizeInput = document.getElementById("sizeInput");
3638
const sizeLabel = document.getElementById("sizeLabel");
@@ -187,3 +189,19 @@ sizeChangeButton.onclick = () => {
187189
stop();
188190
updatePatternButtons();
189191
};
192+
193+
saveButton.onclick = async () => {
194+
window.parent.postMessage({ type: "save_board", data: board }, "*");
195+
};
196+
197+
loadButton.onclick = async () => {
198+
window.parent.postMessage({ type: "request:load_board" }, "*");
199+
};
200+
201+
on.load_board = (loadedBoard) => {
202+
console.log("on.load_board");
203+
board = loadedBoard;
204+
renderBoard();
205+
generationChange(0);
206+
stop();
207+
};

src/lib/prisma.server.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { PrismaPg } from "@prisma/adapter-pg";
2+
import { PrismaClient } from "@prisma/generated/client";
3+
4+
export const prisma = new PrismaClient({
5+
adapter: new PrismaPg({
6+
connectionString: process.env.DATABASE_URL,
7+
}),
8+
});

src/routes/+page.svelte

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import * as icons from "$lib/icons/index.ts";
88
import patterns from "$lib/board-templates";
99
import { onMount } from "svelte";
10+
import { loadBoard, saveBoard } from "./api.ts";
1011
1112
let code = $state(lifeGameJS);
1213
@@ -40,6 +41,33 @@
4041
function sendEvent(event: string, message?: unknown) {
4142
preview_iframe?.contentWindow?.postMessage({ type: event, data: message }, "*");
4243
}
44+
45+
onMount(() => {
46+
const handler = async (event: MessageEvent<unknown>) => {
47+
console.log("handler call");
48+
const data = event.data as
49+
| { type: "unknown event" }
50+
| { type: "save_board"; data: boolean[][] }
51+
| { type: "request:load_board" };
52+
if (data.type === "save_board") {
53+
console.log("board saved!");
54+
await saveBoard(data.data);
55+
return;
56+
}
57+
58+
if (data.type === "request:load_board") {
59+
console.log("loaded board");
60+
const board = await loadBoard();
61+
if (board) {
62+
sendEvent("load_board", board);
63+
}
64+
return;
65+
}
66+
};
67+
68+
window.addEventListener("message", handler);
69+
return () => window.removeEventListener("message", handler);
70+
});
4371
</script>
4472

4573
<div class="navbar bg-[#E0E0E0] shadow-sm">

0 commit comments

Comments
 (0)