Edge SQL API

The Edge SQL API provides an interface for interacting with an EdgeSQL database from Edge Functions running on the Azion Edge Platform. This document outlines the key components of the API and provides code samples for each.


Database

Creates a connection to an EdgeSQL database for your application. Returns a Connection type object.

Methods

MethodDescriptionSignatureParametersReturn
OpenOpens a connection to the read replica of an EdgeSQL databasestatic async open(name)name: stringConnection

Connection

Represents the communication channel with a specific EdgeSQL database. This object can be obtained by opening a database.

Methods

MethodDescriptionSignatureParametersReturn
ExecuteExecutes an SQL commandasync execute(sql,params)sql: <string>
- '?' positional parameter
- :<param_name>
params:
- Array of positional values
- Dictionary of values
-
QueryExecutes an SQL commandasync query(sql, params)sql: <string>
- '?' positional parameter
- :<param_name>
params:
- Array of positional values
- Dictionary of values
Rows
PreparePrepares an SQL command for execution from the combination of the command and parametersasync prepare(sql,params)sql: <string>
- '?' positional parameter
- :<param_name>
params:
- Array of positional values
- Dictionary of values
Statement

Statement

An abstraction of an SQL command, the Statement object allows for the execution of the represented command. It can be created from a Connection.prepare command.

Methods

MethodDescriptionSignatureParametersReturn
ExecuteExecutes an SQL commandasync execute(sql, params)sql: <string>
- '?' positional parameter
- :<param_name>
params:
- Array of positional values
- Dictionary of values
-
QueryExecutes an SQL commandasync query(sql, params)sql: <string>
- '?' positional parameter
- :<param_name>
params:
- Array of positional values
- Dictionary of values
Rows

Attributes

AttributeDescriptionSignatureParametersReturn
parameterCountReturns the number of parameters related of statementparameterCount()-int32
parameterNameReturns the name of parameterparameterName(index)index: int32String
columnsReturns the list of columns related of the statementcolumns()-Object

Rows

Represents the result set returned by an SQL query.

Methods

MethodDescriptionSignatureParametersReturn
nextReturns the next line of the query responseasync next()-<Row> or null

Attributes

AttributeDescriptionSignatureParametersReturn
columnCountReturns the number of columns from the query resultcolumnCount()-int32
columnNameReturns the column’s nameparameterName(index)index: int32String
columnTypeReturns the column’s typecolumnType(index)index: int32int32

Row

Represents the set of attributes and values in a row of a result set.

Attributes

AttributeDescriptionSignatureParametersReturn
columnNameReturns the column’s namecolumnName(index)index: int32String
columnTypeReturns the column’s typeColumnType(index)index: int32int32
getValueReturns the attribute valuegetValue(index)index: int32Value
getStringReturns the attribute value as stringgetString(index)index: int32String

Code sample

The code sample shown below presents a way to interact with a database and retrieve data from a table. This example uses a db called mydatabase and the table users.

import { Database } from "azion:sql";
async function db_query() {
let connection = await Database.open("mydatabase");
let rows = await connection.query("select * from users");
let column_count = rows.columnCount();
let column_names = [];
for (let i = 0; i < column_count; i++) {
column_names.push(rows.columnName(i));
}
let response_lines = [];
response_lines.push(column_names.join("|"));
let row = await rows.next();
while (row) {
let row_items = [];
for (let i = 0; i < column_count; i++) {
row_items.push(row.getString(i));
}
response_lines.push(row_items.join("|"));
row = await rows.next();
}
const response_text = response_lines.join("\n");
return response_text;
}
async function handle_request(request) {
if (request.method != "GET") {
return new Response("Method not allowed", { status: 405 });
}
try {
return new Response(await db_query());
} catch (e) {
console.log(e.message, e.stack);
return new Response(e.message, { status: 500 });
}
}
addEventListener("fetch", (event) =>
event.respondWith(handle_request(event.request))
);

Considering the database and table both exist, the output would be:

id|name
1|User1
2|User2
3|User3
4|User4

Contributors