Top Related Projects
The fantastic ORM library for Golang, aims to be developer friendly
general purpose extensions to golang's database/sql
Generate a Go ORM tailored to your database schema.
Simple and Powerful ORM for Go, support mysql,postgres,tidb,sqlite3,mssql,oracle, Moved to https://gitea.com/xorm/xorm
Golang ORM with focus on PostgreSQL features and performance
Quick Overview
encode/orm is a lightweight, asynchronous ORM (Object-Relational Mapping) library for Python. It provides a simple and intuitive interface for working with databases, supporting multiple database backends including SQLite, PostgreSQL, and MySQL. The library is designed to work seamlessly with async/await syntax and popular async web frameworks like FastAPI and Starlette.
Pros
- Asynchronous by design, providing efficient database operations for high-performance applications
- Supports multiple database backends, offering flexibility in choosing the right database for your project
- Integrates well with popular async web frameworks, making it easy to incorporate into existing projects
- Lightweight and easy to use, with a clean and intuitive API
Cons
- Relatively new project, which may mean less community support and fewer resources compared to more established ORMs
- Limited documentation and examples available, potentially making it challenging for beginners to get started
- May lack some advanced features found in more mature ORM libraries
- Smaller ecosystem of extensions and plugins compared to larger ORM projects
Code Examples
- Defining a model:
from orm import Model, Integer, String
class User(Model):
id = Integer(primary_key=True)
name = String(max_length=100)
email = String(max_length=100, unique=True)
- Creating a database connection and table:
import asyncio
from orm import Database
async def main():
database = Database("sqlite:///example.db")
await database.connect()
await User.create_table()
asyncio.run(main())
- Inserting and querying data:
async def example():
# Insert a new user
user = await User.objects.create(name="John Doe", email="john@example.com")
# Query users
all_users = await User.objects.all()
john = await User.objects.get(name="John Doe")
Getting Started
To get started with encode/orm, follow these steps:
-
Install the library:
pip install orm
-
Create a database connection and define your models:
from orm import Model, Integer, String, Database database = Database("sqlite:///example.db") class User(Model): id = Integer(primary_key=True) name = String(max_length=100) email = String(max_length=100, unique=True) async def main(): await database.connect() await User.create_table() # Your database operations here import asyncio asyncio.run(main())
-
Use the ORM to perform database operations in your async application.
Competitor Comparisons
The fantastic ORM library for Golang, aims to be developer friendly
Pros of gorm
- More mature and widely adopted in the Go community
- Supports a wider range of databases and features
- Extensive documentation and community support
Cons of gorm
- Heavier and more complex API
- Potential performance overhead due to its feature-rich nature
- Steeper learning curve for beginners
Code Comparison
orm:
type User struct {
ID int `orm:"primary_key"`
Name string `orm:"size(100)"`
}
db.Create(&User{Name: "John"})
gorm:
type User struct {
ID uint `gorm:"primaryKey"`
Name string `gorm:"size:100"`
}
db.Create(&User{Name: "John"})
Key Differences
- Syntax: Both ORMs use struct tags, but with slightly different conventions
- Feature set: gorm offers more advanced features like hooks and associations
- Performance: orm may have a slight edge in raw performance due to its lighter design
- Database support: gorm supports a wider range of databases out of the box
- Community: gorm has a larger and more active community, resulting in more resources and third-party extensions
Conclusion
Choose gorm for complex projects requiring extensive features and wide database support. Opt for orm if you prefer a simpler, lightweight ORM with potentially better performance for basic operations.
general purpose extensions to golang's database/sql
Pros of sqlx
- Lightweight and close to raw SQL, offering more control and flexibility
- Supports both database-specific features and database-agnostic operations
- Provides compile-time query checking for improved safety
Cons of sqlx
- Requires more manual work for complex queries and relationships
- Less abstraction, which may lead to more boilerplate code
- Limited built-in support for schema migrations
Code Comparison
sqlx:
rows, err := db.Query("SELECT id, name FROM users WHERE age > ?", 18)
var users []User
for rows.Next() {
var u User
err := rows.Scan(&u.ID, &u.Name)
users = append(users, u)
}
orm:
users = await User.objects.filter(age__gt=18).all()
Summary
sqlx is a low-level database toolkit that provides a thin layer over SQL, offering more control and flexibility. It's suitable for developers who prefer working close to the database and want to leverage database-specific features.
orm is a high-level ORM that provides a more abstract and Pythonic way of working with databases. It offers easier querying and relationship management but may sacrifice some performance and fine-grained control compared to sqlx.
The choice between the two depends on the project requirements, developer preferences, and the need for database abstraction versus direct SQL control.
Generate a Go ORM tailored to your database schema.
Pros of sqlboiler
- Generates type-safe, performant Go code from existing database schemas
- Supports complex queries and relationships out of the box
- Provides excellent database-specific optimizations
Cons of sqlboiler
- Requires an existing database schema to generate code
- Less flexibility for defining models in code first
- Steeper learning curve for those new to code generation tools
Code Comparison
sqlboiler:
users, err := models.Users().All(ctx, db)
if err != nil {
return err
}
orm:
var users []User
err := db.NewSelect().Model(&users).Scan(ctx)
if err != nil {
return err
}
Key Differences
- sqlboiler generates code based on database schema, while orm allows for in-code model definition
- sqlboiler provides more database-specific optimizations, while orm offers a more generic approach
- sqlboiler's generated code is typically more verbose but offers stronger type safety
- orm has a simpler API and is easier to get started with for beginners
Use Cases
sqlboiler is ideal for projects with existing complex database schemas and a need for high performance. orm is better suited for projects that prioritize simplicity and flexibility in model definitions.
Simple and Powerful ORM for Go, support mysql,postgres,tidb,sqlite3,mssql,oracle, Moved to https://gitea.com/xorm/xorm
Pros of xorm
- More mature and feature-rich, with a longer development history
- Supports a wider range of databases, including MySQL, PostgreSQL, SQLite, and more
- Offers advanced features like caching and event hooks
Cons of xorm
- Larger codebase and potentially steeper learning curve
- Less focus on modern Go idioms and practices
- May have performance overhead due to its extensive feature set
Code Comparison
xorm:
engine, _ := xorm.NewEngine("mysql", "root:password@/dbname?charset=utf8")
user := new(User)
has, _ := engine.ID(1).Get(user)
orm:
db := orm.NewDatabase("mysql://root:password@localhost/dbname")
user := &User{ID: 1}
err := db.Get(ctx, user)
Additional Notes
xorm provides a more traditional ORM experience with a wide range of features, while orm focuses on simplicity and modern Go practices. orm is newer and less mature but may offer better performance and a more idiomatic Go experience. The choice between the two depends on project requirements, database support needs, and developer preferences for API design and feature set.
Golang ORM with focus on PostgreSQL features and performance
Pros of pg
- More mature and feature-rich, with a longer development history
- Offers both ORM and SQL builder functionality
- Better performance, especially for complex queries and large datasets
Cons of pg
- Steeper learning curve due to more extensive API
- Less idiomatic Go code, as it uses struct tags for mapping
- Limited support for non-PostgreSQL databases
Code Comparison
orm:
type User struct {
ID int `orm:"primary_key"`
Name string
}
db.Create(&User{Name: "John"})
pg:
type User struct {
ID int
Name string
}
db.Model(&User{Name: "John"}).Insert()
Summary
orm is a newer, more Go-idiomatic ORM with a focus on simplicity and ease of use. It supports multiple databases but may lack some advanced features.
pg is a mature, PostgreSQL-specific ORM and SQL builder with excellent performance and a rich feature set. It offers more power and flexibility but may require more time to learn and master.
Choose orm for simpler projects or when database portability is important. Opt for pg when working exclusively with PostgreSQL and requiring advanced features or optimal performance.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
ORM
The orm
package is an async ORM for Python, with support for Postgres,
MySQL, and SQLite. ORM is built with:
- SQLAlchemy core for query building.
databases
for cross-database async support.typesystem
for data validation.
Because ORM is built on SQLAlchemy core, you can use Alembic to provide database migrations.
Documentation: https://www.encode.io/orm
Installation
$ pip install orm
You can install the required database drivers with:
$ pip install orm[postgresql]
$ pip install orm[mysql]
$ pip install orm[sqlite]
Driver support is provided using one of asyncpg, aiomysql, or aiosqlite.
Quickstart
Note: Use ipython
to try this from the console, since it supports await
.
import databases
import orm
database = databases.Database("sqlite:///db.sqlite")
models = orm.ModelRegistry(database=database)
class Note(orm.Model):
tablename = "notes"
registry = models
fields = {
"id": orm.Integer(primary_key=True),
"text": orm.String(max_length=100),
"completed": orm.Boolean(default=False),
}
# Create the tables
await models.create_all()
await Note.objects.create(text="Buy the groceries.", completed=False)
note = await Note.objects.get(id=1)
print(note)
# Note(id=1)
— ð —
ORM is BSD licensed code. Designed & built in Brighton, England.
Top Related Projects
The fantastic ORM library for Golang, aims to be developer friendly
general purpose extensions to golang's database/sql
Generate a Go ORM tailored to your database schema.
Simple and Powerful ORM for Go, support mysql,postgres,tidb,sqlite3,mssql,oracle, Moved to https://gitea.com/xorm/xorm
Golang ORM with focus on PostgreSQL features and performance
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot