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.3.tar.gz (5.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.3-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for umi-database-connector-1.5.3.tar.gz
Algorithm Hash digest
SHA256 fdf15fb783c45f61e2400561085069e839d656c43025da644c459e4b1f102488
MD5 d1f2ba163cf7fc532d9b91bc8f448d87
BLAKE2b-256 85a9e985a82762c9e06aaae2d2dbac57412190a9cb5969ef4dba93ed20491097

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for umi_database_connector-1.5.3-py3-none-any.whl
Algorithm Hash digest
SHA256 6534ca38b93b5fae113b2dda033dd117583307d0d7733419aca571cf09c25c1a
MD5 d27037eaaeb3a6a1f39ce7512ef06a9d
BLAKE2b-256 b96822f6d33da14ff96afae0ada741b342d0ffe446c32d646bfc81a21e37549d

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