Using Modal and Nile together
We’ll demonstrate a simple example of using Modal’s serverless compute capabilities with Nile’s serverless database. All the setup steps and line-by-line explanation are shown below. The entire script is available here. To get started, you’ll need a Modal account and a Nile database. You can sign up for Modal here and for Nile here.Setting Up Nile
Once you’ve signed up for Nile, you’ll be promoted to create your first database. Go ahead and do so. You’ll be redirected to the “Query Editor” page of your new database. This is a good time to create the table we’ll be using in this example: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.
Setting Up Modal
Once you’ve signed up for Modal, you’ll be ready to start building your application. In order to connect from Modal to your Nile database, you’ll need to create a secret in Modal. Click on the “Secrets” tab and then “Create Secret”. We recommend choosing “Custom” secret type (although you can use “Postgres” as well). In the secret key, enterDATABASE_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:
Quickstart
Lets try creating and running a simple example. Just one function that runs on Modal and queries the Nile database. First, create a new file callednile_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:
Running on Modal
To run the function on Modal, you can use the following command:Full application
Now that you’ve successfully deployed your first function to Modal and used your first Nile database, let’s put them together in a full application. We’ll create a Sales Assistant application that allows you to embed and summarize sales conversations. The entire application - frontend, backend and LLM - all run on Modal. All the data and embeddings are stored in Nile.Get Started with Sales Assistant