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
2
3/**
4 * Retrieves all memories from the database
5 * @param includeDate Whether to include date-specific memories or not
6 * @param startDate Optional start date to filter memories from (ISO format)
38}
39
40// Clear all memories from the database
41async function clearMemories() {
42 try {
90 await resetTelegramChat();
91
92 console.log("Memory database and telegram chat successfully cleared!");
93 return "Memory database and telegram chat successfully cleared! Stevens will now ask the setup questions again when you restart the conversation with /start.";
94 } catch (error) {
95 console.error("Error setting up empty memory database:", error);
96 throw error;
97 }
4
5/**
6 * Clears all data from the memories database
7 * Can be used to remove demo data or start fresh
8 */
9export default async function cleanDatabase() {
10 try {
11 console.log("Clearing all data from memories database...");
12
13 // Delete all records from the memories table
14 const result = await sqlite.execute(`DELETE FROM ${TABLE_NAME}`);
15
16 console.log(`โ
Deleted ${result.rowsAffected} records from the database.`);
17 return `Deleted ${result.rowsAffected} records from the database.`;
18 } catch (error) {
19 console.error("Error clearing database:", error);
20 throw error;
21 }
25// @ts-ignore: Deno-specific API
26if (import.meta.url === Deno.mainModule) {
27 await cleanDatabase();
28}
1import setupDatabase from "./dbUtils/setupDatabase.ts";
2import cleanDatabase from "./dbUtils/cleanDatabase.ts";
3import getCalendarEvents from "./importers/getCalendarEvents.ts";
4
5/**
6 * Initializes the application by:
7 * 1. Optionally cleaning the database (removing all existing data)
8 * 2. Setting up the database structure
9 * 3. Importing calendar events (if Google Calendar credentials are set)
10 * 4. Setting up any other integrations
14 console.log("Initializing memories app...");
15
16 // Step 0: Optionally clean the database first
17 if (options.cleanFirst) {
18 console.log("Cleaning database...");
19 await cleanDatabase();
20 console.log("โ
Database cleaned");
21 }
22
23 // Step 1: Set up the database
24 await setupDatabase();
25 console.log("โ
Database initialized");
26
27 // Step 2: Import calendar events if credentials are set
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);
13## Technical Architecture
14
15**โ ๏ธ important caveat: the admin dashboard doesn't have auth! currently it just relies on security by obscurity of people not knowing the url to a private val. this is not very secure. if you fork this project and put sensitive data in a database you should think carefully about how to secure it.**
16
17Stevens has been designed with the utmost simplicity and extensibility, much like a well-organized household. At the heart of his operation lies a single "memories" table - a digital equivalent of a butler's meticulous records. This table serves as the foundation for all of Stevens' operations.
45- `dashboard`: the admin view for showing the memories notebook + visualizing imports
46- `dailyBriefing`: stuff related to sending a daily update via telegram
47- `dbUtils`: little one-off scripts for database stuff
48
49## Hiring your own Stevens
57- For the Google Calendar integration you'll need `GOOGLE_CALENDAR_ACCOUNT_ID` and `GOOGLE_CALENDAR_CALENDAR_ID`. See [these instuctions](https://www.val.town/v/stevekrouse/pipedream) for details.
58
59**important caveat: the admin dashboard doesn't have auth! currently it just relies on security by obscurity of people not knowing the url to a private val. this is not very secure, if you put sensitive data in a database you should think carefully about how to secure it.**
60
61Overall it's a simple enough project that I encourage you to just copy the ideas and run in your own direction rather than try to use it as-is.
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.
374];
375
376// Insert memories into the database
377async function insertDemoMemories() {
378 try {
415 await insertDemoMemories();
416
417 console.log("Demo database successfully populated!");
418 return "Demo database successfully populated!";
419 } catch (error) {
420 console.error("Error populating demo database:", error);
421 throw error;
422 }