34
35/**
36 * Store a chat message in the database
37 */
38export async function storeChatMessage(
130 }
131
132 console.log(`Weather forecast updated in the database.`);
133 return summary;
134}
125 }
126
127 console.log(`Calendar events imported into the database.`);
128 return events;
129 } catch (error) {
8
9/**
10 * Retrieves previously generated fun facts from the memories database
11 * @returns Array of previous fun facts
12 */
47
48/**
49 * Inserts a fun fact into the memories database
50 * @param date Date for the fun fact in ISO format
51 * @param factText The fun fact text
208```
209โโโ backend/
210โ โโโ database/
211โ โ โโโ migrations.ts # Schema definitions
212โ โ โโโ queries.ts # DB query functions
270- Handle API calls properly with proper error catching
271
272### Database Patterns
273- Run migrations on startup or comment out for performance
274- Change table names when modifying schemas rather than altering
67`;
68
69// --- DATABASE INITIALIZATION ---
70// This function ensures our tables are ready before the application handles requests.
71async function initializeDatabase() {
72 await sqlite.execute(`
73 CREATE TABLE IF NOT EXISTS loads (
99 const { count: loadCount } = await sqlite.execute("SELECT COUNT(*) as count FROM loads");
100 if (loadCount === 0) {
101 console.log("Seeding database with initial loads...");
102 await sqlite.execute({
103 sql:
117 const { count: carrierCount } = await sqlite.execute("SELECT COUNT(*) as count FROM carriers");
118 if (carrierCount === 0) {
119 console.log("Seeding database with initial carriers...");
120 await sqlite.batch([
121 {
345const app = new Hono();
346
347// Middleware for database initialization and CORS
348app.use("*", cors());
349app.use("*", async (c, next) => {
350 await initializeDatabase();
351 await next();
352});
433 }
434
435 // If approved, update the database
436 const updateResult = await sqlite.execute({
437 sql: "UPDATE loads SET subcontractor_id = ?, status = 'claimed', claimed_at = CURRENT_TIMESTAMP WHERE id = ?",
21## Further resources
22
23- [React Hono Example](https://www.val.town/x/stevekrouse/reactHonoExample) is a bigger example project, with a SQLite database table, queries, client-side CSS, a favicon, and shared code that runs on both client and server.
1import { Hono } from "https://esm.sh/hono@3.11.7";
2import type { CreateTaskRequest, UpdateTaskRequest, CreateProjectRequest, UpdateProjectRequest } from "../../shared/types.ts";
3import * as queries from "../database/queries.ts";
4
5const api = new Hono();
1import { Hono } from "https://esm.sh/hono@3.11.7";
2import { readFile, serveFile } from "https://esm.town/v/std/utils/index.ts";
3import { initializeDatabase } from "./database/schema.ts";
4import api from "./routes/api.ts";
5
11});
12
13// Initialize database on startup
14await initializeDatabase();
15
16// API routes
6const CONTEXTS_TABLE = 'gtd_contexts_v1';
7
8export async function initializeDatabase() {
9 // Create tasks table
10 await sqlite.execute(`