libSQL is a SQLite fork with built-in WebAssembly support. It lets SQL queries call WebAssembly user-defined functions (UDFs), placing custom data-processing logic inside the database rather than moving data to an external application. This combines SQLite’s lightweight database model with the language flexibility of code that compiles to Wasm. section 5.3
A Wasm UDF can be written in any Wasm-compilable language instead of being limited to traditional C or SQL implementations. libSQL registers the module as a SQL function, so queries can invoke custom operations alongside ordinary database work. section 5.3 section 5.3.2
To install a compiled Wasm function in libSQL:
- Compile the Rust project for the wasm32-unknown-unknown target, producing a .wasm module. section 5.3.2
- Convert the module to a SQLite literal blob with wasm_hex_dump, or use the human-readable WebAssembly Text Format (WAT). section 5.3.2
- Initialize the Wasm function table, remove any old definitions, and create SQL functions with CREATE FUNCTION ... LANGUAGE wasm. section 5.3.2
- Mount the data directory in a libSQL Docker container, read the SQL definition file, and call the functions from the interactive shell. section 5.3.3
Listing 5.5 data/create_wasm_udfs.sql file
.init_wasm_func_table DROP FUNCTION IF EXISTS encrypt; DROP FUNCTION IF EXISTS decrypt; CREATE FUNCTION encrypt LANGUAGE wasm AS X'0061736d01...'; CREATE FUNCTION decrypt LANGUAGE wasm AS X'0061736d01...';
This SQL file shows the complete registration pattern: initialize the Wasm function table, clear prior encrypt and decrypt functions, then create replacements backed by embedded Wasm blobs. section 5.3.2
Encrypting and decrypting inside SQL
An encrypt UDF accepts the text “Hello, I am a Wasm UDF!” and the key “dan”, returning an encrypted value. Passing that value to decrypt with the same key returns the original string, confirming that both Wasm functions execute through libSQL SQL calls. section 5.3.3
CREATE FUNCTION my_add LANGUAGE wasm AS '
(module
(func $my_add (param i64 i64) (result i64)
local.get 0
local.get 1
i64.add)
(memory 16)
(export "my_add" (func $my_add))
(export "memory" (memory 0))
)';
A WAT-defined my_add function illustrates an alternative to embedding a compiled binary: define a Wasm module directly in SQL, export its function and memory, then call it as my_add(1, 41). section 5.3.3
