Bỏ qua, tới nội dung chính

Kiến trúc · ADR 0004

Prisma on PostgreSQL 17 with pgvector

Trạng thái
accepted
Ngày
2026-09-21
Người quyết định
Alex

Context

The platform stores relational data (users, courses, quizzes, attempts) and, from phase 3, text embeddings for retrieval over uploaded lectures. Weak-topic analysis is SQL over attempt rows, so the relational store matters as much as the vector one. The team has four people and one semester; running a second database only for vectors is operational weight we cannot afford.

Decision

  • One PostgreSQL 17 database with the pgvector extension, from the pgvector/pgvector:pg17 image. Embeddings live next to the rows they describe and are queried with plain SQL joins.
  • Prisma 6 as the ORM, pinned to 6.19.3 (see ADR-0002, point 7). schema.prisma is the source of truth for tables; prisma migrate owns every schema change.
  • Extensions are not the ORM's job. infra/db/init/001-extensions.sql creates vector and pg_trgm once, when the Postgres volume is first initialised.
  • The client is generated into packages/database/generated/prisma, not node_modules, so pnpm deploy --prod ships it inside the container image. binaryTargets includes linux-musl-openssl-3.0.x for the alpine image.
  • Conventions: UUID primary keys, createdAt/updatedAt on every table, soft delete (deletedAt) for anything a quiz attempt may reference.

Alternatives considered

  • A dedicated vector store (Pinecone, Qdrant). A second system to run, back up and keep consistent, for a corpus of a few thousand chunks. Rejected.
  • TypeORM (used in the team's reference project). Weaker migration story and looser types. Prisma's generated client matches the strict TypeScript settings used here.
  • Drizzle. Closer to SQL, but the team has no experience with it, and Prisma's tooling (Studio, migrate) shortens onboarding.

Consequences

  • Prisma has no vector type. When embeddings land, the column is Unsupported("vector(…)"), similarity queries go through $queryRaw, and HNSW indexes are added in hand-edited migrations.
  • Pinning Prisma 6 means reading the v6 docs, not latest.
  • A fresh volume gets the extensions automatically. An existing volume created before an extension was added to the init script needs a migration that runs CREATE EXTENSION IF NOT EXISTS.