Skip to content

feat: default database and collection in MetadataDbClient #152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 24, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions docs/source/ExamplesDocDBRestApi.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ Count Example 1: Get # of records with a certain subject_id
from aind_data_access_api.document_db import MetadataDbClient

API_GATEWAY_HOST = "api.allenneuraldynamics.org"
DATABASE = "metadata_index"
COLLECTION = "data_assets"
# Default database and collection names are set in the client
# DATABASE = "metadata_index"
# COLLECTION = "data_assets"

docdb_api_client = MetadataDbClient(
host=API_GATEWAY_HOST,
database=DATABASE,
collection=COLLECTION,
# database=DATABASE,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be better to remove these comments and just make a note above on how to override the fields if needed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

# collection=COLLECTION,
)

filter = {"subject.subject_id": "731015"}
Expand Down Expand Up @@ -136,8 +137,6 @@ It's possible to attach a custom Session to retry certain requests errors:
from aind_data_access_api.document_db import MetadataDbClient

API_GATEWAY_HOST = "api.allenneuraldynamics.org"
DATABASE = "metadata_index"
COLLECTION = "data_assets"

retry = Retry(
total=5,
Expand All @@ -151,8 +150,6 @@ It's possible to attach a custom Session to retry certain requests errors:

with MetadataDbClient(
host=API_GATEWAY_HOST,
database=DATABASE,
collection=COLLECTION,
session=session,
) as docdb_api_client:
records = docdb_api_client.retrieve_docdb_records(limit=10)
Expand Down
9 changes: 5 additions & 4 deletions docs/source/UserGuide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,14 @@ REST API
from aind_data_access_api.document_db import MetadataDbClient

API_GATEWAY_HOST = "api.allenneuraldynamics.org"
DATABASE = "metadata_index"
COLLECTION = "data_assets"
# Default database and collection names are set in the client
# DATABASE = "metadata_index"
# COLLECTION = "data_assets"

docdb_api_client = MetadataDbClient(
host=API_GATEWAY_HOST,
database=DATABASE,
collection=COLLECTION,
# database=DATABASE,
# collection=COLLECTION,
)

filter = {"subject.subject_id": "731015"}
Expand Down
30 changes: 30 additions & 0 deletions src/aind_data_access_api/document_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,36 @@ def __exit__(self, exc_type, exc_val, exc_tb):
class MetadataDbClient(Client):
"""Class to manage reading and writing to metadata db"""

def __init__(
self,
host: str,
database: str = "metadata_index",
collection: str = "data_assets",
version: str = "v1",
boto: Optional[BotoSession] = None,
session: Optional[Session] = None,
):
"""
Instantiate a MetadataDbClient.

Parameters
----------
host : str
database : str
collection : str
version : str
boto : Optional[BotoSession]
session : Optional[Session]
"""
super().__init__(
host=host,
database=database,
collection=collection,
version=version,
boto=boto,
session=session,
)

def retrieve_docdb_records(
self,
filter_query: Optional[dict] = None,
Expand Down
22 changes: 20 additions & 2 deletions tests/test_document_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,8 +437,6 @@ class TestMetadataDbClient(unittest.TestCase):

example_client_args = {
"host": "example.com/",
"database": "metadata_db",
"collection": "data_assets",
}

example_record_list = [
Expand All @@ -452,6 +450,26 @@ class TestMetadataDbClient(unittest.TestCase):
for id_num in range(0, 10)
]

def test_metadatadbclient_constructor(self):
"""Tests class constructor"""
client = MetadataDbClient(**self.example_client_args)

self.assertEqual("example.com", client.host)
self.assertEqual("metadata_index", client.database)
self.assertEqual("data_assets", client.collection)
self.assertEqual("v1", client.version)
self.assertEqual(
"https://example.com/v1/metadata_index/data_assets",
client._base_url,
)

client = MetadataDbClient(**self.example_client_args, version="v2")
self.assertEqual("v2", client.version)
self.assertEqual(
"https://example.com/v2/metadata_index/data_assets",
client._base_url,
)

@patch("aind_data_access_api.document_db.Client._find_records")
def test_retrieve_docdb_records(
self,
Expand Down