A SurrealDB adapter for better-auth, providing seamless authentication integration with SurrealDB.
- π Full Authentication Support: Complete integration with better-auth's authentication system
- π SurrealDB Native: Built specifically for SurrealDB with optimized queries
- π Schema Generation: Automatic schema generation for better-auth tables
- π Advanced Queries: Support for complex WHERE clauses, sorting, and pagination
- π‘οΈ Type Safety: Full TypeScript support with comprehensive type definitions
- π§ͺ Tested: Comprehensive test suite using better-auth's adapter testing framework
bun add better-auth-surrealdbimport { betterAuth } from "better-auth";
import { surrealAdapter } from "better-auth-surrealdb";
import { surrealdbNodeEngines } from "@surrealdb/node";
export const auth = betterAuth({
appName: "My App",
emailAndPassword: {
enabled: true,
},
database: surrealAdapter({
endpoint: "ws://localhost:8000",
namespace: "myapp",
database: "myapp",
engines: surrealdbNodeEngines(),
}),
});| Option | Type | Required | Description |
|---|---|---|---|
endpoint |
string |
β | SurrealDB connection endpoint |
namespace |
string |
β | Database namespace |
database |
string |
β | Database name |
engines |
Engines |
β | SurrealDB engines configuration |
debugLogs |
AdapterDebugLogs |
β | Enable debug logging |
usePlural |
boolean |
β | Use plural table names |
Local SurrealDB:
surrealAdapter({
endpoint: "ws://localhost:8000",
namespace: "myapp",
database: "myapp",
})In-Memory (for testing):
surrealAdapter({
endpoint: "mem://",
namespace: "test",
database: "test",
engines: surrealdbNodeEngines(),
})Cloud SurrealDB:
surrealAdapter({
endpoint: "wss://your-instance.surrealdb.com",
namespace: "myapp",
database: "myapp",
username: "your-username",
password: "your-password",
})The adapter automatically generates SurrealDB schema definitions for better-auth tables:
DEFINE TABLE user SCHEMALESS;
DEFINE FIELD name ON TABLE user TYPE string;
DEFINE FIELD email ON TABLE user TYPE string;
DEFINE INDEX userUserUnique ON TABLE user COLUMNS email UNIQUE;
DEFINE FIELD emailVerified ON TABLE user TYPE bool;
DEFINE FIELD image ON TABLE user TYPE option<string>;
DEFINE FIELD createdAt ON TABLE user TYPE datetime;
DEFINE FIELD updatedAt ON TABLE user TYPE datetime;
DEFINE TABLE session SCHEMALESS;
DEFINE FIELD expiresAt ON TABLE session TYPE datetime;
DEFINE FIELD token ON TABLE session TYPE string;
DEFINE INDEX sessionSessionUnique ON TABLE session COLUMNS token UNIQUE;
DEFINE FIELD createdAt ON TABLE session TYPE datetime;
DEFINE FIELD updatedAt ON TABLE session TYPE datetime;
DEFINE FIELD ipAddress ON TABLE session TYPE option<string>;
DEFINE FIELD userAgent ON TABLE session TYPE option<string>;
DEFINE FIELD userId ON TABLE session TYPE record<user>;
DEFINE TABLE account SCHEMALESS;
DEFINE FIELD accountId ON TABLE account TYPE string;
DEFINE FIELD providerId ON TABLE account TYPE string;
DEFINE FIELD userId ON TABLE account TYPE record<user>;
DEFINE FIELD accessToken ON TABLE account TYPE option<string>;
DEFINE FIELD refreshToken ON TABLE account TYPE option<string>;
DEFINE FIELD idToken ON TABLE account TYPE option<string>;
DEFINE FIELD accessTokenExpiresAt ON TABLE account TYPE option<datetime>;
DEFINE FIELD refreshTokenExpiresAt ON TABLE account TYPE option<datetime>;
DEFINE FIELD scope ON TABLE account TYPE option<string>;
DEFINE FIELD password ON TABLE account TYPE option<string>;
DEFINE FIELD createdAt ON TABLE account TYPE datetime;
DEFINE FIELD updatedAt ON TABLE account TYPE datetime;
DEFINE TABLE verification SCHEMALESS;
DEFINE FIELD identifier ON TABLE verification TYPE string;
DEFINE FIELD value ON TABLE verification TYPE string;
DEFINE FIELD expiresAt ON TABLE verification TYPE datetime;
DEFINE FIELD createdAt ON TABLE verification TYPE option<datetime>;
DEFINE FIELD updatedAt ON TABLE verification TYPE option<datetime>;The adapter supports all better-auth database operations:
- β Count: Count records with WHERE conditions
- β Find One: Find single record with WHERE conditions
- β Find Many: Find multiple records with pagination and sorting
- β Create: Create new records
- β Update: Update single record
- β Update Many: Update multiple records
- β Delete: Delete single record
- β Delete Many: Delete multiple records
eq- Equalne- Not equalgt- Greater thangte- Greater than or equallt- Less thanlte- Less than or equalin- In arraystarts_with- String starts withends_with- String ends withcontains- String contains
- Bun (recommended) or Node.js
- SurrealDB instance
# Clone the repository
git clone https://github.com/msanchezdev/better-auth-surrealdb.git
cd better-auth-surrealdb
# Install dependencies
bun install
# Run tests
bun test
# Run linter
bun run lint
# Fix linting issues
bun run lint:fix# Navigate to example directory
cd example
# Install dependencies
bun install
# Run the example
bun run index.tsThe project includes comprehensive tests using better-auth's adapter testing framework:
import { surrealdbNodeEngines } from "@surrealdb/node";
import { runAdapterTest } from "better-auth/adapters/test";
import { surrealAdapter } from "better-auth-surrealdb";
describe("SurrealDB Adapter", async () => {
const adapter = surrealAdapter({
endpoint: "mem://",
namespace: "test",
database: "test",
engines: surrealdbNodeEngines(),
debugLogs: {
isRunningAdapterTests: true,
},
});
await runAdapterTest({
getAdapter: async (betterAuthOptions = {}) => {
return adapter(betterAuthOptions);
},
});
});- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Run the test suite
- Submit a pull request
This project is licensed under the MIT License.
- better-auth - The authentication library this adapter is built for
- SurrealDB - The database this adapter connects to
- @surrealdb/node - Official SurrealDB Node.js client