Skip to content

feat: support SQLite as an alternative to PostgreSQL - #21

Closed
harsHdokyc wants to merge 1 commit into
AienTech:mainfrom
harsHdokyc:feat/sqlite-support
Closed

harsHdokyc wants to merge 1 commit into
AienTech:mainfrom
harsHdokyc:feat/sqlite-support

Conversation

@harsHdokyc

Copy link
Copy Markdown

Closes #17

Summary

Set DATABASE_URL=file:/data/dossier.db to run Dossier on a single SQLite file in one container. PostgreSQL stays the default and is unchanged.

Changes

  • Schema: prisma/sqlite/schema.prisma is generated from prisma/schema.prisma (yarn sqlite-schema; a test fails if it's stale), with its own migrations in prisma/sqlite/migrations. prisma7.config.ts picks the schema and migrations by DATABASE_URL.
  • Client: src/lib/database.ts → createPrismaClient() picks the Postgres or SQLite client and adapter (@prisma/adapter-better-sqlite3). The app stays typed with the Postgres client.
  • Single connection: better-sqlite3 has one connection, so a query from another request ran inside whatever transaction was open and was lost if it rolled back. Such queries now wait for the transaction to end; queries made from inside a transaction's own callback still run at once. A regression test covers this; it fails without the fix.
  • Postgres-only features:
    • FOR UPDATE → ${forUpdate()}, which is empty on SQLite.
    • skipDuplicates → upserts with update: {}, or removing duplicates first.
    • SQLite sorts enums alphabetically, so the PDF zip sorts its documents in code.
    • citext → COLLATE NOCASE.
    • The built-in-theme trigger is ported to SQLite.
    • Settings → About shows the SQLite file size.
  • Config: DATABASE_URL accepts file: with an absolute path.
  • Docker/CI: yarn generate builds both clients. The seed bundle leaves better-sqlite3 external. CI runs the database tests on SQLite too.
  • Docs: README (single-container setup), DEVELOPMENT.md (SQLite workflow and caveats), .env.example.

Testing

  • yarn lint, yarn typecheck: pass
  • yarn test:coverage on PostgreSQL: 1686 passed, 26 skipped (unchanged), thresholds met
  • yarn vitest run --project db on SQLite: 436 passed, 1 skipped (a race that SQLite's one-at-a-time transactions rule out)
  • yarn build + standalone server on SQLite: migrate, seed (incl. --demo), sign-in, failed-login throttle, main pages, Settings → About
  • Not tested: Docker image build (left to CI); e2e still runs on PostgreSQL only

Known limitations

  • SQLite allows one writer at a time, which is fine for a single user.
  • No automatic migration between databases: export, then import.
  • A later schema change needs a migration for both databases (see DEVELOPMENT.md).

AI assistance (Claude Code) was used; I reviewed every change and ran the tests above.

DATABASE_URL picks the database: postgresql://… as before, or
file:/absolute/path.db for SQLite, so Dossier can run as a single
container (AienTech#17). PostgreSQL stays the default and is unchanged.

- prisma/sqlite/schema.prisma is generated from prisma/schema.prisma
  (yarn sqlite-schema; a test fails when it's stale), with its own
  migrations; prisma7.config.ts picks the pair by DATABASE_URL.
- createPrismaClient() in src/lib/database.ts picks the client and
  adapter. On SQLite, queries from outside an open transaction wait for
  it to end, so a rollback can't undo them.
- FOR UPDATE only on PostgreSQL; skipDuplicates replaced by upserts or
  deduplication; enum order sorted in code where it matters.
- Citext's case-insensitive names become COLLATE NOCASE, and the
  built-in theme trigger is ported to SQLite.
- CI runs the database tests on SQLite too; README, DEVELOPMENT.md and
  .env.example document it.

Closes AienTech#17

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011GfGRofhBkPHKP9dUZYLr9
@AienTech

Copy link
Copy Markdown
Owner

Hi, thanks for the PR. This one however is already being implemented and quite differs from the current implementation made by Claude.

Thank you anyway for taking the time

@AienTech AienTech closed this Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request] Add SQLite Support as an Alternative to PostgreSQL

2 participants