How to understand a codebase's data access layer
Find the data model, how queries are written, where transactions happen and which code writes to which tables, in apps using Prisma, Drizzle, TypeORM, Mongoose or raw SQL.
If you understand an application's data (what's stored, how it relates and who writes it), you understand most of what the application does. The data access layer is where that's visible.
Start with the schema
Find the single best description of the data model:
- Prisma:
prisma/schema.prisma - Drizzle: files with
pgTableorsqliteTable, located throughdrizzle.config.ts - TypeORM: classes decorated with
@Entity - Mongoose:
new Schema(...)in a models folder - Raw SQL: migration files, or the database itself
Read the main entities and their relationships. Sketch them: five boxes and lines between them is enough to start.
Find how queries are written
Is access centralised (repositories, services, a db/ folder) or are queries written wherever they're needed? Centralised access is easier to reason about; scattered queries mean you must search the codebase for every use of a table.
Search patterns help:
prisma.order.for Prisma.from(orders)ordb.query.ordersfor DrizzlegetRepository(Order)or injected repositories for TypeORMOrder.findorOrder.updateOnefor Mongoose
Map writers for each important table
For each important entity, list every place that creates, updates or deletes it. Bugs in data are almost always caused by an unexpected writer.
| Table | Writers |
|---|---|
| orders | services/checkout.ts:88 (create), jobs/expire.ts:20 (cancel), admin/orders.ts:45 (refund) |
Look for transactions
Operations that must succeed or fail together (creating an order and its line items, moving money) should be in a transaction. Find transaction calls ($transaction, db.transaction, BEGIN) and check multi-step writes without one.
Check for hidden behaviour in the data layer
- ORM hooks (
beforeCreate,pre('save')) that change data or trigger side effects - Database triggers and functions defined in migrations
- Soft deletes, where "deleted" rows are filtered out by default
- Default values and constraints that live only in the database
Watch for performance traps
- N+1 queries: a query inside a loop
- Loading whole related collections when only a count is needed
- Missing indexes on fields used for filtering
Ask questions with citations
Ask RepoGrasp "which code paths write to the orders table, and which use transactions?" It reads source and schema files (including schema.prisma) and cites each writer. Combine with a search for the table name in SQL migrations, since triggers and constraints live there.