Skip to content

Storage configuration

hololinked.storage.config.SQLDBConfig

Bases: BaseModel

Configuration validator for SQL databases for PostgreSQL and MySQL.

Source code in repo/hololinked/hololinked/storage/config.py
class SQLDBConfig(BaseModel):
    """Configuration validator for SQL databases for PostgreSQL and MySQL."""

    provider: Literal["postgresql", "mysql"] = "postgresql"
    """Database provider, postgresql or mysql"""
    host: StrictStr = "localhost"
    """PostgreSQL server host"""
    port: StrictInt = 5432
    """server port, default 5432"""
    database: StrictStr = "hololinked"
    """database name, default hololinked"""
    user: StrictStr = "hololinked"
    """user name, default hololinked, recommended not to use admin user"""
    password: SecretStr = Field(default=SecretStr(""), repr=False)
    """user password, default empty"""
    dialect: Literal["asyncpg", "psycopg", "asyncmy", "mysqldb", ""] = ""
    """dialect to use, default psycopg for postgresql"""
    uri: SecretStr = Field(default=SecretStr(""), repr=False)
    """Full database URI, overrides other settings, default empty"""

    model_config = ConfigDict(extra="forbid")

    @property
    def URL(self) -> str:
        """Fully qualified database URL."""
        if self.uri:
            return self.uri.get_secret_value()
        if self.provider == "postgresql":
            return f"postgresql{'+' + self.dialect if self.dialect else ''}://{self.user}:{self.password.get_secret_value()}@{self.host}:{self.port}/{self.database}"
        return f"mysql{'+' + self.dialect if self.dialect else ''}://{self.user}:{self.password.get_secret_value()}@{self.host}:{self.port}/{self.database}"

    @model_validator(mode="after")
    def _at_least_one(self):
        if not self.uri and (not self.host or not self.port or not self.database or not self.user or not self.password):
            raise ValueError("Provide either database URI or all of 'host', 'port', 'database', 'user', and 'password'")
        return self

Attributes

provider class-attribute instance-attribute

provider: Literal['postgresql', 'mysql'] = 'postgresql'

Database provider, postgresql or mysql

host class-attribute instance-attribute

host: StrictStr = 'localhost'

PostgreSQL server host

port class-attribute instance-attribute

port: StrictInt = 5432

server port, default 5432

database class-attribute instance-attribute

database: StrictStr = 'hololinked'

database name, default hololinked

user class-attribute instance-attribute

user: StrictStr = 'hololinked'

user name, default hololinked, recommended not to use admin user

password class-attribute instance-attribute

password: SecretStr = Field(default=SecretStr(''), repr=False)

user password, default empty

dialect class-attribute instance-attribute

dialect: Literal['asyncpg', 'psycopg', 'asyncmy', 'mysqldb', ''] = ''

dialect to use, default psycopg for postgresql

uri class-attribute instance-attribute

uri: SecretStr = Field(default=SecretStr(''), repr=False)

Full database URI, overrides other settings, default empty

URL property

URL: str

Fully qualified database URL.

hololinked.storage.config.SQLiteConfig

Bases: BaseModel

Configuration validator for SQLite database.

Source code in repo/hololinked/hololinked/storage/config.py
class SQLiteConfig(BaseModel):
    """Configuration validator for SQLite database."""

    provider: Literal["sqlite"] = "sqlite"
    """Database provider, only sqlite is supported"""
    dialect: SecretStr = Field(default=SecretStr("pysqlite"), repr=False)
    """dialect to use, aiosqlite for async, pysqlite for sync"""
    file: str = ""
    """SQLite database file, default is empty string, which leads to an DB with name of thing ID"""
    in_memory: bool = False
    """Use in-memory SQLite database, default False as it is not persistent"""
    uri: SecretStr = Field(default=SecretStr(""), repr=False)
    """Full database URI, overrides other settings, default empty"""

    model_config = ConfigDict(extra="forbid")

    @property
    def URL(self) -> str:
        """Fully qualified database URL."""
        if self.uri:
            return self.uri.get_secret_value()
        if self.in_memory:
            return f"sqlite+{self.dialect.get_secret_value()}:///:memory:"
        elif self.file:
            return f"sqlite+{self.dialect.get_secret_value()}:///{self.file}"
        raise NotImplementedError("Either 'uri' or 'file' or 'in_memory' must be provided for SQLiteConfig")

