Spider-ORM is a Python ORM (Object-Relational Mapping) library designed to simplify interactions with relational databases like MySQL and SQLite. It allows you to define database models as Python classes and provides a simple interface for performing database operations such as table creation, data insertion, querying, filtering, updating, and deletion.
To install Spider-ORM, clone the repository and install the dependencies:
git clone https://github.com/your-username/spider-orm.git
cd spider-orm
pip install -r requirements.txt
Spider-ORM supports connecting to different DBMSs (Database Management Systems). Below is an example of how to configure a MySQL database connection:
from spiderweb_orm.mysql.connection import MysqlConnection
DB_CONNECTION = MysqlConnection(
host='localhost',
user='root',
password='root',
database='mysql_db'
)
Define your database models as classes that inherit from models.Model
. Each class attribute represents a field in the database table.
from spiderweb_orm import fields, models
class User(models.Model):
id = fields.IntegerField(primary_key=True, auto_increment=True)
name = fields.CharField(max_length=120, null=False)
email = fields.EmailField(max_length=255, null=False)
password = fields.PasswordField(max_length=128, null=False)
joined_on = fields.DateTimeField(auto_now=True)
image = fields.ImageField()
is_active = fields.BooleanField(default=True)
class MetaData:
rdbms = DB_CONNECTION
To create a table in the database based on a model, use the create_table()
method:
user_table = User()
user_table.create_table()
To insert new records into the table, create an instance of the model and call the save()
method:
user = User(
name='Simon Dev',
email='simondev@gmail.com',
password='mypassword',
image='img.png'
)
user.save()
To retrieve all records from a table, use the all()
method:
users = user_table.all()
Filter records using the filter()
method. Example of filtering by ID and active status:
users_filtered = user_table.filter(id__lt=20, is_active=True)
To update existing records, use the update()
method:
user_table.update(email='newemail@gmail.com', id=10)
To delete records, use the delete()
method:
user_table.delete(id=1)
Tests can be run using pytest
. To ignore failures and continue running tests, use the --continue-on-collection-errors
flag or mark tests that may fail with pytest.mark.xfail
.
pytest --continue-on-collection-errors
Or, within the test code:
import pytest
@pytest.mark.xfail
def test_func():
assert False # This failure will be ignored
This project is licensed under the MIT License.