Skip to main content

A python wrapper for our python services

Project description

UMI database connector

This is a python wrapper for streamlining the querying process for our python services. The idea of this package is for NLP service to use different MongoDB databases on one cluster.

Class DatabaseConnector

A singleton that manages the MongoDB client and connections to different databases on the same cluster. Example:

from umi.database.database_connector import DatabaseConnector

connector = DatabaseConnector(atlas_db_uri="cluster-uri", mongodb_user="user", mongodb_password="pwd")

Note that cluster-uri needs to be a valid cluster uri that resolves on DNS.

Methods

DatabaseConnector.register_database

This method adds a database to the list of available databases.

from umi.database.database_connector import DatabaseConnector

connector = DatabaseConnector(atlas_db_uri="cluster-uri", mongodb_user="user", mongodb_password="pwd")

connector.register_database("test") # Registers the database test on the list

connector.register_database("test_active_db", setActive=True) # Registers the database test_active_db on the list and sets it as the active databse

The active database is the one used by default on find, update, agreggate and delete operations

DatabaseConnector.switch_db

Changes the active database to a new one that has been previously registered.

from umi.database.database_connector import DatabaseConnector

connector = DatabaseConnector(atlas_db_uri="cluster-uri", mongodb_user="user", mongodb_password="pwd")
connector.register_database("test")
connector.register_database("test_active_db", setActive=True)
connector.switch_db("test") # Switches to test
connector.switch_db("unseen") # Raises an error, the database hasn't been registered

DatabaseConnector.find

Creates and launches a find query.

from umi.database.database_connector import DatabaseConnector

connector = DatabaseConnector(atlas_db_uri="cluster-uri", mongodb_user="user", mongodb_password="pwd")
connector.register_database("test_active_db", setActive=True)
query_options = {
    "collection": "test_collection",
    "query": { "name": "Find test insert" },
    "result_as": "list"
}
result = db_instance.find(query_options=query_options) # Use the active database
# or
result = db_instance.find(query_options=query_options, database="another_database") # Use the specified database to run the query

DatabaseConnector.insert

Executes an insert operation.

from umi.database.database_connector import DatabaseConnector

connector = DatabaseConnector(atlas_db_uri="cluster-uri", mongodb_user="user", mongodb_password="pwd")
connector.register_database("test_active_db", setActive=True)
insert_options = {
    "collection": "test_collection",
    "document": { "name": "this is a test"}
}
result = db_instance.insert(insert_options=insert_options) # Use the active database
# or
result = db_instance.insert(insert_options=insert_options, database="another_database") # Use the specified database to run the insert

DatabaseConnector.update

Executes an update many operation on a collection.

from umi.database.database_connector import DatabaseConnector

connector = DatabaseConnector(atlas_db_uri="cluster-uri", mongodb_user="user", mongodb_password="pwd")
connector.register_database("test_active_db", setActive=True)
query_options = {
    "collection": "test_collection",
    "criteria": { "name": "the name to match"},
    "value": { "$set": {"name": "matched value"} },
    "result_as": "list"
}
result = db_instance.update(query_options=query_options) # Use the active database
# or
result = db_instance.update(query_options=query_options, database="another_database") # Use the specified database to run the insert

DatabaseConnector.aggregate

Executes an aggregation pipleine on a collection.

from umi.database.database_connector import DatabaseConnector

connector = DatabaseConnector(atlas_db_uri="cluster-uri", mongodb_user="user", mongodb_password="pwd")
connector.register_database("test_active_db", setActive=True)
query_options = {
    "collection": "test_collection",
    "pipeline": [{"$match":{"name":"the name to match"}},{"$project":"..."}],
    "allow_disk":true,
    "result_as": "list" # or cursor
}
result = db_instance.aggregate(query_options=query_options) # Use the active database
# or
result = db_instance.aggregate(query_options=query_options, database="another_database") # Use the specified database to run the insert

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

umi-database-connector-1.5.2.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

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

umi_database_connector-1.5.2-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file umi-database-connector-1.5.2.tar.gz.

File metadata

  • Download URL: umi-database-connector-1.5.2.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.18

File hashes

Hashes for umi-database-connector-1.5.2.tar.gz
Algorithm Hash digest
SHA256 23e555dc26361d5f310a0bdd64908ba8cdcf6774da0f4d6ea88999b4ba814930
MD5 0af49082e54e1fea0e5dd15aed8b0243
BLAKE2b-256 b6b75cfb1748cd6bfce765f5b6952b6e9c07a2e7f7357691a01012f966d77b5b

See more details on using hashes here.

File details

Details for the file umi_database_connector-1.5.2-py3-none-any.whl.

File metadata

File hashes

Hashes for umi_database_connector-1.5.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7f377a83eda380148ecf694639e5d0dcdfc0ba5f393870b01baa636d4f6e3fa9
MD5 3c181818342961187cb93bd3c6bdefa0
BLAKE2b-256 885a15956f604dd5539a82ec83314edc44e1495e9bac3b27ad5016e35f966472

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