192```
193โโโ backend/
194โ โโโ database/
195โ โ โโโ migrations.ts # Schema definitions
196โ โ โโโ queries.ts # DB query functions
251 ```
252
253### Database Patterns
254- Run migrations on startup or comment out for performance
255- Change table names when modifying schemas rather than altering
4import { convertToCoreMessages, type CoreUserMessage, streamText } from "npm:ai";
5import { Hono } from "npm:hono";
6import { overLimit, trackUsage } from "../database/queries.tsx";
7import { getTextEditorTool, makeChangeValTypeTool, makeDeleteTool, thinkTool } from "../tools/index.ts";
8import fileWithLinesNumbers from "../utils/fileWithLinesNumbers.ts";
4import { 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 } = {};
21
22/**
23 * Store a message in the database using Val Town's blob storage
24 */
25export default async function storeMessage(
2import { serveStatic } from "https://esm.sh/@hono/node-server@1.2.0/serve-static";
3import { readFile, serveFile } from "https://esm.town/v/std/utils@85-main/index.ts";
4import { setupDatabase, saveSubmission, getSubmission, updateSubmission } from "./database.ts";
5import { sendConfirmationEmail } from "./email.ts";
6import { generateUniqueId } from "./utils.ts";
59app.post("/submit", async (c) => {
60 try {
61 // Initialize database if needed
62 await setupDatabase();
63
64 const formData = await c.req.formData();
73 const uniqueId = generateUniqueId();
74
75 // Save to database
76 await saveSubmission(uniqueId, companyName, email);
77
105 const { id: _, images: __, ...fieldsToUpdate } = body;
106
107 // Update the submission in the database
108 await updateSubmission(id, fieldsToUpdate);
109
7- Simple landing page with LauraMepson branding
8- Form to collect company name and email
9- SQLite database storage for submissions
10- Email functionality to send users a unique link
11- Continuation page for adding more information
15
16- `index.ts` - Main HTTP endpoint and route handling
17- `database.ts` - SQLite database operations
18- `email.ts` - Email sending functionality
19- `utils.ts` - Helper functions
22- `image-storage.ts` - Image storage and retrieval functionality
23
24## Database Schema
25
26The application uses a SQLite database with the following schema:
27
28```sql
401. User visits the landing page
412. User submits company name and email
423. Data is saved to SQLite database
434. User receives an email with a unique link
445. User can click the link to add more information
456. Additional information is saved to the database
467. User can upload and manage images related to their submission
47
61## Development Notes
62
63- When modifying the database schema, create a new table with a new version number
64- Email templates are defined in `email.ts`
65- The unique ID generation is handled in `utils.ts`
13}
14
15// Set up the database tables
16export async function setupDatabase() {
17 try {
18 // Create submissions table
25 )`);
26
27 console.log("Database setup complete");
28 } catch (error) {
29 console.error("Error setting up database:", error);
30 throw error;
31 }
102 };
103
104 // Update the database
105 await sqlite.execute(
106 `UPDATE ${SUBMISSIONS_TABLE}
1import { serveFile } from "https://esm.town/v/std/utils/index.ts";
2import { generateCode } from "./backend/generate-code.ts";
3import { createTables } from "./database/migrations.ts";
4import { createProject, getCode, getNextVersionNumber, insertVersion } from "./database/queries.ts";
5
6await createTables();
45 }
46
47 // Now it's time to upload things to database and blob storage
48 // First, add to database, and get the ID
49 const id = await sqlite.execute(
50 `INSERT INTO ${TABLE_NAME} (title, data, type, time) VALUES ($title, $data, $type, $time)`,
4import { convertToCoreMessages, type CoreUserMessage, streamText } from "npm:ai";
5import { Hono } from "npm:hono";
6import { overLimit, trackUsage } from "../database/queries.tsx";
7import { getTextEditorTool, makeChangeValTypeTool, makeDeleteTool, thinkTool } from "../tools/index.ts";
8import fileWithLinesNumbers from "../utils/fileWithLinesNumbers.ts";