The default database module for LitePolis that compatible with Polis
Project description
LitePolis Database Default
This is the default database module that compatible with Polis. It provides a unified interface for interacting with the database, aggregating operations from various manager classes for Users, Conversations, Comments, and Votes.
Quick Start
- Install the module:
litepolis-cli add-deps litepolis-database-default
- Configure database connection:
# ~/.litepolis/litepolis.config
[litepolis_database_default]
database_url: "postgresql://user:pass@localhost:5432/litepolis"
# database_url: "starrocks://<User>:<Password>@<Host>:<Port>/<Catalog>.<Database>"
- Basic usage:
from litepolis_database_default import DatabaseActor
# Create a user
user = DatabaseActor.create_user({
"email": "test@example.com",
"auth_token": "auth_token",
})
# Create a conversation
conv = DatabaseActor.create_conversation({
"title": "Test Conversation",
"description": "This is a test conversation."
})
# Create a comment
comment = DatabaseActor.create_comment({
"text_field": "This is a test comment.",
"user_id": user.id,
"conversation_id": conv.id,
})
# Create a vote
vote = DatabaseActor.create_vote({
"value": 1,
"user_id": user.id,
"comment_id": comment.id
})
API Reference
For detailed API documentation, including all available methods and their parameters, please refer to the generated documentation: API references
Detailed Usage Examples
The DatabaseActor class provides a unified interface to interact with different database entities. Below are examples for common operations:
Users
| Column | Type | Description |
|---|---|---|
| id | INTEGER | Unique identifier for the user. |
| VARCHAR | User’s email address. Must be unique. | |
| auth_token | VARCHAR | Authentication token for the user. |
| is_admin | BOOLEAN | Indicates whether the user is an administrator. |
| created | DATETIME | Timestamp indicating when the user was created. |
| modified | DATETIME | Timestamp indicating when the user was last modified. |
from litepolis_database_default import DatabaseActor
from datetime import datetime
# Create a user
user = DatabaseActor.create_user({
"email": "test@example.com",
"auth_token": "auth_token",
})
# Read a user by ID
user = DatabaseActor.read_user(user_id=1)
# Read a user by email
user = DatabaseActor.read_user_by_email(email="test@example.com")
# List users with pagination
users = DatabaseActor.list_users(page=1, page_size=10)
# Update a user
user = DatabaseActor.update_user(user_id=1, data={"email": "new_email@example.com"})
# Delete a user
success = DatabaseActor.delete_user(user_id=1)
# Search users by email
users = DatabaseActor.search_users_by_email(query="example.com")
# List users by admin status
users = DatabaseActor.list_users_by_admin_status(is_admin=True)
# List users created in a date range
start = datetime(2023, 1, 1)
end = datetime(2023, 1, 31)
users = DatabaseActor.list_users_created_in_date_range(start_date=start, end_date=end)
# Count users
count = DatabaseActor.count_users()
Conversations
| Column | Type | Description |
|---|---|---|
| id | INTEGER | Unique identifier for the conversation. |
| user_id | INTEGER | Foreign key referencing the user who created the conversation. |
| title | VARCHAR | Title of the conversation. |
| description | VARCHAR | Description of the conversation. |
| is_archived | BOOLEAN | Indicates whether the conversation is archived. |
| created | DATETIME | Timestamp indicating when the conversation was created. |
| modified | DATETIME | Timestamp indicating when the conversation was last modified. |
from litepolis_database_default import DatabaseActor
from datetime import datetime
# Create a conversation
conversation = DatabaseActor.create_conversation({
"title": "New Conversation",
"description": "A new conversation about a topic.",
"user_id": 1 # Optional: Link to a user
})
# Read a conversation by ID
conversation = DatabaseActor.read_conversation(conversation_id=1)
# List conversations with pagination and ordering
conversations = DatabaseActor.list_conversations(page=1, page_size=10, order_by="title", order_direction="asc")
# Update a conversation
updated_conversation = DatabaseActor.update_conversation(conversation_id=1, data={"title": "Updated Title"})
# Delete a conversation
success = DatabaseActor.delete_conversation(conversation_id=1)
# Search conversations
conversations = DatabaseActor.search_conversations(query="search term")
# List conversations by archived status
conversations = DatabaseActor.list_conversations_by_archived_status(is_archived=True)
# List conversations created in a date range
start = datetime(2023, 1, 1)
end = datetime(2023, 1, 31)
conversations = DatabaseActor.list_conversations_created_in_date_range(start_date=start, end_date=end)
# Count conversations
count = DatabaseActor.count_conversations()
# Archive a conversation
archived_conversation = DatabaseActor.archive_conversation(conversation_id=1)
Comments
| Column | Type | Description |
|---|---|---|
| id | INTEGER | Primary key for the comment. |
| text_field | VARCHAR | The content of the comment. |
| user_id | INTEGER | Foreign key referencing the user who created the comment. |
| conversation_id | INTEGER | Foreign key referencing the conversation the comment belongs to. |
| parent_comment_id | INTEGER | Foreign key referencing the parent comment (for replies). |
| created | DATETIME | Timestamp of when the comment was created. |
| modified | DATETIME | Timestamp of when the comment was last modified. |
from litepolis_database_default import DatabaseActor
from datetime import datetime
# Create a comment
comment = DatabaseActor.create_comment({
"text_field": "This is a comment.",
"user_id": 1,
"conversation_id": 1,
# "parent_comment_id": 2 # Optional: for replies
})
# Read a comment by ID
comment = DatabaseActor.read_comment(comment_id=1)
# List comments by conversation ID
comments = DatabaseActor.list_comments_by_conversation_id(conversation_id=1, page=1, page_size=10, order_by="created", order_direction="asc")
# Update a comment
updated_comment = DatabaseActor.update_comment(comment_id=1, data={"text_field": "Updated comment text."})
# Delete a comment
success = DatabaseActor.delete_comment(comment_id=1)
# Search comments
comments = DatabaseActor.search_comments(query="search term")
# List comments by user ID
comments = DatabaseActor.list_comments_by_user_id(user_id=1, page=1, page_size=10)
# List comments created in a date range
start = datetime(2023, 1, 1)
end = datetime(2023, 1, 31)
comments = DatabaseActor.list_comments_created_in_date_range(start_date=start, end_date=end)
# Count comments in a conversation
count = DatabaseActor.count_comments_in_conversation(conversation_id=1)
# Get a comment with its replies
comment = DatabaseActor.get_comment_with_replies(comment_id=1)
Votes
| Column | Type | Description |
|---|---|---|
| id | INTEGER | Primary key for the vote. |
| user_id | INTEGER | Foreign key referencing the user who created the vote. |
| comment_id | INTEGER | Foreign key referencing the comment being voted on. |
| value | INTEGER | The value of the vote. |
| created | DATETIME | Timestamp of when the vote was created. |
| modified | DATETIME | Timestamp of when the vote was last modified. |
from litepolis_database_default import DatabaseActor
from datetime import datetime
# Create a new vote
vote = DatabaseActor.create_vote({
"value": 1, # e.g., 1 for upvote, -1 for downvote
"user_id": 1,
"comment_id": 1
})
# Read a vote by ID
vote = DatabaseActor.read_vote(vote_id=1)
# Get a vote by user and comment
vote = DatabaseActor.get_vote_by_user_comment(user_id=1, comment_id=1)
# List votes by comment ID with pagination and ordering
votes = DatabaseActor.list_votes_by_comment_id(comment_id=1, page=1, page_size=10, order_by="created", order_direction="asc")
# Update a vote
updated_vote = DatabaseActor.update_vote(vote_id=1, data={"value": -1})
# Delete a vote
success = DatabaseActor.delete_vote(vote_id=1)
# List votes by user ID with pagination
votes = DatabaseActor.list_votes_by_user_id(user_id=1, page=1, page_size=10)
# List votes created in a date range
start = datetime(2023, 1, 1)
end = datetime(2023, 1, 31)
votes = DatabaseActor.list_votes_created_in_date_range(start_date=start, end_date=end)
# Count votes for a comment
count = DatabaseActor.count_votes_for_comment(comment_id=1)
# Get vote value distribution for a comment
distribution = DatabaseActor.get_vote_value_distribution_for_comment(comment_id=1)
StarRocks Integration
This module includes custom integration with StarRocks using SQLModel. This allows defining database tables that are compatible with StarRocks' specific requirements and features, such as distribution keys and properties.
To leverage this integration, use the @register_table decorator on your SQLModel classes:
from sqlmodel import SQLModel, Field
from typing import Optional
from datetime import datetime, UTC
from .utils_StarRocks import register_table
from sqlalchemy import Index # Import Index for table_args
@register_table(
distributed_by="HASH(id)", # Specify distribution key
properties={ # Optional StarRocks-specific properties
"compression": "LZ4",
"enable_persistent_index": "true",
# Add other properties as needed
}
)
class YourModel(SQLModel, table=True):
__tablename__ = "your_table_name"
id: Optional[int] = Field(default=None, primary_key=True)
# ... other fields ...
@register_table(
distributed_by="HASH(id)",
properties={
"compression": "LZ4",
"bloom_filter_columns": "email" # Optimize email lookups
}
)
class User(SQLModel, table=True):
__tablename__ = "users"
id: Optional[int] = Field(default=None, primary_key=True)
email: str = Field(
unique=True,
sa_column_kwargs={"comment": "Unique email address"}
)
is_active: bool = Field(default=True)
@register_table(distributed_by="HASH(id)")
class MigrationRecord(SQLModel, table=True):
__tablename__ = "migrations"
__table_args__ = (
Index("ix_migrations_executed_at", "executed_at"),
)
id: str = Field(primary_key=True) # Migration filename
hash: str = Field(nullable=False) # Content hash
executed_at: datetime = Field(
default_factory=lambda: datetime.now(UTC)
)
Key aspects of the StarRocks integration:
@register_tabledecorator: Used to specify StarRocks-specific DDL hints likedistributed_byandproperties.distributed_by: Defines the distribution strategy for the table (e.g.,HASH(column)).properties: A dictionary for setting StarRocks table properties likecompression,enable_persistent_index,bloom_filter_columns, etc.- Automatic DDL Generation: The
create_db_and_tables()function (used internally byDatabaseActorinitialization) generates the appropriate StarRocks DDL based on the registered models and their hints. - Handling of SQLModel features: The integration handles standard SQLModel features like primary keys, foreign keys, and indexes, translating them into StarRocks-compatible DDL where necessary.
For more details on the StarRocks integration, refer to the utils_StarRocks.py module and the API documentation.
License
MIT Licensed. See LICENSE for details.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file litepolis_database_default-0.1.2.tar.gz.
File metadata
- Download URL: litepolis_database_default-0.1.2.tar.gz
- Upload date:
- Size: 35.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f0a22e324a40bcb7d3a8f32a0f1f7698fd57c4a8d80b0c288c07afcd79934cb6
|
|
| MD5 |
8f1d57c679d1df1bb5c3a43dfd51b8ae
|
|
| BLAKE2b-256 |
e28888cbdf959dd16f16d561b697017b843df2215c87beec163d31c5e35d9dd5
|
Provenance
The following attestation bundles were made for litepolis_database_default-0.1.2.tar.gz:
Publisher:
python-publish.yml on NewJerseyStyle/LitePolis-database-default
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
litepolis_database_default-0.1.2.tar.gz -
Subject digest:
f0a22e324a40bcb7d3a8f32a0f1f7698fd57c4a8d80b0c288c07afcd79934cb6 - Sigstore transparency entry: 1171862853
- Sigstore integration time:
-
Permalink:
NewJerseyStyle/LitePolis-database-default@ce3fd40cfc9daa11b219b05881f043f248d38cbc -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/NewJerseyStyle
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@ce3fd40cfc9daa11b219b05881f043f248d38cbc -
Trigger Event:
release
-
Statement type:
File details
Details for the file litepolis_database_default-0.1.2-py3-none-any.whl.
File metadata
- Download URL: litepolis_database_default-0.1.2-py3-none-any.whl
- Upload date:
- Size: 39.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1e7ac04c735b39ffb6078e33a2c93d4056158c888f748155ecbecd0e3236b1dd
|
|
| MD5 |
867b5fa6bec526005ec482ac0cf4bd9e
|
|
| BLAKE2b-256 |
b8c471cac1e16dcbbf2c10a93de4649a9bd80abb3f6df13c87343510ddf243ef
|
Provenance
The following attestation bundles were made for litepolis_database_default-0.1.2-py3-none-any.whl:
Publisher:
python-publish.yml on NewJerseyStyle/LitePolis-database-default
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
litepolis_database_default-0.1.2-py3-none-any.whl -
Subject digest:
1e7ac04c735b39ffb6078e33a2c93d4056158c888f748155ecbecd0e3236b1dd - Sigstore transparency entry: 1171862865
- Sigstore integration time:
-
Permalink:
NewJerseyStyle/LitePolis-database-default@ce3fd40cfc9daa11b219b05881f043f248d38cbc -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/NewJerseyStyle
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@ce3fd40cfc9daa11b219b05881f043f248d38cbc -
Trigger Event:
release
-
Statement type: