Skip to main content

A CLI tool to generate Python TypedDict models from a MongoDB database.

Project description

MongoDB TypeGen

A command-line tool to connect to a MongoDB database, inspect collections, and automatically generate Python TypedDict models. This brings static type checking to your MongoDB documents, helping you write safer, more maintainable code.

Key Features

  • Automatic Schema Inference: Analyzes documents to infer field names and types.
  • Robust TypedDict Generation: Creates Python TypedDict classes for each collection.
  • Powerful CLI: Offers multiple commands (generate, list-collections, preview) for a streamlined workflow.
  • Handles Complex Schemas:
    • Recursively generates TypedDict classes for nested documents.
    • Correctly identifies optional fields and fields with multiple Union types.
    • Preserves original field names, even those with spaces or special characters.
  • Flexible & Configurable:
    • Filter for specific collections to include or exclude.
    • Customize the number of documents to sample for schema inference.
    • Perform a "dry run" to see generated code without writing to a file.

Installation

pip install mongodb-typegen

Usage

The primary command is generate, which creates the Python models file.

mongodb-typegen generate --db <database_name>

You can also use other commands like list-collections and preview for a better workflow.

Commands

generate

Generate TypedDict models from your MongoDB collections.

Argument Alias Default Description
--uri -u mongodb://localhost:27017/ MongoDB connection string.
--db -d (Required) Name of the MongoDB database.
--out -o generated_models.py Output file path for generated models.
--sample-size -s 100 Number of documents to sample per collection.
--collections -c Comma-separated list of collections to process.
--exclude -e Comma-separated list of collections to exclude.
--dry-run Show generated code without writing to a file.
--verbose -v Enable verbose output.
--quiet -q Suppress all output except errors.

Example:

# Generate models for all collections in the 'analytics' database
mongodb-typegen generate --db analytics

# Generate models for specific collections and save to a different file
mongodb-typegen generate --db ecommerce --collections users,products --out models/db_types.py

# Perform a dry run to preview the output for the 'logs' collection
mongodb-typegen generate --db app_logs --collections logs --dry-run

list-collections

List all collections in a specified database.

Argument Alias Default Description
--uri -u mongodb://localhost:27017/ MongoDB connection string.
--db -d (Required) Name of the MongoDB database.

Example:

mongodb-typegen list-collections --db my_app

preview

Preview the inferred schema and TypedDict for a single collection without generating a file. This is useful for quick inspection.

Argument Alias Default Description
--uri -u mongodb://localhost:27017/ MongoDB connection string.
--db -d (Required) Name of the MongoDB database.
COLLECTION_NAME (Required) The name of the collection to preview.
--sample-size -s 10 Number of documents to sample for the preview.

Example:

mongodb-typegen preview --db my_app users

Example Output

Given a collection named users with documents like this:

{
    "_id": ObjectId("60d5f3f7e8b4f6f8f8f8f8f8"),
    "full name": "Alice",
    "email": "alice@example.com",
    "age": 30,
    "is_active": true,
    "profile": {
        "bio": "Developer",
        "website": "https://a.com"
    }
}

The tool will generate the following TypedDict classes:

# This file was auto-generated by mongodb-typegen. Do not edit manually.

from typing import TypedDict, List, Optional, Union, Any
from datetime import datetime
from bson.objectid import ObjectId

UsersProfile = TypedDict("UsersProfile", {
    'bio': str,
    'website': str
})

Users = TypedDict("Users", {
    '_id': ObjectId,
    'age': int,
    'email': str,
    'full name': str,
    'is_active': bool,
    'profile': UsersProfile
})

License

This project is licensed under the MIT License. See the LICENSE file 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

mongodb_typegen-1.0.3.tar.gz (35.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mongodb_typegen-1.0.3-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file mongodb_typegen-1.0.3.tar.gz.

File metadata

  • Download URL: mongodb_typegen-1.0.3.tar.gz
  • Upload date:
  • Size: 35.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for mongodb_typegen-1.0.3.tar.gz
Algorithm Hash digest
SHA256 12a64b41eadd04fefe24c1b3dd1ea494c5ad1c8214be8c682f5276e657857b61
MD5 dd5e83d0c5794867e262909aca3700eb
BLAKE2b-256 0a8f595fdafbd6d5317a63f82737aca1b9b36dba9348ea8e8d9df7e1e0139ae5

See more details on using hashes here.

File details

Details for the file mongodb_typegen-1.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for mongodb_typegen-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 f5908e43c3a8814a6277ec54e89b9b3e51966b11438861b394ea132df9db868e
MD5 f80457bf594c3cc851f5a756ecc84205
BLAKE2b-256 e35f2ac49db017f8b1abee5d208f840e71a12edd34c785967edc830a127ea2cc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page