-
Notifications
You must be signed in to change notification settings - Fork 91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Instructions for integrating piccolo inside existing application? #506
Comments
I'll add this to the docs at some point. I'd recommend creating a new project using You can then copy over the relevant bits to your existing project. Alternatively, doing it from scratch, you'll need to do the following: Create a new Piccolo project fileCreate a new piccolo project new This contains your database details, and is used to register Piccolo apps. Create a new appThe app contains your tables and migrations. Run this at the root of your project: # Replace 'my_app' with whatever you want to call your app
piccolo app new my_app Register this new appRegister this new app in APP_REGISTRY = AppRegistry(
apps=[
"my_app.piccolo_app",
]
) While you're at it, make sure the database credentials are correct in Make Piccolo tables for your current databaseNow, if you run: piccolo schema generate It will output Piccolo tables for your current database. Copy the output into In from piccolo.conf.apps import AppConfig, table_finder
APP_CONFIG = AppConfig(
...
table_classes=table_finder(["my_app.tables"], exclude_imported=True),
) Create an initial migrationpiccolo migrations new my_app --auto These tables already exist in your database, as it's an existing project, so you need to fake apply this initial migration: piccolo migrations forwards my_app --fake Using the Piccolo tables in your appNow you're basically setup - to make database queries: from my_app.tables import MyTable
async def my_blacksheep_endpoint():
data = await MyTable.select()
return data Making new migrationsJust modify the files in
I hope that helps. |
Thank you Dan for those instructions. Will give them a try. One question on piccolo_conf.py. https://github.com/Neoteroi/essentials-configuration is a library from the blacksheep creator.
The above code reads config from config.yaml, overwrites some of those config from config.${APP_ENVIRONMENT}.yaml and other Environment variables. That way, we can have different db credentials for dev, qa and prod environments. Is there any way to make this possible with piccolo_conf.py? Thanks |
Yes, it's possible. I haven't used import os
from dotenv import load_dotenv
from piccolo.engine.postgres import PostgresEngine
# Loads environment variables from a .env file.
load_dotenv()
DB = PostgresEngine(
config={
"port": int(os.environ.get("PG_PORT", "5432")),
"user": os.environ.get("PG_USER", "postgres"),
"password": os.environ.get("PG_PASSWORD", ""),
"database": os.environ.get("PG_DATABASE", "my_project"),
"host": os.environ.get("PG_HOST", "localhost"),
}
) |
That did give me an idea.
I would have to call load_configuration() twice , once here and once in the app, but I guess still works. Thanks |
We are currently using asyncpg in a blacksheep application. Since piccolo is based on top of asyncpg and provides migration support, we want to migrate to piccolo.
But the only instructions available, are for creating new project.
It would be great if some documentation could be provided on integrating into existing projects.
Thanks
The text was updated successfully, but these errors were encountered: