Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Turso 5 min read How-to mediumAdept

Storing Vercel's AI SDK chat messages in a Turso database

Vercel AI SDK's useChat hook doesn't persist conversations, so chat history can be stored in a Turso (libSQL) table and reloaded on page render. A server component queries the messages table and passes rows as initialMessages to useChat. The API route inserts the user's last message before calling streamText, and an onFinish callback inserts the assistant's reply. Suggested extensions include a chats/sessions table with foreign keys, background persistence via server actions, or a database per user or chat.

Related posts

  • Building an AI-powered ChatBot using Vercel, OpenAI, and Postgres

    Neon mediumAdept

    A ChatGPT-style documentation assistant (Ask Neon) is built by storing OpenAI text-embedding-ada-002 vectors in Postgres with pgvector, retrieving context by cosine distance, and feeding it to text-davinci-003 from a Vercel Edge Function. A window-function query caps retrieved context at a cumulative 1,500 tokens to fit the 4,096-token model limit, and an ivfflat index (lists ≈ 4·sqrt(rows)) speeds up the similarity search. Neon's WebSocket-based serverless driver works around Edge Functions' lack of TCP, and streaming completions via SSE parsing return answers faster. User feedback is stored for later fine-tuning.

  • Making a Real-Time Chat App with Supabase on Replit

    Replit mediumAdept

    A real-time public chat app can be built with a Vue 3 and Tailwind CSS frontend on Replit backed by Supabase's hosted Postgres. The walkthrough creates a messages table via SQL, enables replication-based real-time events, and wraps the supabase-js client in a Vue composable. That composable handles paginated message loading by range, INSERT subscriptions for live updates, and inserts for sending. Users are identified only by a random username stored in localStorage, and older messages load when the list is scrolled to the top.

  • Save Resend email events to your Turso Database

    Turso mediumAdept

    Resend email webhook events (sent, delivered, bounced, opened, clicked, etc.) can be persisted to a Turso database through a small Hono/Node.js handler using @libsql/client. The walkthrough creates `events` and `clicks` tables. It parses the webhook payload, generates UUIDs, flattens recipient arrays to CSV, and inserts click details separately for `email.clicked` events. Local testing uses a Hookdeck CLI tunnel. Production advice covers verifying signatures with svix, queueing events for background processing, adding indexes, and deploying to a cloud host.

  • Toasty now fully supports Turso

    Turso mediumAdept

    Toasty, the async Rust ORM from Tokio's creator, now supports Turso in three modes behind one API: local files with optional concurrent writes (transactions automatically wrapped in BEGIN CONCURRENT), a serverless remote mode over HTTP with URL and auth token, and local-first sync with explicit pull/push calls. Only the driver constructor changes between modes, so the same models and queries run unchanged. For remote use, toasty::batch() combines independent statements into one round trip and returns typed tuple results, avoiding a network round trip per transaction statement.

  • Bringing SQLite to Vercel Functions with Turso

    Turso mediumAdept

    Turso's partial sync replicates a subset of SQLite database pages into Vercel Functions so reads run in-process with no network round-trip, while writes go to the remote Turso database for durability and consistency. On cold start the driver fetches the first 128 KiB of the SQLite file, then pulls further 128 KiB segments on demand into the ephemeral /tmp directory, so cold-start cost scales with the working set rather than total database size. The experimental @tursodatabase/vercel-experimental package also auto-provisions databases by name for database-per-tenant or per-agent-session patterns. It offers an optional local-write batching mode that trades consistency for lower write latency, and uses a waitUntil() safety net that pushes pending changes if close() is forgotten.

The quest for software engineering knowledge.

Look:
Layout: