Integrating with Databases

1. Connecting to SQL Databases

DriverDB
pgPostgreSQL
mysql2MySQL/MariaDB
better-sqlite3SQLite (sync)
tediousSQL Server

2. Connecting to NoSQL Databases

DBDriver
MongoDBmongodb / mongoose
DynamoDB@aws-sdk/client-dynamodb
Redisioredis / @upstash/redis
Cassandracassandra-driver

3. Using ORM Integration

ORMDetail
PrismaSchema-first, generates type-safe client
DrizzleSQL-like, lightweight, edge-friendly
TypeORMDecorator-based
MikroORMUnit-of-work pattern

4. Mapping Types to Tables

Example: Prisma → GraphQL

// prisma model User { id Int @id; email String; name String? }
type User {
  id: ID!
  email: EmailAddress!
  name: String  # nullable matches Prisma optional
}

5. Generating Schema from Database

ToolDetail
HasuraAuto GraphQL from Postgres/MS-SQL/etc.
PostGraphilePostgres → GraphQL with introspection
Prisma + PothosCode-first with prisma-plugin
Nexus prismaGenerates types from Prisma schema

6. Implementing Query Builders

LibraryDetail
KyselyType-safe SQL builder
KnexMature, multi-dialect
DrizzleSchema-aware builder

7. Using Connection Pooling

SettingDetail
maxMatch DB max_connections / pods
idleTimeout30s to release stale
ServerlessUse PgBouncer / Prisma Accelerate / Neon proxy

8. Handling Transactions

Example: Prisma transaction

const result = await prisma.$transaction(async (tx) => {
  const order = await tx.order.create({ data: { userId, total } });
  await tx.inventory.update({ where: { sku }, data: { qty: { decrement: 1 } } });
  return order;
}, { isolationLevel: "Serializable" });

9. Implementing Database Migrations

ToolDetail
Prisma MigrateSQL migrations from schema diff
Drizzle Kitgenerate + migrate commands
node-pg-migratePlain SQL/JS migrations
Sqitch / FlywayStandalone, language-agnostic

10. Optimizing Database Access

PracticeDetail
Project only requested columnsInspect info in resolver
Indexes on join + sort columnsAvoid sequential scans
Batch via DataLoaderSolve N+1
Cursor paginationAvoid OFFSET
Materialized viewsFor heavy aggregates