This project is my #oneweekchallenge project.
Start: 12 February 2021
End : 18 February 2021
This is a mini orm library for python. This library uses sqlite3. Therefore, the sqlite3 is necessary.
from db_builder.fields import *
from db_builder.db_model import DB_Model
class MyPost(DB_Model):
title = CharField(length=100)
content = TextField()
created_time = DateTimeField(auto_now=True)
def __str__(self):
return self.title
# for creating table
MyPost().create_table()
# for inserting a new record
post = MyPost(title="My first post","Hello everyone. This is my first post!")
post.save()
# for updating a exist record
post = MyPost().objects().filter(title="My first post") # returns a queryset array
post[0].title = "New title of my first post"
post[0].save()
#for delete a exist record
post = MyPost().objects().filter(title="My first post") # returns a queryset array
post[0].delete()
MyPost().objects().get(post_id)
post = MyPost().objects().filter(title="My first post") # using one column
post = MyPost().objects().filter(title="My first post",content="Hello everyone. This is my first post!") # using multiple columns
- DB_Validator (parent class)
- BigLetterValidator
- EmailValidator
- LengthValidator
- NumberValidator
- SmallLetterValidator
- SpecialCharValidator
- DB_Field (parent class)
- TextField
- CharField
- DateField
- DateTimeField
- TimeField
- BooleanField
- IntegerField
- JsonField
- IDField
- URLField
- SlugField
- FileField - need config
- ImageField - need config
- FloatField
- EmailField
- ForeignKeyField
- DB_Error (parent class)
- .NotFoundKwarg
- .ModelError
- .OperationError - not exist
- .QueryError
- .FieldError
- .ValidatorError
- .SettingError - not exist
- .StructureError - not exist
- StringStructureParser (have some bugs)
structure = StringStructureParser("[email protected]","<user>@<company>.<extension>")
print( structure.parse() )
"""
Output:
{'user': 'user', 'company': 'example', 'extension': 'com'}
"""
from flask.globals import g
# flask app contruction
def connect_db():
Query = DB_Query("DB_URL")
class ob():
pass
MyPost.__Query__ = Query
ob.MyPost = MyPost
return ob
@app.before_request
def before_request():
g.db = connect_db()
# codes...
# Usage
g.db.Mypost(...) # classical usage