Tina4-Python ORM#
๐ฅ Hot Tips
- You never need the ORM. Raw
Databaseis perfect for 90% of cases - But when you want it... it feels like writing plain Python classes
- Zero boilerplate: no
__init__, nosave()method to write - Works with SQLite, PostgreSQL, MySQL, MSSQL, Firebird, all the same
Quick Start - 8 lines {#quick-start}#
python
from tina4_python import ORM, orm, Databasefrom tina4_python import IntegerField, StringField, DateTimeFieldโclass User(ORM): id = IntegerField(primary_key=True, auto_increment=True) name = StringField() email = StringField(unique=True) created_at = DateTimeField()โ# Connect once (usually in your main.py)orm(Database("sqlite3:test.db"))โ# That's it - table is auto-created on first useuser = User({"name": "Alice", "email": "alice@example.com"})user.save() # โ INSERTprint(user.id) # โ 1โuser.name = "Alice Wonder"user.save() # โ UPDATEโuser = User()user.load("email = ?", ["alice@example.com"])print(user.name) # โ Alice WonderField Types {#field-types}#
| Field Type | Python type | SQL example | Notes |
|---|---|---|---|
IntegerField() | int | INTEGER | |
StringField() | str | VARCHAR(255) | length=500 optional |
TextField() | str | TEXT | unlimited |
DateTimeField() | datetime | TIMESTAMP | |
NumericField() | Decimal | NUMERIC(10,2) | |
BlobField() | bytes | BLOB | |
JSONBField() | dict/list | JSON / JSONB | auto serialize/deserialize |
ForeignKeyField(...) | int | INTEGER REFERENCES table(id) | import from tina4_python.FieldTypes |
Field Options#
python
name = StringField( default="Anonymous", null=False, length=100)created_at = DateTimeField()status = IntegerField(default=1)Foreign Keys - Beautiful & Simple {#foreign-keys}#
python
from tina4_python.FieldTypes import ForeignKeyFieldโclass Post(ORM): id = IntegerField(primary_key=True, auto_increment=True) title = StringField() author_id = ForeignKeyField(references=User)Usage:
python
post = Post({"title": "Hello World", "author_id": 1})post.save()โ# Loadpost = Post()post.load("id = ?", [1])print(post.title)Core Methods {#core-methods}#
| Method | Example | What it does | Returns |
|---|---|---|---|
.save() | user.save() | INSERT or UPDATE | |
.load(where, params) | user.load("id = ?", [1]) | Load single record into instance | bool |
.select() / .fetch() | User().select() | Returns records (default limit 10) | DatabaseResult |
.fetch_one() | User().fetch_one() | Fetches one record | DatabaseResult |
.delete() | user.delete() | Delete record | |
.create_table() | User().create_table() | Auto-create table | |
.to_dict() | user.to_dict() | Convert instance to dict | dict |
Migrations - One command {#migrations}#
python
from tina4_python.Migration import migrateโmigrate(Database("sqlite3:test.db")) # creates/updates all ORM tablesFull Example - Real Project Ready {#full-example}#
python
from tina4_python import ORM, orm, Databasefrom tina4_python import IntegerField, StringField, TextField, DateTimeFieldfrom tina4_python.FieldTypes import ForeignKeyFieldโorm(Database("sqlite3:app.db")) # โ one line to rule them allโclass Category(ORM): id = IntegerField(primary_key=True, auto_increment=True) name = StringField(unique=True)โclass Article(ORM): id = IntegerField(primary_key=True, auto_increment=True) title = StringField() content = TextField() category_id = ForeignKeyField(references=Category) created_at = DateTimeField()โ# Auto-create tablesCategory().create_table()Article().create_table()โ# Use itcat = Category({"name": "Tech"})cat.save()โarticle = Article({"title": "Tina4 is awesome", "content": "Great framework", "category_id": 1})article.save()โ# Queryresult = Article().select(filter="category_id = ?", params=[1])for a in result.records: print(a["title"])Summary - The Tina4 ORM Philosophy {#summary}#
- No
__init__needed - No
save()method to write - No migration files
- No sessions
- No query builder hell
Just:
python
user = User({"name": "Bob"})user.save()โ@post("/api/users")async def post_api_users(request, response): user = User(request.body) user.save() return response(user.to_dict())