Database Name to Index Mapper
A cross-platform utility for mapping database names to sequential indices. Originally designed for Redis/Valkey database selection, but useful for any scenario where you need consistent string-to-number mapping.
Features
- Sequential mapping: Automatically assigns sequential indices (0, 1, 2, ...) to database names
- Cross-platform: Works on Linux, macOS, and Windows with appropriate config paths
- CLI and Python API: Use from command line or import as a Python library
- Utility parameter mapping: Configure database parameters for different CLI tools
- Persistent storage: Mappings are stored in JSON configuration file
- Safe for concurrent use: Processes that add mappings at the same time get distinct indices
- Prefix filtering: List mappings by prefix for organized namespacing
Installation
uv add db-name-to-idx-mapper # as a project dependency
uv tool install db-name-to-idx-mapper # CLI only
pip install db-name-to-idx-mapper
Quick Start
Command Line Usage
# Add a new mapping (fails if exists)
db-name-to-idx-mapper add-mapping myapp.cache
# Output: 0
# stderr: Added mapping: myapp.cache -> 0
# Ensure mapping exists (creates if needed, returns existing if present)
db-name-to-idx-mapper ensure-mapping myapp.sessions
# Output: 1
# Map name to index
db-name-to-idx-mapper map myapp.cache
# Output: 0
# List all mappings
db-name-to-idx-mapper list-mappings
# Output:
# myapp.cache:0
# myapp.sessions:1
# List mappings with prefix
db-name-to-idx-mapper list-mappings myapp
# Output:
# myapp.cache:0
# myapp.sessions:1
# Get maximum index (useful for configuring database limits)
db-name-to-idx-mapper get-max-index
# Output: 1
# Configure utility parameter mappings
db-name-to-idx-mapper add-utility-db-param-map redis-cli -n
db-name-to-idx-mapper add-utility-db-param-map redis-benchmark -select
# Get utility parameter
db-name-to-idx-mapper get-utility-db-param redis-cli
# Output: -n
# Execute utility with database mapping
db-name-to-idx-mapper exec redis-cli -n myapp.cache GET somekey
# Executes: redis-cli -n 0 GET somekey
# Execute for multiple databases with prefix
db-name-to-idx-mapper exec redis-cli -n myapp KEYS '*'
# Executes: redis-cli -n 0 KEYS *
# redis-cli -n 1 KEYS *
# (for all databases matching "myapp" prefix)
Python API Usage
from db_name_to_idx_mapper import DbNameToIdxMapper
# Initialize mapper
mapper = DbNameToIdxMapper()
# Add mappings
cache_idx = mapper.ensure_mapping("myapp.cache").index
session_idx = mapper.ensure_mapping("myapp.sessions").index
# Use in your Redis configuration
CACHES = {
'default': {
'BACKEND': 'django_redis.cache.RedisCache',
'LOCATION': f'redis://127.0.0.1:6379/{cache_idx}',
}
}
# List mappings with prefix
app_mappings = mapper.list_mappings("myapp")
for mapping in app_mappings:
print(f"{mapping['name']} -> {mapping['index']}")
# Get max index for Redis databases configuration
max_idx = mapper.get_max_mapping_index()
redis_databases = max_idx + 1 # Number of databases needed
Configuration
Config File Location
By default, the configuration file is stored at:
- Linux:
/etc/db-name-to-idx-mapper/config.json - macOS:
~/Library/Application Support/db-name-to-idx-mapper/config.json - Windows:
%APPDATA%\db-name-to-idx-mapper\config.json
Custom Config Path
You can specify a custom config file location using the --config-path parameter with system aliases:
# Use temporary directory
db-name-to-idx-mapper --config-path "{TEMP}/my-config.json" add-mapping test.db
# Use custom path
db-name-to-idx-mapper --config-path "/home/user/my-mappings.json" list-mappings
Available aliases:
{TEMP}: System temporary directory{CONFIG}: System configuration directory
Every process that uses the mapper needs read access to the config file. Adding a mapping additionally needs
write access to its directory: the file is replaced atomically and writers serialise on config.json.lock next
to it. ensure_mapping for an existing name only reads.
Config File Format
{
"mappings": {
"myapp.cache": 0,
"myapp.sessions": 1,
"myapp.queue": 2
},
"utilities": {
"redis-cli": "-n",
"redis-benchmark": "-select",
"valkey-cli": "-n"
}
}
Use Cases
Redis/Valkey Database Management
Instead of remembering database numbers:
redis-cli -n 0 SET key value # Which app uses DB 0?
redis-cli -n 1 GET session # What about DB 1?
Use meaningful names:
# Map names to indices
db-name-to-idx-mapper ensure-mapping myapp.cache # -> 0
db-name-to-idx-mapper ensure-mapping myapp.sessions # -> 1
# Use with redis-cli (traditional approach with wrapper script)
redis-cli -n $(db-name-to-idx-mapper map myapp.cache) SET key value
redis-cli -n $(db-name-to-idx-mapper map myapp.sessions) GET session:123
# Or use the exec command for cleaner syntax
db-name-to-idx-mapper exec redis-cli -n myapp.cache SET key value
db-name-to-idx-mapper exec redis-cli -n myapp.sessions GET session:123
# Execute commands on all databases with a prefix
db-name-to-idx-mapper exec redis-cli -n myapp FLUSHDB
# Or even create yourself a nice alias:
alias valkey-cli='db-name-to-idx-mapper exec docker exec -i cache valkey-cli'
valkey-cli -n myapp.cache SET key value
Django Multiple Databases
from db_name_to_idx_mapper import DbNameToIdxMapper
mapper = DbNameToIdxMapper()
DATABASES = {
'users': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': f'app_db_{mapper.ensure_mapping("users").index}',
},
'analytics': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': f'app_db_{mapper.ensure_mapping("analytics").index}',
}
}
Microservice Port Management
# Assign consistent ports to services
service_port = 8000 + mapper.ensure_mapping("user-service").index # 8000
api_port = 8000 + mapper.ensure_mapping("api-gateway").index # 8001
API Reference
DbNameToIdxMapper Class
Methods
DbNameToIdxMapper(config_file: str = None): Mapper onconfig_file, default path per platform (see above); creates the config directory if missingadd_mapping(name: str) -> int: Add new mapping, raisesValueErrorif it existsensure_mapping(name: str) -> MappingResult: Ensure mapping exists, returnsMappingResult(created: bool, index: int)map(name: str) -> int: Get index for name, raisesKeyErrorif not foundlist_mappings(prefix: str = None) -> List[Dict]: List of{"name": str, "index": int}sorted by name; withprefix, only names starting withprefix + "."get_max_mapping_index() -> int: Get highest mapping index, returns -1 if no mappingsadd_utility_db_param_map(utility_name: str, param_name: str): Add utility parameter mappingget_utility_db_param(utility_name: str) -> str: Get utility parameter name,-nif the utility has none
CLI Commands
All CLI commands output parsable results to stdout and descriptive messages to stderr.
Command Reference
exec <utility> [args...]: Execute a utility command with automatic database index mapping. The value after the utility's database parameter (-nunless configured withadd-utility-db-param-map) is replaced: a mapped name by its index, a prefix by each index under it (the command runs once per mapping, seelist-mappings); any other value is passed unchanged. The exit code is the last non-zero exit code of the runs, 0 if all succeed.
Example:
# Single database execution
db-name-to-idx-mapper exec redis-cli -n myapp.cache GET key
# Multiple database execution (prefix matching)
db-name-to-idx-mapper exec redis-cli -n myapp KEYS "*"
Requirements
- Python 3.8+
- No external dependencies
License
MIT License
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
uv sync # creates .venv with the dev dependency group
uv run pytest
uv run ruff check . && uv run ruff format --check .
Metadata
Release files for db-name-to-idx-mapper 1.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| db_name_to_idx_mapper-1.0.3.tar.gz | 10.0 kB | Without provenance | Details for db_name_to_idx_mapper-1.0.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| db_name_to_idx_mapper-1.0.3-py3-none-any.whl | Python 3 | none | any | Details for db_name_to_idx_mapper-1.0.3-py3-none-any.whl |
Total release size: 21.5 kB
Release files / db_name_to_idx_mapper-1.0.3.tar.gz
| Download URL | db_name_to_idx_mapper-1.0.3.tar.gz |
|---|---|
| Size | 10.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1389b1037588d1b5fe7cebb573b57248c8eb2fe9eedfb724905acd6fbeeace79
|
|
BLAKE2b-256 checksum How to use checksums |
4f5ac42e1e232473f81fbf6bc806123fcfec89d5c8d972c7e8ef5dc94262f7b6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.6 {"installer":{"name":"uv","version":"0.13.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / db_name_to_idx_mapper-1.0.3-py3-none-any.whl
| Download URL | db_name_to_idx_mapper-1.0.3-py3-none-any.whl |
|---|---|
| Size | 11.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
99b4fdf168d8994cda322e9b90cadbe33026a5043a4b833530fec088970497c4
|
|
BLAKE2b-256 checksum How to use checksums |
888f2c86a88207d7930e2db6a90a715f497bb827a44cc9623fe3c8546805aec4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.6 {"installer":{"name":"uv","version":"0.13.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Security for db-name-to-idx-mapper 1.0.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.