Package: py-tiny-orm
py-tiny-orm - A Minimal SQLite ORMpy-tiny-ormis a minimal Object-relational mapping system which stores
python classes in SQLite.Classes in your code extend the base classes provided by this module, and
a backing model is automatically generated. This model can then be applied
to an SQLite cursor, and data read, updated, or searched.See thequick start exmaple.(Simple) Tables"Table" Data Class"TableModel" Model ClassTableModel.allTableModel.getTableModel.get_manyTableModel.searchTableModel.storeJoin Tables"JoinTable" Data Class"JoinModel" Model ClassJoinModel.of_left/of_rightJoinModel.ids_for_left/ids_for_rightJoinModel.from_left/from_rightJoinModel.clear_left/clear_rightJoinModel.storeJoinModel.remove(Simple) Tables"Table" Data ClassA "Table" is a data structure that contains program defined fields.
They MUST inherited fromorm.Tableand contain typed properties, so that the
model can be generated.The following data types can be used:Python TypeSQLite TypeintINTEGERstrTEXTbytesBLOBfloatREALboolSMALLINTOptional[ x ]x will not haveNOT NULLTableINT+FOREIGN KEY"TableModel" Model ClassThe Model generated for these tables is of the typeTableModel.
You retrieved this from the Table class methodmodelpassing a sqlite cursor.Table.create_tablesmethod generates the table in the database.withsqlite3.connect(":memory:")asconn:cursor=conn.cursor()MyJoinTable.create_table(cursor)model=MyJoinTalbe.model(cursor)TableModel.allall(self) -> 'List[ModelledTable]'Returns all records on the current table.Note: records will be loaded into memory before being returned,
in order to optimise the number of queries to realted tables.TableModel.getget(self, unique_id: int) -> Optional[T]Gets a record by ID, or None if no record with that ID exists.TableModel.get_manyget_many(self, *ids: int) -> Dict[int, T]Gets all records that exist with ID in the supplied list.Entries in the dict are not generated for records which do not exist.TableModel.searchsearch(self, **kwargs: Any) -> List[T]Gets records for this model which match the given filters.You can filter using any field in the table, or by a foreign object.classFoo(Table["Foo"]):foo_id:intclassBar(Table["Bar"]bar_id:intfoo:Fooname:str# Search by standard fieldBar.model(cursor).search(name="Hello")# Search by foreign IDBar.model(cursor).search(foo_id=1)# Search by foreign objectBar.model(cursor).search(foo=Foo(1))# Search by local ID# NOTE: This is value, but using Model.get() is faster.Bar.model(cursor).search(bar_id=123)TableModel.storestore(self, record: T) -> boolWrites a record to the database.In all cases, this is done as an INSERT OR REPLACE statement.
If the ID field is not set, this may cause the ID of a record to change,
where it is matched via a unique key.The ID field will be updated with the inserted row's ID.Join TablesA Join table represents a many-to-many mapping between two simple Tables.
These tables are referred to as theLeftandRighttables; operations
are however bi-directional."JoinTable" Data ClassThe data entity for a 'Join' Table.A JoinTable has precisely two fields, which are foreign keys to two other
regular Tables, called Left and Right.classRoleMapping(JoinTable[User,Role]):user:Userrole:RoleNote that the JoinModel does not currently return instances of the
JoinTable, but future features might; instead functions will return
sequences of the "Left" or "Right" Tables, based on the calls made."JoinModel" Model ClassAs withTable, the model is retrieved from the class methodmodelusing an sqlite3 cursor, and acreate_tablesmethod
is available to generate the table in the database.withsqlite3.connect(":memory:")asconn:cursor=conn.cursor()MyJoinTable.create_table(cursor)model=MyJoinTalbe.model(cursor)JoinModel.of_left/of_rightof_left(self, left: Left) -> List[Right]of_right(self, right: Right) -> List[Left]Returns all records which map to a given record.JoinModel.ids_for_left/ids_for_rightids_for_left(self, left: Left) -> List[int]ids_for_right(self, left: Right) -> List[int]Returns allids present which map to a given record.This is the same extracting the IDs from the records returned by
theof_left/of_rightfunction, but saves the overhead of doing
any extra foreign key lookups.JoinModel.from_left/from_rightfrom_left(self, **kwargs: Any) -> List[Right]from_right(self, **kwargs: Any) -> List[Left]Returns all unique Right records which map to Left records that match
the given search criteria. No information about which Left they
matched is maintained.This will have the same result as:rights=Left.model().search(**kwargs)lefts={}forrightinright:lefts=join_model.of_right(right)lefts.update({l.left_id:lforlinlefts})returnlefts.values()but this function will be considerably more efficient.JoinModel.clear_left/clear_rightclear_left(self, left: Left) -> Noneclear_right(self, right: Right) -> NoneDeletes all records in the join table that feature the given recordJoinModel.storestore(self, left: Left, right: Right) -> boolAdds a mapping between the supplied Left and RightNo action is taken if this mapping already existsJoinModel.removeremove(self, left: Left, right: Right) -> boolRemoves a mapping between the supplied Left and Right.No action is taken if this mapping does not exist.