Why Use an ORM?
ORMs provide several benefits for application development:- Type safety - Map database tables to programming language types
- Productivity - Write less boilerplate code for common operations
- Maintainability - Centralize database logic and schema definitions
- Database abstraction - Switch between databases with minimal code changes
- Migration management - Track and version schema changes
Supported ORMs by Language
JavaScript/TypeScript
- Prisma
- Sequelize
- TypeORM
- Knex.js
Support level: FullPrisma is a modern, type-safe ORM with first-class CockroachDB support.Features:
- Full TypeScript support with generated types
- Intuitive data modeling
- Automatic migrations
- Built-in connection pooling
Python
- SQLAlchemy
- Django
Support level: FullSQLAlchemy is Python’s most popular ORM. Use the Avoid transaction state mutationsDo NOT call these methods inside
sqlalchemy-cockroachdb dialect for best compatibility.Best Practices
Use therun_transaction() helper functionThe run_transaction() function handles transaction retries automatically:run_transaction():session.commit()- Handled automaticallysession.rollback()- Handled automaticallysession.flush()- Not supported due to lack of nested transactions
Go
- GORM
- upper/db
Support level: FullGORM is Go’s feature-rich ORM library.
Java
- Hibernate
- jOOQ
- Spring Data JPA
Support level: FullHibernate 5.4.19+ includes native CockroachDB dialect support.
Specify the CockroachDB dialect in your Hibernate configuration for optimal compatibility.
Ruby
- Active Record
Support level: FullActive Record is Ruby on Rails’ ORM framework.
The
activerecord-cockroachdb-adapter version must match your Active Record major version.Common ORM Patterns
Handling Transactions
Most ORMs provide transaction support. Always use transactions for operations that modify multiple rows:- TypeScript (Prisma)
- Python (SQLAlchemy)
- Go (GORM)
Managing Schema Migrations
Use your ORM’s migration tools to version control your schema:Connection Pooling
Configure connection pools appropriately for your workload:- Prisma
- SQLAlchemy
ORM Performance Tips
Use Eager Loading
Avoid N+1 queries by loading related data upfront
Batch Operations
Use bulk inserts/updates instead of individual operations
Select Specific Fields
Only query the columns you need
Use Indexes
Add indexes for frequently queried fields
Next Steps
Best Practices
Learn development best practices for CockroachDB
Client Drivers
View all supported client drivers
Transaction Handling
Deep dive into transaction retry logic
Performance Optimization
Optimize your ORM queries for production