common_service.py


Overview

common_service.py defines the CommonService class, a foundational service layer component that encapsulates common database operations using the Peewee ORM. It provides a standardized interface for Create, Read, Update, and Delete (CRUD) operations, batch processing, filtering, and querying across all database models in the InfiniFlow application.

By centralizing these operations, the class promotes code reuse, consistency, and maintainability for all service classes that interact with the database models. Subclasses should specify their target Peewee model by setting the model attribute.


Class: CommonService

Description

A base service class offering generic database operation methods for Peewee models. It supports single and batch CRUD operations, flexible queries with filtering and ordering, and utility methods to handle large input lists by chunking.

Attributes

Attribute

Type

Description

model

Peewee Model Class (subclass)

The target Peewee model class this service operates on. Must be overridden in subclasses.


Methods

query(cls, cols=None, reverse=None, order_by=None, **kwargs)

Executes a flexible query on the model with optional column selection, sorting, and filter criteria.


get_all(cls, cols=None, reverse=None, order_by=None)

Fetches all records from the model's table with optional column selection and ordering. Defaults to ordering by create_time if reverse is specified but order_by is not.


get(cls, **kwargs)

Retrieve a single record matching the filter criteria.


get_or_none(cls, **kwargs)

Retrieve a single record matching filters or return None if not found.


save(cls, **kwargs)

Create and save a new record to the database, forcing an insert operation.


insert(cls, **kwargs)

Insert a new record with automatic ID generation and timestamp fields.


insert_many(cls, data_list, batch_size=100)

Batch insert multiple records efficiently.


update_many_by_id(cls, data_list)

Update multiple records identified by their IDs.


update_by_id(cls, pid, data)

Update a single record by its ID.


get_by_id(cls, pid)

Retrieve a single record by ID.


get_by_ids(cls, pids, cols=None)

Retrieve multiple records by a list of IDs.


delete_by_id(cls, pid)

Delete a record by its ID.


delete_by_ids(cls, pids)

Delete multiple records by their IDs.


filter_delete(cls, filters)

Delete records matching given filter conditions.


filter_update(cls, filters, update_data)

Update records matching given filters.


cut_list(tar_list, n)

Static utility to split a list into chunks of size n.


filter_scope_list(cls, in_key, in_filters_list, filters=None, cols=None)

Retrieve records where a field matches any value in a large list with optional extra filters and column selection.


Important Implementation Details


Interaction with Other Parts of the System


Visual Diagram

classDiagram
    class CommonService {
        - model
        + query(cols=None, reverse=None, order_by=None, **kwargs)
        + get_all(cols=None, reverse=None, order_by=None)
        + get(**kwargs)
        + get_or_none(**kwargs)
        + save(**kwargs)
        + insert(**kwargs)
        + insert_many(data_list, batch_size=100)
        + update_many_by_id(data_list)
        + update_by_id(pid, data)
        + get_by_id(pid)
        + get_by_ids(pids, cols=None)
        + delete_by_id(pid)
        + delete_by_ids(pids)
        + filter_delete(filters)
        + filter_update(filters, update_data)
        + cut_list(tar_list, n)
        + filter_scope_list(in_key, in_filters_list, filters=None, cols=None)
    }

Summary

The CommonService class in common_service.py serves as a reusable, consistent service layer for database operations in the InfiniFlow application. With comprehensive support for CRUD, batch processing, filtering, and ordering, it abstracts away repetitive database code and enforces best practices such as transactional safety and automatic metadata management. This class is designed to be subclassed with specific Peewee models to provide tailored data access layers across the system.