tenants
table that is built-in to Nile.
Next, you’ll want to pick up your database connection string: Navigate to the “Settings” page, select “Connections” and click “Generate credentials”.
Copy the connection string and keep it in a secure location. You’ll need it to connect to your database from Modal.
DATABASE_URL
. In the secret value, enter the connection string you picked up in the previous step.
Make sure you leave out the psql
and replace postgres://
with postgresql://
. When you save the secret, call it my-nile-secret
.
Next, you’ll want to install modal and set it up:
nile_modal_quickstart.py
.
We’ll start by setting up the Modal app and function. You can see that we are using a pre-existing image that Modal provides,
and we are installing psycopg2
which is a Postgres client for Python. Then we create the modal app.
show_todos
is a helper function that will query simply return all todos in the database with their respective tenant name.
You can see that once we connect to the virtual tenant database, the same query will only show the todos for the tenant we connected to.
Create the helper functions create_tenant
, create_todo
and show_todos
and you’re ready to run the function: