ぼざろクリーチャーシリーズ DB 兼 API(自分用)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

33 lines
729 B

  1. from typing import Self
  2. import eloquent
  3. class DatabaseManager (eloquent.DatabaseManager):
  4. pass
  5. class Model (eloquent.Model):
  6. def upsert (
  7. self,
  8. *args: str,
  9. ) -> None:
  10. q = self.query ()
  11. for arg in args:
  12. q = q.where (arg, getattr (self, arg))
  13. row = q.first ()
  14. if row is not None:
  15. self.id = row.id
  16. self._Model__exists = True # pylint: disable = protected-access
  17. self.save ()
  18. @classmethod
  19. def with_ (
  20. cls,
  21. *relations: str,
  22. ) -> eloquent.QueryBuilder[Self]:
  23. q = cls.query ()
  24. for relation in relations:
  25. q = q._load_relation (relation)
  26. return q