Skip to content

feat(ts-sdk): add serverless adapter for @tursodatabase/serverless - #325

Merged
penberg merged 4 commits into
tursodatabase:mainfrom
wondamo:feat/ts-sdk-serverless-adapter
Mar 22, 2026
Merged

penberg merged 4 commits into
tursodatabase:mainfrom
wondamo:feat/ts-sdk-serverless-adapter

Conversation

@wondamo

@wondamo wondamo commented Mar 15, 2026

Copy link
Copy Markdown
Contributor

Adds an adapter that wraps @tursodatabase/serverless Connection to be compatible with AgentFS's DatabasePromise interface, enabling AgentFS to work with remote Turso/sqld instances over HTTP.

Usage

import { connect } from "@tursodatabase/serverless";
import { AgentFS } from "agentfs-sdk";
import { createServerlessAdapter } from "agentfs-sdk/serverless";

const conn = connect({
  url: process.env.TURSO_DATABASE_URL,
  authToken: process.env.TURSO_AUTH_TOKEN,
});

const db = createServerlessAdapter(conn);
const agent = await AgentFS.openWith(db);

How it works

The main challenge is that DatabasePromise.prepare() is synchronous while @tursodatabase/serverless's prepare() is async (it fetches column metadata over HTTP). The adapter uses a LazyStatement that defers the actual prepare() call until run()/get()/all() is called — those are already async, so the deferral is invisible to callers.

Changes

  • sdk/typescript/src/integrations/serverless/ — adapter and exports
  • sdk/typescript/tests/serverless.test.ts — integration tests against a local sqld instance
  • sdk/typescript/package.json — adds agentfs-sdk/serverless export path, @tursodatabase/serverless as optional peer dependency

Closes #156

wondamo added 4 commits March 15, 2026 17:40
Adds an adapter that wraps @tursodatabase/serverless Connection to be
compatible with AgentFS's DatabasePromise interface, enabling AgentFS
to work with remote Turso/sqld instances over HTTP.

Usage:
  import { connect } from "@tursodatabase/serverless";
  import { createServerlessAdapter } from "agentfs-sdk/serverless";

  const conn = connect({ url: "libsql://mydb.turso.io", authToken: "..." });
  const db = createServerlessAdapter(conn);
  const agent = await AgentFS.openWith(db);

The key challenge is that DatabasePromise.prepare() is synchronous while
the serverless driver's prepare() is async. The adapter uses a LazyStatement
that defers prepare() until run/get/all is called.

Closes tursodatabase#156
The serverless integration tests require a running sqld instance.
Skip them gracefully in CI where sqld is not present.
- Import Connection/Statement types from @tursodatabase/serverless
  instead of hand-rolled interfaces
- Return DatabasePromise type instead of any
- Forward pluck() and safeIntegers() to underlying statement
- Throw explicit errors for unsupported operations (bind, pragma,
  backup, serialize, etc.) instead of silent no-ops
- Validate connection in connect() with SELECT 1
- Don't cache stmtPromise to avoid stale raw/pluck/safeIntegers state
- Test imports from the export path instead of internal source
- Re-add stmtPromise caching to avoid extra HTTP prepare() per call,
  apply raw/pluck/safeIntegers at execution time on resolved statement
- Fix transaction() to return a wrapper function matching DatabasePromise
  signature: (...bindParameters) => Promise<any>
- Add comments explaining the parameter passing convention difference
  between DatabasePromise (rest params) and serverless (single array)
@penberg
penberg merged commit 86d70b6 into tursodatabase:main Mar 22, 2026
34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

agentfs-serverless package?

2 participants