Skip to main content

A python package for connecting with Cassandra, MongoDB, mySQL.

Project description

dbLinkPro

dbLinkPro is a versatile Python package designed to simplify database interactions by offering a unified interface for performing CRUD (Create, Read, Update, Delete) operations across Cassandra, MongoDB, and MySQL databases. This package not only supports essential database operations but also enhances the user experience with additional features specific to each database type.

Features

Cassandra-Specific Enhancements

  • Docker Integration: Automatically starts a Cassandra Docker container if it's not running. Handles container creation, volume management, and startup checks.
  • Connection Timeout Handling: Implements retry logic to handle connection delays, ensuring that Cassandra is fully operational before proceeding.
  • Keyspace and Table Management: Create and manage keyspaces and tables with flexible schema definitions. Switch between keyspaces easily.
  • Schema Retrieval: Retrieve and validate table schemas, including checking the compatibility of data types for updates and deletions.
  • Bulk Data Ingestion: Supports bulk insertion of data from CSV and Excel files into Cassandra tables.
  • Custom Query Handling: Construct and execute custom queries for inserting, updating, and deleting records with validation based on schema types.
  • Container Lifecycle Management: Start, stop, and remove Cassandra Docker containers programmatically.

Installation

You can install dbLinkPro using pip:

pip install dbLinkPro

Usage

MySQL

from database_automation import mysql_crud

Create a connection instance

connection = mysql_crud.MySQLConnection(
    host='localhost',
    user='your_username',
    password='your_password',
    database='test_db',
    port=3306
)

Connect to the database

connection.connect()

Create a new database if it does not exist

connection.create_database()

Table operations

columns = {
    'id': 'INT AUTO_INCREMENT PRIMARY KEY',
    'name': 'VARCHAR(100)',
    'age': 'INT'
}
connection.create_table('person', columns)

record = {
    'name': 'Alice',
    'age': 28
}
connection.insert_record('person', record)

records = connection.select_record('person')
print(records)  # Output: [[1, 'Alice', 28]]

update_values = {'age': 29}
connection.update_record('person', update_values, 'name = "Alice"')

connection.delete_record('person', 'name = "Alice"')

Disconnect from the database

connection.disconnect()

MongoDB

from database_automation import mongo_crud

Create an instance of MongoOperation

mongo = mongo_crud.MongoOperation(
    client_url='mongodb://localhost:27017/',
    database_name='test_db',
    collection_name='test_collection'
)

Create a MongoDB client

client = mongo.create_mongo_client()

Create or access the database

database = mongo.create_database()

Create or access the collection

collection = mongo.create_collection()

CRUD operations

single_record = {'name': 'John Doe', 'age': 35}
mongo.insert_record(single_record, 'test_collection')

multiple_records = [
    {'name': 'Jane Smith', 'age': 28},
    {'name': 'Emily Davis', 'age': 40}
]
mongo.insert_record(multiple_records, 'test_collection')

mongo.bulk_insert('data.csv', 'test_collection')

mongo.bulk_insert('data.xlsx', 'test_collection')

Cassandra

from database_automation import cassandra_crud

Create an instance of CassandraOperation

cassandra = cassandra_crud.CassandraOperation(
    contact_points=['127.0.0.1'],
    volume='cassandra_data'
)

Connect to Cassandra

session = cassandra.connect(username='your_username', password='your_password')

Create a keyspace

cassandra.create_keyspace('test_keyspace', strategy='SimpleStrategy', replicas=1)

Use the created keyspace

cassandra.use_keyspace('test_keyspace')

Define table schema

schema = {
    'id': 'int',
    'name': 'text',
    'age': 'int'
    }

Create a table

cassandra.create_table('test_table', schema)

Insert a record

record = {'id': 1, 'name': 'John Doe', 'age': 30}
cassandra.insert_record('test_table', record)

Bulk insert from a CSV file

cassandra.bulk_insert('data.csv', 'test_table')

Fetch Records

rows = cassandra.fetch_records('test_table')
for row in rows:
    print(row)

Update a record

update_values = {'name': 'Jane Doe', 'age': 29}
cassandra.update_record('test_table', 'id', 1, update_values)

Delete a record

cassandra.delete_record('test_table', 'id', 1)

Close the connection

cassandra.close()

Contributing

I welcome contributions to dbLinkPro. If you'd like to contribute, please..

  1. Fork the repository on GitHub.
  2. Create a new branch for your changes.
  3. Make your changes and commit them with descriptive messages.
  4. Push your changes to your fork.
  5. Submit a pull request with a detailed explanation of your changes.

License

dbLinkPro is licensed under the MIT License.

Author/Maintainer

Sneh Pillai

Contact Information

For support or questions, please contact: snehpillai02@gmail.com

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

dblinkpro-0.0.2.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

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

dbLinkPro-0.0.2-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file dblinkpro-0.0.2.tar.gz.

File metadata

  • Download URL: dblinkpro-0.0.2.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for dblinkpro-0.0.2.tar.gz
Algorithm Hash digest
SHA256 4c7a841a8fd438a47013705250f1b9bf90a05a30662a627c05e7b3dbac8df40d
MD5 b65e0dd19bda4dbac286ad24f0a8361c
BLAKE2b-256 0a573220098c61fe81e91c2c97a7f82c9febf6d599e26af25f4910f3deed68e7

See more details on using hashes here.

File details

Details for the file dbLinkPro-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: dbLinkPro-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for dbLinkPro-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8c167661da6a392ed9085f566a5a96f7fdb89f80753d99f22997854d1471cba1
MD5 e0249bbcb1a01a6503554b2c95e99e44
BLAKE2b-256 049e9fb53ccbcd55d1430a7b3cef4dca7f1eba73885b7fd3b461b18713ec0e8a

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