Serve a REST API from a function backed by SQL Database
Deploy one function that serves every endpoint of a REST API, reads SQL Database through a replica, and caches the list response.
You deploy one function that serves every endpoint of a REST API under /api/tasks with the Azion CLI: it reads the rows from SQL Database through a read replica, writes them through the Azion API, and keeps the list response in cache for 60 seconds. To add writes to a function you already run, refer to Write rows to SQL Database from a function.
Prerequisites
- SQL Database enabled on your account, and the Edit SQL Database permission. The product is in Preview, so request access through Technical Support.
- A database named
tasks-api, its identifier, and thetaskstable with two rows. The identifier is theidthe create response returns. To create them, refer to Create a database using the API and Create tables and query data. - The database identifier and a personal token stored as the environment variables
TASKS_DB_IDandTASKS_SQL_TOKEN, as Store the database identifier and the token shows. A variable reaches the function only after a deploy, so create both before the deploy. - Azion CLI installed, with your personal token saved. To set it up, refer to Azion CLI quickstart.
- Node.js and a package manager, which the CLI uses to build the project.
The function reads a table created with CREATE TABLE IF NOT EXISTS tasks (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL, completed INTEGER NOT NULL DEFAULT 0);, holding the rows INSERT INTO tasks (title, completed) VALUES ('Write the API spec', 0); and INSERT INTO tasks (title, completed) VALUES ('Deploy the API', 1); add. completed is an integer column, 0 or 1, because the function reads it as a number. The examples call the API on api.example.com. Replace it with the domain the deploy prints, and the names with your own.
Write the API function
The function is one ES Modules handler that implements every endpoint under /api/tasks. It reads with the Database class of the Azion.Sql global, sends each write to the query endpoint of the Azion API with TASKS_DB_ID and TASKS_SQL_TOKEN, and stores the list response in the cache tasks-api under max-age=60. Every POST and DELETE deletes that stored list after its write.
To create the project and add the function:
Run azion init, enter tasks-api as the name, and select the Javascript preset and the Hello World template, as Deploy a function with Azion CLI shows. Then go to the project directory.
Replace the contents of index.js, the entrypoint the build reads, with the code below.
The project’s index.js now holds the API function. A path outside /api/tasks answers 404, a method the function does not handle answers 405, and a failed write answers 500 and logs its message.
The Build REST and GraphQL APIs use case uses the values of this example.
Deploy the API
One function serves every endpoint, so one deploy ships them all together. To deploy the API, run azion deploy from the project directory. The command builds the project, creates the application and the function, instantiates the function, and prints the domain that serves it. For the output of the command, refer to Deploy the project.
The API answers on the domain the deploy printed, every endpoint runs in the function, and the list response is cached for 60 seconds. The first deploy can take several minutes to answer from every location.
The Build REST and GraphQL APIs use case uses the values of this example.
Confirm the API answers
Each check calls the API on its domain. A first deploy that does not answer yet is still propagating; wait a few minutes and retry.
-
The list endpoint reads the database. Request the list:
The response carries
200and the two rows the table holds: -
The list comes from the cached copy. Repeat the request within 60 seconds. The
x-tasks-cached-atheader carries the same value as in the first response. -
A write reaches the database. Create a task:
ShellThe response carries
201and the ID the database assigned:A
500here means the write failed. Read the message the function logged, as Query function console logs shows:Azion API answered 401points at theTASKS_SQL_TOKENvalue, and a database error string points at the statement. -
A write refreshes the list. Request the list again. The response carries a new
x-tasks-cached-atvalue and three tasks. -
A missing task answers 404. Delete the same task twice:
The first call answers
{"message":"Task deleted"}, and the second answers404with{"error":"Task not found"}.
The API reads, writes, and deletes tasks, and serves the list from its cached copy between writes.
These checks confirm the Build REST and GraphQL APIs use case.