Enterprise-ready database package built on Prisma with hardened defaults, resiliency, and observability for PostgreSQL and MongoDB.
- 🗄️ Multi-Database Support: PostgreSQL and MongoDB with parity APIs
- 🔌 Connection Pooling: PgBouncer-style configuration with automatic pool management
- 🔄 Migration Management: Automated schema migrations with rollback automation and drift checks
- 🌱 Data Seeding: Configurable, hashed, and idempotent seed routines with guardrails
- 🔒 Type Safety: Full TypeScript support with generated types
- 📊 Observability: Structured logging, query metrics, readiness, and health probes
- 🧠 Resiliency: Retry/backoff for connection bootstrap and graceful shutdown helpers
- 🛡️ Security: Prepared statements, optional unsafe escape hatch, and destructive-operation confirmations
npm install @kitiumai/database
# or
yarn add @kitiumai/database# Copy environment file
cp .env.example .env
# Install deps and generate Prisma client
npm install
npm run db:generate
# Run migrations and seed
npm run db:migrate:deploy
npm run db:seedUse the client in your application:
import { initializeDatabase, getDatabase, readinessCheck } from '@kitiumai/database';
await initializeDatabase({
retry: { maxRetries: 5, retryDelay: 200 },
observability: { enableMetrics: true },
});
const ready = await readinessCheck();
if (ready.status !== 'ready') throw new Error('Database not ready');
const db = getDatabase();
const users = await db.user.findMany();loadDatabaseConfig normalizes environment variables and merges overrides. Key variables:
DATABASE_URL– PostgreSQL connection stringDATABASE_POOL_MIN/DATABASE_POOL_MAX/DATABASE_POOL_IDLE_TIMEOUT/DATABASE_POOL_CONNECTION_TIMEOUT– poolingDATABASE_MAX_RETRIES/DATABASE_RETRY_DELAY– retry/backoff for startupDATABASE_ENABLE_LOGGING/DATABASE_LOG_LEVEL– structured log controlsDATABASE_ENABLE_METRICS/DATABASE_ENABLE_TRACING– observability togglesDATABASE_SHUTDOWN_TIMEOUT– graceful shutdown wait (ms)DEFAULT_ADMIN_PASSWORD– seed default password (hashed automatically)ALLOW_CLEAR_DATABASE– set totrueto enable destructiveclearDatabaseMONGODB_URL/MONGODB_DB/MONGODB_POOL_SIZE– MongoDB connectivity
Call validateDatabaseConfig to enforce required values in CI/CD.
loadDatabaseConfig(overrides?: Partial<DatabaseConfig>): DatabaseConfiginitializeDatabase(config?: Partial<DatabaseConfig>): Promise<PrismaClient>– PgBouncer-style pooling, retry/backoff, structured logging, graceful shutdown hooks.initializeMongoDatabase(config?: Partial<DatabaseConfig>): Promise<MongoClient>– Pooled MongoDB connection with readiness checks.getDatabase(): PrismaClienthealthCheck(): Promise<boolean>– Liveness probe.readinessCheck(): Promise<HealthReport>– Readiness probe with diagnostics.databaseMetrics(): Record<string, unknown>– In-memory query metrics snapshot.disconnectDatabase(options?: { wait?: boolean; timeoutMs?: number }): Promise<void>– Graceful shutdown.
executeQuery<T>(query: Prisma.Sql, operation?: string): Promise<T[]>– Parameterized raw SQL with metrics/tracing hooks.executeUnsafeQuery<T>(query: string, params?: unknown[]): Promise<T[]>– Escape hatch for legacy raw strings.
Example safe query:
import { executeQuery } from '@kitiumai/database';
import { Prisma } from '@prisma/client';
const admins = await executeQuery<{ id: string }>(Prisma.sql`
SELECT id FROM "User" WHERE role = ${'ADMIN'}
`);createConnectionPool(databaseUrl: string, config: PoolingConfig): stringgetPoolingConfigFromEnv(): PoolingConfiggeneratePgBouncerConfig(databaseUrl: string, config: PoolingConfig, pgBouncerPort?: number): string
migrationRunner(): Promise<MigrationResult[]>– Runsprisma migrate deployand returns applied migrations.rollbackToMigration(migrationId: string): Promise<boolean>– Wrapsprisma migrate resolve --rolled-back.getMigrationHistory()/isMigrationsUpToDate()/validateSchema()– Drift and metadata helpers.
seedDatabase(config?: Partial<DatabaseConfig>): Promise<SeedResult>– Hashed, idempotent seeds (configurable hasher and defaults).clearDatabase(config?: Partial<DatabaseConfig>): Promise<SeedResult>– Protected destructive helper (requiresALLOW_CLEAR_DATABASE=true).
getMongoDatabase()/disconnectMongoDatabase()/mongoHealthCheck()– MongoDB lifecycle and probes with pooled client.
- Structured JSON logs via
logStructuredwith configurable logger name and level. - Query metrics (
totalQueries,failures,averageDurationMs,p95DurationMs) viadatabaseMetrics. - Retry/backoff for client bootstrap and raw queries with circuit-breaker friendly hooks.
- Graceful shutdown waits for disconnection and honors configurable timeouts.
npm run migration:create # Create a new migration
npm run db:migrate:deploy # Apply migrations
npm run db:migrate:dev # Create/apply migrations in development
npm run db:reset # Drop, recreate, migrate, seed
npm run db:seed # Seed with initial data
npm run db:studio # Open Prisma Studio
npm run db:generate # Generate Prisma client
npm run db:validate # Validate Prisma schema- Prepared statements by default; unsafe helper provided for legacy flows only.
- Seed passwords are hashed with bcrypt; override via
DEFAULT_ADMIN_PASSWORDorpasswordHasherconfig. - Destructive operations require explicit opt-in (
ALLOW_CLEAR_DATABASE=true).
See CHANGELOG.md for unreleased and historical updates.