Skip to content
GitHubDiscord

NOSQL (lexigram-nosql)

NoSQL document store support for the Lexigram Framework (MongoDB, DynamoDB, Firestore).


lexigram-nosql provides async document-store backends behind a clean protocol interface. It ships with a MongoDB driver (Motor-based), a fluent query builder, aggregation pipelines, the repository pattern with specifications, a migration manager, and Named DI multi-backend support. All backends are resolved through the container.


Terminal window
uv add lexigram lexigram-nosql
# With MongoDB support
uv add "lexigram-nosql[mongodb]"
# With DynamoDB support
uv add "lexigram-nosql[dynamodb]"
# With Firestore support
uv add "lexigram-nosql[firestore]"
from lexigram import Application
from lexigram.di.module import Module, module
from lexigram.nosql import NoSQLModule
from lexigram.nosql.config import MongoDBConfig, NoSQLConfig
from lexigram.contracts.data.nosql.nosql import DocumentStoreProtocol
@module(
imports=[
NoSQLModule.configure(
NoSQLConfig(
driver="mongodb",
mongodb=MongoDBConfig(
uri="mongodb://localhost:27017",
database="myapp",
),
)
)
]
)
class AppModule(Module):
pass
async def main() -> None:
async with Application.boot(modules=[AppModule]) as app:
store = await app.container.resolve(DocumentStoreProtocol)
collection = await store.get_collection("users")
doc_id = await collection.insert_one({"name": "Alice", "age": 30})
users = await collection.find({"age": {"$gte": 25}})
print(users)
if __name__ == "__main__":
import asyncio
asyncio.run(main())

Zero-config usage: Call NoSQLModule.configure() with no arguments to use all defaults.

application.yaml
nosql:
driver: "mongodb"
mongodb:
uri: "mongodb://localhost:27017"
database: "myapp"
max_pool_size: 100
Section titled “Option 2 — Profiles + Environment Variables (recommended)”
Terminal window
export LEX_NOSQL__DRIVER=mongodb
export LEX_NOSQL__MONGODB__URI=mongodb://localhost:27017
export LEX_NOSQL__MONGODB__DATABASE=myapp
from lexigram.nosql import NoSQLModule
from lexigram.nosql.config import NoSQLConfig, MongoDBConfig
NoSQLModule.configure(
NoSQLConfig(
driver="mongodb",
mongodb=MongoDBConfig(uri="mongodb://localhost:27017", database="myapp"),
)
)

NoSQLConfig

FieldDefaultEnv varDescription
enabledtrueLEX_NOSQL__ENABLEDEnable NoSQL support
driver"mongodb"LEX_NOSQL__DRIVERNoSQL driver ("mongodb", "dynamodb", "firestore")
mongodbMongoDBConfig()MongoDB-specific connection configuration
backends[]Named backend entries for multi-backend DI registration

MongoDBConfig

FieldDefaultEnv varDescription
uri"mongodb://localhost:27017"LEX_NOSQL__MONGODB__URIMongoDB connection URI
database"lexigram"LEX_NOSQL__MONGODB__DATABASEDatabase name
max_pool_size100LEX_NOSQL__MONGODB__MAX_POOL_SIZEMaximum connection pool size
min_pool_size10LEX_NOSQL__MONGODB__MIN_POOL_SIZEMinimum connection pool size
retry_writestrueLEX_NOSQL__MONGODB__RETRY_WRITESEnable write retries
retry_readstrueLEX_NOSQL__MONGODB__RETRY_READSEnable read retries
read_preference"primaryPreferred"LEX_NOSQL__MONGODB__READ_PREFERENCERead preference mode
write_concern_w"majority"LEX_NOSQL__MONGODB__WRITE_CONCERN_WWrite concern level
auth_source"admin"LEX_NOSQL__MONGODB__AUTH_SOURCEAuthentication database
MethodDescription
NoSQLModule.configure(config, enable_ttl=True)Configure with explicit config
NoSQLModule.scope(*repositories)Scope repository classes into a feature module
NoSQLModule.stub()Minimal config for testing
  • MongoDB backend — async Motor-based with connection pooling and retry logic
  • Query builder — type-safe fluent API for MongoDB queries and projections
  • Aggregation pipelines — composable pipeline stages for complex aggregations
  • Repositories — base DocumentRepository pattern with specification support
  • Migration manager — index creation, field operations, and collection management
  • Named DI multi-backend — multiple backends registered via Annotated[DocumentStoreProtocol, Named("analytics")]
  • TTL index support — automatic time-to-live index creation for document expiration
  • Session and transaction context managersmongodb_session() and mongodb_transaction() for ACID operations
async with Application.boot(modules=[NoSQLModule.stub()]) as app:
store = await app.container.resolve(DocumentStoreProtocol)
# Test with in-memory backend
FileWhat it contains
src/lexigram/nosql/module.pyNoSQLModule.configure(), .scope(), .stub()
src/lexigram/nosql/config.pyNoSQLConfig, MongoDBConfig, NamedNoSQLConfig
src/lexigram/nosql/di/provider.pyNoSQLProvider boot and registration
src/lexigram/nosql/backends/mongodb/backend.pyMongoDBDocumentStore implementation
src/lexigram/nosql/query/builder.pyDocumentQueryBuilder
src/lexigram/nosql/query/pipeline.pyAggregationPipeline
src/lexigram/nosql/repository/base.pyDocumentRepository base class
src/lexigram/nosql/migration/manager.pyMigrationManager