pynenc_mongo.util.mongo_collections

Collection specifications for all MongoDB-based Pynenc components. Centralized location for all collection definitions and indexes.

Module Contents

Classes

CollectionSpec

Specification for a collection with its indexes

MongoCollections

Abstract base class for MongoDB collections with app_id-based prefix enforcement.

Functions

sanitize_collection_prefix

Sanitize an app_id for safe use as a MongoDB collection name prefix.

API

pynenc_mongo.util.mongo_collections.sanitize_collection_prefix(app_id: str, max_prefix_len: int = 40) str[source]

Sanitize an app_id for safe use as a MongoDB collection name prefix.

Replaces any character that is not alphanumeric or underscore with an underscore, prepends an underscore if the result starts with a digit, then truncates to fit within max_prefix_len (including an 8-character deterministic hash suffix). The hash is always computed from the full original app_id, so different app_ids that truncate to the same string still produce distinct prefixes.

Parameters:
  • app_id (str) – The application identifier to sanitize

  • max_prefix_len (int) – Maximum length of the returned prefix (default 40)

Returns:

A string safe for use in MongoDB collection names

class pynenc_mongo.util.mongo_collections.CollectionSpec[source]

Specification for a collection with its indexes

name: str

None

indexes: list[pymongo.IndexModel]

‘field(…)’

class pynenc_mongo.util.mongo_collections.MongoCollections(conf: pynenc_mongo.conf.config_mongo.ConfigMongo, prefix: str, app_id: str)[source]

Abstract base class for MongoDB collections with app_id-based prefix enforcement.

Initialization

_prefixed_name(base_name: str) str[source]

Return the collection name prefixed with the sanitized app_id.

instantiate_retriable_coll(spec: pynenc_mongo.util.mongo_collections.CollectionSpec) pynenc_mongo.util.mongo_client.RetryableCollection[source]

Instantiate a RetryableCollection for the given CollectionSpec.

Prepends the sanitized app_id prefix to the collection name so that different apps sharing the same database are fully isolated.

Parameters:

spec – Specification for the collection

Returns:

RetryableCollection instance

purge_all() None[source]

Purge all collections belonging to this app.