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 hashes)

Uploaded Source

Built Distribution

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

Uploaded Python 3

Supported by

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