Skip to main content

A simple, lightweight, and file-based JSON database library for Python.

Project description

jsondb-python

A simple, lightweight, and file-based JSON database library for Python, now with proper error handling, a modular structure, and a powerful interactive REPL.

Overview

jsondb-python provides two ways to manage your data:

  1. As a Python Library: A simple interface for storing and retrieving data in a JSON file. It's perfect for small projects where a full-fledged database is overkill.
  2. As an Interactive REPL: A feature-rich command-line interface (CLI) to manage your databases directly from the terminal, no scripting required.

Installation

pip install jsondb-python

For the best REPL experience with auto-completion and history, it is recommended to install prompt_toolkit:

pip install prompt-toolkit

Usage

As a Python Library (Quick Start)

from jsondb import JsonDB

# Initialize the database
db = JsonDB('my_database.json')

try:
    # Create a table
    db.create_table('users')

    # Insert data
    db.insert_data('users', {'id': 1, 'name': 'Alice', 'role': 'admin'})
    db.insert_data('users', {'id': 2, 'name': 'Bob', 'role': 'user'})

    # Update data where role is 'user'
    db.update_data(
        'users',
        condition=lambda user: user.get('role') == 'user',
        new_data={'id': 2, 'name': 'Bob', 'role': 'member'}
    )

    # Delete data where id is 1
    db.delete_data('users', condition=lambda user: user.get('id') == 1)

    # Show final data
    db.show_data('users')

except db.TableExistsError as e:
    print(f"Setup failed because a table already exists: {e}")
except db.Error as e: # Catch any library-specific error
    print(f"An error occurred with the database: {e}")
except Exception as e:
    print(f"A general error occurred: {e}")

Interactive REPL (CLI)

The library includes a powerful interactive REPL (Read-Eval-Print Loop) to manage your databases from the command line.

Launching the REPL:

# Start the REPL in the main menu
jsondb

# Or open a database file directly
jsondb ./path/to/your/database.json

Key Features:

  • Interactive Management: Create, edit, and manage your JSON databases without writing Python code.
  • Smart Auto-Completion: Press Tab to get suggestions for commands, file paths, and table names.
  • Command History: Use the Up/Down arrow keys to navigate your command history.
  • User-Friendly Interface: A colorized and structured interface makes database management easy.
  • Built-in Help: Type .help in any mode to see a list of available commands.
  • Safe Operations: Features auto-saving on exit and automatic backup creation to prevent data loss.

Example Session:

$ jsondb
🌟 JsonDB >>> .build
📁 Enter database path (example: data/mydb.json): users.json
✅ Database 'users.json' created/opened successfully!
💡 Use '.create <table_name>' to create a new table
📦 [users.json] >>> .create people
✅ Table 'people' created successfully!
📦 [users.json] >>> .use people
✅ Successfully selected table 'people'!
💡 Use '.insert' to add data
📋 [people] >>> .insert
# ... interactive prompt to add data ... Data added successfully!
📋 [people] >>> .show
# ... displays table data ...
📋 [people] >>> .exit
💾 Performing auto-save before exiting...
👋 Thank you for using JsonDB REPL!
💾 Your data has been safely saved

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

jsondb_python-1.0.5.tar.gz (20.5 kB view details)

Uploaded Source

Built Distribution

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

jsondb_python-1.0.5-py3-none-any.whl (20.5 kB view details)

Uploaded Python 3

File details

Details for the file jsondb_python-1.0.5.tar.gz.

File metadata

  • Download URL: jsondb_python-1.0.5.tar.gz
  • Upload date:
  • Size: 20.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.11

File hashes

Hashes for jsondb_python-1.0.5.tar.gz
Algorithm Hash digest
SHA256 6ef4aa7263e35368ad3ede3801327385d3379656e3375a237ef71af36e52cc8d
MD5 f639fe35f0b8a36570104f6280558b98
BLAKE2b-256 2eeaf4d93b58cf409e8ee58def652bd5465bbb18a669f4a05b54f72d9b1f0954

See more details on using hashes here.

File details

Details for the file jsondb_python-1.0.5-py3-none-any.whl.

File metadata

  • Download URL: jsondb_python-1.0.5-py3-none-any.whl
  • Upload date:
  • Size: 20.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.11

File hashes

Hashes for jsondb_python-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 74818e4c1ab4da49c5803c8ba891352ae587ebe89479281bcc4d12a9cb234c8b
MD5 824f1172c2b1dd8b8d5c71f5bb0db6dc
BLAKE2b-256 b9c2d6ef243861034bcc24c50e77145bfbbfd1db6e8adf67b0a47633fccde590

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