Peewee is a lightweight and expressive Object-Relational Mapping (ORM) library for Python. It allows developers to interact with relational databases with ease. In this blog post, we will explore how to integrate Peewee into a business logic layer to create a seamless and efficient data management system.
Setting up Peewee
First, we need to install Peewee. You can install it using pip:
pip install peewee
Peewee supports a wide range of databases, including SQLite, MySQL, PostgreSQL, and many more. We will be using SQLite for this example.
Creating the Models
Peewee uses Python classes to represent database tables. Each class defines the fields and relationships for a specific table. Let’s create a simple example with two models: Customer
and Order
.
from peewee import *
DATABASE = SqliteDatabase('database.db')
class BaseModel(Model):
class Meta:
database = DATABASE
class Customer(BaseModel):
name = CharField()
email = CharField(unique=True)
class Order(BaseModel):
customer = ForeignKeyField(Customer, backref='orders')
total = DecimalField()
timestamp = DateTimeField(default=datetime.datetime.now)
In the above code, we define two models: Customer
and Order
, which inherit from BaseModel
. We also define the fields for each model, such as name
and email
for the Customer
model, and total
and timestamp
for the Order
model.
Initializing the Database
Before we can start using the models, we need to initialize the database and tables. Let’s add a function to handle the database initialization.
def create_tables():
with DATABASE:
DATABASE.create_tables([Customer, Order])
Performing Database Operations
Now that we have set up the models and the database, we can start performing CRUD operations on the data. Let’s define some methods in our business logic layer to interact with the database.
Creating a Customer
def create_customer(name, email):
customer = Customer.create(name=name, email=email)
return customer
Creating an Order
def create_order(customer, total):
order = Order.create(customer=customer, total=total)
return order
Retrieving Orders for a Customer
def get_orders_for_customer(customer):
orders = Order.select().where(Order.customer == customer)
return orders
Updating a Customer
def update_customer(customer, name=None, email=None):
if name:
customer.name = name
if email:
customer.email = email
customer.save()
return customer
Deleting a Customer
def delete_customer(customer):
customer.delete_instance()
Conclusion
By integrating Peewee into our business logic layer, we can easily handle database operations while keeping our code clean and readable. Peewee provides a powerful and intuitive way to interact with databases, making it a great choice for any Python project that requires database management.
In this blog post, we covered the basics of setting up Peewee, creating models, initializing the database, and performing CRUD operations. There is much more you can do with Peewee, such as complex queries, joins, and transactions.
If you want to explore more about Peewee, I recommend checking out the official documentation at https://docs.peewee-orm.com/. Happy coding!