Skip to content

Add Database.limit() and Database.setLimit() - #246

Draft
jeffreykwan wants to merge 1 commit into
tursodatabase:mainfrom
jeffreykwan:jk-connection-limits
Draft

jeffreykwan wants to merge 1 commit into
tursodatabase:mainfrom
jeffreykwan:jk-connection-limits

Conversation

@jeffreykwan

Copy link
Copy Markdown

Depends on tursodatabase/libsql#2305, which adds Connection::limit and Connection::set_limit to the libsql crate. This stays a draft until that change is in a release; then I'll bump the libsql version here. CI will fail until then because the new API isn't on crates.io yet.

We run user-written SQL through libsql-js and want to cap how large a single string or BLOB can get, so one query can't allocate a huge value. SQLite supports this per connection with sqlite3_limit(SQLITE_LIMIT_LENGTH, ...), but there's no PRAGMA for it, so it has to be exposed natively.

API. db.limit(id) returns the current value and db.setLimit(id, value) sets a new one and returns the previous value, in both the sync (compat.js) and promise (promise.js) APIs. A new Limit object holds the SQLITE_LIMIT_* constants (Limit.LENGTH, Limit.SQL_LENGTH, ...). Invalid IDs throw, and calling either on a closed database throws the usual closed-database error. Docs are in docs/api.md.

Testing. New tests in extensions.test.js and async.test.js cover lowering Limit.LENGTH, negative values, invalid IDs, and use after close. I ran the full integration suite locally against the libsql branch and it passes. In a local check, a query asking format() for a 500 MB string peaked at about 520 MB RSS without a limit, and about 47 MB with a 4 MB Limit.LENGTH, where it returns NULL right away.

🤖 Generated with Claude Code

Expose SQLite's per-connection run-time limits so callers can, for
example, cap SQLITE_LIMIT_LENGTH and stop a single query from building
a huge string or BLOB. Adds a Limit constants object that matches the
SQLITE_LIMIT_* values. setLimit() returns the previous value.

Requires libsql's Connection::limit and Connection::set_limit.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
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.

1 participant