Skip to content

Commit bfc8404

Browse files
committed
feat(cli): implement database builder with lazy import
1 parent 78c110c commit bfc8404

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import logging
2+
from typing import Optional, Type
3+
4+
from vectorcode.cli_utils import Config
5+
from vectorcode.database.base import DatabaseConnectorBase
6+
7+
logger = logging.getLogger(name=__name__)
8+
9+
10+
def get_database_connector(config: Config) -> DatabaseConnectorBase:
11+
"""
12+
It's CRUCIAL to keep the `import`s of the database connectors in the branches.
13+
This allow them to be lazy-imported. This also allow us to keep the main package
14+
lightweight because we don't have to include dependencies for EVERY database.
15+
16+
"""
17+
cls: Optional[Type[DatabaseConnectorBase]] = None
18+
19+
match config.db_type:
20+
case "ChromaDB0Connector":
21+
from vectorcode.database.chroma0 import ChromaDB0Connector
22+
23+
cls = ChromaDB0Connector
24+
case _:
25+
raise ValueError(f"Unrecognised database type: {config.db_type}")
26+
27+
return cls.create(config)

src/vectorcode/subcommands/drop.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import logging
22

33
from vectorcode.cli_utils import Config
4-
from vectorcode.database.chroma0 import ChromaDB0Connector
4+
from vectorcode.database import get_database_connector
55
from vectorcode.database.errors import CollectionNotFoundError
66

77
logger = logging.getLogger(name=__name__)
88

99

1010
async def drop(config: Config) -> int:
11-
database = ChromaDB0Connector(config)
11+
database = get_database_connector(config)
1212
try:
1313
await database.drop(str(config.project_root))
1414
if not config.pipe:

0 commit comments

Comments
 (0)