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
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();
1import { Hono } from 'https://esm.sh/hono@3.11.7';
2import { readFile, serveFile } from "https://esm.town/v/std/utils@85-main/index.ts";
3import { initializeDatabase, cleanupExpiredSessions } from './database/schema.ts';
4import {
5 createUser,
12 getSession,
13 deleteSession
14} from './database/users.ts';
15import {
16 createArtwork,
22 searchArtworks,
23 getArtworkCountByUserId
24} from './database/artworks.ts';
25import {
26 hashPassword,
36const app = new Hono();
37
38// Initialize database on startup
39initializeDatabase().catch(console.error);
40
41// Clean up expired sessions periodically
1import { sqlite } from "https://esm.town/v/stevekrouse/sqlite";
2
3// Database table names with versioning for schema changes
4export const TABLES = {
5 USERS: 'pixelart_users_v1',
8};
9
10// Initialize database tables
11export async function initializeDatabase() {
12 try {
13 // Users table
64 await sqlite.execute(`CREATE INDEX IF NOT EXISTS idx_sessions_expires_at ON ${TABLES.SESSIONS}(expires_at)`);
65
66 console.log('Database initialized successfully');
67 } catch (error) {
68 console.error('Error initializing database:', error);
69 throw error;
70 }
1import { Hono } from "https://esm.sh/hono@3.11.7";
2import { sqlite } from "https://esm.town/v/stevekrouse/sqlite";
3import { getAllTools, getToolsByCategory, searchTools, getFeaturedTools, getToolStats } from "../database/queries.ts";
4import { CATEGORIES } from "../../shared/types.ts";
5
2import { readFile, serveFile } from "https://esm.town/v/std/utils@85-main/index.ts";
3import { sqlite } from "https://esm.town/v/stevekrouse/sqlite";
4import { createTables, seedData } from "./database/migrations.ts";
5import { getAllTools, getToolStats } from "./database/queries.ts";
6import { CATEGORIES } from "../shared/types.ts";
7import toolsRouter from "./routes/tools.ts";
14});
15
16// Initialize database on startup
17try {
18 await createTables();
19 await seedData();
20 console.log('โ
Database initialized successfully');
21} catch (error) {
22 console.error('โ Database initialization failed:', error);
23}
24
26app.route("/api/tools", toolsRouter);
27
28// Manual database seeding endpoint (for testing)
29app.get("/api/seed", async c => {
30 try {
61 return c.json({
62 success: true,
63 message: "Database seeded successfully",
64 stats,
65 testResult: result
103 html = html.replace("</head>", `${dataScript}</head>`);
104 } catch (dbError) {
105 console.error("Database error, serving without initial data:", dbError);
106 // Serve with empty initial data if database fails
107 const initialData = {
108 tools: [],
128app.get("/api/test-table", async c => {
129 try {
130 const { TABLE_NAME } = await import("./database/migrations.ts");
131 const tools = await sqlite.execute(`SELECT * FROM ${TABLE_NAME} LIMIT 5`);
132 return c.json({
32 }
33
34 console.log('๐ฑ Starting database seed...');
35 } catch (error) {
36 console.log('๐ฑ Table is empty or doesn\'t exist, proceeding with seed');
9- [ ] make it one click to branch off like old jp townie demos
10- [ ] opentownie as a pr bot
11- [ ] 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
12- [ ] do a browser use or screenshot thing to give it access to its own visual output
13- [ ] Have it default to creating a new branch off main
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."),