Azure SDK for JavaScript – SQL Database
Overview
The Azure SDK for JavaScript provides a set of libraries that simplify working with Azure SQL Database from Node.js or browser based applications. These libraries handle authentication, request construction, response parsing, and offer a fluent API for common operations such as creating databases, executing queries, and managing firewall rules.
Installation
Install the package via npm or yarn:
npm install @azure/arm-sql @azure/identity
For client-side usage, you can also load the library from a CDN:
<script type="module" src="https://cdn.jsdelivr.net/npm/@azure/arm-sql@latest/dist/index.js"></script>
Quick Start
This example demonstrates how to list all SQL servers in a subscription using the DefaultAzureCredential for authentication.
import { DefaultAzureCredential } from "@azure/identity";
import { SqlManagementClient } from "@azure/arm-sql";
async function listServers() {
const credential = new DefaultAzureCredential();
const subscriptionId = process.env.AZURE_SUBSCRIPTION_ID;
const client = new SqlManagementClient(credential, subscriptionId);
const servers = await client.servers.list();
for (const server of servers) {
console.log(`Server: ${server.name} - Location: ${server.location}`);
}
}
listServers().catch(console.error);
For executing T‑SQL queries against a database, use the @azure/identity token with the mssql driver:
import sql from "mssql";
import { DefaultAzureCredential } from "@azure/identity";
async function executeQuery() {
const credential = new DefaultAzureCredential();
const token = await credential.getToken("https://database.windows.net/.default");
const pool = await sql.connect({
server: "my-sql-server.database.windows.net",
database: "mydatabase",
authentication: {
type: "azure-active-directory-access-token",
options: { token: token.token }
},
options: { encrypt: true }
});
const result = await pool.request().query("SELECT TOP 10 * FROM dbo.Customers");
console.log(result.recordset);
await pool.close();
}
executeQuery().catch(console.error);
API Reference
| Class / Interface | Description | Typical Use |
|---|---|---|
SqlManagementClient |
Top‑level client for managing SQL resources (servers, databases, firewall rules). | Provisioning, scaling, and configuration. |
ServersOperations |
Operations related to SQL servers (create, delete, list). | Server lifecycle management. |
DatabasesOperations |
Manage individual databases within a server. | Create, delete, get connection strings. |
FirewallRulesOperations |
Configure firewall rules for server access. | Allow/deny IP ranges. |
DefaultAzureCredential |
Unified credential supporting Managed Identity, VS Code, Azure CLI, etc. | Authentication for any Azure SDK. |
mssql driver |
Node.js driver for executing T‑SQL against Azure SQL. | Run ad‑hoc queries, stored procedures. |