LiteDB is a lightweight wrapper for SQLite3, designed to simplify database interactions by providing an easy-to-use interface for managing data with buckets and schemas.
- Lightweight and easy-to-use SQLite3 wrapper.
- Support for in-memory and file-based repositories.
- Schema-based bucket creation and management.
- Query data with filtering and sorting.
- Automatic schema validation and indexing.
LiteDB provides a powerful and expressive DSL for querying data. The DSL allows you to build complex queries using conditions and logical operators.
You can create conditions using the where
function and apply comparison operators:
equal_to(value)
: Matches records where the field is equal to the given value.not_equal_to(value)
: Matches records where the field is not equal to the given value.less_than(value)
: Matches records where the field is less than the given value.less_or_equal_to(value)
: Matches records where the field is less than or equal to the given value.greater_than(value)
: Matches records where the field is greater than the given value.greater_or_equal_to(value)
: Matches records where the field is greater than or equal to the given value.exists_in(values)
: Matches records where the field exists in the given list of values.
Example:
from litedb.query import where
# Filter records where age is greater than 25
condition = where("age").greater_than(25)
You can combine multiple conditions using logical operators:
&
: Combines two conditions with a logical AND.|
: Combines two conditions with a logical OR.
Example:
from litedb.query import where, and_
# Filter records where age is greater or equal to 18 AND less than 65
condition = where("age").greater_or_equal_to(18) & where("age").less_than(65)
You can sort query results using the asc and desc functions:
asc(field_name)
: Sorts results in ascending order by the specified field.desc(field_name)
: Sorts results in descending order by the specified field.
And combine multiple sort opeartions with &
.
Example:
from litedb.query import asc, desc
# Sort results by age in ascending order, then by name in descending order
sort_order = asc("age") & desc("name")
You can create an in-memory repository or a file-based repository:
from litedb import Repository
# In-memory repository
with Repository() as repo:
print("In-memory repository created")
# File-based repository
with Repository("data.ldb") as repo:
print("File-based repository created")
Buckets are created with a schema that defines the fields and their properties:
from litedb import Field
with Repository() as repo:
bucket = repo.create_bucket(
name="users",
schema=[
Field("id", is_key=True),
Field("name"),
Field("age", indexed=True),
]
)
print(f"Bucket created: {bucket.name}")
If a bucket already exists in the repository, you can open it by its name:
with Repository("data.ldb") as repo:
bucket = repo.bucket("users")
print(f"Opened bucket: {bucket.name}")
# Example: Fetch all data from the bucket
for item in bucket:
print(item)
You can add data to a bucket using the save or save_all methods:
bucket.save({"id": 1, "name": "Alice", "age": 30}, update_if_exists=False)
bucket.save_all([
{"id": 2, "name": "Bob", "age": 25},
{"id": 3, "name": "Charlie", "age": 35},
])
You can get the number of the items on the bucket using two methods:
number_of_items = bucket.count():
print(number_of_items)
# Or just using the len function
bucket_size = len(bucket)
print(bucket_size)
You can get access all data store in a bucket by using an ierator:
for item in bucket.all():
print(item)
# Or just using the bucket object
for item in bucket:
print(item)
You can query data using filters and sorting:
from litedb.query import where, asc
# Filter data
results = bucket.filter(where("age").greater_than(25))
for item in results:
print(item)
# Sort data
sorted_results = bucket.filter(query=where("age").greater_than(25), sort=asc("age"))
for item in sorted_results:
print(item)
You can retrieve data by its key:
user = bucket[1] # Or bucket.get(1)
print(user) # Output: {'id': 1, 'name': 'Alice', 'age': 30}
You can delete data by its key:
bucket.delete(1)
To remove a bucket from the repository:
repo.drop_bucket("users")
Always close the repository when you're done, or use the with
statement to ensure its always closed:
repo.close()
To run the tests, use pytest
:
pytest
This project is licensed under the MIT License. See the LICENSE
file for details.