- Fix the model code generation
- user defines the databases via panel or config file
- Simple and fluid design
- Easy to set up
- Can work with known visualization and CI tools
- dorm creates drivers based on conf
- Tests connection strength
- Gathers meta data from databases
- drivers discovers databases based on conf
- Discovers the tables, columns and relations
- Detects manytomany relations
- Generates model code from database information
- drivers generates models and inserts itself to the models as conf
- Driver analyzes the models and generates model instances
- With a tinydb or reducer solution models can easly be found
- models
Cases to be considered when generating a model:
- PrimaryKey should be defined first (not necessary)
- Related Models should be defined before the target Model
- ForeignKeys should be replaced with the correct Model name
- ManyToMany tables should not be inclueded in code instead they should be defined using ManyToMany field
- All of the Model attributes and the Model's Node should be included in the generated docstring
from datetime import datetime
from database.drivers import Sqlite
from database import models
class Question(models.Model):
question_text = models.Char(max_length=200)
pub_date = models.DateTime()
class Choice(models.Model):
question = models.ForeignKey(Question)
choice_text = models.Char(max_length=200)
votes = models.Integer()
# Create the database file (also opens the transaction)
with Sqlite("poll.db") as db:
try:
db.create_table(Question)
db.create_table(Choice)
except Exception:
pass
# insert some data
question = Question(question_text="What is your favorite color?", pub_date=datetime.now())
question.save()
choice_1 = Choice(question=question.id, choice_text="red", votes=0)
choice_2 = Choice(question=question.id, choice_text="blue", votes=0)
choice_1.save()
choice_2.save()
# read from database
with Sqlite("poll.db") as db:
first_question = Question.select().where(id=1).first()
red_choices = Choice.select().where(choice_text="red").all()
# Get first 5 results from all databases
first_5_choices = Choice.select()[:5]
# Get first 5 results from first 5 databases
first_5_choices_dbs = Choice.select()[:5, :5]
# Get results as pandas.DataFrame
all_choices_as_df = Choice.select().as_df()
with ExitStack() as stack:
[stack.enter_context(Sqlite(db['address'])) for db in DATABASES]
# get dask distributed dataframe
ddf = Choice.select().as_distributed_df()
result = ddf.votes.value_counts().compute()
most_common_10_votes = result.sort_values()[-10:]
print(most_common_10_votes)
from datetime import datetime
from database import models
from services.restful import rest, app
# accessible with host:port/question or host:port/question/id
@rest()
class Question(models.Model):
id = models.PrimaryKey()
question_text = models.Char(max_length=200)
pub_date = models.DateTime()
# restrict http methods
@rest(without=["post", "delete", "patch"])
class Choice(models.Model):
id = models.PrimaryKey()
question = models.ForeignKey(Question)
choice_text = models.Char(max_length=200)
votes = models.Integer()
if __name__ == "__main__":
app.run()
See the LICENSE file for license rights and limitations (Mozilla Public License 2.0).