liveBook · Manning

libSQL

As explained in Server-Side WebAssembly

libSQL is an open-source, SQLite-compatible database engine that supports embedding and extending database functionality.

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

How Server-Side WebAssembly covers it

How deep it goes
Demonstrates compilation, blob or WAT representation, SQL registration, Docker loading, and query execution.
Examples
Uses Rust encrypt and decrypt functions plus a WAT my_add function that returns 42 for inputs 1 and 41.
What it assumes
Introduces Wasm blobs and WAT while using a Rust project and SQL function definitions.
What you can do after
Apply it via libSQL SQL and Docker to register and run Wasm-backed functions.
How it connects
Connects libSQL with SQLite, WebAssembly, user-defined functions, Rust, WebAssembly Text Format, and Docker.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Server-Side WebAssembly. The section links point to the parts of the book it draws on.