diff --git a/cognee/modules/graph/models/Edge.py b/cognee/modules/graph/models/Edge.py new file mode 100644 index 000000000..51cc4cbd2 --- /dev/null +++ b/cognee/modules/graph/models/Edge.py @@ -0,0 +1,58 @@ +from datetime import datetime, timezone +from sqlalchemy import ( + # event, + DateTime, + JSON, + UUID, + Text, +) + +# from sqlalchemy.schema import DDL +from sqlalchemy.orm import Mapped, mapped_column + +from cognee.infrastructure.databases.relational import Base + + +class Edge(Base): + __tablename__ = "edges" + + id: Mapped[UUID] = mapped_column(UUID(as_uuid=True), primary_key=True) + + slug: Mapped[UUID] = mapped_column(UUID(as_uuid=True), nullable=False) + + user_id: Mapped[UUID] = mapped_column(UUID(as_uuid=True), nullable=False) + + data_id: Mapped[UUID] = mapped_column(UUID(as_uuid=True), index=True, nullable=False) + + dataset_id: Mapped[UUID] = mapped_column(UUID(as_uuid=True), index=True, nullable=False) + + source_node_id: Mapped[UUID] = mapped_column(UUID(as_uuid=True), nullable=False) + destination_node_id: Mapped[UUID] = mapped_column(UUID(as_uuid=True), nullable=False) + + relationship_name: Mapped[str] = mapped_column(Text, nullable=False) + + label: Mapped[str | None] = mapped_column(Text) + attributes: Mapped[dict | None] = mapped_column(JSON) + + created_at: Mapped[datetime] = mapped_column( + DateTime(timezone=True), default=lambda: datetime.now(timezone.utc), nullable=False + ) + + # __table_args__ = ( + # {"postgresql_partition_by": "HASH (user_id)"}, # partitioning by user + # ) + + +# Enable row-level security (RLS) for edges +# enable_edge_rls = DDL(""" +# ALTER TABLE edges ENABLE ROW LEVEL SECURITY; +# """) +# create_user_isolation_policy = DDL(""" +# CREATE POLICY user_isolation_policy +# ON edges +# USING (user_id = current_setting('app.current_user_id')::uuid) +# WITH CHECK (user_id = current_setting('app.current_user_id')::uuid); +# """) + +# event.listen(Edge.__table__, "after_create", enable_edge_rls) +# event.listen(Edge.__table__, "after_create", create_user_isolation_policy) diff --git a/cognee/modules/graph/models/Node.py b/cognee/modules/graph/models/Node.py new file mode 100644 index 000000000..e69de29bb diff --git a/cognee/modules/graph/models/Untitled b/cognee/modules/graph/models/Untitled new file mode 100644 index 000000000..3616f65f0 --- /dev/null +++ b/cognee/modules/graph/models/Untitled @@ -0,0 +1,59 @@ +from datetime import datetime, timezone +from sqlalchemy import ( + DateTime, + Index, + # event, + String, + JSON, + UUID, +) + +# from sqlalchemy.schema import DDL +from sqlalchemy.orm import Mapped, mapped_column + +from cognee.infrastructure.databases.relational import Base + + +class Node(Base): + __tablename__ = "nodes" + + id: Mapped[UUID] = mapped_column(UUID(as_uuid=True), primary_key=True) + + slug: Mapped[UUID] = mapped_column(UUID(as_uuid=True), nullable=False) + + user_id: Mapped[UUID] = mapped_column(UUID(as_uuid=True), nullable=False) + + data_id: Mapped[UUID] = mapped_column(UUID(as_uuid=True), nullable=False) + + dataset_id: Mapped[UUID] = mapped_column(UUID(as_uuid=True), index=True, nullable=False) + + label: Mapped[str | None] = mapped_column(String(255)) + type: Mapped[str] = mapped_column(String(255), nullable=False) + indexed_fields: Mapped[list] = mapped_column(JSON, nullable=False) + + attributes: Mapped[dict | None] = mapped_column(JSON) + + created_at: Mapped[datetime] = mapped_column( + DateTime(timezone=True), default=lambda: datetime.now(timezone.utc), nullable=False + ) + + __table_args__ = ( + Index("index_node_dataset_slug", "dataset_id", "slug"), + Index("index_node_dataset_data", "dataset_id", "data_id"), + # {"postgresql_partition_by": "HASH (user_id)"}, # HASH partitioning on user_id + ) + + +# Enable row-level security (RLS) for nodes +# enable_node_rls = DDL(""" +# ALTER TABLE nodes ENABLE ROW LEVEL SECURITY; +# """) +# create_user_isolation_policy = DDL(""" +# CREATE POLICY user_isolation_policy +# ON nodes +# USING (user_id = current_setting('app.current_user_id')::uuid) +# WITH CHECK (user_id = current_setting('app.current_user_id')::uuid); +# """) + +# event.listen(Node.__table__, "after_create", enable_node_rls) +# event.listen(Node.__table__, "after_create", create_user_isolation_policy) \ No newline at end of file diff --git a/cognee/modules/graph/models/__init__.py b/cognee/modules/graph/models/__init__.py new file mode 100644 index 000000000..5920e89c4 --- /dev/null +++ b/cognee/modules/graph/models/__init__.py @@ -0,0 +1,2 @@ +from .Edge import Edge +from .Node import Node