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.

source.py 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import json
  2. from datetime import datetime
  3. from typing import Optional
  4. from sqlalchemy import DateTime, String, func
  5. from sqlalchemy.dialects.postgresql import JSONB
  6. from sqlalchemy.orm import Mapped, mapped_column
  7. from models.base import Base
  8. from .engine import db
  9. from .types import StringUUID
  10. class DataSourceOauthBinding(Base):
  11. __tablename__ = "data_source_oauth_bindings"
  12. __table_args__ = (
  13. db.PrimaryKeyConstraint("id", name="source_binding_pkey"),
  14. db.Index("source_binding_tenant_id_idx", "tenant_id"),
  15. db.Index("source_info_idx", "source_info", postgresql_using="gin"),
  16. )
  17. id = mapped_column(StringUUID, server_default=db.text("uuid_generate_v4()"))
  18. tenant_id = mapped_column(StringUUID, nullable=False)
  19. access_token: Mapped[str] = mapped_column(String(255), nullable=False)
  20. provider: Mapped[str] = mapped_column(String(255), nullable=False)
  21. source_info = mapped_column(JSONB, nullable=False)
  22. created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, server_default=func.current_timestamp())
  23. updated_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, server_default=func.current_timestamp())
  24. disabled: Mapped[Optional[bool]] = mapped_column(db.Boolean, nullable=True, server_default=db.text("false"))
  25. class DataSourceApiKeyAuthBinding(Base):
  26. __tablename__ = "data_source_api_key_auth_bindings"
  27. __table_args__ = (
  28. db.PrimaryKeyConstraint("id", name="data_source_api_key_auth_binding_pkey"),
  29. db.Index("data_source_api_key_auth_binding_tenant_id_idx", "tenant_id"),
  30. db.Index("data_source_api_key_auth_binding_provider_idx", "provider"),
  31. )
  32. id = mapped_column(StringUUID, server_default=db.text("uuid_generate_v4()"))
  33. tenant_id = mapped_column(StringUUID, nullable=False)
  34. category: Mapped[str] = mapped_column(String(255), nullable=False)
  35. provider: Mapped[str] = mapped_column(String(255), nullable=False)
  36. credentials = mapped_column(db.Text, nullable=True) # JSON
  37. created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, server_default=func.current_timestamp())
  38. updated_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, server_default=func.current_timestamp())
  39. disabled: Mapped[Optional[bool]] = mapped_column(db.Boolean, nullable=True, server_default=db.text("false"))
  40. def to_dict(self):
  41. return {
  42. "id": self.id,
  43. "tenant_id": self.tenant_id,
  44. "category": self.category,
  45. "provider": self.provider,
  46. "credentials": json.loads(self.credentials),
  47. "created_at": self.created_at.timestamp(),
  48. "updated_at": self.updated_at.timestamp(),
  49. "disabled": self.disabled,
  50. }