33 行
729 B
Python
33 行
729 B
Python
from typing import Self
|
|
|
|
import eloquent
|
|
|
|
|
|
class DatabaseManager (eloquent.DatabaseManager):
|
|
pass
|
|
|
|
|
|
class Model (eloquent.Model):
|
|
def upsert (
|
|
self,
|
|
*args: str,
|
|
) -> None:
|
|
q = self.query ()
|
|
for arg in args:
|
|
q = q.where (arg, getattr (self, arg))
|
|
row = q.first ()
|
|
if row is not None:
|
|
self.id = row.id
|
|
self._Model__exists = True # pylint: disable = protected-access
|
|
self.save ()
|
|
|
|
@classmethod
|
|
def with_ (
|
|
cls,
|
|
*relations: str,
|
|
) -> eloquent.QueryBuilder[Self]:
|
|
q = cls.query ()
|
|
for relation in relations:
|
|
q = q._load_relation (relation)
|
|
return q
|