192```
193โโโ backend/
194โ โโโ database/
195โ โ โโโ migrations.ts # Schema definitions
196โ โ โโโ queries.ts # DB query functions
252 ```
253
254### Database Patterns
255- Run migrations on startup or comment out for performance
256- Change table names when modifying schemas rather than altering
10 overLimit,
11 startTrackingUsage,
12} from "../database/queries.tsx";
13import {
14 getTextEditorTool,
4import { INFERENCE_CALLS_TABLE, USAGE_TABLE } from "./schema.tsx";
5
6// Eventually we'll have a user database,
7// but in the meantime, we can cache user info in memory
8const userIdCache: { [key: string]: any } = {};
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)