You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.0 KiB
Python

import peewee
import os
from typing import Any, Dict, Optional
SKRIPTPFAD = os.path.abspath(os.path.dirname(__file__))
DB_PROXY = peewee.Proxy()
class BaseModel(peewee.Model):
class Meta:
database = DB_PROXY
class FlussDaten(BaseModel):
ts = peewee.DateTimeField()
messstelle = peewee.CharField()
geohash = peewee.CharField(12)
durchfluss = peewee.FloatField()
pegelstand = peewee.FloatField()
def insert_many(daten):
FlussDaten.insert_many(daten).execute()
def create_tables() -> None:
DB_PROXY.create_tables([FlussDaten])
def init_db(name: str, type_: str = "sqlite", config: Optional[Dict[str, Any]] = None):
config = config or {}
drivers = {
"sqlite": peewee.SqliteDatabase,
"mysql": peewee.MySQLDatabase,
"postgresql": peewee.PostgresqlDatabase,
}
try:
cls = drivers[type_]
except KeyError:
raise ValueError("Unknown database type: {}".format(type_)) from None
del config["database"]
db = cls(name, **config)
return db