Skip to main content

simple orm, to manage your database.

Project description

My ORM

It's a simple ORM built with Python and sqlite.
This is not a real ORM to use in your project.

Use My ORM

You can see sample codes in examples.py.
I will be glad if you cooperate in the development of this project 😊

Install My ORM

pip install dori-orm

Imports

Import DB, ResultConfig, operators and columns from the dori-orm library.
DB: For create classes and database table.
ResultConfig: Change the result, e.g. ordering and limit.
operators: SQL operators like AND, OR, NOT
columns: To create table columns with specific data type and constraints. For example age = columns.Integer(nullable=False) means age INTEGER NOT NULL

from dori_orm import DB, ResultConfig
from dori_orm.operators import AND, OR, NOT
from dori_orm import columns

Create Table

Create class and inheritance from DB, tables created automatically in database(set db name with file name) when you inheritance from DB, you can define class variable and use columns to create table column.

class Person(DB):
    name = columns.Text(nullable=False)
    family = columns.Text(nullable=False)
    age = columns.Integer()
    phone = columns.Integer()
    salary = columns.Real(default=100_000)


class School(DB):
    name = columns.VarChar(nullable=False, unique=True)
    created_at = columns.Date()
    address = columns.Text()
    students_count = columns.SmallInt(default=300)


class Student(DB):
    person = columns.ForeignKey(Person)
    school = columns.ForeignKey(School)
    class_name = columns.VarChar()

Insert Data

Insert data to table with create instance from class. this code create two row in database with this arguments.

person1 = Person(
    name='Mohammad',
    family='Dori',
    age=20,
    phone=1234567890,
    salary=110_000,
)
print(person1.age)  # 20

person2 = Person(
    name='John',
    family='Gits',
    age=30,
    phone=1234567890,
)

Select Data

Select rows from table.
all method select all rows from table.
get method, select all rows with defined columns. filter method select all rows that match the given conditions.

print(Person.all())
# Result:
[
    <'id':1, 'name':'Mohammad', 'family':'Dori', 'age':20, 'phone':1234567890, 'salary':110000.0>,
    <'id':2, 'name':'John', 'family':'Gits', 'age':30, 'phone':1234567890, 'salary':100000.0>
]
print(Person.get('id', 'name', 'family'))
# Result:
[
    <'id':1, 'name':'Mohammad', 'family':'Dori'>,
    <'id':2, 'name':'John', 'family':'Gits'>
]
print(Person.filter(id=1, name='Mohammad'))
# Result:
[
    <'id':1, 'name':'Mohammad', 'family':'Dori', 'age':20, 'phone':1234567890, 'salary':110000.0>
]

Advance Filtering

You can use operators in filter method. like AND, OR, NOT, BETWEEN, LIKE, IN, =, !=, <, <=, >, >=.

AND: e.g. AND(x='', y=123) that means x='' AND y=123

rows = Person.filter(
    AND(id=2, name='Ali')
)

OR: e.g. OR(x='', y=123) that means x='' OR y=123

rows = Person.filter(
    OR(id=2, name='Ali')
)

NOT: e.g. NOT(OR(x='', y=123)) that means NOT (x='' OR y=123)

rows = Person.filter(
    NOT(AND(id=2, name='Ali'))
)

You can use another operator in operator.

rows = Person.filter(
    OR(OR(name='Ali', id=2), OR(salary=10, age=20))
)

BETWEEN: Return row, if it value between x and y.

print(Person.filter(id__between=(2, 8)))

LIKE: Use pattern with % and _ to filter rows.

print(Person.filter(
    name__like='Mo%',
    config=ResultConfig(
        limit=2,
        order_by='age',
    )
))

lt: less than, means <

print(Person.filter(id__lt=5))

lte: less than or equal, means <=

print(Person.filter(id__lte=5))

gt: greater than, means >

print(Person.filter(id__gt=5))

gte: greater than or equal, means >=

print(Person.filter(id__gte=5))

not: not equal, means !=

print(Person.filter(id__n=5))

You can use any filter together.

print(Person.filter(
    OR(
        id__n=5,
        name__in=('Mohammad', 'Salar'),
        age__gte=8
    )
))

Result Methods

result.count() return count of results.
result.first() return first row in result.
result.last() return last row in result.

not_mohammad = Person.filter(name__n='Mohammad')
print(not_mohammad.count())
print(not_mohammad.first())
print(not_mohammad.last())

Iterate on result.

for row in not_mohammad:
    print(row.name)
    row.remove()
    # row.update(...)

Update Row

person1 = Person(
    name='Mohammad',
    family='Dori',
    age=20,
    phone=1234567890,
    salary=110_000,
)

print(person1)
person1.update(name='Salar')
print(person1)

Table Class Method

max: Return maximum value of column.

print(Person.max('salary'))

min: Return minimum value of column.

print(Person.min('salary'))

sum: Return sum of column values.

print(Person.sum('salary'))

avg: Return average of column values.

print(Person.avg('salary'))

count: Return count of rows in table.

print(Person.count())

first: Return first row of table.

print(Person.first())

last: Return last row of table.

print(Person.last())

Result configuration

limit Limit the number of result rows.
order_by Order result by columns.
reverse Use with order_by, False means sort ASC and True means sort DESC.

print(Person.all(
    config=ResultConfig(
        order_by='id',
        reverse=True
    )
))
print(Person.get(
    config=ResultConfig(
        limit=5
    )
))

Foreign Key

person1 = Person(
    name='Mohammad',
    family='Dori',
    age=20,
    phone=1234567890,
    salary=110_000,
)
school1 = School(
    name='The Sample School',
    created_at='2002-01-04',
    address='1600 Amphitheatre Parkway in Mountain View, California',
)

print(school1)

student = Student(
    person=person1,
    school=school1,
    class_name='A3',
)

print(school1.id)
print(person1.id)

print(student)

Change Easy

Remove class_name column and add gpa column. now add a row to table.

class Student(DB):
    person = columns.ForeignKey(Person)
    school = columns.ForeignKey(School)
    gpa = columns.TinyInt(default=20)

person1 = Person(
    name='Mohammad',
    family='Dori',
    age=20,
    phone=1234567890,
    salary=110_000,
)
school1 = School(
    name='The Sample School',
    created_at='2002-01-04',
    address='1600 Amphitheatre Parkway in Mountain View, California',
)

print(school1)

student = Student(
    person=person1,
    school=school1,
    gpa=10,
)

print(school1.id)
print(person1.id)

print(student)

See All Query Usage

print(Person.queries())

Links

Download Source Code: Click Here

My Github Account: Click Here

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dori-orm-4.6.1.tar.gz (10.1 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page