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 { runMigrations } from "./database/migrations.ts";
4import { getMessageTemplates, getContacts, getSettings } from "./database/queries.ts";
5import messages from "./routes/messages.ts";
6import scheduler from "./routes/scheduler.ts";
13});
14
15// Initialize database on startup
16await runMigrations();
17
1import { Hono } from "https://esm.sh/hono@3.11.7";
2import type { ApiResponse } from "../../shared/types.ts";
3import { getPendingMessages, updateMessageStatus, createScheduledMessage } from "../database/queries.ts";
4import { generateWhatsAppUrl, getNextRecurrence } from "../../shared/utils.ts";
5
7 updateMessageStatus,
8 createOrUpdateContact
9} from "../database/queries.ts";
10import { formatPhoneNumber, validatePhoneNumber, isValidFutureDateTime, generateId } from "../../shared/utils.ts";
11
10
11export async function runMigrations() {
12 console.log('Running database migrations...');
13
14 // Scheduled Messages table
67 await insertDefaultSettings();
68
69 console.log('Database migrations completed');
70}
71
25- **Frontend**: React with TypeScript
26- **Backend**: Hono API framework
27- **Database**: SQLite for message storage
28- **Styling**: TailwindCSS
29- **Storage**: Local storage + server backup
34โโโ backend/
35โ โโโ index.ts # Main API server
36โ โโโ database/
37โ โ โโโ migrations.ts # Database schema
38โ โ โโโ queries.ts # Database operations
39โ โโโ routes/
40โ โโโ messages.ts # Message CRUD operations
95- **HTTP-Only Cookies**: Session cookies are not accessible via JavaScript
96- **Token Refresh**: Automatic refresh of expired access tokens
97- **Secure Storage**: Tokens are stored securely in the database
98
99## Troubleshooting
3## Description
4
5This is an AI-powered Grocery Shopping assistant that turns an imprecise list of items like "milk, bread, eggs" into a Kroger cart full of groceries. It relies on the Kroger API to search each item, then uses an LLM to decide which specific UPC to add to the cart (again, using the Kroger API). Once an item is selected, we store the UPC in the database so we can avoid consulting the LLM for that item again.
6
7Households have preferences for which items to buy, and what priorities they have for each item. For example, we prefer to buy free range eggs and poultry, but don't particularly care whether they are organic or not. These details are stored in a database, which should also be consulted to add context to the LLM's decisions.
8
9## Kroger OAuth Implementation
35- **User Management**: Create/update users based on Kroger profile ID
36
37## Database Schema
38
39The database consists of the following tables:
40
41### kroger_users_1
119โโโ backend/
120โ โโโ index.ts # Main HTTP handler with OAuth routes
121โ โโโ database/
122โ โ โโโ migrations.ts # Database schema setup
123โ โ โโโ krogerQueries.ts # Database query functions
124โ โโโ services/
125โ โโโ krogerAuth.ts # Kroger OAuth service
1// Script to set up the telegram_chats table in SQLite
2// Run this script manually to create the database table
3
4export default async function setupTelegramChatDb() {
25 `);
26
27 return "Telegram chat database table created successfully.";
28 } catch (error) {
29 console.error("Error setting up telegram_chats table:", error);
4
5* `index.ts` - this is the **entrypoint** for this whole project
6* `database/` - this contains the code for interfacing with the app's SQLite database table
7
8## Hono
26## CRUD API Routes
27
28This app has two CRUD API routes: for reading and inserting into the messages table. They both speak JSON, which is standard. They import their functions from `/backend/database/queries.ts`. These routes are called from the React app to refresh and update data.
29
30## Errors
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.