Tormeti is an ORM library for Python. It's still in early development, but we plan to eventually support SQLite, MySQL, and PostgreSQL.
TODO: add instructions for installation.
To use Tormeti, you need to define your database models as subclasses of the Model class. Each model represents a table in the database, and each attribute of the model represents a column in the table. You can use the Field class to specify the type of each column.
Here's an example:
from orm import Model, Field
class User(Model):
username = Field(str)
email = Field(str)
age = Field(int)
This defines a User model with four columns: id (an integer primary key which is automatically defined on all models), username (a string), an email (a string) and an age (integer).
To interact with the database, you first need to create a database object using one of the supported database URLs:
from orm import SQLiteDatabase
db = SQLiteDatabase('sqlite://:memory:')
This creates an SQLite database in memory.
You can then create the table corresponding to your model using the create_table
method (or create_tables
for multiple):
db.create_table(User)
# or
db.create_tables([User, ...])
This generates and executes the SQL statement necessary to create the User table in the database.
You can then create new instances of your model and save them to the database:
user = User(
id=1,
username='john',
email='[email protected]',
)
user.save()
This creates a new row in the User table with the specified values.
You can retrieve instances from the database using the get method:
user = User.get(id=1)
This retrieves the User instance with the specified id value.
You can also update and delete instances using the save and delete methods:
user.username = 'jane'
user.save()
user.delete()
Database | Supported |
---|---|
SQLite | ✅ (partially) |
MySQL | ❌ |
PostgreSQL | ❌ |
Contributions are welcome! If you find a bug or have an idea for a new feature, please open an issue on GitHub. If you would like to contribute code, please open a pull request with your changes.
This project is licensed under the MIT License - see the LICENSE file for details.