Skip to content

Basic Usage

AutoDLA works with models, to start, you'll need to first build a usable model that inherits from Object:

from autodla import Object, primary_key

class User(Object):
    id: primary_key = primary_key.auto_increment()
    name: str
    age: int

WARNING: For model definition there is 1 rule to ensure good data integrity:

  • Each Model should have one and only one field of type primary_key (id in this case)

If you try to use this, it will fail, as the main focus of the library is to interact with a DataBase, you need a DataBase connection, we'll use PostgreSQL for this example.

pip install autodla[db-postgres] #install db connector

We need to instanciate the DataBase and then attach the Model into it.

from autodla.dbs import PostgresDB, MemoryDB

# MemoryDB is purely in-memory. PostgresDB keeps a local SQLite store and
# automatically syncs it to PostgreSQL.
db = MemoryDB()  # or PostgresDB()
db.attach([User])

Done!

You now can use your object as you would normally and the changes are going to be reflected on the DataBase, enjoy!


Uses

Create a user

user = User.new(name="John", age=30)

Retrieve all users

users = User.all(limit=None, skip=0)

Integrity of python id for the percieved same object

print(id(user) === id(users[-1]))
# This prints True

This example is available in the repository