From 2e7badab4e44f7c629664aa37eec69f0f62308e1 Mon Sep 17 00:00:00 2001 From: Szucs Krisztian Date: Fri, 5 May 2017 08:19:09 +0200 Subject: [PATCH 1/3] aiopeewee example --- examples/sanic_aiopeewee.py | 63 +++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 examples/sanic_aiopeewee.py diff --git a/examples/sanic_aiopeewee.py b/examples/sanic_aiopeewee.py new file mode 100644 index 00000000..9cd22bed --- /dev/null +++ b/examples/sanic_aiopeewee.py @@ -0,0 +1,63 @@ +from sanic import Sanic +from sanic.response import json + +from aiopeewee import AioModel, AioMySQLDatabase +from peewee import CharField, TextField, DateTimeField +from peewee import ForeignKeyField, PrimaryKeyField +from playhouse.shortcuts import model_to_dict + + +db = AioMySQLDatabase('test', user='root', password='', + host='127.0.0.1', port=3306) + + +class User(AioModel): + username = CharField() + + class Meta: + database = db + + +class Blog(AioModel): + user = ForeignKeyField(User) + title = CharField(max_length=25) + content = TextField(default='') + pub_date = DateTimeField(null=True) + pk = PrimaryKeyField() + + class Meta: + database = db + + +app = Sanic(__name__) + + +@app.listener('before_server_start') +async def setup(app, loop): + # create connection pool + await db.connect(loop) + # create table if not exists + await db.create_tables([User, Blog], safe=True) + + +@app.listener('before_server_stop') +async def stop(app, loop): + # close connection pool + await db.close() + await to_asyncio_future(app.client._shutdown()) + + +@app.post('/users') +async def add_user(request): + user = await User.create(**request.json) + return json(model_to_dict(user)) + + +@app.get('/users/count') +async def user_count(request): + count = await User.select().count() + return json({'count': count}) + + +if __name__ == '__main__': + app.run(host="0.0.0.0", port=8000) From d24e1ae1106335e50a6c6c0c168f6f927ed9b192 Mon Sep 17 00:00:00 2001 From: Szucs Krisztian Date: Fri, 5 May 2017 08:25:18 +0200 Subject: [PATCH 2/3] removed lines from distributed example --- examples/sanic_aiopeewee.py | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/sanic_aiopeewee.py b/examples/sanic_aiopeewee.py index 9cd22bed..345231a4 100644 --- a/examples/sanic_aiopeewee.py +++ b/examples/sanic_aiopeewee.py @@ -44,7 +44,6 @@ async def setup(app, loop): async def stop(app, loop): # close connection pool await db.close() - await to_asyncio_future(app.client._shutdown()) @app.post('/users') From a42b254c33292407ed9285ff59af10c8974dbbcc Mon Sep 17 00:00:00 2001 From: Szucs Krisztian Date: Fri, 5 May 2017 10:51:12 +0200 Subject: [PATCH 3/3] use async version of model_to_dict --- examples/sanic_aiopeewee.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/examples/sanic_aiopeewee.py b/examples/sanic_aiopeewee.py index 345231a4..53146862 100644 --- a/examples/sanic_aiopeewee.py +++ b/examples/sanic_aiopeewee.py @@ -1,10 +1,9 @@ from sanic import Sanic from sanic.response import json -from aiopeewee import AioModel, AioMySQLDatabase +from aiopeewee import AioModel, AioMySQLDatabase, model_to_dict from peewee import CharField, TextField, DateTimeField from peewee import ForeignKeyField, PrimaryKeyField -from playhouse.shortcuts import model_to_dict db = AioMySQLDatabase('test', user='root', password='', @@ -49,7 +48,7 @@ async def stop(app, loop): @app.post('/users') async def add_user(request): user = await User.create(**request.json) - return json(model_to_dict(user)) + return json(await model_to_dict(user)) @app.get('/users/count')