# Reverse-Engineering SQLite Databases into Drizzle/Prisma Schemas, SVG ERDs, and DuckDB Analytics

Inheriting an undocumented `.sqlite` or `.db` file is a familiar headache. Maybe it came from a prototype, a mobile app backup, or a legacy microservice. Nobody left a schema diagram, half the foreign keys were never declared as constraints, and now you need to understand the data model, pull a clean Excel sheet for a stakeholder, and wire the tables into TypeScript.

Normally that means juggling four different tools: DB Browser for SQLite to peek at tables, a separate ERD tool to map relationships, a Python script to convert tables into `.xlsx`, and hand-writing Drizzle or Prisma models from `sqlite_master` output.

To collapse that workflow into one local browser tab, I built the [SQLite & DuckDB Viewer, Editor, ERD & Forensic Studio](https://kivarotools.com/developer/sqlite-viewer) on KivaroTools. Everything executes locally in browser memory via a zero-CDN TypeScript B-tree parser—no file uploads, no signup, and no desktop installs. Here is how four practical SQLite workflows work under the hood.

## 1\. Profiling Unknown Tables in One Line with DuckDB `SUMMARIZE`

When you open an unfamiliar database with 15+ tables, writing manual `SELECT COUNT(*), MIN(...), MAX(...), COUNT(DISTINCT ...)` queries for every column is tedious.

DuckDB's analytical extensions solve this cleanly. Running `SUMMARIZE` on a SQLite table gives you column types, min/max bounds, null counts, and approximate cardinality in a single command—and `EXCLUDE` lets you drop noisy JSON blobs without listing 25 column names by hand:

```sql

-- Profile every column in the orders table in one shot

SUMMARIZE orders;



-- Inspect paid orders while dropping bulky JSON payloads

SELECT * EXCLUDE (metadata_json)

FROM orders

WHERE status = 'PAID'

ORDER BY amount_usd DESC;

```

In Tab 1 of the studio, you can run both standard SQLite queries (`JOIN`, `PRAGMA`, `EXPLAIN QUERY PLAN`) and DuckDB analytical queries (`SUMMARIZE`, `DESCRIBE`, `SELECT * EXCLUDE`, `GROUP BY ALL`) directly against loaded `.sqlite`, `.db`, `.sqlite3`, or `.duckdb` files—plus edit cells inline and save back a valid binary `.sqlite` file.

## 2\. Reverse-Engineering Drizzle, Prisma, and SVG ERDs (Including Implicit `*_id` Links)

Half of the SQLite databases in the wild never declared explicit `FOREIGN KEY` constraints—they just have columns like `organization_id` or `user_id` sitting in child tables. A naive ERD generator that only reads `PRAGMA foreign_key_list` draws a pile of disconnected boxes.

Mapping both declared foreign keys and implicit `*_id` naming conventions gives you an accurate visual ER diagram (exportable as Vector `.svg`, Mermaid, or DBML) and lets you compile straight into TypeScript ORM definitions like Drizzle (`schema.ts`), Prisma (`schema.prisma`), or PostgreSQL migrations:

```ts

import { sqliteTable, integer, text } from 'drizzle-orm/sqlite-core';



export const users = sqliteTable('users', {

  id: integer('id').primaryKey({ autoIncrement: true }),

  organization_id: integer('organization_id')

    .notNull()

    .references(() => organizations.id),

  full_name: text('full_name').notNull(),

  plan_tier: text('plan_tier').notNull(),

});

```

Before migrating that schema to production, running a 12-point integrity audit (`PRAGMA foreign_key_check` + unindexed FK detection) catches orphaned child rows and missing foreign-key indexes that silently slow down `JOIN`s.

## 3\. Diffing Two SQLite Snapshots Locally (`sqldiff`)

When debugging why staging behaves differently from a local seed database, eyeballing two `CREATE TABLE` dumps wastes time.

Running a local side-by-side database diff compares two `.sqlite` or `.sql` files directly in browser memory—highlighting added, removed, and modified tables and generating a copy-ready `ALTER TABLE` SQL migration patch without sending either snapshot to an external server.

## 4\. Exporting CWE-1236 Safe Excel (`.xlsx`) Spreadsheets

A subtle security bug when exporting user-generated database rows directly to `.xlsx` or `.csv` for non-technical teammates is **spreadsheet formula injection (CWE-1236)**. If a text field starts with `=`, `+`, `-`, or `@`, Excel can evaluate it as a formula when someone opens the file.

When exporting tables to Excel (`.xlsx`) or RFC 4180 CSV from Tab 5, neutralizing leading formula triggers protects whoever opens the spreadsheet—alongside 9 other local export targets (binary `.sqlite`, Drizzle, Prisma, PostgreSQL, DuckDB, SQLite Dump, Mermaid, DBML, and a SHA-256 audit certificate).

## Practical Limits & Architecture

- **Client-side scope:** Database files are parsed and modified strictly inside browser RAM and are never uploaded to KivaroTools servers. Performance stays smooth up to roughly **100–250 MB**.
- **When to use desktop tools:** For multi-GB databases, heavy batch ETL, or SQLCipher / WhatsApp `.crypt` encrypted files, stick with desktop DB Browser for SQLite. For columnar `.parquet`, `.avro`, or `.feather` files, use the dedicated [Parquet, Avro & Feather Viewer](https://kivarotools.com/developer/parquet-viewer).
- **Stack:** Built with Next.js (App Router), React, TypeScript, Tailwind CSS, and Cloudflare Workers, using a self-contained TypeScript B-tree parser so corporate CSP rules never block external `.wasm` CDN loads.

You can test the full workflow right away using the built-in `SaaS Prod` (`cloudscale_saas_prod.sqlite`), `Forensic DB`, or `Chrome / iOS` sample databases inside the [KivaroTools SQLite & DuckDB Studio](https://kivarotools.com/developer/sqlite-viewer).
