13## A Few Honkin' Notes
14
15### database migrations need love and care 🫂
16
17Not totally sure how to generate and apply drizzle migrations yet!
5 * For this reason, we need to manually create the users table.
6 */
7export const migrateDatabase = async () => {
8 await sqlite.execute(`
9CREATE TABLE IF NOT EXISTS \`users\` (
6import usersApi from "./app/users.ts";
7import { db } from "./db/client.ts";
8import { migrateDatabase } from "./db/migrate.ts";
9
10// Migrate database on startup to make sure the proper table exists to store users
11// NOTE - Please comment on the template project if you have ideas on how to do migrations more cleanly on Valtown
12await migrateDatabase();
13
14const app = new Hono<AppType>();
15
16/**
17 * Middleware for setting up the database and storing it on the Hono app's context
18 */
19app.use(async (c, next) => {
1// Import drizzle deps with npm: specifier because exports don't get picked up properly by typescript when using esm.sh
2import { drizzle, type LibSQLDatabase } from "npm:drizzle-orm@0.39.3/libsql";
3import { and, eq, desc, relations, sql } from "npm:drizzle-orm@0.39.3";
4import { integer, sqliteTable, text } from "npm:drizzle-orm@0.39.3/sqlite-core";
5
6export { drizzle };
7export type { LibSQLDatabase };
8export { and, eq, desc, relations, sql };
9export { integer, sqliteTable, text };
5
6/**
7 * The typed database client, allowing us to construct sql queries and
8 * use Drizzle's ORM features against the ValTown sqlite database
9 */
10export const db: DBType = drizzle(sqlite as any, {
1// Import drizzle deps with npm: specifier because exports don't get picked up properly by typescript when using esm.sh
2import { drizzle, type LibSQLDatabase } from "npm:drizzle-orm@0.39.3/libsql";
3import { and, eq, desc, relations, sql } from "npm:drizzle-orm@0.39.3";
4import { integer, sqliteTable, text } from "npm:drizzle-orm@0.39.3/sqlite-core";
5
6export { drizzle };
7export type { LibSQLDatabase };
8export { and, eq, desc, relations, sql };
9export { integer, sqliteTable, text };
333
334 try {
335 // Initialize database with enhanced error handling
336 await sqlite.execute(`
337 CREATE TABLE IF NOT EXISTS ${KEY}_tools_${SCHEMA_VERSION} (
367 `);
368
369 serverLog("INFO", "Database initialized successfully", {
370 key: KEY,
371 schemaVersion: SCHEMA_VERSION,
372 });
373 } catch (dbInitError) {
374 serverLog("ERROR", "Database initialization failed", {
375 error: dbInitError instanceof Error ? dbInitError.message : String(dbInitError),
376 key: KEY,
378 return new Response(
379 JSON.stringify({
380 error: "Database initialization failed",
381 details: String(dbInitError),
382 }),
535 const toolRequest = agentAResponse.tool;
536
537 // Store the tool in the database
538 const toolId = toolRequest.id || crypto.randomUUID();
539 await sqlite.execute(
590 const toolResponse: ToolResponse = JSON.parse(agentBContent);
591
592 // Store the tool request and response in the database
593 const requestId = crypto.randomUUID();
594 await sqlite.execute(
680 const toolResponse: ToolResponse = JSON.parse(completionContent);
681
682 // Store the tool request and response in the database
683 const requestId = toolRequest.id || crypto.randomUUID();
684 await sqlite.execute(
811 }
812
813 // If it's a new tool, store it in the database
814 if (agentAResponse.tool) {
815 const toolId = crypto.randomUUID();
4
5// Comprehensive fake name data by country and gender
6const nameDatabase = {
7 Bangladesh: {
8 male: ["Md. Rahim", "Abdul Khan", "Jahangir Ahmed", "Rashid Islam", "Akram Hossain"],
65
66 const generateEmail = () => {
67 const names = nameDatabase[country][gender];
68 const randomName = names[Math.floor(Math.random() * names.length)];
69
109 style={{ width: "100%", marginBottom: "10px" }}
110 >
111 {Object.keys(nameDatabase).map(countryName => (
112 <option key={countryName} value={countryName}>{countryName}</option>
113 ))}
1import { generateCode } from "./backend/generate-code";
2import { createTables } from "./database/migrations";
3import { createProject, getCode, getNextVersionNumber, insertVersion } from "./database/queries";
4
5async function servePublicFile(path: string): Promise<Response> {
1import { generateCode } from "./backend/generate-code";
2import { createTables } from "./database/migrations";
3import { createProject, getCode, getNextVersionNumber, insertVersion } from "./database/queries";
4
5async function servePublicFile(path: string): Promise<Response> {