MS SQL
Microsoft SQL Server is supported by the core better_auth gem through the
direct MSSQL adapter for Rack applications.
MSSQL support uses Sequel internally for safer parameter binding over TinyTDS.
This does not expose a public Sequel adapter; configure
BetterAuth::Adapters::MSSQL directly with a URL or an existing Sequel
database object.
Install Dependencies
gem "better_auth"
gem "sequel"
gem "tiny_tds"bundle installExample Usage
require "better_auth"
auth = BetterAuth.auth(
secret: ENV.fetch("BETTER_AUTH_SECRET"),
base_url: ENV.fetch("BETTER_AUTH_URL"),
database: ->(options) {
BetterAuth::Adapters::MSSQL.new(
options,
url: ENV.fetch("DATABASE_URL")
)
}
)You may also pass an existing Sequel database object when your app owns connection management.
require "sequel"
require "tiny_tds"
database = Sequel.connect(ENV.fetch("DATABASE_URL"))
auth = BetterAuth.auth(
secret: ENV.fetch("BETTER_AUTH_SECRET"),
database: ->(options) {
BetterAuth::Adapters::MSSQL.new(
options,
connection: database
)
}
)Schema generation & migration
The Ruby schema helpers generate MSSQL DDL from the same Better Auth
configuration used at runtime, including plugins, custom model names, custom
field names, and additional_fields.
MS SQL Schema Generation | MS SQL Schema Migration |
|---|---|
| ✅ Supported | ✅ Supported |
config = BetterAuth::Configuration.new(
secret: ENV.fetch("BETTER_AUTH_SECRET"),
database: :memory,
plugins: []
)
statements = BetterAuth::Schema::SQL.create_statements(
config,
dialect: :mssql
)
puts statements.join("\n\n")require "better_auth"
require "sequel"
require "tiny_tds"
config = BetterAuth::Configuration.new(
secret: ENV.fetch("BETTER_AUTH_SECRET"),
database: :memory,
plugins: []
)
statements = BetterAuth::Schema::SQL.create_statements(
config,
dialect: :mssql
)
database = Sequel.connect(ENV.fetch("DATABASE_URL"))
statements.each { |sql| database.run(sql) }Joins (Experimental)
Enable experimental joins when you want Better Auth to ask the database adapter to fetch related records in one adapter call.
auth = BetterAuth.auth(
secret: ENV.fetch("BETTER_AUTH_SECRET"),
experimental: { joins: true },
database: ->(options) {
BetterAuth::Adapters::MSSQL.new(
options,
url: ENV.fetch("DATABASE_URL")
)
}
)The MSSQL adapter supports native joins for core relationships and for plugin
schema relationships declared with references.
Schema
The Ruby schema maps Better Auth logical fields to MSSQL physical columns.
| Better Auth field | MSSQL column |
|---|---|
emailVerified | email_verified |
userId | user_id |
createdAt | created_at |
expiresAt | expires_at |
JSON-like fields are stored as varchar(8000) JSON strings and parsed on
output, date fields use datetime2(3), indexed strings use varchar(255), and
ids are generated as hex strings by default. Set
advanced: { database: { generate_id: "uuid" } } to generate UUID strings
instead.
Additional Information
- The direct adapter supports the shared Better Auth CRUD contract, where operators, sorting, pagination, counts, Sequel-backed transactions, and native joins.
- The
DATABASE_URLshould use a Sequel/TinyTDS-compatible connection URL. - Direct SQL generation is intended for non-Rails apps or custom migration workflows.