/ src / python / txtai / database / schema / orm.py
orm.py
 1  """
 2  ORM Module
 3  """
 4  
 5  # Conditional import
 6  try:
 7      from sqlalchemy import Column, DateTime, Float, JSON, Integer, LargeBinary, String, Text
 8      from sqlalchemy.orm import DeclarativeBase
 9  
10      ORM = True
11  except ImportError:
12      ORM = False
13  
14  
15  # Standard database schema using object relational mapping (ORM).
16  if ORM:
17  
18      def idcolumn():
19          """
20          Creates an id column. This method creates an unbounded text field for platforms that support it.
21  
22          Returns:
23              id column definition
24          """
25  
26          return String(512).with_variant(Text(), "sqlite", "postgresql")
27  
28      class Base(DeclarativeBase):
29          """
30          Base mapping.
31          """
32  
33      class Batch(Base):
34          """
35          Batch temporary table mapping.
36          """
37  
38          __tablename__ = "batch"
39          __table_args__ = {"prefixes": ["TEMPORARY"]}
40  
41          autoid = Column(Integer, primary_key=True, autoincrement=True)
42          indexid = Column(Integer)
43          id = Column(idcolumn())
44          batch = Column(Integer)
45  
46      class Score(Base):
47          """
48          Scores temporary table mapping.
49          """
50  
51          __tablename__ = "scores"
52          __table_args__ = {"prefixes": ["TEMPORARY"]}
53  
54          indexid = Column(Integer, primary_key=True, autoincrement=False)
55          score = Column(Float)
56  
57      class Document(Base):
58          """
59          Documents table mapping.
60          """
61  
62          __tablename__ = "documents"
63  
64          id = Column(idcolumn(), primary_key=True)
65          data = Column(JSON)
66          tags = Column(Text)
67          entry = Column(DateTime(timezone=True))
68  
69      class Object(Base):
70          """
71          Objects table mapping.
72          """
73  
74          __tablename__ = "objects"
75  
76          id = Column(idcolumn(), primary_key=True)
77          object = Column(LargeBinary)
78          tags = Column(Text)
79          entry = Column(DateTime(timezone=True))
80  
81      class SectionBase(Base):
82          """
83          Generic sections table mapping. Allows multiple section table names for reindexing.
84          """
85  
86          __abstract__ = True
87  
88          indexid = Column(Integer, primary_key=True, autoincrement=False)
89          id = Column(idcolumn(), index=True)
90          text = Column(Text)
91          tags = Column(Text)
92          entry = Column(DateTime(timezone=True))
93  
94      class Section(SectionBase):
95          """
96          Section table mapping.
97          """
98  
99          __tablename__ = "sections"