Attributes

provider class-attribute instance-attribute

provider: Literal['sqlite'] = 'sqlite'

Database provider, only sqlite is supported

dialect class-attribute instance-attribute

dialect: SecretStr = Field(default=SecretStr('pysqlite'), repr=False)

dialect to use, aiosqlite for async, pysqlite for sync

file class-attribute instance-attribute

file: str = ''

SQLite database file, default is empty string, which leads to an DB with name of thing ID

in_memory class-attribute instance-attribute

in_memory: bool = False

Use in-memory SQLite database, default False as it is not persistent

uri class-attribute instance-attribute

uri: SecretStr = Field(default=SecretStr(''), repr=False)

Full database URI, overrides other settings, default empty

URL property

URL: str

Fully qualified database URL.

hololinked.storage.config.MongoDBConfig

Bases: BaseModel

Configuration validator for MongoDB database.

Source code in repo/hololinked/hololinked/storage/config.py
class MongoDBConfig(BaseModel):
    """Configuration validator for MongoDB database."""

    provider: Literal["mongo"] = "mongo"
    """Database provider, only mongo is supported"""
    host: StrictStr = "localhost"
    """MongoDB server host"""
    port: StrictInt = 27017
    """server port, default 27017"""
    database: StrictStr = "hololinked"
    """database name, default hololinked"""
    user: StrictStr = ""
    """user name, default empty, recommended not to use admin user"""
    password: SecretStr = Field(default=SecretStr(""), repr=False)
    """user password, default empty"""
    authSource: StrictStr = ""
    """authentication source database, default empty"""
    uri: SecretStr = Field(default=SecretStr(""), repr=False)
    """Full database URI, overrides other settings, default empty"""

    model_config = ConfigDict(extra="forbid")

    @property
    def URL(self) -> str:
        """Fully qualified database URL."""
        if self.uri:
            return self.uri.get_secret_value()
        if self.user and self.password:
            if self.authSource:
                return f"mongodb://{self.user}:{self.password.get_secret_value()}@{self.host}:{self.port}/?authSource={self.authSource}"
            return f"mongodb://{self.user}:{self.password.get_secret_value()}@{self.host}:{self.port}/"
        return f"mongodb://{self.host}:{self.port}/"

    @model_validator(mode="after")
    def _at_least_one(self):
        if not self.uri and (not self.host or not self.port or not self.database or not self.user or not self.password):
            raise ValueError("Provide either database URI or all of 'host', 'port', 'database', 'user', and 'password'")
        return self

Attributes

provider class-attribute instance-attribute

provider: Literal['mongo'] = 'mongo'

Database provider, only mongo is supported

host class-attribute instance-attribute

host: StrictStr = 'localhost'

MongoDB server host

port class-attribute instance-attribute

port: StrictInt = 27017

server port, default 27017

database class-attribute instance-attribute

database: StrictStr = 'hololinked'

database name, default hololinked

user class-attribute instance-attribute

user: StrictStr = ''

user name, default empty, recommended not to use admin user

password class-attribute instance-attribute

password: SecretStr = Field(default=SecretStr(''), repr=False)

user password, default empty

authSource class-attribute instance-attribute

authSource: StrictStr = ''

authentication source database, default empty

uri class-attribute instance-attribute

uri: SecretStr = Field(default=SecretStr(''), repr=False)

Full database URI, overrides other settings, default empty

URL property

URL: str

Fully qualified database URL.