198```
199โโโ backend/
200โ โโโ database/
201โ โ โโโ migrations.ts # Schema definitions
202โ โ โโโ queries.ts # DB query functions
257 ```
258
259### Database Patterns
260- Run migrations on startup or comment out for performance
261- Change table names when modifying schemas rather than altering
1import { Hono } from "https://esm.sh/hono@3.11.7";
2import { create, decode, verify } from "https://deno.land/x/djwt@v2.8/mod.ts";
3import { createUser, getUserByUsername, getUserByEmail, verifyPassword } from "../database/queries.ts";
4import { formatApiResponse } from "../../shared/utils.ts";
5import { CreateUserInput, LoginInput, User } from "../../shared/types.ts";
15app.use("*", cors());
16
17// Database setup
18const BOOKS_TABLE = "book_club_books";
19const VOTES_TABLE = "book_club_votes";
20
21async function setupDatabase() {
22 // Create books table
23 await sqlite.execute(`
44}
45
46// Run database setup
47// Make sure database is set up before handling requests
48let dbInitialized = false;
49setupDatabase()
50 .then(() => {
51 dbInitialized = true;
52 console.log("Database initialized successfully");
53 })
54 .catch(error => {
55 console.error("Database initialization failed:", error);
56 throw error; // Re-throw to see the error in logs
57 });
58
59// Middleware to check if database is initialized
60app.use("*", async (c, next) => {
61 if (!dbInitialized && !c.req.path.includes("frontend/")) {
62 return c.json({ error: "Database is initializing, please try again in a moment" }, 503);
63 }
64 return await next();
6
7- `index.ts` - Main entry point for the API
8- `/database` - Database schema and queries
9- `/routes` - API routes for properties and admin functionality
10
7 deletePropertyImage,
8 verifyAdminCredentials
9} from "../database/queries.ts";
10import { ApiResponse, PropertyFormData } from "../../shared/types.ts";
11import { blob } from "https://esm.town/v/std/blob";
7 getPropertiesWithImages,
8 getPropertyWithImages
9} from "../database/queries.ts";
10import { ApiResponse, Property, PropertyWithImages } from "../../shared/types.ts";
11
14
15- `/backend` - API and server-side code
16 - `/database` - Database schema and queries
17 - `/routes` - API endpoints
18- `/frontend` - Client-side code and assets
31- Hono (Backend API)
32- React (Frontend)
33- SQLite (Database)
34- TailwindCSS (Styling)
2import { cors } from "https://esm.sh/@hono/cors@0.0.6";
3import { readFile, serveFile } from "https://esm.town/v/std/utils@85-main/index.ts";
4import { setupDatabase } from "./database/migrations.ts";
5import { getTodos, getTodoById, createTodo, updateTodo, deleteTodo } from "./database/queries.ts";
6
7// Initialize the database
8try {
9 await setupDatabase();
10 console.log("Database setup complete");
11} catch (error) {
12 console.error("Database setup failed:", error);
13}
14
2import { TODO_TABLE } from "./migrations.ts";
3
4// Convert database row to Todo object
5function rowToTodo(row: any) {
6 return {
4export const TODO_TABLE = 'todos_v1';
5
6export async function setupDatabase() {
7 // Create the todos table if it doesn't exist
8 await sqlite.execute(`
15 `);
16
17 console.log(`Database setup complete. Table: ${TODO_TABLE}`);
18}