SQL
Azion Lib functions of the @aziontech/sql package that create, list, read, and delete SQL Database databases and run statements on them.
The @aziontech/sql package is the Azion Lib library for SQL Database. It creates, lists, reads, and deletes databases, and it runs SQL statements on a database you name, through Azion API v4. Its functions take positional arguments, and every function reports a failure inside the object it returns.
Install the package:
Every sample on this page is an ES module that uses top-level await and runs in Node.js. The TypeScript samples bring in types with import type, so they still load after their type annotations are stripped.
Authentication
Each function takes your personal token from the AZION_TOKEN environment variable. To pass the token in code, create a client with createClient and set its token field.
| Variable | Description |
|---|---|
AZION_TOKEN | Your Azion personal token. |
AZION_DEBUG | With true, the functions log the response bodies the API returns. |
A .env file with both variables looks like this:
For how every Azion Lib package reads the token and the debug setting, refer to How Azion Lib works.
Response envelope
Each function resolves to an AzionDatabaseResponse, { data?, error? }. A call that succeeds fills data. A call that fails fills error with { message, operation }, where operation names the request that failed, such as post database or apiQuery.
A statement that fails inside useQuery also fails the call: error holds the database error, such as no such table: nope, and data is unset.
Two results differ from that shape:
- deleteDatabase fills
datawith{ state: 'pending' }only. The envelope carries noid. - getDatabase returns an empty object,
{}, for a name that matches no database. Neitherdatanorerroris set.
A statement run with useExecute resolves to this envelope. The results array holds one entry per statement:
Output:
The query result leaves out rows_read, rows_written, and query_duration_ms, which the Azion API returns for each statement. To meter consumption, read those fields from the API. For more information, refer to Databases and queries.
createClient
Creates a client that holds a token and request options. createClient is also the default export of the package.
| Parameter | Type | Required | Description |
|---|---|---|---|
token | string | No | Your Azion personal token. |
options | AzionClientOptions | No | Request options for every call the client makes. |
Returns an AzionSQLClient with four methods: createDatabase, deleteDatabase, getDatabase, and getDatabases. They take the same arguments as the functions of the same name, without options. The client runs no statements: use useQuery, useExecute, or the database methods for that. For one client that covers every Azion Lib module, refer to Client.
This sample creates a client and a database with it:
Output:
createDatabase
Creates a database. The function returns while the database is still being provisioned, so its status reads creating. The status changes to created within seconds; read it with getDatabase or getDatabases.
| Parameter | Type | Required | Description |
|---|---|---|---|
name | string | Yes | The name of the database. For the characters and length a name accepts, refer to Database names. |
options | AzionClientOptions | No | Request options. |
Returns data as the created AzionDatabase. An account holds a limited number of databases; past that number, the call fills error with the message listed in Errors.
Output:
getDatabase
Returns one database by its name.
| Parameter | Type | Required | Description |
|---|---|---|---|
name | string | Yes | The name of the database. |
options | AzionClientOptions | No | Request options. |
Returns data as an AzionDatabase, with the database methods. The function searches the databases of the account by the name and returns the first match. A name that matches no database returns {}: data and error are both unset, so the sample’s else branch logs undefined for error.
Output:
getDatabases
Lists the databases of the account, one page at a time.
| Parameter | Type | Required | Description |
|---|---|---|---|
params | AzionDatabaseCollectionOptions | No | Pagination, search, and ordering. |
options | AzionClientOptions | No | Request options. |
Returns data as an AzionDatabaseCollections: databases holds the page, and count holds the number of databases.
Output:
deleteDatabase
Deletes a database by its ID. The deletion is asynchronous: the API accepts the request, and the database leaves the list within seconds. Deletion is permanent, and the rows the database held cannot be recovered.
| Parameter | Type | Required | Description |
|---|---|---|---|
id | number | Yes | The ID of the database to delete. |
options | AzionClientOptions | No | Request options. |
Returns data as an AzionDatabaseDeleteResponse, { state: 'pending' }. The response carries no id, so the sample logs the ID it passed.
Output:
useExecute
Runs SQL statements, such as an INSERT or a CREATE TABLE, on the database you name.
| Parameter | Type | Required | Description |
|---|---|---|---|
name | string | Yes | The name of the database. |
statements | string[] | Yes | The SQL statements to run, in order. |
options | AzionClientOptions | No | Request options. |
Returns data as an AzionDatabaseQueryResponse. Read state from data, not from the envelope: data.state is executed after a successful run. The function looks the database up by its name first, then sends the statements.
This sample inserts a row into a users table with id and name columns:
Output:
Write string literals in single quotes inside a statement, as the sample does.
useQuery
Runs SQL queries, such as a SELECT, on the database you name, and returns the rows they read.
| Parameter | Type | Required | Description |
|---|---|---|---|
name | string | Yes | The name of the database. |
statements | string[] | Yes | The SQL statements to run, in order. |
options | AzionClientOptions | No | Request options. |
Returns data as an AzionDatabaseQueryResponse. Each statement has one entry in data.results, in the order of statements. The rows of the first statement are in data.results[0].rows, and its column names are in data.results[0].columns. data.toObject() returns the same rows as objects keyed by column name.
Output:
getTables
Lists the tables of a database by running PRAGMA table_list on it.
| Parameter | Type | Required | Description |
|---|---|---|---|
databaseName | string | Yes | The name of the database. |
options | AzionClientOptions | No | Request options. |
Returns data as an AzionDatabaseQueryResponse. data.results[0] holds one row per table, with the columns schema, name, type, ncol, wr, and strict. The table name is the second value of each row. The list includes the SQLite tables sqlite_schema and sqlite_temp_schema.
The database that getDatabase returns also carries getTables, as a method that takes no name. This sample reads a database, then lists its tables with that method:
Output:
Database methods
The database that getDatabase returns carries three methods that act on that database. They return the same envelope as the matching functions and take no database name:
| Method | Arguments | Matching function |
|---|---|---|
query | statements: string[], options?: AzionClientOptions | useQuery |
execute | statements: string[], options?: AzionClientOptions | useExecute |
getTables | options?: AzionClientOptions | getTables |
This sample inserts and counts rows through the methods, then lists the tables with the standalone getTables function:
Output:
Errors
A failed call returns one of these messages in error.message, and error.operation names the request.
| Message | Cause | What to do |
|---|---|---|
Database <name> not found | The name passed to useQuery matches no database in the account. error.operation is apiQuery. | Check the name with getDatabases. |
no such table: <name> | A statement names a table the database does not hold. error.operation is apiQuery. | Check the table names with getTables. |
The maximum number of databases has been reached. | The account already holds the maximum number of databases. The API answers 403, and error.operation is post database. | Delete a database you no longer need with deleteDatabase. For the number each plan allows, refer to Limits per plan. |
A name that matches no database does not reach this table when you call getDatabase: the function returns {}, with no error.
Types
The package exports the types below. Import them with import type.
AzionSQLClient
The client that createClient returns.
| Method | Arguments | Returns |
|---|---|---|
createDatabase | name: string | Promise<AzionDatabaseResponse<AzionDatabase>> |
deleteDatabase | id: number | Promise<AzionDatabaseResponse<AzionDatabaseDeleteResponse>> |
getDatabase | name: string | Promise<AzionDatabaseResponse<AzionDatabase>> |
getDatabases | params?: AzionDatabaseCollectionOptions | Promise<AzionDatabaseResponse<AzionDatabaseCollections>> |
AzionClientOptions
Request options that every function takes in options, and that createClient applies to all its calls.
| Property | Type | Required | Description |
|---|---|---|---|
debug | boolean | No | Logs the response bodies the API returns. |
force | boolean | No | Forces the operation, even when it can destroy data. |
env | AzionEnvironment | No | The environment the calls go to. |
external | boolean | No | Forces the REST API instead of the API built into the runtime. |
AzionEnvironment
The environment a call goes to.
AzionDatabaseResponse
The envelope every function returns. For how to read it, refer to Response envelope.
| Property | Type | Required | Description |
|---|---|---|---|
data | T | No | The result of the call. |
error | AzionSQLError | No | The error of a failed call. |
AzionSQLError
The error a failed call returns in error.
| Property | Type | Required | Description |
|---|---|---|---|
message | string | Yes | The error message. |
operation | string | Yes | The request that failed. |
metadata | Record<string, unknown> | No | Additional details about the error. |
AzionDatabase
A database. The package also exports its fields alone as Database.
| Property | Type | Required | Description |
|---|---|---|---|
id | number | Yes | The ID of the database. |
name | string | Yes | The name of the database. |
status | 'creating' | 'created' | 'deleting' | Yes | The provisioning state of the database. |
active | boolean | Yes | Whether the database is active. |
lastModified | string | Yes | When the database last changed. |
lastEditor | string | null | Yes | The account that last changed the database. |
productVersion | string | Yes | The product version. |
query, execute, getTables | functions | Yes | The database methods. |
AzionDatabaseCollections
A page of databases.
| Property | Type | Required | Description |
|---|---|---|---|
databases | AzionDatabase[] | No | The databases on the page. |
count | number | No | The number of databases. |
AzionDatabaseCollectionOptions
Pagination and filtering for getDatabases.
| Property | Type | Required | Description |
|---|---|---|---|
page | number | No | The page number. |
page_size | number | No | The number of databases per page, from 1 to 100. The API returns 10 when it is omitted. |
search | string | No | A term that filters the databases. |
ordering | string | No | The field that orders the results. |
AzionDatabaseDeleteResponse
What deleteDatabase returns in data.
| Property | Type | Required | Description |
|---|---|---|---|
state | 'pending' | 'failed' | 'executed' | Yes | The state of the deletion. A deletion the API accepts returns pending. |
AzionDatabaseQueryResponse
What useQuery, useExecute, getTables, and the database methods return in data. The package also exports it as AzionDatabaseExecutionResponse.
| Property | Type | Required | Description |
|---|---|---|---|
state | 'pending' | 'failed' | 'executed' | 'executed-runtime' | Yes | The state of the run. |
results | QueryResult[] | No | One entry per statement, in the order of statements. |
toObject | () => JsonObjectQueryExecutionResponse | null | Yes | Returns { state, results }, where each entry holds statement and rows as objects keyed by column name. |
QueryResult
The result of one statement.
| Property | Type | Required | Description |
|---|---|---|---|
statement | string | No | The kind of statement, such as SELECT, INSERT, or CREATE. |
columns | string[] | No | The column names. Set on a statement that returns rows. |
rows | (string | number)[][] | No | The rows, each an array of values in the order of columns. |
error | string | No | The error of the statement. |
AzionQueryExecutionParams
Statements with their parameters. No function on this page takes this type as an argument.
| Property | Type | Required | Description |
|---|---|---|---|
statements | string[] | Yes | The SQL statements. |
params | Array<AzionQueryParams | Record<string, AzionQueryParams>> | Yes | The parameters of the statements. |
AzionQueryParams
A parameter value of a SQL statement.