1# Database
2
3This app uses [Val Town SQLite](https://docs.val.town/std/sqlite/) to manage data. Every Val Town account comes with a free SQLite database, hosted on [Turso](https://turso.tech/). This folder is broken up into two files:
4
5* `migrations.ts` - code to set up the database tables the app needs
6* `queries.ts` - functions to run queries against those tables, which are imported and used in the main Hono server in `/backend/index.ts`
7
8## Migrations
9
10In `backend/database/migrations.ts`, this app creates a new SQLite table `reactHonoStarter_messages` to store messages.
11
12This "migration" runs once on every app startup because it's imported in `index.ts`. You can comment this line out for a slight (30ms) performance improvement on cold starts. It's left in so that users who fork this project will have the migration run correctly.
13
14SQLite has much more limited support for altering existing tables as compared to other databases. Often it's easier to create new tables with the schema you want, and then copy the data over. Happily LLMs are quite good at those sort of database operations, but please reach out in the [Val Town Discord](https://discord.com/invite/dHv45uN5RY) if you need help.
15
16## Queries
17
18The queries file is where running the migrations happen in this app. It'd also be reasonable for that to happen in index.ts, or as is said above, for that line to be commented out, and only run when actual changes are made to your database schema.
19
20The queries file exports functions to get and write data. It relies on shared types and data imported from the `/shared` directory.
3import { createTables, tableName } from "./migrations.ts";
4
5// This will create the database table if it doesn't exist.
6// This will run every time the app starts up. You can
7// comment out this line for a modest (30ms) perforamnce improvement
8// on cold starts. It's left in to ensure the database tables are
9// automatically set up correctly for users who fork this app.
10await createTables();
13
14 However, you should know that SQLite has much more limited
15 support for altering existing tables as compared to other databases.
16 Often it's easier to create new tables with the schema you want, and then
17 copy the data over. */
2import { readFile, servePublicFile } from "https://esm.town/v/stevekrouse/utils@187-main/serve-public/index.ts";
3import { Hono } from "npm:hono";
4import { getMessages, insertMessage } from "./database/queries.ts";
5
6const app = new Hono();
20- **Language:** Python
21- **GUI:** Tkinter
22- **Database:** SQLite
23- **Other Libraries:** CSV, datetime, os
24
154```
155โโโ backend/
156โ โโโ database/
157โ โ โโโ migrations.ts # Schema definitions
158โ โ โโโ queries.ts # DB query functions
213 ```
214
215### Database Patterns
216- Run migrations on startup or comment out for performance
217- Change table names when modifying schemas rather than altering
15- [ ] Rebuild as React Router?
16- [ ] opentownie as a pr bot
17- [ ] give it the ability to see its own client-side and server-side logs by building a middleware that shoves them into a SQL light database date and then give it a tool to access them
18- [ ] do a browser use or screenshot thing to give it access to its own visual output
19- [ ] Have it default to creating a new branch off main and then embedding and iframe to the resulting http val and give you a link to a pr opening url
7export const thinkTool = tool({
8 description:
9 "Use the tool to think about something. It will not obtain new information or change the database, but just append the thought to the log. Use it when complex reasoning or some cache memory is needed.",
10 parameters: z.object({
11 thought: z.string().describe("A thought to think about."),
149```
150โโโ backend/
151โ โโโ database/
152โ โ โโโ migrations.ts # Schema definitions
153โ โ โโโ queries.ts # DB query functions
208 ```
209
210### Database Patterns
211- Run migrations on startup or comment out for performance
212- Change table names when modifying schemas rather than altering
172
173/**
174 * Get database statistics for the homepage
175 */
176export async function getSearchStats(): Promise<{
837 const offset = (page - 1) * pageSize;
838
839 // Start all database queries in parallel
840 // 1. Launch count queries
841 const countsPromise = Promise.all([
924 );
925
926 // Wait for all database operations to complete in parallel
927 const [
928 [totalFileResults, totalProjectResults, totalUserResults],