Skip to content

mitrofun/weakorm

Repository files navigation

WeakORM

Build Status Scrutinizer Code Quality

Simple ORM for sqlite.

Installation

pip3 install git+https://github.com/mitrofun/weakorm

Use

An example of using work with ORM can be found in the file example.py

Create database

To create a database, run

from weekorm.db import DataBase
db = DataBase('example.sqlite')

Creating database models

Create a class inherited from Model

from weekorm import model

class User(model.Model):
    name = model.CharField(max_length=20)
    email = model.CharField(max_length=40, unique=True)
    is_admin = model.BooleanField(default=False)

The following types of fields exist in ORM

  • CharField - Text field, max_length parameters-maximum field size, default 255.The default value ".
  • IntegerField - Numeric integer field, default value 0
  • FloatField - Numeric floating-point field field, default 0.0
  • BooleanField - Boolean field, default False
  • DateTimeField - Field with date and time.Example of use.
from weekorm import model
from datetime import datetime
  
class Article(model.Model):
    title = model.CharField(max_length=100)
    ....
    created = model.DateTimeField(default=datetime.now())
  • ForeignKey - Field with a foreign key.Example of use.
from weekorm import model
  
class User(model.Model):
    name = model.CharField(max_length=20)
    email = model.CharField(max_length=40, unique=True)
    birthday = model.DateTimeField()

    def __str__(self):
        return self.name

class Staff(model.Model):
    user = model.ForeignKey(User)
    position = model.CharField(max_length=40)

    def __str__(self):
        return f'{self.position} - {self.user.name}'

Saving data to database

user = User(
    name='Mik',
    email='mik@gmail.com',
    birthday=datetime(year=1983, month=12, day=6)
)
user.save()
staff = Staff(user=user, position='Tester')
staff.save()

Select data

staff = Staff.query().filter(position='Tester').first()

Update data

staff.position = 'Developer'
staff.save()

Update data in query

You can also update the data for a set of features that meet the selection condition.

Staff.query().filter(position='Tester').update(position='QA')

Develop

Local

To develop locally, install the dependencies

pip3 install -r requirements/develop.txt

Running tests

pytest

Docker

You don't need to install dependencies locally to run tests in Docker.Just run the command

make docker-build && make docker-test

Requirements

  • python 3.6+

Contributors

License

weakorm is released under the MIT License. See the LICENSE file for more details.

About

Simple ORM for sqlite

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published