More Features
AutoDLA includes a variety of features to improve quality of life for the developer.
Let's create a base Object called User and work with it in this page to explore different concepts:
from autodla import Object, primary_key
from autodla.dbs import PostgresDB, MemoryDB
class User(Object):
id: primary_key = primary_key.auto_increment()
name: str
age: int
# MemoryDB stores everything in RAM. PostgresDB uses SQLite locally and
# syncs changes to PostgreSQL.
db = MemoryDB() # or PostgresDB()
db.attach([User])
Data Generation
AutoDLA can automatically generate data using the DataGenerator class.
Usage:
this class includes a variety ofstaticmethods that allow you to generate distinct types of data:
# Create 3 Users and save them into a list of users
users = []
for i in range(3):
usr = User.new(
name=DataGenerator.name(),
age=DataGenerator.age()
)
users.append(usr)
users variable
| id | name | age |
|---|---|---|
| ee102517-ea1b-466f-8c2a-61d49c1e7425 | Karen | 20 |
| c0e9b676-32dc-4add-b1a5-b60e464afd30 | William | 10 |
| d4c12093-756a-4438-9bf1-c7fc0cb563ed | Susan | 15 |
Data Audit by design
Tables generated by AutoDLA are transactional tables by design. This allows for more in depth data analysis without the hussle of generating and mantaining this infrastructures.
For the example we are working with, in the DB the table public.users was generated defined with the next columns :
| column_name | data_type |
|---|---|
dla_object_id |
uuid |
dla_modified_at |
timestamp |
dla_modified_by |
text |
dla_operation |
text |
dla_is_current |
boolean |
dla_is_active |
boolean |
| id | uuid |
| age | integer |
| name | text |
These dla_* fields power AutoDLA's versioning and audit features. Every object tracks its modification history automatically.
The rows currently in the table are these:
| id | name | age | dla_object_id |
dla_modified_at |
dla_modified_by |
dla_operation |
dla_is_current |
dla_is_active |
|---|---|---|---|---|---|---|---|---|
| ee102517-ea1b-466f-8c2a-61d49c1e7425 | Karen | 20 | b83e5016-acd7-476f-8b30-f19154cdbbbf | '03-04-2025 00:00:00' | SYSTEM | INSERT | True | True |
| c0e9b676-32dc-4add-b1a5-b60e464afd30 | William | 10 | a300842d-da73-41b0-bb97-998601254769 | '03-04-2025 00:00:00' | SYSTEM | INSERT | True | True |
| d4c12093-756a-4438-9bf1-c7fc0cb563ed | Susan | 15 | 81b5ba22-f381-416f-865f-997c6970d664 | '03-04-2025 00:00:00' | SYSTEM | INSERT | True | True |
but if we apply some change, its going to be applied in the table, saving the changes
Resulting in the table now being (see how a row was added and the previous last in theis_current field was modified):
| id | name | age | dla_object_id |
dla_modified_at |
dla_modified_by |
dla_operation |
dla_is_current |
dla_is_active |
|---|---|---|---|---|---|---|---|---|
| ee102517-ea1b-466f-8c2a-61d49c1e7425 | Karen | 20 | b83e5016-acd7-476f-8b30-f19154cdbbbf | '03-04-2025 00:00:00' | SYSTEM | INSERT | True | True |
| c0e9b676-32dc-4add-b1a5-b60e464afd30 | William | 10 | a300842d-da73-41b0-bb97-998601254769 | '03-04-2025 00:00:00' | SYSTEM | INSERT | True | True |
| d4c12093-756a-4438-9bf1-c7fc0cb563ed | Susan | 15 | 81b5ba22-f381-416f-865f-997c6970d664 | '03-04-2025 00:00:00' | SYSTEM | INSERT | False | True |
| 531a0f7b-fc95-4e71-90c9-ad7d7c00720d | Susan | 25 | 81b5ba22-f381-416f-865f-997c6970d664 | '03-04-2025 00:01:15' | SYSTEM | UPDATE | True | True |
This data can be accessed in 2 ways:
User.get_table_res(): returning the complete table for the model User- or for each user with the method
usr.history(): returning the complete table for the specific instance
The working memory will have always the last version of each object:
| id | name | age |
|---|---|---|
| ee102517-ea1b-466f-8c2a-61d49c1e7425 | Karen | 20 |
| c0e9b676-32dc-4add-b1a5-b60e464afd30 | William | 10 |
| d4c12093-756a-4438-9bf1-c7fc0cb563ed | Susan | 25 |
Automatic relationships
Because AutoDLA uses the Model abstraction instead of the Table abstraction (used by most ORMs), each Model can have multiple tables representing it's data. AutoDLA handles the background tables required to fullfil the specified relationship.
To show this, let's create a new Model:
class Group(Object):
id: primary_key = primary_key.auto_increment()
group_name: str
participants: list[User]
public.group: This represents the data that doesn't uses a relationshippublic.group__participants__user: This creates a table for the specific relationship with the other Model
When the program memory is updated for you to use the python objects, a JOIN clause is automatically applied in the background.
In practice, once you setup the models, you can just use them as any other Python object, AutoDLA will handle everything.
# Group creation
grp = Group.new(
group_name = 'Group 1',
participants = [
users[0]
]
)
# Group modification: add another user
grp.update(participants=grp.participants + [users[1]])
# Reference the user from the group
usr = grp.participants[0]
print(usr)
### User( name=Karen , age=20 )
# Modify user
usr.update(age=usr.age + 1)
print(users[0]) # previously defined list
### User( name=Karen , age=21 )
Filtering
AutoDLA includes a built-in method to turn lambda functions into SQL Querys, thanks to this, to filter data in AutoDLA all you need to do is to write a function for the condition you want to apply
# get by name
filtered_users = User.filter(lambda x: x.name == 'Karen')
# get by age
filtered_users = User.filter(lambda x: x.age > 18)
# use python functions
filtered_users = User.filter(lambda x: x.name.startswith('K'))
# boolean expressions
filtered_users = User.filter(lambda x: x.name == 'Karen' and x.age > 18)
In-memory object integrity
Something that others ORMs fail to accomplish is the integrity between the observed objects in the DB and the object you are working with in memory, leading to data issues down the line like duplicated values and others. AutoDLA handles this integrity, the objects you are working with, are always the same as they are in the DB.
u1 = users[0] # User ( name=Karen , age=21 )
u2 = grp.participants[0] # User ( name=Karen , age=21 )
u3 = User.filter(lambda x: x.name == 'Karen')[0] # User ( name=Karen , age=21 )
u1 == u2 # True
u1 == u3 # True
u2 == u3 # True
id(u1) == id(u2) #True
Auto-generated admin panel
AutoDLA can automatically generate an admin panel for CRUD operations, learn more about this in AutoDLA WEB