
from pycs import db
from pycs.database.util import commit_on_return

class BaseModel(db.Model):
    __abstract__ = True

    id = db.Column(db.Integer, primary_key=True)


    def remove(self, commit: bool = True) -> None:
        """
        remove this instance from the database

        :return:
        """
        db.session.delete(self)

        if commit:
            self.commit()

    @classmethod
    def new(cls, commit=False, **kwargs):
        obj = cls(**kwargs)
        db.session.add(obj)

        if commit:
            self.commit()

    def commit(self):
        db.session.commit()

class NamedBaseModel(BaseModel):
    __abstract__ = True

    name = db.Column(db.String, nullable=False)

    @commit_on_return
    def set_name(self, name: str):
        self.name = name
