@@ -35,12 +35,17 @@ from commondao.annotation import TableId
3535from pydantic import BaseModel
3636from typing import Annotated
3737
38- # Define your Pydantic model with TableId annotation
38+ # Define your Pydantic models with TableId annotation
3939class User (BaseModel ):
4040 id : Annotated[int , TableId(' users' )] # First field with TableId is the primary key
4141 name: str
4242 email: str
4343
44+ class UserInsert (BaseModel ):
45+ id : Annotated[Optional[int ], TableId(' users' )] = None # Optional for auto-increment
46+ name: str
47+ email: str
48+
4449async def main ():
4550 # Connect to database
4651 config = {
@@ -54,7 +59,7 @@ async def main():
5459
5560 async with connect(** config) as db:
5661 # Insert a new user using Pydantic model
57- user = User( id = 1 , name = ' John Doe' , email = ' john@example.com' )
62+ user = UserInsert( name = ' John Doe' , email = ' john@example.com' )
5863 await db.insert(user)
5964
6065 # Query the user by key with Pydantic model validation
@@ -91,30 +96,35 @@ from pydantic import BaseModel
9196from commondao.annotation import TableId
9297from typing import Annotated, Optional
9398
94- class User (BaseModel ):
99+ class UserInsert (BaseModel ):
95100 id : Annotated[Optional[int ], TableId(' users' )] = None # Auto-increment primary key
96101 name: str
97102 email: str
98103
99104# Insert using Pydantic model (id will be auto-generated)
100- user = User (name = ' John' , email = ' john@example.com' )
105+ user = UserInsert (name = ' John' , email = ' john@example.com' )
101106await db.insert(user)
102107print (f " New user id: { db.lastrowid()} " ) # Get the auto-generated id
103108
104109# Insert with ignore option (skips duplicate key errors)
105- user2 = User (name = ' Jane' , email = ' jane@example.com' )
110+ user2 = UserInsert (name = ' Jane' , email = ' jane@example.com' )
106111await db.insert(user2, ignore = True )
107112```
108113
109114### Update Data (with Pydantic Models)
110115
111116``` python
117+ class UserUpdate (BaseModel ):
118+ id : Optional[int ] = None
119+ name: Optional[str ] = None
120+ email: Optional[str ] = None
121+
112122# Update by primary key (id must be provided)
113- user = User (id = 1 , name = ' John Smith' , email = ' john.smith@example.com' )
123+ user = UserUpdate (id = 1 , name = ' John Smith' , email = ' john.smith@example.com' )
114124await db.update_by_id(user)
115125
116- # Update by custom key (partial update - only non-None fields)
117- user_update = User (name = ' Jane Doe' , email = ' jane.doe@example.com' ) # id can be None
126+ # Update by custom key (partial update - only specified fields)
127+ user_update = UserUpdate (name = ' Jane Doe' , email = ' jane.doe@example.com' )
118128await db.update_by_key(user_update, key = {' email' : ' john.smith@example.com' })
119129```
120130
@@ -294,23 +304,23 @@ result = await db.execute_query(
294304from commondao.annotation import TableId
295305from typing import Annotated, Optional
296306
297- class Order (BaseModel ):
307+ class OrderInsert (BaseModel ):
298308 id : Annotated[Optional[int ], TableId(' orders' )] = None
299309 customer_id: int
300310 total: float
301311
302- class OrderItem (BaseModel ):
312+ class OrderItemInsert (BaseModel ):
303313 id : Annotated[Optional[int ], TableId(' order_items' )] = None
304314 order_id: int
305315 product_id: int
306316
307317async with connect(host = ' localhost' , user = ' root' , db = ' testdb' ) as db:
308318 # Start transaction (autocommit=False by default)
309- order = Order (customer_id = 1 , total = 99.99 )
319+ order = OrderInsert (customer_id = 1 , total = 99.99 )
310320 await db.insert(order)
311321 order_id = db.lastrowid() # Get the auto-generated order id
312322
313- item = OrderItem (order_id = order_id, product_id = 42 )
323+ item = OrderItemInsert (order_id = order_id, product_id = 42 )
314324 await db.insert(item)
315325
316326 # Commit the transaction
0 commit comments