Chalk supports Microsoft SQL Server and Azure SQL Database as a SQL source. You can configure the Microsoft SQL Server-specific options using the MSSQLSource init args, or configure the source through your dashboard, and reference the source in your code.

Adding Microsoft SQL Server

On the dashboard, you can plug in the configuration for your Microsoft SQL Server database.

Authentication Methods

Chalk supports three authentication methods:

SQL Authentication - Traditional username and password authentication

Azure AD Managed Identity - When running in Azure (such as in AKS or Azure VMs), Chalk can automatically authenticate using Managed Identity. Simply omit authentication credentials when configuring your source.

Azure AD Service Principal - For authentication using an Azure AD application, provide client_id, client_secret, and tenant_id.

Single Integration

If you have only one Microsoft SQL Server connection that you’d like to add to Chalk, you do not need to specify any arguments to construct the source in your code.

from chalk.sql import MSSQLSource

mssql = MSSQLSource()

@online
def fn(...) -> ...:
    return mssql.query(...).first()

Multiple Integrations

Chalk's injects environment variables to support data integrations. But what happens when you have two data sources of the same kind? When you create a new data source from your dashboard, you have an option to provide a name for the integration. You can then reference this name in the code directly.
from chalk.sql import MSSQLSource

risk = MSSQLSource(name="RISK")
marketing = MSSQLSource(name="MARKETING")

@online
def risk_resolver(...) -> ...:
    return risk.query(...).first()

@online
def marketing_resolver(...) -> ...:
    return marketing.query(...).first()
Named integrations inject environment variables with the standard names prefixed by the integration name. For example, if your integration is called RISK, then the variable MSSQL_HOST will be injected as RISK_MSSQL_HOST.

Environment Variables

You can also configure the integration directly using environment variables on your local machine or from those added through the generic environment variable support.

SQL Authentication

import os
from chalk.sql import MSSQLSource

mssql = MSSQLSource(
    host=os.getenv("MSSQL_HOST"),
    port=os.getenv("MSSQL_TCP_PORT"),
    db=os.getenv("MSSQL_DATABASE"),
    user=os.getenv("MSSQL_USER"),
    password=os.getenv("MSSQL_PWD"),
)

@online
def fn(...) -> ...:
    return mssql.query(...).first()

Azure AD Service Principal

import os
from chalk.sql import MSSQLSource

mssql = MSSQLSource(
    host=os.getenv("MSSQL_HOST"),
    port=os.getenv("MSSQL_TCP_PORT"),
    db=os.getenv("MSSQL_DATABASE"),
    client_id=os.getenv("MSSQL_CLIENT_ID"),
    client_secret=os.getenv("MSSQL_CLIENT_SECRET"),
    tenant_id=os.getenv("MSSQL_TENANT_ID"),
)

@online
def fn(...) -> ...:
    return mssql.query(...).first()

Managed Identity

import os
from chalk.sql import MSSQLSource

# Only connection details needed - authentication is automatic
mssql = MSSQLSource(
    host=os.getenv("MSSQL_HOST"),
    port=os.getenv("MSSQL_TCP_PORT"),
    db=os.getenv("MSSQL_DATABASE"),
)

@online
def fn(...) -> ...:
    return mssql.query(...).first()