import os

from peewee import Model
from playhouse.sqlite_ext import SqliteExtDatabase

db_path = "./data/database"
os.makedirs(db_path, exist_ok=True)

db = SqliteExtDatabase(
    db_path + "/sqlite.db",
    pragmas={  # see https://www.sqlite.org/pragma.html
        "auto_vacuum": 1,
        "busy_timeout": 5000,
        "cache_size": round(-64e3),
        "foreign_keys": 1,
        "ignore_check_constraints": 0,
        "journal_mode": "wal",
        "synchronous": 0,
    },
    timeout=5
)


class BaseModel(Model):
    """A base model that will use our Sqlite database."""

    class Meta:
        global db
        database = db
