A Python object relational mapper for SQLite.
Following a basic tutorial to demonstrate how to use the ORM.
-
Define a Post
model in a post.py
file.
from orm import Model
class Post(Model):
text = str
def __init__(self, text):
self.text = text
-
Import Database
to create a data access object.
>>> from orm import Database
>>> db = Database('db.sqlite')
-
Import the Post
model and link it to the database.
>>> from post import Post
>>> Post.db = db
-
Create a post and save it in the staging area (without commit) of database.
>>> post = Post('Hello World').save()
>>> print(post.id)
1
-
Change the hello world post and update it in the database.
>>> post = Post.manager().get(id=1)
>>> post.text = 'Hello Mundo'
>>> post.update()
>>> post.text
Hello Mundo
-
Commit all staged operations (save
and update
) to the database.
>>> db.commit()
-
Delete the object and commit.
>>> post.delete()
>>> db.commit()
-
Create a manager that can perform CRUD operations in the database.
>>> objects = Post.manager(db)
-
Save and get a post.
>>> objects.save(Post('Hello', 'World'))
>>> objects.get(2)
{'text': 'World', 'id': 2, 'title': 'Hello'}
-
Close the database without commit the changes
>>> db.close()
-
Get all posts from database.
>>> list(objects.all())
[]
The MIT License.